blob: cc6d073b1033a4ce439f8588e262bcddc58a253d [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
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000530 unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d);
Douglas Gregor98339b92011-08-25 20:47:51 +0000531 unsigned Bits = ReadUnalignedLE16(d);
532 bool CPlusPlusOperatorKeyword = Bits & 0x01;
533 Bits >>= 1;
534 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
535 Bits >>= 1;
536 bool Poisoned = Bits & 0x01;
537 Bits >>= 1;
538 bool ExtensionToken = Bits & 0x01;
539 Bits >>= 1;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000540 bool hadMacroDefinition = Bits & 0x01;
541 Bits >>= 1;
Douglas Gregor98339b92011-08-25 20:47:51 +0000542 bool hasMacroDefinition = Bits & 0x01;
543 Bits >>= 1;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000544
Douglas Gregor98339b92011-08-25 20:47:51 +0000545 assert(Bits == 0 && "Extra bits in the identifier?");
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000546 DataLen -= 8;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000547
Douglas Gregor98339b92011-08-25 20:47:51 +0000548 // Build the IdentifierInfo itself and link the identifier ID with
549 // the new IdentifierInfo.
550 IdentifierInfo *II = KnownII;
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000551 if (!II) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000552 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000553 KnownII = II;
554 }
Douglas Gregor057df202012-01-18 20:56:22 +0000555 Reader.markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +0000556 II->setIsFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000557
Douglas Gregor98339b92011-08-25 20:47:51 +0000558 // Set or check the various bits in the IdentifierInfo structure.
559 // Token IDs are read-only.
560 if (HasRevertedTokenIDToIdentifier)
561 II->RevertTokenIDToIdentifier();
562 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
563 assert(II->isExtensionToken() == ExtensionToken &&
564 "Incorrect extension token flag");
565 (void)ExtensionToken;
566 if (Poisoned)
567 II->setIsPoisoned(true);
568 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
569 "Incorrect C++ operator keyword flag");
570 (void)CPlusPlusOperatorKeyword;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000571
Douglas Gregor98339b92011-08-25 20:47:51 +0000572 // If this identifier is a macro, deserialize the macro
573 // definition.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000574 if (hadMacroDefinition) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000575 // FIXME: Check for conflicts?
576 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor13292642011-12-02 15:45:10 +0000577 unsigned LocalSubmoduleID = ReadUnalignedLE32(d);
578
579 // Determine whether this macro definition should be visible now, or
580 // whether it is in a hidden submodule.
581 bool Visible = true;
582 if (SubmoduleID GlobalSubmoduleID
583 = Reader.getGlobalSubmoduleID(F, LocalSubmoduleID)) {
584 if (Module *Owner = Reader.getSubmodule(GlobalSubmoduleID)) {
585 if (Owner->NameVisibility == Module::Hidden) {
586 // The owning module is not visible, and this macro definition should
587 // not be, either.
588 Visible = false;
589
590 // Note that this macro definition was hidden because its owning
591 // module is not yet visible.
592 Reader.HiddenNamesMap[Owner].push_back(II);
593 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000594 }
Douglas Gregor13292642011-12-02 15:45:10 +0000595 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000596
597 Reader.setIdentifierIsMacro(II, F, Offset, Visible && hasMacroDefinition);
Douglas Gregor13292642011-12-02 15:45:10 +0000598 DataLen -= 8;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000599 }
600
Douglas Gregoreee242f2011-10-27 09:33:13 +0000601 Reader.SetIdentifierInfo(ID, II);
602
Douglas Gregor98339b92011-08-25 20:47:51 +0000603 // Read all of the declarations visible at global scope with this
604 // name.
Douglas Gregor98339b92011-08-25 20:47:51 +0000605 if (DataLen > 0) {
606 SmallVector<uint32_t, 4> DeclIDs;
607 for (; DataLen > 0; DataLen -= 4)
608 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
609 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000610 }
611
Douglas Gregor98339b92011-08-25 20:47:51 +0000612 return II;
613}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000614
Douglas Gregor98339b92011-08-25 20:47:51 +0000615unsigned
616ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
617 llvm::FoldingSetNodeID ID;
618 ID.AddInteger(Key.Kind);
619
620 switch (Key.Kind) {
621 case DeclarationName::Identifier:
622 case DeclarationName::CXXLiteralOperatorName:
623 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
624 break;
625 case DeclarationName::ObjCZeroArgSelector:
626 case DeclarationName::ObjCOneArgSelector:
627 case DeclarationName::ObjCMultiArgSelector:
628 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
629 break;
630 case DeclarationName::CXXOperatorName:
631 ID.AddInteger((OverloadedOperatorKind)Key.Data);
632 break;
633 case DeclarationName::CXXConstructorName:
634 case DeclarationName::CXXDestructorName:
635 case DeclarationName::CXXConversionFunctionName:
636 case DeclarationName::CXXUsingDirective:
637 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000638 }
639
Douglas Gregor98339b92011-08-25 20:47:51 +0000640 return ID.ComputeHash();
641}
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000642
Douglas Gregor98339b92011-08-25 20:47:51 +0000643ASTDeclContextNameLookupTrait::internal_key_type
644ASTDeclContextNameLookupTrait::GetInternalKey(
645 const external_key_type& Name) const {
646 DeclNameKey Key;
647 Key.Kind = Name.getNameKind();
648 switch (Name.getNameKind()) {
649 case DeclarationName::Identifier:
650 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
651 break;
652 case DeclarationName::ObjCZeroArgSelector:
653 case DeclarationName::ObjCOneArgSelector:
654 case DeclarationName::ObjCMultiArgSelector:
655 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
656 break;
657 case DeclarationName::CXXOperatorName:
658 Key.Data = Name.getCXXOverloadedOperator();
659 break;
660 case DeclarationName::CXXLiteralOperatorName:
661 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
662 break;
663 case DeclarationName::CXXConstructorName:
664 case DeclarationName::CXXDestructorName:
665 case DeclarationName::CXXConversionFunctionName:
666 case DeclarationName::CXXUsingDirective:
667 Key.Data = 0;
668 break;
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000669 }
670
Douglas Gregor98339b92011-08-25 20:47:51 +0000671 return Key;
672}
673
Douglas Gregor98339b92011-08-25 20:47:51 +0000674std::pair<unsigned, unsigned>
675ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
676 using namespace clang::io;
677 unsigned KeyLen = ReadUnalignedLE16(d);
678 unsigned DataLen = ReadUnalignedLE16(d);
679 return std::make_pair(KeyLen, DataLen);
680}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000681
Douglas Gregor98339b92011-08-25 20:47:51 +0000682ASTDeclContextNameLookupTrait::internal_key_type
683ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
684 using namespace clang::io;
685
686 DeclNameKey Key;
687 Key.Kind = (DeclarationName::NameKind)*d++;
688 switch (Key.Kind) {
689 case DeclarationName::Identifier:
690 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
691 break;
692 case DeclarationName::ObjCZeroArgSelector:
693 case DeclarationName::ObjCOneArgSelector:
694 case DeclarationName::ObjCMultiArgSelector:
695 Key.Data =
696 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
697 .getAsOpaquePtr();
698 break;
699 case DeclarationName::CXXOperatorName:
700 Key.Data = *d++; // OverloadedOperatorKind
701 break;
702 case DeclarationName::CXXLiteralOperatorName:
703 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
704 break;
705 case DeclarationName::CXXConstructorName:
706 case DeclarationName::CXXDestructorName:
707 case DeclarationName::CXXConversionFunctionName:
708 case DeclarationName::CXXUsingDirective:
709 Key.Data = 0;
710 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000711 }
712
Douglas Gregor98339b92011-08-25 20:47:51 +0000713 return Key;
714}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000715
Douglas Gregor98339b92011-08-25 20:47:51 +0000716ASTDeclContextNameLookupTrait::data_type
717ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
718 const unsigned char* d,
Nick Lewyckyb346d2f2012-04-16 02:51:46 +0000719 unsigned DataLen) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000720 using namespace clang::io;
721 unsigned NumDecls = ReadUnalignedLE16(d);
Douglas Gregor9b8b20f2012-01-06 16:09:53 +0000722 LE32DeclID *Start = (LE32DeclID *)d;
Douglas Gregor98339b92011-08-25 20:47:51 +0000723 return std::make_pair(Start, Start + NumDecls);
724}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000725
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000726bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000727 llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000728 const std::pair<uint64_t, uint64_t> &Offsets,
729 DeclContextInfo &Info) {
730 SavedStreamPosition SavedPosition(Cursor);
731 // First the lexical decls.
732 if (Offsets.first != 0) {
733 Cursor.JumpToBit(Offsets.first);
734
735 RecordData Record;
736 const char *Blob;
737 unsigned BlobLen;
738 unsigned Code = Cursor.ReadCode();
739 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
740 if (RecCode != DECL_CONTEXT_LEXICAL) {
741 Error("Expected lexical block");
742 return true;
743 }
744
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000745 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
746 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000747 }
748
749 // Now the lookup table.
750 if (Offsets.second != 0) {
751 Cursor.JumpToBit(Offsets.second);
752
753 RecordData Record;
754 const char *Blob;
755 unsigned BlobLen;
756 unsigned Code = Cursor.ReadCode();
757 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
758 if (RecCode != DECL_CONTEXT_VISIBLE) {
759 Error("Expected visible lookup table block");
760 return true;
761 }
762 Info.NameLookupTableData
763 = ASTDeclContextNameLookupTable::Create(
764 (const unsigned char *)Blob + Record[0],
765 (const unsigned char *)Blob,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000766 ASTDeclContextNameLookupTrait(*this, M));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000767 }
768
769 return false;
770}
771
Chris Lattner5f9e2722011-07-23 10:55:15 +0000772void ASTReader::Error(StringRef Msg) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000773 Error(diag::err_fe_pch_malformed, Msg);
774}
775
776void ASTReader::Error(unsigned DiagID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000777 StringRef Arg1, StringRef Arg2) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000778 if (Diags.isDiagnosticInFlight())
779 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
780 else
781 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000782}
783
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000784/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000785bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000786 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000787 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000788 ActualOriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000789 SuggestedPredefines,
790 FileMgr);
Douglas Gregore721f952009-04-28 18:58:38 +0000791 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000792}
793
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000794//===----------------------------------------------------------------------===//
795// Source Manager Deserialization
796//===----------------------------------------------------------------------===//
797
Douglas Gregorbd945002009-04-13 16:31:14 +0000798/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000799/// \returns true if there was an error.
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000800bool ASTReader::ParseLineTable(ModuleFile &F,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000801 SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000802 unsigned Idx = 0;
803 LineTableInfo &LineTable = SourceMgr.getLineTable();
804
805 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000806 std::map<int, int> FileIDs;
807 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000808 // Extract the file name
809 unsigned FilenameLen = Record[Idx++];
810 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
811 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000812 MaybeAddSystemRootToFilename(Filename);
Jay Foad65aa6882011-06-21 15:13:30 +0000813 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
Douglas Gregorbd945002009-04-13 16:31:14 +0000814 }
815
816 // Parse the line entries
817 std::vector<LineEntry> Entries;
818 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000819 int FID = Record[Idx++];
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000820 assert(FID >= 0 && "Serialized line entries for non-local file.");
821 // Remap FileID from 1-based old view.
822 FID += F.SLocEntryBaseID - 1;
Douglas Gregorbd945002009-04-13 16:31:14 +0000823
824 // Extract the line entries
825 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000826 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000827 Entries.clear();
828 Entries.reserve(NumEntries);
829 for (unsigned I = 0; I != NumEntries; ++I) {
830 unsigned FileOffset = Record[Idx++];
831 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000832 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000833 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000834 = (SrcMgr::CharacteristicKind)Record[Idx++];
835 unsigned IncludeOffset = Record[Idx++];
836 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
837 FileKind, IncludeOffset));
838 }
Douglas Gregor47d9de62012-06-08 16:40:28 +0000839 LineTable.AddEntry(FileID::get(FID), Entries);
Douglas Gregorbd945002009-04-13 16:31:14 +0000840 }
841
842 return false;
843}
844
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000845namespace {
846
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000847class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000848public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000849 const ino_t ino;
850 const dev_t dev;
851 const mode_t mode;
852 const time_t mtime;
853 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000855 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +0000856 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000857};
858
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000859class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000860 public:
861 typedef const char *external_key_type;
862 typedef const char *internal_key_type;
863
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000864 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000865
866 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000867 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000868 }
869
870 static internal_key_type GetInternalKey(const char *path) { return path; }
871
872 static bool EqualKey(internal_key_type a, internal_key_type b) {
873 return strcmp(a, b) == 0;
874 }
875
876 static std::pair<unsigned, unsigned>
877 ReadKeyDataLength(const unsigned char*& d) {
878 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
879 unsigned DataLen = (unsigned) *d++;
880 return std::make_pair(KeyLen + 1, DataLen);
881 }
882
883 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
884 return (const char *)d;
885 }
886
887 static data_type ReadData(const internal_key_type, const unsigned char *d,
888 unsigned /*DataLen*/) {
889 using namespace clang::io;
890
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000891 ino_t ino = (ino_t) ReadUnalignedLE32(d);
892 dev_t dev = (dev_t) ReadUnalignedLE32(d);
893 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000894 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000895 off_t size = (off_t) ReadUnalignedLE64(d);
896 return data_type(ino, dev, mode, mtime, size);
897 }
898};
899
900/// \brief stat() cache for precompiled headers.
901///
902/// This cache is very similar to the stat cache used by pretokenized
903/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +0000904class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000905 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000906 CacheTy *Cache;
907
908 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000909public:
Chris Lattner74e976b2010-11-23 19:28:12 +0000910 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
911 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000912 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
913 Cache = CacheTy::Create(Buckets, Base);
914 }
915
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000916 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Chris Lattner898a0612010-11-23 21:17:56 +0000918 LookupResult getStat(const char *Path, struct stat &StatBuf,
919 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000920 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +0000921 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000922
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000923 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000924 if (I == Cache->end()) {
925 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +0000926 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000927 }
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000929 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000930 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Chris Lattner10e286a2010-11-23 19:19:34 +0000932 StatBuf.st_ino = Data.ino;
933 StatBuf.st_dev = Data.dev;
934 StatBuf.st_mtime = Data.mtime;
935 StatBuf.st_mode = Data.mode;
936 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +0000937 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000938 }
939};
940} // end anonymous namespace
941
942
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000943/// \brief Read a source manager block
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000944ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000945 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000946
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000947 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +0000948
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000949 // Set the source-location entry cursor to the current position in
950 // the stream. This cursor will be used to read the contents of the
951 // source manager block initially, and then lazily read
952 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000953 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000954
955 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000956 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000957 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000958 return Failure;
959 }
960
961 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000962 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000963 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000964 return Failure;
965 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000966
Douglas Gregor14f79002009-04-10 03:52:48 +0000967 RecordData Record;
968 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000969 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +0000970 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000971 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000972 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000973 return Failure;
974 }
Douglas Gregore1d918e2009-04-10 23:10:45 +0000975 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000976 }
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Douglas Gregor14f79002009-04-10 03:52:48 +0000978 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
979 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000980 SLocEntryCursor.ReadSubBlockID();
981 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000982 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000983 return Failure;
984 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000985 continue;
986 }
Mike Stump1eb44332009-09-09 15:08:12 +0000987
Douglas Gregor14f79002009-04-10 03:52:48 +0000988 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000989 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +0000990 continue;
991 }
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Douglas Gregor14f79002009-04-10 03:52:48 +0000993 // Read a record.
994 const char *BlobStart;
995 unsigned BlobLen;
996 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000997 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000998 default: // Default behavior: ignore.
999 break;
1000
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001001 case SM_SLOC_FILE_ENTRY:
1002 case SM_SLOC_BUFFER_ENTRY:
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001003 case SM_SLOC_EXPANSION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001004 // Once we hit one of the source location entries, we're done.
1005 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001006 }
1007 }
1008}
1009
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001010/// \brief If a header file is not found at the path that we expect it to be
1011/// and the PCH file was moved from its original location, try to resolve the
1012/// file by assuming that header+PCH were moved together and the header is in
1013/// the same place relative to the PCH.
1014static std::string
1015resolveFileRelativeToOriginalDir(const std::string &Filename,
1016 const std::string &OriginalDir,
1017 const std::string &CurrDir) {
1018 assert(OriginalDir != CurrDir &&
1019 "No point trying to resolve the file if the PCH dir didn't change");
1020 using namespace llvm::sys;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001021 SmallString<128> filePath(Filename);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001022 fs::make_absolute(filePath);
1023 assert(path::is_absolute(OriginalDir));
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001024 SmallString<128> currPCHPath(CurrDir);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001025
1026 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1027 fileDirE = path::end(path::parent_path(filePath));
1028 path::const_iterator origDirI = path::begin(OriginalDir),
1029 origDirE = path::end(OriginalDir);
1030 // Skip the common path components from filePath and OriginalDir.
1031 while (fileDirI != fileDirE && origDirI != origDirE &&
1032 *fileDirI == *origDirI) {
1033 ++fileDirI;
1034 ++origDirI;
1035 }
1036 for (; origDirI != origDirE; ++origDirI)
1037 path::append(currPCHPath, "..");
1038 path::append(currPCHPath, fileDirI, fileDirE);
1039 path::append(currPCHPath, path::filename(Filename));
1040 return currPCHPath.str();
1041}
1042
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001043/// \brief Read in the source location entry with the given ID.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001044ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(int ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001045 if (ID == 0)
1046 return Success;
1047
Douglas Gregor0cdd7982011-07-21 18:46:38 +00001048 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001049 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001050 return Failure;
1051 }
1052
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001053 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001054 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001055 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001056 unsigned BaseOffset = F->SLocEntryBaseOffset;
Sebastian Redl9137a522010-07-16 17:50:48 +00001057
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001058 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001059 unsigned Code = SLocEntryCursor.ReadCode();
1060 if (Code == llvm::bitc::END_BLOCK ||
1061 Code == llvm::bitc::ENTER_SUBBLOCK ||
1062 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001063 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001064 return Failure;
1065 }
1066
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001067 RecordData Record;
1068 const char *BlobStart;
1069 unsigned BlobLen;
1070 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1071 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001072 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001073 return Failure;
1074
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001075 case SM_SLOC_FILE_ENTRY: {
Douglas Gregora081da52011-11-16 20:05:18 +00001076 if (Record.size() < 7) {
1077 Error("source location entry is incorrect");
1078 return Failure;
1079 }
1080
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001081 // We will detect whether a file changed and return 'Failure' for it, but
1082 // we will also try to fail gracefully by setting up the SLocEntry.
1083 ASTReader::ASTReadResult Result = Success;
1084
Douglas Gregora081da52011-11-16 20:05:18 +00001085 bool OverriddenBuffer = Record[6];
1086
1087 std::string OrigFilename(BlobStart, BlobStart + BlobLen);
1088 std::string Filename = OrigFilename;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001089 MaybeAddSystemRootToFilename(Filename);
Douglas Gregora4581a12011-11-17 19:08:51 +00001090 const FileEntry *File =
1091 OverriddenBuffer? FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1092 (time_t)Record[5])
1093 : FileMgr.getFile(Filename, /*OpenFile=*/false);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001094 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1095 OriginalDir != CurrentDir) {
1096 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1097 OriginalDir,
1098 CurrentDir);
1099 if (!resolved.empty())
1100 File = FileMgr.getFile(resolved);
1101 }
Axel Naumann04331162011-01-27 10:55:51 +00001102 if (File == 0)
1103 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1104 (time_t)Record[5]);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001105 if (File == 0) {
1106 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001107 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001108 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001109 Error(ErrorStr.c_str());
1110 return Failure;
1111 }
Mike Stump1eb44332009-09-09 15:08:12 +00001112
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001113 if (!DisableValidation &&
1114 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001115#if !defined(LLVM_ON_WIN32)
1116 // In our regression testing, the Windows file system seems to
1117 // have inconsistent modification times that sometimes
1118 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001119 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001120#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001121 )) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +00001122 Error(diag::err_fe_pch_file_modified, Filename);
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001123 Result = Failure;
Douglas Gregor2d52be52010-03-21 22:49:54 +00001124 }
1125
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001126 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001127 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001128 // This is the module's main file.
1129 IncludeLoc = getImportLocation(F);
1130 }
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +00001131 SrcMgr::CharacteristicKind
1132 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1133 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001134 ID, BaseOffset + Record[0]);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001135 SrcMgr::FileInfo &FileInfo =
1136 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
Douglas Gregora081da52011-11-16 20:05:18 +00001137 FileInfo.NumCreatedFIDs = Record[7];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001138 if (Record[3])
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001139 FileInfo.setHasLineDirectives();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001140
Douglas Gregora081da52011-11-16 20:05:18 +00001141 const DeclID *FirstDecl = F->FileSortedDecls + Record[8];
1142 unsigned NumFileDecls = Record[9];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001143 if (NumFileDecls) {
1144 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
Argyrios Kyrtzidis9d128d02011-10-31 07:20:08 +00001145 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1146 NumFileDecls));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001147 }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001148
Douglas Gregor35f9ae62011-11-17 01:44:33 +00001149 const SrcMgr::ContentCache *ContentCache
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +00001150 = SourceMgr.getOrCreateContentCache(File,
1151 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
Douglas Gregor35f9ae62011-11-17 01:44:33 +00001152 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1153 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
Douglas Gregora081da52011-11-16 20:05:18 +00001154 unsigned Code = SLocEntryCursor.ReadCode();
1155 Record.clear();
1156 unsigned RecCode
1157 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1158
1159 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1160 Error("AST record has invalid code");
1161 return Failure;
1162 }
1163
1164 llvm::MemoryBuffer *Buffer
1165 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
1166 Filename);
1167 SourceMgr.overrideFileContents(File, Buffer);
1168 }
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001169
1170 if (Result == Failure)
1171 return Failure;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001172 break;
1173 }
1174
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001175 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001176 const char *Name = BlobStart;
1177 unsigned Offset = Record[0];
1178 unsigned Code = SLocEntryCursor.ReadCode();
1179 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001180 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001181 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001182
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001183 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001184 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001185 return Failure;
1186 }
1187
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001188 llvm::MemoryBuffer *Buffer
Douglas Gregora081da52011-11-16 20:05:18 +00001189 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
1190 Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001191 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID,
1192 BaseOffset + Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001193
Douglas Gregor6236a292011-12-02 21:56:05 +00001194 if (strcmp(Name, "<built-in>") == 0 && F->Kind == MK_PCH) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001195 PCHPredefinesBlock Block = {
1196 BufferID,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001197 StringRef(BlobStart, BlobLen - 1)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001198 };
1199 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001200 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001201
1202 break;
1203 }
1204
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001205 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001206 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruthbf340e42011-07-26 03:03:05 +00001207 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001208 ReadSourceLocation(*F, Record[2]),
1209 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001210 Record[4],
1211 ID,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001212 BaseOffset + Record[0]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001213 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001214 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001215 }
1216
1217 return Success;
1218}
1219
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001220/// \brief Find the location where the module F is imported.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001221SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001222 if (F->ImportLoc.isValid())
1223 return F->ImportLoc;
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001224
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001225 // Otherwise we have a PCH. It's considered to be "imported" at the first
1226 // location of its includer.
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001227 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001228 // Main file is the importer. We assume that it is the first entry in the
1229 // entry table. We can't ask the manager, because at the time of PCH loading
1230 // the main file entry doesn't exist yet.
1231 // The very first entry is the invalid instantiation loc, which takes up
1232 // offsets 0 and 1.
1233 return SourceLocation::getFromRawEncoding(2U);
1234 }
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001235 //return F->Loaders[0]->FirstLoc;
1236 return F->ImportedBy[0]->FirstLoc;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001237}
1238
Chris Lattner6367f6d2009-04-27 01:05:14 +00001239/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1240/// specified cursor. Read the abbreviations that are at the top of the block
1241/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001242bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001243 unsigned BlockID) {
1244 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001245 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001246 return Failure;
1247 }
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Chris Lattner6367f6d2009-04-27 01:05:14 +00001249 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001250 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001251 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001252
Chris Lattner6367f6d2009-04-27 01:05:14 +00001253 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001254 if (Code != llvm::bitc::DEFINE_ABBREV) {
1255 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001256 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001257 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001258 Cursor.ReadAbbrevRecord();
1259 }
1260}
1261
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001262void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001263 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001264
Douglas Gregor37e26842009-04-21 23:56:24 +00001265 // Keep track of where we are in the stream, then jump back there
1266 // after reading this macro.
1267 SavedStreamPosition SavedPosition(Stream);
1268
1269 Stream.JumpToBit(Offset);
1270 RecordData Record;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001271 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001272 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001273
Douglas Gregor37e26842009-04-21 23:56:24 +00001274 while (true) {
1275 unsigned Code = Stream.ReadCode();
1276 switch (Code) {
1277 case llvm::bitc::END_BLOCK:
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001278 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001279
1280 case llvm::bitc::ENTER_SUBBLOCK:
1281 // No known subblocks, always skip them.
1282 Stream.ReadSubBlockID();
1283 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001284 Error("malformed block record in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001285 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001286 }
1287 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001288
Douglas Gregor37e26842009-04-21 23:56:24 +00001289 case llvm::bitc::DEFINE_ABBREV:
1290 Stream.ReadAbbrevRecord();
1291 continue;
1292 default: break;
1293 }
1294
1295 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001296 const char *BlobStart = 0;
1297 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001298 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001299 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001300 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001301 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001302 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001303 case PP_MACRO_OBJECT_LIKE:
1304 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001305 // If we already have a macro, that means that we've hit the end
1306 // of the definition of the macro we were looking for. We're
1307 // done.
1308 if (Macro)
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001309 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001310
Douglas Gregor95eab172011-07-28 20:55:49 +00001311 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001312 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001313 Error("macro must have a name in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001314 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001315 }
Mike Stump1eb44332009-09-09 15:08:12 +00001316
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001317 unsigned NextIndex = 1;
1318 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001319 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001320
1321 SourceLocation UndefLoc = ReadSourceLocation(F, Record, NextIndex);
1322 if (UndefLoc.isValid())
1323 MI->setUndefLoc(UndefLoc);
1324
1325 MI->setIsUsed(Record[NextIndex++]);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001326 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001328 bool IsPublic = Record[NextIndex++];
Douglas Gregoraa93a872011-10-17 15:32:29 +00001329 MI->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001330
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001331 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001332 // Decode function-like macro info.
Douglas Gregor7143aab2011-09-01 17:04:32 +00001333 bool isC99VarArgs = Record[NextIndex++];
1334 bool isGNUVarArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001335 MacroArgs.clear();
Douglas Gregor7143aab2011-09-01 17:04:32 +00001336 unsigned NumArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001337 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregor7143aab2011-09-01 17:04:32 +00001338 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001339
1340 // Install function-like macro info.
1341 MI->setIsFunctionLike();
1342 if (isC99VarArgs) MI->setIsC99Varargs();
1343 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001344 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001345 PP.getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001346 }
1347
1348 // Finally, install the macro.
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001349 PP.setMacroInfo(II, MI, /*LoadedFromAST=*/true);
Douglas Gregor37e26842009-04-21 23:56:24 +00001350
1351 // Remember that we saw this macro last so that we add the tokens that
1352 // form its body to it.
1353 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001354
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001355 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1356 Record[NextIndex]) {
1357 // We have a macro definition. Register the association
1358 PreprocessedEntityID
1359 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1360 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1361 PPRec.RegisterMacroDefinition(Macro,
1362 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001363 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001364
Douglas Gregor37e26842009-04-21 23:56:24 +00001365 ++NumMacrosRead;
1366 break;
1367 }
Mike Stump1eb44332009-09-09 15:08:12 +00001368
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001369 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001370 // If we see a TOKEN before a PP_MACRO_*, then the file is
1371 // erroneous, just pretend we didn't see this.
1372 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Douglas Gregor37e26842009-04-21 23:56:24 +00001374 Token Tok;
1375 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001376 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001377 Tok.setLength(Record[1]);
Douglas Gregor95eab172011-07-28 20:55:49 +00001378 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregor37e26842009-04-21 23:56:24 +00001379 Tok.setIdentifierInfo(II);
1380 Tok.setKind((tok::TokenKind)Record[3]);
1381 Tok.setFlag((Token::TokenFlags)Record[4]);
1382 Macro->AddTokenToBody(Tok);
1383 break;
1384 }
David Blaikie7530c032012-01-17 06:56:22 +00001385 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001386 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001387}
1388
Douglas Gregor86c67d82011-07-28 22:39:26 +00001389PreprocessedEntityID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001390ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
Argyrios Kyrtzidis1f6d2252011-09-19 20:40:02 +00001391 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001392 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1393 assert(I != M.PreprocessedEntityRemap.end()
1394 && "Invalid index into preprocessed entity index remap");
1395
1396 return LocalID + I->second;
Douglas Gregor86c67d82011-07-28 22:39:26 +00001397}
1398
Douglas Gregor98339b92011-08-25 20:47:51 +00001399unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1400 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001401}
Douglas Gregor98339b92011-08-25 20:47:51 +00001402
1403HeaderFileInfoTrait::internal_key_type
1404HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1405
1406bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1407 if (strcmp(a, b) == 0)
1408 return true;
1409
1410 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1411 return false;
Douglas Gregor99a922b2011-12-09 16:22:07 +00001412
1413 // Determine whether the actual files are equivalent.
1414 bool Result = false;
1415 if (llvm::sys::fs::equivalent(a, b, Result))
Douglas Gregor98339b92011-08-25 20:47:51 +00001416 return false;
1417
Douglas Gregor99a922b2011-12-09 16:22:07 +00001418 return Result;
Douglas Gregor98339b92011-08-25 20:47:51 +00001419}
1420
1421std::pair<unsigned, unsigned>
1422HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1423 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1424 unsigned DataLen = (unsigned) *d++;
1425 return std::make_pair(KeyLen + 1, DataLen);
1426}
1427
1428HeaderFileInfoTrait::data_type
1429HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1430 unsigned DataLen) {
1431 const unsigned char *End = d + DataLen;
1432 using namespace clang::io;
1433 HeaderFileInfo HFI;
1434 unsigned Flags = *d++;
1435 HFI.isImport = (Flags >> 5) & 0x01;
1436 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1437 HFI.DirInfo = (Flags >> 2) & 0x03;
1438 HFI.Resolved = (Flags >> 1) & 0x01;
1439 HFI.IndexHeaderMapHeader = Flags & 0x01;
1440 HFI.NumIncludes = ReadUnalignedLE16(d);
Douglas Gregor541ba162011-10-17 18:53:12 +00001441 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1442 ReadUnalignedLE32(d));
Douglas Gregor98339b92011-08-25 20:47:51 +00001443 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1444 // The framework offset is 1 greater than the actual offset,
1445 // since 0 is used as an indicator for "no framework name".
1446 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1447 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1448 }
1449
1450 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1451 (void)End;
1452
1453 // This HeaderFileInfo was externally loaded.
1454 HFI.External = true;
1455 return HFI;
1456}
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001457
Douglas Gregor13292642011-12-02 15:45:10 +00001458void ASTReader::setIdentifierIsMacro(IdentifierInfo *II, ModuleFile &F,
1459 uint64_t LocalOffset, bool Visible) {
1460 if (Visible) {
1461 // Note that this identifier has a macro definition.
1462 II->setHasMacroDefinition(true);
1463 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001464
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001465 // Adjust the offset to a global offset.
Douglas Gregor2d2689a2011-07-28 21:16:51 +00001466 UnreadMacroRecordOffsets[II] = F.GlobalBitOffset + LocalOffset;
Douglas Gregor295a2a62010-10-30 00:23:06 +00001467}
1468
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001469void ASTReader::ReadDefinedMacros() {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001470 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1471 E = ModuleMgr.rend(); I != E; ++I) {
1472 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001473
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001474 // If there was no preprocessor block, skip this file.
1475 if (!MacroCursor.getBitStreamReader())
1476 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001477
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001478 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001479 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001480
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001481 RecordData Record;
1482 while (true) {
1483 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001484 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001485 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001486
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001487 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1488 // No known subblocks, always skip them.
1489 Cursor.ReadSubBlockID();
1490 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001491 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001492 return;
1493 }
1494 continue;
1495 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001496
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001497 if (Code == llvm::bitc::DEFINE_ABBREV) {
1498 Cursor.ReadAbbrevRecord();
1499 continue;
1500 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001501
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001502 // Read a record.
1503 const char *BlobStart;
1504 unsigned BlobLen;
1505 Record.clear();
1506 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1507 default: // Default behavior: ignore.
1508 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001509
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001510 case PP_MACRO_OBJECT_LIKE:
1511 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregor95eab172011-07-28 20:55:49 +00001512 getLocalIdentifier(**I, Record[0]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001513 break;
1514
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001515 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001516 // Ignore tokens.
1517 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001518 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001519 }
1520 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001521
1522 // Drain the unread macro-record offsets map.
1523 while (!UnreadMacroRecordOffsets.empty())
1524 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1525}
1526
1527void ASTReader::LoadMacroDefinition(
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001528 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
Douglas Gregor295a2a62010-10-30 00:23:06 +00001529 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
Douglas Gregor295a2a62010-10-30 00:23:06 +00001530 uint64_t Offset = Pos->second;
1531 UnreadMacroRecordOffsets.erase(Pos);
1532
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001533 RecordLocation Loc = getLocalBitOffset(Offset);
1534 ReadMacroRecord(*Loc.F, Loc.Offset);
Douglas Gregor295a2a62010-10-30 00:23:06 +00001535}
1536
1537void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1538 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1539 = UnreadMacroRecordOffsets.find(II);
1540 LoadMacroDefinition(Pos);
Douglas Gregor88a35862010-01-04 19:18:44 +00001541}
1542
Douglas Gregoreee242f2011-10-27 09:33:13 +00001543namespace {
1544 /// \brief Visitor class used to look up identifirs in an AST file.
1545 class IdentifierLookupVisitor {
1546 StringRef Name;
Douglas Gregor057df202012-01-18 20:56:22 +00001547 unsigned PriorGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001548 IdentifierInfo *Found;
1549 public:
Douglas Gregor057df202012-01-18 20:56:22 +00001550 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration)
1551 : Name(Name), PriorGeneration(PriorGeneration), Found() { }
Douglas Gregoreee242f2011-10-27 09:33:13 +00001552
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001553 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00001554 IdentifierLookupVisitor *This
1555 = static_cast<IdentifierLookupVisitor *>(UserData);
1556
Douglas Gregor057df202012-01-18 20:56:22 +00001557 // If we've already searched this module file, skip it now.
1558 if (M.Generation <= This->PriorGeneration)
1559 return true;
1560
Douglas Gregoreee242f2011-10-27 09:33:13 +00001561 ASTIdentifierLookupTable *IdTable
1562 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1563 if (!IdTable)
1564 return false;
1565
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001566 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1567 M, This->Found);
1568
Douglas Gregoreee242f2011-10-27 09:33:13 +00001569 std::pair<const char*, unsigned> Key(This->Name.begin(),
1570 This->Name.size());
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001571 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Trait);
Douglas Gregoreee242f2011-10-27 09:33:13 +00001572 if (Pos == IdTable->end())
1573 return false;
1574
1575 // Dereferencing the iterator has the effect of building the
1576 // IdentifierInfo node and populating it with the various
1577 // declarations it needs.
1578 This->Found = *Pos;
1579 return true;
1580 }
1581
1582 // \brief Retrieve the identifier info found within the module
1583 // files.
1584 IdentifierInfo *getIdentifierInfo() const { return Found; }
1585 };
1586}
1587
1588void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
Douglas Gregor057df202012-01-18 20:56:22 +00001589 unsigned PriorGeneration = 0;
David Blaikie4e4d0842012-03-11 07:00:24 +00001590 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001591 PriorGeneration = IdentifierGeneration[&II];
1592
1593 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration);
1594 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
1595 markIdentifierUpToDate(&II);
1596}
1597
1598void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1599 if (!II)
1600 return;
1601
1602 II->setOutOfDate(false);
1603
1604 // Update the generation for this identifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001605 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001606 IdentifierGeneration[II] = CurrentGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001607}
1608
Chris Lattner5f9e2722011-07-23 10:55:15 +00001609const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001610 std::string Filename = filenameStrRef;
1611 MaybeAddSystemRootToFilename(Filename);
1612 const FileEntry *File = FileMgr.getFile(Filename);
1613 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1614 OriginalDir != CurrentDir) {
1615 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1616 OriginalDir,
1617 CurrentDir);
1618 if (!resolved.empty())
1619 File = FileMgr.getFile(resolved);
1620 }
1621
1622 return File;
1623}
1624
Douglas Gregore650c8c2009-07-07 00:12:59 +00001625/// \brief If we are loading a relocatable PCH file, and the filename is
1626/// not an absolute path, add the system root to the beginning of the file
1627/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001628void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001629 // If this is not a relocatable PCH file, there's nothing to do.
1630 if (!RelocatablePCH)
1631 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001632
Michael J. Spencer256053b2010-12-17 21:22:22 +00001633 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregore650c8c2009-07-07 00:12:59 +00001634 return;
1635
Douglas Gregor832d6202011-07-22 16:35:34 +00001636 if (isysroot.empty()) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001637 // If no system root was given, default to '/'
1638 Filename.insert(Filename.begin(), '/');
1639 return;
1640 }
Mike Stump1eb44332009-09-09 15:08:12 +00001641
Douglas Gregor832d6202011-07-22 16:35:34 +00001642 unsigned Length = isysroot.size();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001643 if (isysroot[Length - 1] != '/')
1644 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001645
Douglas Gregor832d6202011-07-22 16:35:34 +00001646 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregore650c8c2009-07-07 00:12:59 +00001647}
1648
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001649ASTReader::ASTReadResult
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001650ASTReader::ReadASTBlock(ModuleFile &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001651 llvm::BitstreamCursor &Stream = F.Stream;
1652
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001653 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001654 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001655 return Failure;
1656 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001657
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001658 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001659 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001660 while (!Stream.AtEndOfStream()) {
1661 unsigned Code = Stream.ReadCode();
1662 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001663 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001664 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001665 return Failure;
1666 }
Chris Lattner7356a312009-04-11 21:15:38 +00001667
Argyrios Kyrtzidis1f941242012-09-21 01:30:00 +00001668 DeclContext *DC = Context.getTranslationUnitDecl();
1669 if (!DC->hasExternalVisibleStorage() && DC->hasExternalLexicalStorage())
1670 DC->setMustBuildLookupTable();
1671
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001672 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001673 }
1674
1675 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1676 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001677 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001678 // We lazily load the decls block, but we want to set up the
1679 // DeclsCursor cursor to point into it. Clone our current bitcode
1680 // cursor to it, enter the block and read the abbrevs in that block.
1681 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001682 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001683 if (Stream.SkipBlock() || // Skip with the main cursor.
1684 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001685 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001686 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001687 return Failure;
1688 }
1689 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001690
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001691 case DECL_UPDATES_BLOCK_ID:
1692 if (Stream.SkipBlock()) {
1693 Error("malformed block record in AST file");
1694 return Failure;
1695 }
1696 break;
1697
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001698 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001699 F.MacroCursor = Stream;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001700 if (!PP.getExternalSource())
1701 PP.setExternalSource(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00001702
Douglas Gregorecdcb882010-10-20 22:00:55 +00001703 if (Stream.SkipBlock() ||
1704 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001705 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001706 return Failure;
1707 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001708 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001709 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001710
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001711 case PREPROCESSOR_DETAIL_BLOCK_ID:
1712 F.PreprocessorDetailCursor = Stream;
1713 if (Stream.SkipBlock() ||
1714 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1715 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1716 Error("malformed preprocessor detail record in AST file");
1717 return Failure;
1718 }
1719 F.PreprocessorDetailStartOffset
1720 = F.PreprocessorDetailCursor.GetCurrentBitNo();
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001721
1722 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00001723 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001724 if (!PP.getPreprocessingRecord()->getExternalSource())
1725 PP.getPreprocessingRecord()->SetExternalSource(*this);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001726 break;
1727
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001728 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001729 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001730 case Success:
1731 break;
1732
1733 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001734 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001735 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001736
1737 case IgnorePCH:
1738 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001739 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001740 break;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001741
1742 case SUBMODULE_BLOCK_ID:
1743 switch (ReadSubmoduleBlock(F)) {
1744 case Success:
1745 break;
1746
1747 case Failure:
1748 Error("malformed submodule block in AST file");
1749 return Failure;
1750
1751 case IgnorePCH:
1752 return IgnorePCH;
1753 }
1754 break;
1755
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00001756 case COMMENTS_BLOCK_ID: {
1757 llvm::BitstreamCursor C = Stream;
1758 if (Stream.SkipBlock() ||
1759 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
1760 Error("malformed comments block in AST file");
1761 return Failure;
1762 }
1763 CommentsCursors.push_back(std::make_pair(C, &F));
1764 break;
1765 }
1766
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001767 default:
1768 if (!Stream.SkipBlock())
1769 break;
1770 Error("malformed block record in AST file");
1771 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001772 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001773 continue;
1774 }
1775
1776 if (Code == llvm::bitc::DEFINE_ABBREV) {
1777 Stream.ReadAbbrevRecord();
1778 continue;
1779 }
1780
1781 // Read and process a record.
1782 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001783 const char *BlobStart = 0;
1784 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001785 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001786 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001787 default: // Default behavior: ignore.
1788 break;
1789
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001790 case METADATA: {
1791 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1792 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001793 : diag::warn_pch_version_too_new);
1794 return IgnorePCH;
1795 }
1796
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001797 bool hasErrors = Record[5];
1798 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1799 Diag(diag::err_pch_with_compiler_errors);
1800 return IgnorePCH;
1801 }
1802
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001803 RelocatablePCH = Record[4];
1804 if (Listener) {
1805 std::string TargetTriple(BlobStart, BlobLen);
1806 if (Listener->ReadTargetTriple(TargetTriple))
1807 return IgnorePCH;
1808 }
1809 break;
1810 }
1811
Douglas Gregore95b9192011-08-17 21:07:30 +00001812 case IMPORTS: {
1813 // Load each of the imported PCH files.
1814 unsigned Idx = 0, N = Record.size();
1815 while (Idx < N) {
1816 // Read information about the AST file.
1817 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1818 unsigned Length = Record[Idx++];
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001819 SmallString<128> ImportedFile(Record.begin() + Idx,
Douglas Gregore95b9192011-08-17 21:07:30 +00001820 Record.begin() + Idx + Length);
1821 Idx += Length;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001822
Douglas Gregore95b9192011-08-17 21:07:30 +00001823 // Load the AST file.
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001824 switch(ReadASTCore(ImportedFile, ImportedKind, &F)) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001825 case Failure: return Failure;
1826 // If we have to ignore the dependency, we'll have to ignore this too.
1827 case IgnorePCH: return IgnorePCH;
1828 case Success: break;
1829 }
1830 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001831 break;
1832 }
1833
Douglas Gregora119da02011-08-02 16:26:37 +00001834 case TYPE_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001835 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001836 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001837 return Failure;
1838 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001839 F.TypeOffsets = (const uint32_t *)BlobStart;
1840 F.LocalNumTypes = Record[0];
Douglas Gregore3605012011-08-02 18:32:54 +00001841 unsigned LocalBaseTypeIndex = Record[1];
1842 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor1e849b62011-07-29 00:21:44 +00001843
Douglas Gregora119da02011-08-02 16:26:37 +00001844 if (F.LocalNumTypes > 0) {
1845 // Introduce the global -> local mapping for types within this module.
1846 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
1847
1848 // Introduce the local -> global mapping for types within this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001849 F.TypeRemap.insertOrReplace(
1850 std::make_pair(LocalBaseTypeIndex,
1851 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregora119da02011-08-02 16:26:37 +00001852
1853 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
1854 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001855 break;
Douglas Gregora119da02011-08-02 16:26:37 +00001856 }
1857
Douglas Gregor496c7092011-08-03 15:48:04 +00001858 case DECL_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001859 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001860 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001861 return Failure;
1862 }
Argyrios Kyrtzidis9d31fa72011-10-27 18:47:35 +00001863 F.DeclOffsets = (const DeclOffset *)BlobStart;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001864 F.LocalNumDecls = Record[0];
Douglas Gregor496c7092011-08-03 15:48:04 +00001865 unsigned LocalBaseDeclID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001866 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor96e973f2011-07-20 00:27:43 +00001867
Douglas Gregor496c7092011-08-03 15:48:04 +00001868 if (F.LocalNumDecls > 0) {
1869 // Introduce the global -> local mapping for declarations within this
1870 // module.
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001871 GlobalDeclMap.insert(
1872 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregor496c7092011-08-03 15:48:04 +00001873
1874 // Introduce the local -> global mapping for declarations within this
1875 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001876 F.DeclRemap.insertOrReplace(
1877 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
Douglas Gregor496c7092011-08-03 15:48:04 +00001878
Douglas Gregora1be2782011-12-17 23:38:30 +00001879 // Introduce the global -> local mapping for declarations within this
1880 // module.
1881 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
1882
Douglas Gregor496c7092011-08-03 15:48:04 +00001883 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
1884 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001885 break;
Douglas Gregor496c7092011-08-03 15:48:04 +00001886 }
1887
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001888 case TU_UPDATE_LEXICAL: {
Douglas Gregor35942772011-09-09 21:34:22 +00001889 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001890 DeclContextInfo &Info = F.DeclContextInfos[TU];
1891 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
1892 Info.NumLexicalDecls
1893 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregor35942772011-09-09 21:34:22 +00001894 TU->setHasExternalLexicalStorage(true);
Sebastian Redld692af72010-07-27 18:24:41 +00001895 break;
1896 }
1897
Sebastian Redle1dde812010-08-24 00:50:04 +00001898 case UPDATE_VISIBLE: {
Douglas Gregor496c7092011-08-03 15:48:04 +00001899 unsigned Idx = 0;
1900 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Benjamin Kramerb1758c62012-04-15 12:36:49 +00001901 ASTDeclContextNameLookupTable *Table =
1902 ASTDeclContextNameLookupTable::Create(
Douglas Gregor496c7092011-08-03 15:48:04 +00001903 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redle1dde812010-08-24 00:50:04 +00001904 (const unsigned char *)BlobStart,
Douglas Gregor393f2492011-07-22 00:38:23 +00001905 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregor35942772011-09-09 21:34:22 +00001906 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
1907 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001908 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001909 TU->setHasExternalVisibleStorage(true);
Sebastian Redle1dde812010-08-24 00:50:04 +00001910 } else
Douglas Gregor496c7092011-08-03 15:48:04 +00001911 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redle1dde812010-08-24 00:50:04 +00001912 break;
1913 }
1914
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001915 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001916 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001917 return IgnorePCH;
1918 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001919
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001920 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001921 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001922 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001923 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001924 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001925 (const unsigned char *)F.IdentifierTableData + Record[0],
1926 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00001927 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001928
1929 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001930 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001931 break;
1932
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001933 case IDENTIFIER_OFFSET: {
Sebastian Redl2da08f92010-07-19 22:28:42 +00001934 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001935 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001936 return Failure;
1937 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001938 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1939 F.LocalNumIdentifiers = Record[0];
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001940 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001941 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor67268d02011-07-20 00:59:32 +00001942
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001943 if (F.LocalNumIdentifiers > 0) {
1944 // Introduce the global -> local mapping for identifiers within this
1945 // module.
1946 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
1947 &F));
1948
1949 // Introduce the local -> global mapping for identifiers within this
1950 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001951 F.IdentifierRemap.insertOrReplace(
1952 std::make_pair(LocalBaseIdentifierID,
1953 F.BaseIdentifierID - LocalBaseIdentifierID));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001954
1955 IdentifiersLoaded.resize(IdentifiersLoaded.size()
1956 + F.LocalNumIdentifiers);
1957 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001958 break;
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001959 }
1960
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001961 case EXTERNAL_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001962 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1963 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorfdd01722009-04-14 00:24:19 +00001964 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001965
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001966 case SPECIAL_TYPES:
Douglas Gregor393f2492011-07-22 00:38:23 +00001967 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1968 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregorad1de002009-04-18 05:55:16 +00001969 break;
1970
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001971 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001972 TotalNumStatements += Record[0];
1973 TotalNumMacros += Record[1];
1974 TotalLexicalDeclContexts += Record[2];
1975 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001976 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001977
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001978 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001979 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1980 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001981 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001982
Sean Huntebcbe1d2011-05-04 23:29:54 +00001983 case DELEGATING_CTORS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001984 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1985 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00001986 break;
1987
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001988 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor31e37b22011-07-28 18:09:57 +00001989 if (Record.size() % 4 != 0) {
1990 Error("invalid weak identifiers record");
1991 return Failure;
1992 }
1993
1994 // FIXME: Ignore weak undeclared identifiers from non-original PCH
1995 // files. This isn't the way to do it :)
1996 WeakUndeclaredIdentifiers.clear();
1997
1998 // Translate the weak, undeclared identifiers into global IDs.
1999 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2000 WeakUndeclaredIdentifiers.push_back(
2001 getGlobalIdentifierID(F, Record[I++]));
2002 WeakUndeclaredIdentifiers.push_back(
2003 getGlobalIdentifierID(F, Record[I++]));
2004 WeakUndeclaredIdentifiers.push_back(
2005 ReadSourceLocation(F, Record, I).getRawEncoding());
2006 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2007 }
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002008 break;
2009
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002010 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002011 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2012 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor14c22f22009-04-22 22:18:58 +00002013 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002014
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002015 case SELECTOR_OFFSETS: {
Sebastian Redl059612d2010-08-03 21:58:15 +00002016 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00002017 F.LocalNumSelectors = Record[0];
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002018 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002019 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor96958cb2011-07-20 01:10:58 +00002020
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002021 if (F.LocalNumSelectors > 0) {
2022 // Introduce the global -> local mapping for selectors within this
2023 // module.
2024 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2025
2026 // Introduce the local -> global mapping for selectors within this
2027 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002028 F.SelectorRemap.insertOrReplace(
2029 std::make_pair(LocalBaseSelectorID,
2030 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor83941df2009-04-25 17:48:32 +00002031
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002032 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2033 }
2034 break;
2035 }
2036
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002037 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002038 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002039 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002040 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002041 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002042 F.SelectorLookupTableData + Record[0],
2043 F.SelectorLookupTableData,
Douglas Gregor409448c2011-07-21 22:35:25 +00002044 ASTSelectorLookupTrait(*this, F));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002045 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002046 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002047
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002048 case REFERENCED_SELECTOR_POOL:
Douglas Gregor8451ec72011-07-28 14:41:43 +00002049 if (!Record.empty()) {
2050 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2051 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2052 Record[Idx++]));
2053 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2054 getRawEncoding());
2055 }
2056 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00002057 break;
2058
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002059 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002060 if (!Record.empty() && Listener)
2061 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002062 break;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002063
2064 case FILE_SORTED_DECLS:
2065 F.FileSortedDecls = (const DeclID *)BlobStart;
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002066 F.NumFileSortedDecls = Record[0];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002067 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002068
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002069 case SOURCE_LOCATION_OFFSETS: {
2070 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002071 F.LocalNumSLocEntries = Record[0];
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002072 unsigned SLocSpaceSize = Record[1];
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002073 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002074 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2075 SLocSpaceSize);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002076 // Make our entry in the range map. BaseID is negative and growing, so
2077 // we invert it. Because we invert it, though, we need the other end of
2078 // the range.
2079 unsigned RangeStart =
2080 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2081 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2082 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2083
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002084 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2085 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2086 GlobalSLocOffsetMap.insert(
2087 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2088 - SLocSpaceSize,&F));
2089
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002090 // Initialize the remapping table.
2091 // Invalid stays invalid.
2092 F.SLocRemap.insert(std::make_pair(0U, 0));
2093 // This module. Base was 2 when being compiled.
2094 F.SLocRemap.insert(std::make_pair(2U,
2095 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002096
2097 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002098 break;
2099 }
2100
Douglas Gregor5d51a1d2011-08-01 16:01:55 +00002101 case MODULE_OFFSET_MAP: {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002102 // Additional remapping information.
2103 const unsigned char *Data = (const unsigned char*)BlobStart;
2104 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregorf33740e2011-08-02 10:56:51 +00002105
2106 // Continuous range maps we may be updating in our module.
2107 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002108 ContinuousRangeMap<uint32_t, int, 2>::Builder
2109 IdentifierRemap(F.IdentifierRemap);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002110 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002111 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2112 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor26ced122011-12-01 00:59:36 +00002113 SubmoduleRemap(F.SubmoduleRemap);
2114 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002115 SelectorRemap(F.SelectorRemap);
Douglas Gregor496c7092011-08-03 15:48:04 +00002116 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregora119da02011-08-02 16:26:37 +00002117 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2118
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002119 while(Data < DataEnd) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002120 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002121 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002122 Data += Len;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002123 ModuleFile *OM = ModuleMgr.lookup(Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002124 if (!OM) {
2125 Error("SourceLocation remap refers to unknown module");
2126 return Failure;
2127 }
Douglas Gregorf33740e2011-08-02 10:56:51 +00002128
2129 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2130 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2131 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor26ced122011-12-01 00:59:36 +00002132 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002133 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2134 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora119da02011-08-02 16:26:37 +00002135 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002136
2137 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2138 SLocRemap.insert(std::make_pair(SLocOffset,
2139 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002140 IdentifierRemap.insert(
2141 std::make_pair(IdentifierIDOffset,
2142 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002143 PreprocessedEntityRemap.insert(
2144 std::make_pair(PreprocessedEntityIDOffset,
2145 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregor26ced122011-12-01 00:59:36 +00002146 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2147 OM->BaseSubmoduleID - SubmoduleIDOffset));
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002148 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2149 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregor496c7092011-08-03 15:48:04 +00002150 DeclRemap.insert(std::make_pair(DeclIDOffset,
2151 OM->BaseDeclID - DeclIDOffset));
2152
Douglas Gregora119da02011-08-02 16:26:37 +00002153 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregore3605012011-08-02 18:32:54 +00002154 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregora1be2782011-12-17 23:38:30 +00002155
2156 // Global -> local mappings.
2157 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002158 }
2159 break;
2160 }
2161
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002162 case SOURCE_MANAGER_LINE_TABLE:
2163 if (ParseLineTable(F, Record))
2164 return Failure;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002165 break;
2166
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00002167 case FILE_SOURCE_LOCATION_OFFSETS:
2168 F.SLocFileOffsets = (const uint32_t *)BlobStart;
2169 F.LocalNumSLocFileEntries = Record[0];
2170 break;
2171
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002172 case SOURCE_LOCATION_PRELOADS: {
2173 // Need to transform from the local view (1-based IDs) to the global view,
2174 // which is based off F.SLocEntryBaseID.
Douglas Gregorf249bf32011-08-25 21:09:44 +00002175 if (!F.PreloadSLocEntries.empty()) {
2176 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2177 return Failure;
2178 }
2179
2180 F.PreloadSLocEntries.swap(Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002181 break;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002182 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002183
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002184 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002185 if (!DisableStatCache) {
2186 ASTStatCache *MyStatCache =
2187 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2188 (const unsigned char *)BlobStart,
2189 NumStatHits, NumStatMisses);
2190 FileMgr.addStatCache(MyStatCache);
2191 F.StatCache = MyStatCache;
2192 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002193 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002194 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002195
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002196 case EXT_VECTOR_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002197 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2198 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorb81c1702009-04-27 20:06:05 +00002199 break;
2200
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002201 case VTABLE_USES:
Douglas Gregordfe65432011-07-28 19:11:31 +00002202 if (Record.size() % 3 != 0) {
2203 Error("Invalid VTABLE_USES record");
2204 return Failure;
2205 }
2206
Sebastian Redl40566802010-08-05 18:21:25 +00002207 // Later tables overwrite earlier ones.
Douglas Gregordfe65432011-07-28 19:11:31 +00002208 // FIXME: Modules will have some trouble with this. This is clearly not
2209 // the right way to do this.
Douglas Gregor409448c2011-07-21 22:35:25 +00002210 VTableUses.clear();
Douglas Gregordfe65432011-07-28 19:11:31 +00002211
2212 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2213 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2214 VTableUses.push_back(
2215 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2216 VTableUses.push_back(Record[Idx++]);
2217 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002218 break;
2219
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002220 case DYNAMIC_CLASSES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002221 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2222 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002223 break;
2224
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002225 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorf2abb522011-07-28 19:26:52 +00002226 if (PendingInstantiations.size() % 2 != 0) {
Axel Naumann39d26c32012-10-02 09:09:43 +00002227 Error("Invalid existing PendingInstantiations");
2228 return Failure;
2229 }
2230
2231 if (Record.size() % 2 != 0) {
Douglas Gregorf2abb522011-07-28 19:26:52 +00002232 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2233 return Failure;
2234 }
Axel Naumann39d26c32012-10-02 09:09:43 +00002235
Douglas Gregorf2abb522011-07-28 19:26:52 +00002236 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2237 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2238 PendingInstantiations.push_back(
2239 ReadSourceLocation(F, Record, I).getRawEncoding());
2240 }
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002241 break;
2242
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002243 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002244 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002245 // FIXME: Modules will have some trouble with this.
2246 SemaDeclRefs.clear();
2247 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2248 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002249 break;
2250
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002251 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002252 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002253 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002254 ActualOriginalFileName.assign(BlobStart, BlobLen);
2255 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002256 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002257 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002258
Douglas Gregor31d375f2011-05-06 21:43:30 +00002259 case ORIGINAL_FILE_ID:
2260 OriginalFileID = FileID::get(Record[0]);
2261 break;
2262
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002263 case ORIGINAL_PCH_DIR:
2264 // The primary AST will be the last to get here, so it will be the one
2265 // that's used.
2266 OriginalDir.assign(BlobStart, BlobLen);
2267 break;
2268
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002269 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002270 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002271 StringRef ASTBranch(BlobStart, BlobLen);
2272 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002273 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002274 return IgnorePCH;
2275 }
2276 break;
2277 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002278
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002279 case PPD_ENTITIES_OFFSETS: {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002280 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart;
2281 assert(BlobLen % sizeof(PPEntityOffset) == 0);
2282 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002283
2284 unsigned LocalBasePreprocessedEntityID = Record[0];
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002285
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002286 unsigned StartingID;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002287 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00002288 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002289 if (!PP.getPreprocessingRecord()->getExternalSource())
2290 PP.getPreprocessingRecord()->SetExternalSource(*this);
2291 StartingID
2292 = PP.getPreprocessingRecord()
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002293 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Douglas Gregor9827a802011-07-29 00:56:45 +00002294 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002295
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002296 if (F.NumPreprocessedEntities > 0) {
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002297 // Introduce the global -> local mapping for preprocessed entities in
2298 // this module.
2299 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2300
2301 // Introduce the local -> global mapping for preprocessed entities in
2302 // this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002303 F.PreprocessedEntityRemap.insertOrReplace(
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002304 std::make_pair(LocalBasePreprocessedEntityID,
2305 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2306 }
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002307
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002308 break;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002309 }
2310
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002311 case DECL_UPDATE_OFFSETS: {
2312 if (Record.size() % 2 != 0) {
2313 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2314 return Failure;
2315 }
2316 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregor496c7092011-08-03 15:48:04 +00002317 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2318 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002319 break;
2320 }
2321
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002322 case DECL_REPLACEMENTS: {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002323 if (Record.size() % 3 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002324 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002325 return Failure;
2326 }
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002327 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
Douglas Gregor496c7092011-08-03 15:48:04 +00002328 ReplacedDecls[getGlobalDeclID(F, Record[I])]
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002329 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002330 break;
2331 }
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002332
Douglas Gregorcff9f262012-01-27 01:47:08 +00002333 case OBJC_CATEGORIES_MAP: {
2334 if (F.LocalNumObjCCategoriesInMap != 0) {
2335 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002336 return Failure;
2337 }
Douglas Gregorcff9f262012-01-27 01:47:08 +00002338
2339 F.LocalNumObjCCategoriesInMap = Record[0];
2340 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)BlobStart;
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002341 break;
2342 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002343
Douglas Gregorcff9f262012-01-27 01:47:08 +00002344 case OBJC_CATEGORIES:
2345 F.ObjCCategories.swap(Record);
2346 break;
2347
Douglas Gregor7c789c12010-10-29 22:39:52 +00002348 case CXX_BASE_SPECIFIER_OFFSETS: {
2349 if (F.LocalNumCXXBaseSpecifiers != 0) {
2350 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2351 return Failure;
2352 }
2353
2354 F.LocalNumCXXBaseSpecifiers = Record[0];
2355 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner1da90142011-07-21 21:15:19 +00002356 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002357 break;
2358 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002359
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002360 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002361 if (Record.size() % 2 != 0) {
2362 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2363 return Failure;
2364 }
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002365
2366 if (F.PragmaDiagMappings.empty())
2367 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002368 else
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002369 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2370 Record.begin(), Record.end());
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002371 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002372
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002373 case CUDA_SPECIAL_DECL_REFS:
2374 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002375 // FIXME: Modules will have trouble with this.
2376 CUDASpecialDeclRefs.clear();
2377 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2378 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002379 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002380
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002381 case HEADER_SEARCH_TABLE: {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002382 F.HeaderFileInfoTableData = BlobStart;
2383 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002384 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002385 if (Record[0]) {
2386 F.HeaderFileInfoTable
2387 = HeaderFileInfoLookupTable::Create(
2388 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002389 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregor95eab172011-07-28 20:55:49 +00002390 HeaderFileInfoTrait(*this, F,
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002391 &PP.getHeaderSearchInfo(),
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002392 BlobStart + Record[2]));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002393
2394 PP.getHeaderSearchInfo().SetExternalSource(this);
2395 if (!PP.getHeaderSearchInfo().getExternalLookup())
2396 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002397 }
2398 break;
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002399 }
2400
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002401 case FP_PRAGMA_OPTIONS:
2402 // Later tables overwrite earlier ones.
2403 FPPragmaOptions.swap(Record);
2404 break;
2405
2406 case OPENCL_EXTENSIONS:
2407 // Later tables overwrite earlier ones.
2408 OpenCLExtensions.swap(Record);
2409 break;
Sean Huntebcbe1d2011-05-04 23:29:54 +00002410
2411 case TENTATIVE_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002412 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2413 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002414 break;
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002415
2416 case KNOWN_NAMESPACES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002417 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2418 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002419 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002420
2421 case IMPORTED_MODULES: {
2422 if (F.Kind != MK_Module) {
2423 // If we aren't loading a module (which has its own exports), make
2424 // all of the imported modules visible.
2425 // FIXME: Deal with macros-only imports.
2426 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2427 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2428 ImportedModules.push_back(GlobalID);
2429 }
2430 }
2431 break;
Douglas Gregora1be2782011-12-17 23:38:30 +00002432 }
Douglas Gregor2171bf12012-01-15 16:58:34 +00002433
Douglas Gregora1be2782011-12-17 23:38:30 +00002434 case LOCAL_REDECLARATIONS: {
Douglas Gregor2171bf12012-01-15 16:58:34 +00002435 F.RedeclarationChains.swap(Record);
2436 break;
2437 }
2438
2439 case LOCAL_REDECLARATIONS_MAP: {
2440 if (F.LocalNumRedeclarationsInMap != 0) {
2441 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
Douglas Gregora1be2782011-12-17 23:38:30 +00002442 return Failure;
2443 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00002444
Douglas Gregor2171bf12012-01-15 16:58:34 +00002445 F.LocalNumRedeclarationsInMap = Record[0];
2446 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)BlobStart;
Douglas Gregora1be2782011-12-17 23:38:30 +00002447 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002448 }
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00002449
2450 case MERGED_DECLARATIONS: {
2451 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2452 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2453 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2454 for (unsigned N = Record[Idx++]; N > 0; --N)
2455 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2456 }
2457 break;
2458 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002459 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002460 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002461 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002462 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002463}
2464
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002465ASTReader::ASTReadResult ASTReader::validateFileEntries(ModuleFile &M) {
Douglas Gregorc69a2922011-08-25 20:58:51 +00002466 llvm::BitstreamCursor &SLocEntryCursor = M.SLocEntryCursor;
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002467
Douglas Gregorc69a2922011-08-25 20:58:51 +00002468 for (unsigned i = 0, e = M.LocalNumSLocFileEntries; i != e; ++i) {
2469 SLocEntryCursor.JumpToBit(M.SLocFileOffsets[i]);
2470 unsigned Code = SLocEntryCursor.ReadCode();
2471 if (Code == llvm::bitc::END_BLOCK ||
2472 Code == llvm::bitc::ENTER_SUBBLOCK ||
2473 Code == llvm::bitc::DEFINE_ABBREV) {
2474 Error("incorrectly-formatted source location entry in AST file");
2475 return Failure;
2476 }
2477
2478 RecordData Record;
2479 const char *BlobStart;
2480 unsigned BlobLen;
2481 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2482 default:
2483 Error("incorrectly-formatted source location entry in AST file");
2484 return Failure;
2485
2486 case SM_SLOC_FILE_ENTRY: {
Douglas Gregora081da52011-11-16 20:05:18 +00002487 // If the buffer was overridden, the file need not exist.
2488 if (Record[6])
2489 break;
2490
Douglas Gregorc69a2922011-08-25 20:58:51 +00002491 StringRef Filename(BlobStart, BlobLen);
2492 const FileEntry *File = getFileEntry(Filename);
2493
2494 if (File == 0) {
2495 std::string ErrorStr = "could not find file '";
2496 ErrorStr += Filename;
2497 ErrorStr += "' referenced by AST file";
2498 Error(ErrorStr.c_str());
2499 return IgnorePCH;
2500 }
2501
Douglas Gregora081da52011-11-16 20:05:18 +00002502 if (Record.size() < 7) {
Douglas Gregorc69a2922011-08-25 20:58:51 +00002503 Error("source location entry is incorrect");
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002504 return Failure;
2505 }
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002506
2507 off_t StoredSize = (off_t)Record[4];
2508 time_t StoredTime = (time_t)Record[5];
2509
2510 // Check if there was a request to override the contents of the file
2511 // that was part of the precompiled header. Overridding such a file
2512 // can lead to problems when lexing using the source locations from the
2513 // PCH.
2514 SourceManager &SM = getSourceManager();
2515 if (SM.isFileOverridden(File)) {
2516 Error(diag::err_fe_pch_file_overridden, Filename);
2517 // After emitting the diagnostic, recover by disabling the override so
2518 // that the original file will be used.
2519 SM.disableFileContentsOverride(File);
2520 // The FileEntry is a virtual file entry with the size of the contents
2521 // that would override the original contents. Set it to the original's
2522 // size/time.
2523 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2524 StoredSize, StoredTime);
2525 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002526
Douglas Gregorc69a2922011-08-25 20:58:51 +00002527 // The stat info from the FileEntry came from the cached stat
2528 // info of the PCH, so we cannot trust it.
2529 struct stat StatBuf;
2530 if (::stat(File->getName(), &StatBuf) != 0) {
2531 StatBuf.st_size = File->getSize();
2532 StatBuf.st_mtime = File->getModificationTime();
2533 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002534
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002535 if ((StoredSize != StatBuf.st_size
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002536#if !defined(LLVM_ON_WIN32)
Douglas Gregorc69a2922011-08-25 20:58:51 +00002537 // In our regression testing, the Windows file system seems to
2538 // have inconsistent modification times that sometimes
2539 // erroneously trigger this error-handling path.
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002540 || StoredTime != StatBuf.st_mtime
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002541#endif
Douglas Gregorc69a2922011-08-25 20:58:51 +00002542 )) {
2543 Error(diag::err_fe_pch_file_modified, Filename);
2544 return IgnorePCH;
2545 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002546
Douglas Gregorc69a2922011-08-25 20:58:51 +00002547 break;
2548 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002549 }
2550 }
2551
2552 return Success;
2553}
2554
Douglas Gregorecc2c092011-12-01 22:20:10 +00002555void ASTReader::makeNamesVisible(const HiddenNames &Names) {
Douglas Gregor13292642011-12-02 15:45:10 +00002556 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
2557 if (Decl *D = Names[I].dyn_cast<Decl *>())
Douglas Gregorf143ffc2012-01-06 16:22:39 +00002558 D->Hidden = false;
Douglas Gregor1d4c1132011-12-20 22:06:13 +00002559 else {
2560 IdentifierInfo *II = Names[I].get<IdentifierInfo *>();
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002561 // FIXME: Check if this works correctly with macro history.
Douglas Gregor1d4c1132011-12-20 22:06:13 +00002562 if (!II->hasMacroDefinition()) {
Douglas Gregorc12906e2012-09-24 19:56:18 +00002563 // Make sure that this macro hasn't been #undef'd in the mean-time.
2564 llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator Known
2565 = PP.Macros.find(II);
2566 if (Known == PP.Macros.end() ||
2567 Known->second->getUndefLoc().isInvalid()) {
2568 II->setHasMacroDefinition(true);
2569 if (DeserializationListener)
2570 DeserializationListener->MacroVisible(II);
2571 }
Douglas Gregor1d4c1132011-12-20 22:06:13 +00002572 }
2573 }
Douglas Gregor13292642011-12-02 15:45:10 +00002574 }
Douglas Gregorecc2c092011-12-01 22:20:10 +00002575}
2576
Douglas Gregor5e356932011-12-01 17:11:21 +00002577void ASTReader::makeModuleVisible(Module *Mod,
2578 Module::NameVisibilityKind NameVisibility) {
2579 llvm::SmallPtrSet<Module *, 4> Visited;
2580 llvm::SmallVector<Module *, 4> Stack;
2581 Stack.push_back(Mod);
2582 while (!Stack.empty()) {
2583 Mod = Stack.back();
2584 Stack.pop_back();
2585
2586 if (NameVisibility <= Mod->NameVisibility) {
2587 // This module already has this level of visibility (or greater), so
2588 // there is nothing more to do.
2589 continue;
2590 }
2591
Douglas Gregor51f564f2011-12-31 04:05:44 +00002592 if (!Mod->isAvailable()) {
2593 // Modules that aren't available cannot be made visible.
2594 continue;
2595 }
2596
Douglas Gregor5e356932011-12-01 17:11:21 +00002597 // Update the module's name visibility.
2598 Mod->NameVisibility = NameVisibility;
2599
Douglas Gregorecc2c092011-12-01 22:20:10 +00002600 // If we've already deserialized any names from this module,
Douglas Gregor5e356932011-12-01 17:11:21 +00002601 // mark them as visible.
Douglas Gregorecc2c092011-12-01 22:20:10 +00002602 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2603 if (Hidden != HiddenNamesMap.end()) {
2604 makeNamesVisible(Hidden->second);
2605 HiddenNamesMap.erase(Hidden);
2606 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002607
2608 // Push any non-explicit submodules onto the stack to be marked as
2609 // visible.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002610 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2611 SubEnd = Mod->submodule_end();
Douglas Gregor5e356932011-12-01 17:11:21 +00002612 Sub != SubEnd; ++Sub) {
Douglas Gregorb7a78192012-01-04 23:32:19 +00002613 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2614 Stack.push_back(*Sub);
Douglas Gregor5e356932011-12-01 17:11:21 +00002615 }
Douglas Gregor07165b92011-12-02 19:11:09 +00002616
2617 // Push any exported modules onto the stack to be marked as visible.
Douglas Gregor0adaa882011-12-05 17:28:06 +00002618 bool AnyWildcard = false;
2619 bool UnrestrictedWildcard = false;
2620 llvm::SmallVector<Module *, 4> WildcardRestrictions;
Douglas Gregor07165b92011-12-02 19:11:09 +00002621 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
2622 Module *Exported = Mod->Exports[I].getPointer();
Douglas Gregor0adaa882011-12-05 17:28:06 +00002623 if (!Mod->Exports[I].getInt()) {
2624 // Export a named module directly; no wildcards involved.
2625 if (Visited.insert(Exported))
Douglas Gregor07165b92011-12-02 19:11:09 +00002626 Stack.push_back(Exported);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002627
2628 continue;
Douglas Gregor07165b92011-12-02 19:11:09 +00002629 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002630
2631 // Wildcard export: export all of the imported modules that match
2632 // the given pattern.
2633 AnyWildcard = true;
2634 if (UnrestrictedWildcard)
2635 continue;
2636
2637 if (Module *Restriction = Mod->Exports[I].getPointer())
2638 WildcardRestrictions.push_back(Restriction);
2639 else {
2640 WildcardRestrictions.clear();
2641 UnrestrictedWildcard = true;
2642 }
2643 }
2644
2645 // If there were any wildcards, push any imported modules that were
2646 // re-exported by the wildcard restriction.
2647 if (!AnyWildcard)
2648 continue;
2649
2650 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
2651 Module *Imported = Mod->Imports[I];
Benjamin Kramerd48bcb22012-08-22 15:37:55 +00002652 if (!Visited.insert(Imported))
Douglas Gregor0adaa882011-12-05 17:28:06 +00002653 continue;
2654
2655 bool Acceptable = UnrestrictedWildcard;
2656 if (!Acceptable) {
2657 // Check whether this module meets one of the restrictions.
2658 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
2659 Module *Restriction = WildcardRestrictions[R];
2660 if (Imported == Restriction || Imported->isSubModuleOf(Restriction)) {
2661 Acceptable = true;
2662 break;
2663 }
2664 }
2665 }
2666
2667 if (!Acceptable)
2668 continue;
2669
Douglas Gregor0adaa882011-12-05 17:28:06 +00002670 Stack.push_back(Imported);
Douglas Gregor07165b92011-12-02 19:11:09 +00002671 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002672 }
2673}
2674
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002675ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregor72a9ae12011-07-22 16:00:58 +00002676 ModuleKind Type) {
Douglas Gregor057df202012-01-18 20:56:22 +00002677 // Bump the generation number.
Douglas Gregorcff9f262012-01-27 01:47:08 +00002678 unsigned PreviousGeneration = CurrentGeneration++;
Douglas Gregor057df202012-01-18 20:56:22 +00002679
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002680 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002681 case Failure: return Failure;
2682 case IgnorePCH: return IgnorePCH;
2683 case Success: break;
2684 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002685
2686 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor057df202012-01-18 20:56:22 +00002687
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002688 // Check the predefines buffers.
Douglas Gregor6236a292011-12-02 21:56:05 +00002689 if (!DisableValidation && Type == MK_PCH &&
Argyrios Kyrtzidis26d43cd2011-09-12 18:09:38 +00002690 // FIXME: CheckPredefinesBuffers also sets the SuggestedPredefines;
2691 // if DisableValidation is true, defines that were set on command-line
2692 // but not in the PCH file will not be added to SuggestedPredefines.
2693 CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002694 return IgnorePCH;
2695
Douglas Gregoreee242f2011-10-27 09:33:13 +00002696 // Mark all of the identifiers in the identifier table as being out of date,
2697 // so that various accessors know to check the loaded modules when the
2698 // identifier is used.
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002699 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2700 IdEnd = PP.getIdentifierTable().end();
2701 Id != IdEnd; ++Id)
Douglas Gregoreee242f2011-10-27 09:33:13 +00002702 Id->second->setOutOfDate(true);
Douglas Gregor057df202012-01-18 20:56:22 +00002703
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002704 // Resolve any unresolved module exports.
Douglas Gregor55988682011-12-05 16:33:54 +00002705 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) {
2706 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I];
2707 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002708 Module *ResolvedMod = getSubmodule(GlobalID);
2709
2710 if (Unresolved.IsImport) {
2711 if (ResolvedMod)
Douglas Gregor55988682011-12-05 16:33:54 +00002712 Unresolved.Mod->Imports.push_back(ResolvedMod);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002713 continue;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002714 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002715
2716 if (ResolvedMod || Unresolved.IsWildcard)
2717 Unresolved.Mod->Exports.push_back(
2718 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002719 }
Douglas Gregor55988682011-12-05 16:33:54 +00002720 UnresolvedModuleImportExports.clear();
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002721
Douglas Gregor35942772011-09-09 21:34:22 +00002722 InitializeContext();
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002723
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002724 if (DeserializationListener)
2725 DeserializationListener->ReaderInitialized(this);
2726
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002727 if (!OriginalFileID.isInvalid()) {
2728 OriginalFileID = FileID::get(ModuleMgr.getPrimaryModule().SLocEntryBaseID
2729 + OriginalFileID.getOpaqueValue() - 1);
2730
2731 // If this AST file is a precompiled preamble, then set the preamble file ID
2732 // of the source manager to the file source file from which the preamble was
2733 // built.
2734 if (Type == MK_Preamble) {
Argyrios Kyrtzidis507097e2011-09-19 20:40:35 +00002735 SourceMgr.setPreambleFileID(OriginalFileID);
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002736 } else if (Type == MK_MainFile) {
2737 SourceMgr.setMainFileID(OriginalFileID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002738 }
Douglas Gregor414cb642010-11-30 05:23:00 +00002739 }
2740
Douglas Gregorcff9f262012-01-27 01:47:08 +00002741 // For any Objective-C class definitions we have already loaded, make sure
2742 // that we load any additional categories.
2743 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
2744 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
2745 ObjCClassesLoaded[I],
2746 PreviousGeneration);
2747 }
2748
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002749 return Success;
2750}
2751
Chris Lattner5f9e2722011-07-23 10:55:15 +00002752ASTReader::ASTReadResult ASTReader::ReadASTCore(StringRef FileName,
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002753 ModuleKind Type,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002754 ModuleFile *ImportedBy) {
2755 ModuleFile *M;
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002756 bool NewModule;
2757 std::string ErrorStr;
2758 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
Douglas Gregor057df202012-01-18 20:56:22 +00002759 CurrentGeneration, ErrorStr);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002760
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002761 if (!M) {
2762 // We couldn't load the module.
2763 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2764 + ErrorStr;
2765 Error(Msg);
2766 return Failure;
2767 }
2768
2769 if (!NewModule) {
2770 // We've already loaded this module.
2771 return Success;
2772 }
2773
2774 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2775 // module?
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002776 if (FileName != "-") {
2777 CurrentDir = llvm::sys::path::parent_path(FileName);
2778 if (CurrentDir.empty()) CurrentDir = ".";
2779 }
2780
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002781 ModuleFile &F = *M;
Sebastian Redl9137a522010-07-16 17:50:48 +00002782 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002783 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002784 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002785
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002786 // Sniff for the signature.
2787 if (Stream.Read(8) != 'C' ||
2788 Stream.Read(8) != 'P' ||
2789 Stream.Read(8) != 'C' ||
2790 Stream.Read(8) != 'H') {
2791 Diag(diag::err_not_a_pch_file) << FileName;
2792 return Failure;
2793 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002794
Douglas Gregor2cf26342009-04-09 22:27:44 +00002795 while (!Stream.AtEndOfStream()) {
2796 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002797
Douglas Gregore1d918e2009-04-10 23:10:45 +00002798 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002799 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002800 return Failure;
2801 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002802
2803 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002804
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002805 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002806 switch (BlockID) {
2807 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002808 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002809 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002810 return Failure;
2811 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002812 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002813 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002814 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002815 case Success:
2816 break;
2817
2818 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002819 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002820
2821 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002822 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002823 // AST block, skipping subblocks, to see if there are other
2824 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002825
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002826 // FIXME: We can't clear loaded slocentries anymore.
2827 //SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002828
2829 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002830 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002831 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002832
Douglas Gregore1d918e2009-04-10 23:10:45 +00002833 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002834 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002835 break;
2836 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002837 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002838 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002839 return Failure;
2840 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002841 break;
2842 }
Mike Stump1eb44332009-09-09 15:08:12 +00002843 }
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002844
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002845 // Once read, set the ModuleFile bit base offset and update the size in
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002846 // bits of all files we've seen.
2847 F.GlobalBitOffset = TotalModulesSizeInBits;
2848 TotalModulesSizeInBits += F.SizeInBits;
2849 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
Douglas Gregorc69a2922011-08-25 20:58:51 +00002850
2851 // Make sure that the files this module was built against are still available.
2852 if (!DisableValidation) {
2853 switch(validateFileEntries(*M)) {
2854 case Failure: return Failure;
2855 case IgnorePCH: return IgnorePCH;
2856 case Success: break;
2857 }
2858 }
Douglas Gregorf249bf32011-08-25 21:09:44 +00002859
2860 // Preload SLocEntries.
2861 for (unsigned I = 0, N = M->PreloadSLocEntries.size(); I != N; ++I) {
2862 int Index = int(M->PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Argyrios Kyrtzidisac1ffcc2011-09-19 20:39:54 +00002863 // Load it through the SourceManager and don't call ReadSLocEntryRecord()
2864 // directly because the entry may have already been loaded in which case
2865 // calling ReadSLocEntryRecord() directly would trigger an assertion in
2866 // SourceManager.
2867 SourceMgr.getLoadedSLocEntryByID(Index);
Douglas Gregorf249bf32011-08-25 21:09:44 +00002868 }
2869
Douglas Gregorc69a2922011-08-25 20:58:51 +00002870
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002871 return Success;
2872}
2873
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002874void ASTReader::InitializeContext() {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002875 // If there's a listener, notify them that we "read" the translation unit.
2876 if (DeserializationListener)
Douglas Gregor35942772011-09-09 21:34:22 +00002877 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
2878 Context.getTranslationUnitDecl());
Douglas Gregor3747ee72010-10-01 01:18:02 +00002879
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002880 // Make sure we load the declaration update records for the translation unit,
2881 // if there are any.
Douglas Gregor35942772011-09-09 21:34:22 +00002882 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
2883 Context.getTranslationUnitDecl());
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002884
Douglas Gregor5f957282011-08-11 22:18:49 +00002885 // FIXME: Find a better way to deal with collisions between these
2886 // built-in types. Right now, we just ignore the problem.
2887
2888 // Load the special types.
Douglas Gregora6ea10e2012-01-17 18:09:05 +00002889 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002890 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2891 if (!Context.CFConstantStringTypeDecl)
2892 Context.setCFConstantStringType(GetType(String));
2893 }
2894
2895 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2896 QualType FileType = GetType(File);
2897 if (FileType.isNull()) {
2898 Error("FILE type is NULL");
2899 return;
2900 }
2901
2902 if (!Context.FILEDecl) {
2903 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2904 Context.setFILEDecl(Typedef->getDecl());
2905 else {
2906 const TagType *Tag = FileType->getAs<TagType>();
2907 if (!Tag) {
2908 Error("Invalid FILE type in AST file");
2909 return;
2910 }
2911 Context.setFILEDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002912 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002913 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002914 }
Douglas Gregor5f957282011-08-11 22:18:49 +00002915
Douglas Gregor72cd7a02011-11-11 19:13:12 +00002916 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002917 QualType Jmp_bufType = GetType(Jmp_buf);
2918 if (Jmp_bufType.isNull()) {
2919 Error("jmp_buf type is NULL");
2920 return;
2921 }
2922
2923 if (!Context.jmp_bufDecl) {
2924 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2925 Context.setjmp_bufDecl(Typedef->getDecl());
2926 else {
2927 const TagType *Tag = Jmp_bufType->getAs<TagType>();
2928 if (!Tag) {
2929 Error("Invalid jmp_buf type in AST file");
2930 return;
2931 }
2932 Context.setjmp_bufDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002933 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002934 }
Mike Stump782fa302009-07-28 02:25:19 +00002935 }
Douglas Gregor02a5e872011-09-10 00:30:18 +00002936
Douglas Gregor72cd7a02011-11-11 19:13:12 +00002937 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002938 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2939 if (Sigjmp_bufType.isNull()) {
2940 Error("sigjmp_buf type is NULL");
2941 return;
2942 }
2943
2944 if (!Context.sigjmp_bufDecl) {
2945 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2946 Context.setsigjmp_bufDecl(Typedef->getDecl());
2947 else {
2948 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
2949 assert(Tag && "Invalid sigjmp_buf type in AST file");
2950 Context.setsigjmp_bufDecl(Tag->getDecl());
2951 }
2952 }
2953 }
2954
2955 if (unsigned ObjCIdRedef
2956 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
2957 if (Context.ObjCIdRedefinitionType.isNull())
2958 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
2959 }
2960
2961 if (unsigned ObjCClassRedef
2962 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
2963 if (Context.ObjCClassRedefinitionType.isNull())
2964 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
2965 }
2966
2967 if (unsigned ObjCSelRedef
2968 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
2969 if (Context.ObjCSelRedefinitionType.isNull())
2970 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
2971 }
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00002972
2973 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
2974 QualType Ucontext_tType = GetType(Ucontext_t);
2975 if (Ucontext_tType.isNull()) {
2976 Error("ucontext_t type is NULL");
2977 return;
2978 }
2979
2980 if (!Context.ucontext_tDecl) {
2981 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
2982 Context.setucontext_tDecl(Typedef->getDecl());
2983 else {
2984 const TagType *Tag = Ucontext_tType->getAs<TagType>();
2985 assert(Tag && "Invalid ucontext_t type in AST file");
2986 Context.setucontext_tDecl(Tag->getDecl());
2987 }
2988 }
2989 }
Douglas Gregor5f957282011-08-11 22:18:49 +00002990 }
2991
Douglas Gregor35942772011-09-09 21:34:22 +00002992 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002993
2994 // If there were any CUDA special declarations, deserialize them.
2995 if (!CUDASpecialDeclRefs.empty()) {
2996 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
Douglas Gregor35942772011-09-09 21:34:22 +00002997 Context.setcudaConfigureCallDecl(
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002998 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2999 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003000
3001 // Re-export any modules that were imported by a non-module AST file.
3002 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3003 if (Module *Imported = getSubmodule(ImportedModules[I]))
3004 makeModuleVisible(Imported, Module::AllVisible);
3005 }
3006 ImportedModules.clear();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003007}
3008
Douglas Gregorecc2c092011-12-01 22:20:10 +00003009void ASTReader::finalizeForWriting() {
3010 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3011 HiddenEnd = HiddenNamesMap.end();
3012 Hidden != HiddenEnd; ++Hidden) {
3013 makeNamesVisible(Hidden->second);
3014 }
3015 HiddenNamesMap.clear();
3016}
3017
Douglas Gregorb64c1932009-05-12 01:31:05 +00003018/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003019/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00003020/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003021std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00003022 FileManager &FileMgr,
David Blaikied6471f72011-09-25 23:23:43 +00003023 DiagnosticsEngine &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003024 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003025 std::string ErrStr;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00003026 OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00003027 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00003028 if (!Buffer) {
Kaelyn Uhrainda01f622012-06-20 00:36:03 +00003029 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003030 return std::string();
3031 }
3032
3033 // Initialize the stream
3034 llvm::BitstreamReader StreamFile;
3035 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00003036 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00003037 (const unsigned char *)Buffer->getBufferEnd());
3038 Stream.init(StreamFile);
3039
3040 // Sniff for the signature.
3041 if (Stream.Read(8) != 'C' ||
3042 Stream.Read(8) != 'P' ||
3043 Stream.Read(8) != 'C' ||
3044 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003045 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003046 return std::string();
3047 }
3048
3049 RecordData Record;
3050 while (!Stream.AtEndOfStream()) {
3051 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00003052
Douglas Gregorb64c1932009-05-12 01:31:05 +00003053 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3054 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00003055
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003056 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003057 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003058 case AST_BLOCK_ID:
3059 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003060 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003061 return std::string();
3062 }
3063 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003064
Douglas Gregorb64c1932009-05-12 01:31:05 +00003065 default:
3066 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003067 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003068 return std::string();
3069 }
3070 break;
3071 }
3072 continue;
3073 }
3074
3075 if (Code == llvm::bitc::END_BLOCK) {
3076 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003077 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003078 return std::string();
3079 }
3080 continue;
3081 }
3082
3083 if (Code == llvm::bitc::DEFINE_ABBREV) {
3084 Stream.ReadAbbrevRecord();
3085 continue;
3086 }
3087
3088 Record.clear();
3089 const char *BlobStart = 0;
3090 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003091 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003092 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00003093 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00003094 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00003095
3096 return std::string();
3097}
3098
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003099ASTReader::ASTReadResult ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003100 // Enter the submodule block.
3101 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3102 Error("malformed submodule block record in AST file");
3103 return Failure;
3104 }
3105
3106 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
Douglas Gregor26ced122011-12-01 00:59:36 +00003107 bool First = true;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003108 Module *CurrentModule = 0;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003109 RecordData Record;
3110 while (true) {
3111 unsigned Code = F.Stream.ReadCode();
3112 if (Code == llvm::bitc::END_BLOCK) {
3113 if (F.Stream.ReadBlockEnd()) {
3114 Error("error at end of submodule block in AST file");
3115 return Failure;
3116 }
3117 return Success;
3118 }
3119
3120 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3121 // No known subblocks, always skip them.
3122 F.Stream.ReadSubBlockID();
3123 if (F.Stream.SkipBlock()) {
3124 Error("malformed block record in AST file");
3125 return Failure;
3126 }
3127 continue;
3128 }
3129
3130 if (Code == llvm::bitc::DEFINE_ABBREV) {
3131 F.Stream.ReadAbbrevRecord();
3132 continue;
3133 }
3134
3135 // Read a record.
3136 const char *BlobStart;
3137 unsigned BlobLen;
3138 Record.clear();
3139 switch (F.Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
3140 default: // Default behavior: ignore.
3141 break;
3142
3143 case SUBMODULE_DEFINITION: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003144 if (First) {
3145 Error("missing submodule metadata record at beginning of block");
3146 return Failure;
3147 }
3148
Douglas Gregore209e502011-12-06 01:10:29 +00003149 if (Record.size() < 7) {
Douglas Gregor1e123682011-12-05 22:27:44 +00003150 Error("malformed module definition");
3151 return Failure;
3152 }
3153
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003154 StringRef Name(BlobStart, BlobLen);
Douglas Gregore209e502011-12-06 01:10:29 +00003155 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3156 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3157 bool IsFramework = Record[2];
3158 bool IsExplicit = Record[3];
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003159 bool IsSystem = Record[4];
3160 bool InferSubmodules = Record[5];
3161 bool InferExplicitSubmodules = Record[6];
3162 bool InferExportWildcard = Record[7];
Douglas Gregor1e123682011-12-05 22:27:44 +00003163
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003164 Module *ParentModule = 0;
Douglas Gregor26ced122011-12-01 00:59:36 +00003165 if (Parent)
3166 ParentModule = getSubmodule(Parent);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003167
3168 // Retrieve this (sub)module from the module map, creating it if
3169 // necessary.
3170 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3171 IsFramework,
3172 IsExplicit).first;
Douglas Gregore209e502011-12-06 01:10:29 +00003173 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3174 if (GlobalIndex >= SubmodulesLoaded.size() ||
3175 SubmodulesLoaded[GlobalIndex]) {
Douglas Gregor26ced122011-12-01 00:59:36 +00003176 Error("too many submodules");
3177 return Failure;
3178 }
Douglas Gregora015cab2011-12-02 17:30:13 +00003179
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00003180 CurrentModule->setASTFile(F.File);
Douglas Gregor305dc3e2011-12-20 00:28:52 +00003181 CurrentModule->IsFromModuleFile = true;
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003182 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Douglas Gregor1e123682011-12-05 22:27:44 +00003183 CurrentModule->InferSubmodules = InferSubmodules;
3184 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3185 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregora015cab2011-12-02 17:30:13 +00003186 if (DeserializationListener)
Douglas Gregore209e502011-12-06 01:10:29 +00003187 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
Douglas Gregora015cab2011-12-02 17:30:13 +00003188
Douglas Gregore209e502011-12-06 01:10:29 +00003189 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003190 break;
3191 }
3192
Douglas Gregor77d029f2011-12-08 19:11:24 +00003193 case SUBMODULE_UMBRELLA_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003194 if (First) {
3195 Error("missing submodule metadata record at beginning of block");
3196 return Failure;
3197 }
3198
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003199 if (!CurrentModule)
3200 break;
3201
3202 StringRef FileName(BlobStart, BlobLen);
3203 if (const FileEntry *Umbrella = PP.getFileManager().getFile(FileName)) {
Douglas Gregor10694ce2011-12-08 17:39:04 +00003204 if (!CurrentModule->getUmbrellaHeader())
Douglas Gregore209e502011-12-06 01:10:29 +00003205 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
Douglas Gregor10694ce2011-12-08 17:39:04 +00003206 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003207 Error("mismatched umbrella headers in submodule");
3208 return Failure;
3209 }
3210 }
3211 break;
3212 }
3213
3214 case SUBMODULE_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003215 if (First) {
3216 Error("missing submodule metadata record at beginning of block");
3217 return Failure;
3218 }
3219
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003220 if (!CurrentModule)
3221 break;
3222
3223 // FIXME: Be more lazy about this!
3224 StringRef FileName(BlobStart, BlobLen);
3225 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3226 if (std::find(CurrentModule->Headers.begin(),
3227 CurrentModule->Headers.end(),
3228 File) == CurrentModule->Headers.end())
Douglas Gregore209e502011-12-06 01:10:29 +00003229 ModMap.addHeader(CurrentModule, File);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003230 }
3231 break;
3232 }
Douglas Gregor26ced122011-12-01 00:59:36 +00003233
Douglas Gregor77d029f2011-12-08 19:11:24 +00003234 case SUBMODULE_UMBRELLA_DIR: {
3235 if (First) {
3236 Error("missing submodule metadata record at beginning of block");
3237 return Failure;
3238 }
3239
3240 if (!CurrentModule)
3241 break;
3242
3243 StringRef DirName(BlobStart, BlobLen);
3244 if (const DirectoryEntry *Umbrella
3245 = PP.getFileManager().getDirectory(DirName)) {
3246 if (!CurrentModule->getUmbrellaDir())
3247 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3248 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3249 Error("mismatched umbrella directories in submodule");
3250 return Failure;
3251 }
3252 }
3253 break;
3254 }
3255
Douglas Gregor26ced122011-12-01 00:59:36 +00003256 case SUBMODULE_METADATA: {
3257 if (!First) {
3258 Error("submodule metadata record not at beginning of block");
3259 return Failure;
3260 }
3261 First = false;
3262
3263 F.BaseSubmoduleID = getTotalNumSubmodules();
Douglas Gregor26ced122011-12-01 00:59:36 +00003264 F.LocalNumSubmodules = Record[0];
3265 unsigned LocalBaseSubmoduleID = Record[1];
3266 if (F.LocalNumSubmodules > 0) {
3267 // Introduce the global -> local mapping for submodules within this
3268 // module.
3269 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3270
3271 // Introduce the local -> global mapping for submodules within this
3272 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00003273 F.SubmoduleRemap.insertOrReplace(
Douglas Gregor26ced122011-12-01 00:59:36 +00003274 std::make_pair(LocalBaseSubmoduleID,
3275 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3276
3277 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3278 }
3279 break;
3280 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003281
Douglas Gregor55988682011-12-05 16:33:54 +00003282 case SUBMODULE_IMPORTS: {
3283 if (First) {
3284 Error("missing submodule metadata record at beginning of block");
3285 return Failure;
3286 }
3287
3288 if (!CurrentModule)
3289 break;
3290
3291 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
3292 UnresolvedModuleImportExport Unresolved;
3293 Unresolved.File = &F;
3294 Unresolved.Mod = CurrentModule;
3295 Unresolved.ID = Record[Idx];
3296 Unresolved.IsImport = true;
3297 Unresolved.IsWildcard = false;
3298 UnresolvedModuleImportExports.push_back(Unresolved);
3299 }
3300 break;
3301 }
3302
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003303 case SUBMODULE_EXPORTS: {
3304 if (First) {
3305 Error("missing submodule metadata record at beginning of block");
3306 return Failure;
3307 }
3308
3309 if (!CurrentModule)
3310 break;
3311
3312 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor55988682011-12-05 16:33:54 +00003313 UnresolvedModuleImportExport Unresolved;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003314 Unresolved.File = &F;
Douglas Gregor55988682011-12-05 16:33:54 +00003315 Unresolved.Mod = CurrentModule;
3316 Unresolved.ID = Record[Idx];
3317 Unresolved.IsImport = false;
3318 Unresolved.IsWildcard = Record[Idx + 1];
3319 UnresolvedModuleImportExports.push_back(Unresolved);
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003320 }
3321
3322 // Once we've loaded the set of exports, there's no reason to keep
3323 // the parsed, unresolved exports around.
3324 CurrentModule->UnresolvedExports.clear();
3325 break;
3326 }
Douglas Gregor51f564f2011-12-31 04:05:44 +00003327 case SUBMODULE_REQUIRES: {
3328 if (First) {
3329 Error("missing submodule metadata record at beginning of block");
3330 return Failure;
3331 }
3332
3333 if (!CurrentModule)
3334 break;
3335
3336 CurrentModule->addRequirement(StringRef(BlobStart, BlobLen),
David Blaikie4e4d0842012-03-11 07:00:24 +00003337 Context.getLangOpts(),
Douglas Gregordc58aa72012-01-30 06:01:29 +00003338 Context.getTargetInfo());
Douglas Gregor51f564f2011-12-31 04:05:44 +00003339 break;
3340 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003341 }
3342 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003343}
3344
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003345/// \brief Parse the record that corresponds to a LangOptions data
3346/// structure.
3347///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003348/// This routine parses the language options from the AST file and then gives
3349/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003350///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003351/// \returns true if the listener deems the file unacceptable, false otherwise.
John McCall260611a2012-06-20 06:18:46 +00003352bool ASTReader::ParseLanguageOptions(const RecordData &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003353 if (Listener) {
3354 LangOptions LangOpts;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003355 unsigned Idx = 0;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003356#define LANGOPT(Name, Bits, Default, Description) \
3357 LangOpts.Name = Record[Idx++];
3358#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3359 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3360#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +00003361
3362 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3363 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3364 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003365
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00003366 unsigned Length = Record[Idx++];
3367 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3368 Record.begin() + Idx + Length);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003369 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003370 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003371
3372 return false;
3373}
3374
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003375std::pair<ModuleFile *, unsigned>
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003376ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003377 GlobalPreprocessedEntityMapType::iterator
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003378 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003379 assert(I != GlobalPreprocessedEntityMap.end() &&
3380 "Corrupted global preprocessed entity map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003381 ModuleFile *M = I->second;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003382 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
3383 return std::make_pair(M, LocalIndex);
3384}
3385
Argyrios Kyrtzidis632dcc92012-10-02 16:10:51 +00003386std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
3387ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
3388 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
3389 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
3390 Mod.NumPreprocessedEntities);
3391
3392 return std::make_pair(PreprocessingRecord::iterator(),
3393 PreprocessingRecord::iterator());
3394}
3395
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00003396std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
3397ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
3398 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
3399 ModuleDeclIterator(this, &Mod,
3400 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
3401}
3402
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003403PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
3404 PreprocessedEntityID PPID = Index+1;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003405 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3406 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003407 unsigned LocalIndex = PPInfo.second;
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003408 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
Douglas Gregor4800a5c2011-02-08 21:58:10 +00003409
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003410 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003411 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003412
3413 unsigned Code = M.PreprocessorDetailCursor.ReadCode();
3414 switch (Code) {
3415 case llvm::bitc::END_BLOCK:
3416 return 0;
3417
3418 case llvm::bitc::ENTER_SUBBLOCK:
3419 Error("unexpected subblock record in preprocessor detail block");
3420 return 0;
3421
3422 case llvm::bitc::DEFINE_ABBREV:
3423 Error("unexpected abbrevation record in preprocessor detail block");
3424 return 0;
3425
3426 default:
3427 break;
3428 }
3429
3430 if (!PP.getPreprocessingRecord()) {
3431 Error("no preprocessing record");
3432 return 0;
3433 }
3434
3435 // Read the record.
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003436 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
3437 ReadSourceLocation(M, PPOffs.End));
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003438 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
3439 const char *BlobStart = 0;
3440 unsigned BlobLen = 0;
3441 RecordData Record;
3442 PreprocessorDetailRecordTypes RecType =
3443 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord(
3444 Code, Record, BlobStart, BlobLen);
3445 switch (RecType) {
3446 case PPD_MACRO_EXPANSION: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003447 bool isBuiltin = Record[0];
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003448 IdentifierInfo *Name = 0;
3449 MacroDefinition *Def = 0;
3450 if (isBuiltin)
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003451 Name = getLocalIdentifier(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003452 else {
3453 PreprocessedEntityID
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003454 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003455 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
3456 }
3457
3458 MacroExpansion *ME;
3459 if (isBuiltin)
3460 ME = new (PPRec) MacroExpansion(Name, Range);
3461 else
3462 ME = new (PPRec) MacroExpansion(Def, Range);
3463
3464 return ME;
3465 }
3466
3467 case PPD_MACRO_DEFINITION: {
3468 // Decode the identifier info and then check again; if the macro is
3469 // still defined and associated with the identifier,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003470 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003471 MacroDefinition *MD
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003472 = new (PPRec) MacroDefinition(II, Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003473
3474 if (DeserializationListener)
3475 DeserializationListener->MacroDefinitionRead(PPID, MD);
3476
3477 return MD;
3478 }
3479
3480 case PPD_INCLUSION_DIRECTIVE: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003481 const char *FullFileNameStart = BlobStart + Record[0];
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00003482 StringRef FullFileName(FullFileNameStart, BlobLen - Record[0]);
3483 const FileEntry *File = 0;
3484 if (!FullFileName.empty())
3485 File = PP.getFileManager().getFile(FullFileName);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003486
3487 // FIXME: Stable encoding
3488 InclusionDirective::InclusionKind Kind
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003489 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003490 InclusionDirective *ID
3491 = new (PPRec) InclusionDirective(PPRec, Kind,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003492 StringRef(BlobStart, Record[0]),
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00003493 Record[1], Record[3],
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003494 File,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003495 Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003496 return ID;
3497 }
3498 }
David Blaikie7530c032012-01-17 06:56:22 +00003499
3500 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003501}
3502
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003503/// \brief \arg SLocMapI points at a chunk of a module that contains no
3504/// preprocessed entities or the entities it contains are not the ones we are
3505/// looking for. Find the next module that contains entities and return the ID
3506/// of the first entry.
3507PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
3508 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
3509 ++SLocMapI;
3510 for (GlobalSLocOffsetMapType::const_iterator
3511 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003512 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003513 if (M.NumPreprocessedEntities)
3514 return getGlobalPreprocessedEntityID(M, M.BasePreprocessedEntityID);
3515 }
3516
3517 return getTotalNumPreprocessedEntities();
3518}
3519
3520namespace {
3521
3522template <unsigned PPEntityOffset::*PPLoc>
3523struct PPEntityComp {
3524 const ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003525 ModuleFile &M;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003526
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003527 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003528
Benjamin Kramer88df1252011-09-21 06:42:26 +00003529 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
3530 SourceLocation LHS = getLoc(L);
3531 SourceLocation RHS = getLoc(R);
3532 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3533 }
3534
3535 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003536 SourceLocation LHS = getLoc(L);
3537 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3538 }
3539
Benjamin Kramer88df1252011-09-21 06:42:26 +00003540 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003541 SourceLocation RHS = getLoc(R);
3542 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3543 }
3544
3545 SourceLocation getLoc(const PPEntityOffset &PPE) const {
3546 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
3547 }
3548};
3549
3550}
3551
3552/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
3553PreprocessedEntityID
3554ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
3555 if (SourceMgr.isLocalSourceLocation(BLoc))
3556 return getTotalNumPreprocessedEntities();
3557
3558 GlobalSLocOffsetMapType::const_iterator
3559 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3560 BLoc.getOffset());
3561 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3562 "Corrupted global sloc offset map");
3563
3564 if (SLocMapI->second->NumPreprocessedEntities == 0)
3565 return findNextPreprocessedEntity(SLocMapI);
3566
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003567 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003568 typedef const PPEntityOffset *pp_iterator;
3569 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3570 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
Argyrios Kyrtzidis4cd06342011-09-22 21:17:02 +00003571
3572 size_t Count = M.NumPreprocessedEntities;
3573 size_t Half;
3574 pp_iterator First = pp_begin;
3575 pp_iterator PPI;
3576
3577 // Do a binary search manually instead of using std::lower_bound because
3578 // The end locations of entities may be unordered (when a macro expansion
3579 // is inside another macro argument), but for this case it is not important
3580 // whether we get the first macro expansion or its containing macro.
3581 while (Count > 0) {
3582 Half = Count/2;
3583 PPI = First;
3584 std::advance(PPI, Half);
3585 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
3586 BLoc)){
3587 First = PPI;
3588 ++First;
3589 Count = Count - Half - 1;
3590 } else
3591 Count = Half;
3592 }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003593
3594 if (PPI == pp_end)
3595 return findNextPreprocessedEntity(SLocMapI);
3596
3597 return getGlobalPreprocessedEntityID(M,
3598 M.BasePreprocessedEntityID + (PPI - pp_begin));
3599}
3600
3601/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
3602PreprocessedEntityID
3603ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
3604 if (SourceMgr.isLocalSourceLocation(ELoc))
3605 return getTotalNumPreprocessedEntities();
3606
3607 GlobalSLocOffsetMapType::const_iterator
3608 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3609 ELoc.getOffset());
3610 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3611 "Corrupted global sloc offset map");
3612
3613 if (SLocMapI->second->NumPreprocessedEntities == 0)
3614 return findNextPreprocessedEntity(SLocMapI);
3615
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003616 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003617 typedef const PPEntityOffset *pp_iterator;
3618 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3619 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
3620 pp_iterator PPI =
3621 std::upper_bound(pp_begin, pp_end, ELoc,
3622 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
3623
3624 if (PPI == pp_end)
3625 return findNextPreprocessedEntity(SLocMapI);
3626
3627 return getGlobalPreprocessedEntityID(M,
3628 M.BasePreprocessedEntityID + (PPI - pp_begin));
3629}
3630
3631/// \brief Returns a pair of [Begin, End) indices of preallocated
3632/// preprocessed entities that \arg Range encompasses.
3633std::pair<unsigned, unsigned>
3634 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
3635 if (Range.isInvalid())
3636 return std::make_pair(0,0);
3637 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
3638
3639 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
3640 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
3641 return std::make_pair(BeginID, EndID);
3642}
3643
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003644/// \brief Optionally returns true or false if the preallocated preprocessed
3645/// entity with index \arg Index came from file \arg FID.
3646llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
3647 FileID FID) {
3648 if (FID.isInvalid())
3649 return false;
3650
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003651 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3652 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003653 unsigned LocalIndex = PPInfo.second;
3654 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
3655
3656 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
3657 if (Loc.isInvalid())
3658 return false;
3659
3660 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
3661 return true;
3662 else
3663 return false;
3664}
3665
Douglas Gregord10a3812011-08-25 18:14:34 +00003666namespace {
3667 /// \brief Visitor used to search for information about a header file.
3668 class HeaderFileInfoVisitor {
3669 ASTReader &Reader;
3670 const FileEntry *FE;
3671
3672 llvm::Optional<HeaderFileInfo> HFI;
3673
3674 public:
3675 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
3676 : Reader(Reader), FE(FE) { }
3677
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003678 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003679 HeaderFileInfoVisitor *This
3680 = static_cast<HeaderFileInfoVisitor *>(UserData);
3681
3682 HeaderFileInfoTrait Trait(This->Reader, M,
3683 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
3684 M.HeaderFileFrameworkStrings,
3685 This->FE->getName());
3686
3687 HeaderFileInfoLookupTable *Table
3688 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
3689 if (!Table)
3690 return false;
3691
3692 // Look in the on-disk hash table for an entry for this file name.
3693 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
3694 &Trait);
3695 if (Pos == Table->end())
3696 return false;
3697
3698 This->HFI = *Pos;
3699 return true;
3700 }
3701
3702 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
3703 };
3704}
3705
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003706HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003707 HeaderFileInfoVisitor Visitor(*this, FE);
3708 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
3709 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003710 if (Listener)
Douglas Gregord10a3812011-08-25 18:14:34 +00003711 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
3712 return *HFI;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003713 }
3714
3715 return HeaderFileInfo();
3716}
3717
David Blaikied6471f72011-09-25 23:23:43 +00003718void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00003719 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003720 ModuleFile &F = *(*I);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003721 unsigned Idx = 0;
3722 while (Idx < F.PragmaDiagMappings.size()) {
3723 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00003724 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
3725 Diag.DiagStatePoints.push_back(
3726 DiagnosticsEngine::DiagStatePoint(&Diag.DiagStates.back(),
3727 FullSourceLoc(Loc, SourceMgr)));
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003728 while (1) {
3729 assert(Idx < F.PragmaDiagMappings.size() &&
3730 "Invalid data, didn't find '-1' marking end of diag/map pairs");
3731 if (Idx >= F.PragmaDiagMappings.size()) {
3732 break; // Something is messed up but at least avoid infinite loop in
3733 // release build.
3734 }
3735 unsigned DiagID = F.PragmaDiagMappings[Idx++];
3736 if (DiagID == (unsigned)-1) {
3737 break; // no more diag/map pairs for this location.
3738 }
3739 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00003740 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
3741 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003742 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003743 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00003744 }
3745}
3746
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003747/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003748ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregora119da02011-08-02 16:26:37 +00003749 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turnere9b76c12011-07-20 21:31:32 +00003750 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003751 ModuleFile *M = I->second;
Douglas Gregore3605012011-08-02 18:32:54 +00003752 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003753}
3754
3755/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00003756///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003757/// The index is the type ID, shifted and minus the number of predefs. This
3758/// routine actually reads the record corresponding to the type at the given
3759/// location. It is a helper routine for GetType, which deals with reading type
3760/// IDs.
Douglas Gregor393f2492011-07-22 00:38:23 +00003761QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003762 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00003763 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00003764
Douglas Gregor0b748912009-04-14 21:18:50 +00003765 // Keep track of where we are in the stream, then jump back there
3766 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003767 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00003768
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003769 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00003770
Douglas Gregord89275b2009-07-06 18:54:52 +00003771 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003772 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00003773
Douglas Gregor393f2492011-07-22 00:38:23 +00003774 unsigned Idx = 0;
Sebastian Redlc3632732010-10-05 15:59:54 +00003775 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003776 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003777 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003778 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3779 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003780 if (Record.size() != 2) {
3781 Error("Incorrect encoding of extended qualifier type");
3782 return QualType();
3783 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003784 QualType Base = readType(*Loc.F, Record, Idx);
3785 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
Douglas Gregor35942772011-09-09 21:34:22 +00003786 return Context.getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00003787 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003788
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003789 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003790 if (Record.size() != 1) {
3791 Error("Incorrect encoding of complex type");
3792 return QualType();
3793 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003794 QualType ElemType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003795 return Context.getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003796 }
3797
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003798 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003799 if (Record.size() != 1) {
3800 Error("Incorrect encoding of pointer type");
3801 return QualType();
3802 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003803 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003804 return Context.getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003805 }
3806
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003807 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003808 if (Record.size() != 1) {
3809 Error("Incorrect encoding of block pointer type");
3810 return QualType();
3811 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003812 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003813 return Context.getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003814 }
3815
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003816 case TYPE_LVALUE_REFERENCE: {
Richard Smithdf1550f2011-04-12 10:38:03 +00003817 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003818 Error("Incorrect encoding of lvalue reference type");
3819 return QualType();
3820 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003821 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003822 return Context.getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003823 }
3824
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003825 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003826 if (Record.size() != 1) {
3827 Error("Incorrect encoding of rvalue reference type");
3828 return QualType();
3829 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003830 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003831 return Context.getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003832 }
3833
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003834 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00003835 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003836 Error("Incorrect encoding of member pointer type");
3837 return QualType();
3838 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003839 QualType PointeeType = readType(*Loc.F, Record, Idx);
3840 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00003841 if (PointeeType.isNull() || ClassType.isNull())
3842 return QualType();
3843
Douglas Gregor35942772011-09-09 21:34:22 +00003844 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003845 }
3846
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003847 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003848 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003849 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3850 unsigned IndexTypeQuals = Record[2];
3851 unsigned Idx = 3;
3852 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003853 return Context.getConstantArrayType(ElementType, Size,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003854 ASM, IndexTypeQuals);
3855 }
3856
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003857 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003858 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003859 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3860 unsigned IndexTypeQuals = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003861 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003862 }
3863
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003864 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003865 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00003866 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3867 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00003868 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3869 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
Douglas Gregor35942772011-09-09 21:34:22 +00003870 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003871 ASM, IndexTypeQuals,
3872 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003873 }
3874
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003875 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003876 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003877 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003878 return QualType();
3879 }
3880
Douglas Gregor393f2492011-07-22 00:38:23 +00003881 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003882 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00003883 unsigned VecKind = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003884 return Context.getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00003885 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003886 }
3887
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003888 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003889 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003890 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003891 return QualType();
3892 }
3893
Douglas Gregor393f2492011-07-22 00:38:23 +00003894 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003895 unsigned NumElements = Record[1];
Douglas Gregor35942772011-09-09 21:34:22 +00003896 return Context.getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003897 }
3898
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003899 case TYPE_FUNCTION_NO_PROTO: {
John McCallf85e1932011-06-15 23:02:42 +00003900 if (Record.size() != 6) {
Douglas Gregora02b1472009-04-28 21:53:25 +00003901 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003902 return QualType();
3903 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003904 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCallf85e1932011-06-15 23:02:42 +00003905 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
3906 (CallingConv)Record[4], Record[5]);
Douglas Gregor35942772011-09-09 21:34:22 +00003907 return Context.getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003908 }
3909
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003910 case TYPE_FUNCTION_PROTO: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003911 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalle23cf432010-12-14 08:05:40 +00003912
3913 FunctionProtoType::ExtProtoInfo EPI;
3914 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmana49218e2011-04-09 08:18:08 +00003915 /*hasregparm*/ Record[2],
3916 /*regparm*/ Record[3],
John McCallf85e1932011-06-15 23:02:42 +00003917 static_cast<CallingConv>(Record[4]),
3918 /*produces*/ Record[5]);
John McCalle23cf432010-12-14 08:05:40 +00003919
John McCallf85e1932011-06-15 23:02:42 +00003920 unsigned Idx = 6;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003921 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003922 SmallVector<QualType, 16> ParamTypes;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003923 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00003924 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalle23cf432010-12-14 08:05:40 +00003925
3926 EPI.Variadic = Record[Idx++];
Richard Smitheefb3d52012-02-10 09:58:53 +00003927 EPI.HasTrailingReturn = Record[Idx++];
John McCalle23cf432010-12-14 08:05:40 +00003928 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00003929 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redl60618fa2011-03-12 11:50:43 +00003930 ExceptionSpecificationType EST =
3931 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3932 EPI.ExceptionSpecType = EST;
Douglas Gregorb0d06e22012-04-04 00:34:49 +00003933 SmallVector<QualType, 2> Exceptions;
Sebastian Redl60618fa2011-03-12 11:50:43 +00003934 if (EST == EST_Dynamic) {
3935 EPI.NumExceptions = Record[Idx++];
Sebastian Redl60618fa2011-03-12 11:50:43 +00003936 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00003937 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redl60618fa2011-03-12 11:50:43 +00003938 EPI.Exceptions = Exceptions.data();
3939 } else if (EST == EST_ComputedNoexcept) {
3940 EPI.NoexceptExpr = ReadExpr(*Loc.F);
Richard Smith7bb698a2012-04-21 17:47:47 +00003941 } else if (EST == EST_Uninstantiated) {
3942 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
3943 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Richard Smithb9d0b762012-07-27 04:22:15 +00003944 } else if (EST == EST_Unevaluated) {
3945 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Sebastian Redl60618fa2011-03-12 11:50:43 +00003946 }
Douglas Gregor35942772011-09-09 21:34:22 +00003947 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00003948 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003949 }
3950
Douglas Gregor409448c2011-07-21 22:35:25 +00003951 case TYPE_UNRESOLVED_USING: {
3952 unsigned Idx = 0;
Douglas Gregor35942772011-09-09 21:34:22 +00003953 return Context.getTypeDeclType(
Douglas Gregor409448c2011-07-21 22:35:25 +00003954 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
3955 }
3956
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003957 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003958 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003959 Error("incorrect encoding of typedef type");
3960 return QualType();
3961 }
Douglas Gregor409448c2011-07-21 22:35:25 +00003962 unsigned Idx = 0;
3963 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003964 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003965 if (!Canonical.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00003966 Canonical = Context.getCanonicalType(Canonical);
3967 return Context.getTypedefType(Decl, Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003968 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003969
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003970 case TYPE_TYPEOF_EXPR:
Douglas Gregor35942772011-09-09 21:34:22 +00003971 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003972
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003973 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003974 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003975 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003976 return QualType();
3977 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003978 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003979 return Context.getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003980 }
Mike Stump1eb44332009-09-09 15:08:12 +00003981
Douglas Gregorf8af9822012-02-12 18:42:33 +00003982 case TYPE_DECLTYPE: {
3983 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
3984 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
3985 }
Anders Carlsson395b4752009-06-24 19:06:50 +00003986
Sean Huntca63c202011-05-24 22:41:36 +00003987 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003988 QualType BaseType = readType(*Loc.F, Record, Idx);
3989 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Sean Huntca63c202011-05-24 22:41:36 +00003990 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003991 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
Sean Huntca63c202011-05-24 22:41:36 +00003992 }
3993
Richard Smith34b41d92011-02-20 03:19:35 +00003994 case TYPE_AUTO:
Douglas Gregor35942772011-09-09 21:34:22 +00003995 return Context.getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith34b41d92011-02-20 03:19:35 +00003996
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003997 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003998 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003999 Error("incorrect encoding of record type");
4000 return QualType();
4001 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004002 unsigned Idx = 0;
4003 bool IsDependent = Record[Idx++];
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004004 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4005 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4006 QualType T = Context.getRecordType(RD);
John McCallf4c73712011-01-19 06:33:43 +00004007 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004008 return T;
4009 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004010
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004011 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004012 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004013 Error("incorrect encoding of enum type");
4014 return QualType();
4015 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004016 unsigned Idx = 0;
4017 bool IsDependent = Record[Idx++];
4018 QualType T
Douglas Gregor35942772011-09-09 21:34:22 +00004019 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00004020 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004021 return T;
4022 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004023
John McCall9d156a72011-01-06 01:58:22 +00004024 case TYPE_ATTRIBUTED: {
4025 if (Record.size() != 3) {
4026 Error("incorrect encoding of attributed type");
4027 return QualType();
4028 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004029 QualType modifiedType = readType(*Loc.F, Record, Idx);
4030 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall9d156a72011-01-06 01:58:22 +00004031 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
Douglas Gregor35942772011-09-09 21:34:22 +00004032 return Context.getAttributedType(kind, modifiedType, equivalentType);
John McCall9d156a72011-01-06 01:58:22 +00004033 }
4034
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004035 case TYPE_PAREN: {
4036 if (Record.size() != 1) {
4037 Error("incorrect encoding of paren type");
4038 return QualType();
4039 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004040 QualType InnerType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004041 return Context.getParenType(InnerType);
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004042 }
4043
Douglas Gregor7536dd52010-12-20 02:24:11 +00004044 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00004045 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00004046 Error("incorrect encoding of pack expansion type");
4047 return QualType();
4048 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004049 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004050 if (Pattern.isNull())
4051 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00004052 llvm::Optional<unsigned> NumExpansions;
4053 if (Record[1])
4054 NumExpansions = Record[1] - 1;
Douglas Gregor35942772011-09-09 21:34:22 +00004055 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004056 }
4057
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004058 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004059 unsigned Idx = 0;
4060 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004061 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004062 QualType NamedType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004063 return Context.getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00004064 }
4065
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004066 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004067 unsigned Idx = 0;
Douglas Gregor409448c2011-07-21 22:35:25 +00004068 ObjCInterfaceDecl *ItfD
4069 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004070 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
John McCallc12c5bb2010-05-15 11:32:37 +00004071 }
4072
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004073 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00004074 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004075 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004076 unsigned NumProtos = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004077 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004078 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor409448c2011-07-21 22:35:25 +00004079 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004080 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004081 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004082
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004083 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004084 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004085 QualType Pointee = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004086 return Context.getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004087 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00004088
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004089 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00004090 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004091 QualType Parm = readType(*Loc.F, Record, Idx);
4092 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCall49a832b2009-10-18 09:09:24 +00004093 return
Douglas Gregor35942772011-09-09 21:34:22 +00004094 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
John McCall49a832b2009-10-18 09:09:24 +00004095 Replacement);
4096 }
John McCall3cb0ebd2010-03-10 03:28:59 +00004097
Douglas Gregorc3069d62011-01-14 02:55:32 +00004098 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4099 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004100 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorc3069d62011-01-14 02:55:32 +00004101 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004102 return Context.getSubstTemplateTypeParmPackType(
Douglas Gregorc3069d62011-01-14 02:55:32 +00004103 cast<TemplateTypeParmType>(Parm),
4104 ArgPack);
4105 }
4106
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004107 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor409448c2011-07-21 22:35:25 +00004108 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004109 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004110 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004111 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004112 return
Douglas Gregor35942772011-09-09 21:34:22 +00004113 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00004114 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004115
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004116 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004117 unsigned Idx = 0;
4118 unsigned Depth = Record[Idx++];
4119 unsigned Index = Record[Idx++];
4120 bool Pack = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004121 TemplateTypeParmDecl *D
4122 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004123 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004124 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004125
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004126 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004127 unsigned Idx = 0;
4128 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004129 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004130 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004131 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004132 if (!Canon.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004133 Canon = Context.getCanonicalType(Canon);
4134 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004135 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004136
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004137 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004138 unsigned Idx = 0;
4139 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004140 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004141 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004142 unsigned NumArgs = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004143 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004144 Args.reserve(NumArgs);
4145 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004146 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004147 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004148 Args.size(), Args.data());
4149 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004150
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004151 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004152 unsigned Idx = 0;
4153
4154 // ArrayType
Douglas Gregor393f2492011-07-22 00:38:23 +00004155 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004156 ArrayType::ArraySizeModifier ASM
4157 = (ArrayType::ArraySizeModifier)Record[Idx++];
4158 unsigned IndexTypeQuals = Record[Idx++];
4159
4160 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00004161 Expr *NumElts = ReadExpr(*Loc.F);
4162 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004163
Douglas Gregor35942772011-09-09 21:34:22 +00004164 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004165 IndexTypeQuals, Brackets);
4166 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004167
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004168 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004169 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004170 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004171 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner5f9e2722011-07-23 10:55:15 +00004172 SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00004173 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004174 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004175 QualType T;
Richard Smith3e4c6c42011-05-05 21:57:07 +00004176 if (Underlying.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004177 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004178 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004179 else
Douglas Gregor35942772011-09-09 21:34:22 +00004180 T = Context.getTemplateSpecializationType(Name, Args.data(),
Richard Smith3e4c6c42011-05-05 21:57:07 +00004181 Args.size(), Underlying);
John McCallf4c73712011-01-19 06:33:43 +00004182 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004183 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004184 }
Eli Friedmanb001de72011-10-06 23:00:33 +00004185
4186 case TYPE_ATOMIC: {
4187 if (Record.size() != 1) {
4188 Error("Incorrect encoding of atomic type");
4189 return QualType();
4190 }
4191 QualType ValueType = readType(*Loc.F, Record, Idx);
4192 return Context.getAtomicType(ValueType);
4193 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004194 }
David Blaikie7530c032012-01-17 06:56:22 +00004195 llvm_unreachable("Invalid TypeCode!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00004196}
4197
Sebastian Redlc3632732010-10-05 15:59:54 +00004198class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004199 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004200 ModuleFile &F;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004201 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00004202 unsigned &Idx;
4203
Sebastian Redlc3632732010-10-05 15:59:54 +00004204 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4205 unsigned &I) {
4206 return Reader.ReadSourceLocation(F, R, I);
4207 }
4208
Douglas Gregor409448c2011-07-21 22:35:25 +00004209 template<typename T>
4210 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4211 return Reader.ReadDeclAs<T>(F, Record, Idx);
4212 }
4213
John McCalla1ee0c52009-10-16 21:56:05 +00004214public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004215 TypeLocReader(ASTReader &Reader, ModuleFile &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004216 const ASTReader::RecordData &Record, unsigned &Idx)
Benjamin Kramerfacde172012-06-06 17:32:50 +00004217 : Reader(Reader), F(F), Record(Record), Idx(Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00004218 { }
John McCalla1ee0c52009-10-16 21:56:05 +00004219
John McCall51bd8032009-10-18 01:05:36 +00004220 // We want compile-time assurance that we've enumerated all of
4221 // these, so unfortunately we have to declare them first, then
4222 // define them out-of-line.
4223#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00004224#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00004225 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004226#include "clang/AST/TypeLocNodes.def"
4227
John McCall51bd8032009-10-18 01:05:36 +00004228 void VisitFunctionTypeLoc(FunctionTypeLoc);
4229 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004230};
4231
John McCall51bd8032009-10-18 01:05:36 +00004232void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00004233 // nothing to do
4234}
John McCall51bd8032009-10-18 01:05:36 +00004235void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004236 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00004237 if (TL.needsExtraLocalData()) {
4238 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4239 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4240 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4241 TL.setModeAttr(Record[Idx++]);
4242 }
John McCalla1ee0c52009-10-16 21:56:05 +00004243}
John McCall51bd8032009-10-18 01:05:36 +00004244void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004245 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004246}
John McCall51bd8032009-10-18 01:05:36 +00004247void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004248 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004249}
John McCall51bd8032009-10-18 01:05:36 +00004250void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004251 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004252}
John McCall51bd8032009-10-18 01:05:36 +00004253void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004254 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004255}
John McCall51bd8032009-10-18 01:05:36 +00004256void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004257 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004258}
John McCall51bd8032009-10-18 01:05:36 +00004259void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004260 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00004261 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004262}
John McCall51bd8032009-10-18 01:05:36 +00004263void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004264 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4265 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004266 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00004267 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004268 else
John McCall51bd8032009-10-18 01:05:36 +00004269 TL.setSizeExpr(0);
4270}
4271void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4272 VisitArrayTypeLoc(TL);
4273}
4274void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4275 VisitArrayTypeLoc(TL);
4276}
4277void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4278 VisitArrayTypeLoc(TL);
4279}
4280void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4281 DependentSizedArrayTypeLoc TL) {
4282 VisitArrayTypeLoc(TL);
4283}
4284void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4285 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004286 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004287}
4288void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004289 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004290}
4291void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004292 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004293}
4294void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +00004295 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
4296 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004297 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor409448c2011-07-21 22:35:25 +00004298 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004299 }
4300}
4301void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4302 VisitFunctionTypeLoc(TL);
4303}
4304void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4305 VisitFunctionTypeLoc(TL);
4306}
John McCalled976492009-12-04 22:46:56 +00004307void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004308 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00004309}
John McCall51bd8032009-10-18 01:05:36 +00004310void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004311 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004312}
4313void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004314 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4315 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4316 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004317}
4318void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004319 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4320 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4321 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4322 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004323}
4324void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004325 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004326}
Sean Huntca63c202011-05-24 22:41:36 +00004327void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4328 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4329 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4330 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4331 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4332}
Richard Smith34b41d92011-02-20 03:19:35 +00004333void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4334 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4335}
John McCall51bd8032009-10-18 01:05:36 +00004336void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004337 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004338}
4339void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004340 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004341}
John McCall9d156a72011-01-06 01:58:22 +00004342void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4343 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4344 if (TL.hasAttrOperand()) {
4345 SourceRange range;
4346 range.setBegin(ReadSourceLocation(Record, Idx));
4347 range.setEnd(ReadSourceLocation(Record, Idx));
4348 TL.setAttrOperandParensRange(range);
4349 }
4350 if (TL.hasAttrExprOperand()) {
4351 if (Record[Idx++])
4352 TL.setAttrExprOperand(Reader.ReadExpr(F));
4353 else
4354 TL.setAttrExprOperand(0);
4355 } else if (TL.hasAttrEnumOperand())
4356 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4357}
John McCall51bd8032009-10-18 01:05:36 +00004358void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004359 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004360}
John McCall49a832b2009-10-18 09:09:24 +00004361void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4362 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004363 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00004364}
Douglas Gregorc3069d62011-01-14 02:55:32 +00004365void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
4366 SubstTemplateTypeParmPackTypeLoc TL) {
4367 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4368}
John McCall51bd8032009-10-18 01:05:36 +00004369void TypeLocReader::VisitTemplateSpecializationTypeLoc(
4370 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004371 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004372 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4373 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4374 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00004375 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
4376 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00004377 Reader.GetTemplateArgumentLocInfo(F,
4378 TL.getTypePtr()->getArg(i).getKind(),
4379 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004380}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004381void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
4382 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4383 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4384}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004385void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004386 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor9e876872011-03-01 18:12:44 +00004387 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004388}
John McCall3cb0ebd2010-03-10 03:28:59 +00004389void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004390 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00004391}
Douglas Gregor4714c122010-03-31 17:34:00 +00004392void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004393 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor2494dd02011-03-01 01:34:45 +00004394 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004395 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004396}
John McCall33500952010-06-11 00:33:02 +00004397void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
4398 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004399 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004400 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Abramo Bagnara66581d42012-02-06 22:45:07 +00004401 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004402 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004403 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4404 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004405 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
4406 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00004407 Reader.GetTemplateArgumentLocInfo(F,
4408 TL.getTypePtr()->getArg(I).getKind(),
4409 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004410}
Douglas Gregor7536dd52010-12-20 02:24:11 +00004411void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
4412 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
4413}
John McCall51bd8032009-10-18 01:05:36 +00004414void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004415 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00004416}
4417void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
4418 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004419 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4420 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004421 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00004422 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004423}
John McCall54e14c42009-10-22 22:37:11 +00004424void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004425 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00004426}
Eli Friedmanb001de72011-10-06 23:00:33 +00004427void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
4428 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4429 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4430 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4431}
John McCalla1ee0c52009-10-16 21:56:05 +00004432
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004433TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004434 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00004435 unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004436 QualType InfoTy = readType(F, Record, Idx);
John McCalla1ee0c52009-10-16 21:56:05 +00004437 if (InfoTy.isNull())
4438 return 0;
4439
Douglas Gregor35942772011-09-09 21:34:22 +00004440 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00004441 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00004442 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00004443 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00004444 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00004445}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004446
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004447QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00004448 unsigned FastQuals = ID & Qualifiers::FastMask;
4449 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004450
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004451 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004452 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004453 switch ((PredefinedTypeIDs)Index) {
4454 case PREDEF_TYPE_NULL_ID: return QualType();
Douglas Gregor35942772011-09-09 21:34:22 +00004455 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
4456 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004457
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004458 case PREDEF_TYPE_CHAR_U_ID:
4459 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00004460 // FIXME: Check that the signedness of CharTy is correct!
Douglas Gregor35942772011-09-09 21:34:22 +00004461 T = Context.CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004462 break;
4463
Douglas Gregor35942772011-09-09 21:34:22 +00004464 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
4465 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
4466 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
4467 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
4468 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
4469 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
4470 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
4471 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
4472 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
4473 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
4474 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
4475 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
4476 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00004477 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004478 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
4479 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
4480 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
4481 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
4482 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
John McCall3c3b7f92011-10-25 17:37:35 +00004483 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004484 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
4485 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
4486 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
4487 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
4488 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
4489 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
4490 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
4491 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
4492 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004493
4494 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
Douglas Gregor35942772011-09-09 21:34:22 +00004495 T = Context.getAutoRRefDeductType();
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004496 break;
John McCall0ddaeb92011-10-17 18:09:15 +00004497
4498 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
4499 T = Context.ARCUnbridgedCastTy;
4500 break;
4501
Meador Ingefb40e3f2012-07-01 15:57:25 +00004502 case PREDEF_TYPE_VA_LIST_TAG:
4503 T = Context.getVaListTagType();
4504 break;
Eli Friedmana6c66ce2012-08-31 00:14:07 +00004505
4506 case PREDEF_TYPE_BUILTIN_FN:
4507 T = Context.BuiltinFnTy;
4508 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004509 }
4510
4511 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00004512 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004513 }
4514
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004515 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004516 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00004517 if (TypesLoaded[Index].isNull()) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004518 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00004519 if (TypesLoaded[Index].isNull())
4520 return QualType();
4521
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004522 TypesLoaded[Index]->setFromAST();
Sebastian Redl30c514c2010-07-14 23:45:08 +00004523 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004524 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00004525 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00004526 }
Mike Stump1eb44332009-09-09 15:08:12 +00004527
John McCall0953e762009-09-24 19:53:00 +00004528 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004529}
4530
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004531QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004532 return GetType(getGlobalTypeID(F, LocalID));
4533}
4534
4535serialization::TypeID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004536ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
Douglas Gregora119da02011-08-02 16:26:37 +00004537 unsigned FastQuals = LocalID & Qualifiers::FastMask;
4538 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
4539
4540 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
4541 return LocalID;
4542
4543 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4544 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
4545 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
4546
4547 unsigned GlobalIndex = LocalIndex + I->second;
4548 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
4549}
4550
John McCall833ca992009-10-29 08:12:44 +00004551TemplateArgumentLocInfo
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004552ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00004553 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00004554 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004555 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00004556 switch (Kind) {
4557 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004558 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00004559 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00004560 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00004561 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004562 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4563 Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00004564 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004565 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregora7fc9012011-01-05 18:58:31 +00004566 SourceLocation());
4567 }
4568 case TemplateArgument::TemplateExpansion: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004569 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4570 Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004571 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004572 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004573 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregorba68eca2011-01-05 17:40:24 +00004574 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00004575 }
John McCall833ca992009-10-29 08:12:44 +00004576 case TemplateArgument::Null:
4577 case TemplateArgument::Integral:
4578 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004579 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004580 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004581 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004582 return TemplateArgumentLocInfo();
4583 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00004584 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00004585}
4586
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004587TemplateArgumentLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004588ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004589 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004590 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004591
4592 if (Arg.getKind() == TemplateArgument::Expression) {
4593 if (Record[Index++]) // bool InfoHasSameExpr.
4594 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4595 }
Sebastian Redlc3632732010-10-05 15:59:54 +00004596 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004597 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004598}
4599
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004600Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00004601 return GetDecl(ID);
4602}
4603
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004604uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
Douglas Gregore92b8a12011-08-04 00:01:48 +00004605 unsigned &Idx){
4606 if (Idx >= Record.size())
Douglas Gregor7c789c12010-10-29 22:39:52 +00004607 return 0;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004608
Douglas Gregore92b8a12011-08-04 00:01:48 +00004609 unsigned LocalID = Record[Idx++];
4610 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004611}
4612
4613CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004614 RecordLocation Loc = getLocalBitOffset(Offset);
4615 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004616 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004617 Cursor.JumpToBit(Loc.Offset);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004618 ReadingKindTracker ReadingKind(Read_Decl, *this);
4619 RecordData Record;
4620 unsigned Code = Cursor.ReadCode();
4621 unsigned RecCode = Cursor.ReadRecord(Code, Record);
4622 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4623 Error("Malformed AST file: missing C++ base specifiers");
4624 return 0;
4625 }
4626
4627 unsigned Idx = 0;
4628 unsigned NumBases = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00004629 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004630 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4631 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004632 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004633 return Bases;
4634}
4635
Douglas Gregor409448c2011-07-21 22:35:25 +00004636serialization::DeclID
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00004637ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004638 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregor496c7092011-08-03 15:48:04 +00004639 return LocalID;
4640
4641 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004642 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregor496c7092011-08-03 15:48:04 +00004643 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
4644
4645 return LocalID + I->second;
Douglas Gregor409448c2011-07-21 22:35:25 +00004646}
4647
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004648bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004649 ModuleFile &M) const {
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004650 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
4651 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4652 return &M == I->second;
4653}
4654
Douglas Gregorcff9f262012-01-27 01:47:08 +00004655ModuleFile *ASTReader::getOwningModuleFile(Decl *D) {
4656 if (!D->isFromASTFile())
4657 return 0;
4658 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
4659 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4660 return I->second;
4661}
4662
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004663SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
4664 if (ID < NUM_PREDEF_DECL_IDS)
4665 return SourceLocation();
4666
4667 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4668
4669 if (Index > DeclsLoaded.size()) {
4670 Error("declaration ID out-of-range for AST file");
4671 return SourceLocation();
4672 }
4673
4674 if (Decl *D = DeclsLoaded[Index])
4675 return D->getLocation();
4676
4677 unsigned RawLocation = 0;
4678 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
4679 return ReadSourceLocation(*Rec.F, RawLocation);
4680}
4681
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004682Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004683 if (ID < NUM_PREDEF_DECL_IDS) {
4684 switch ((PredefinedDeclIDs)ID) {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004685 case PREDEF_DECL_NULL_ID:
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004686 return 0;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004687
4688 case PREDEF_DECL_TRANSLATION_UNIT_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004689 return Context.getTranslationUnitDecl();
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00004690
4691 case PREDEF_DECL_OBJC_ID_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004692 return Context.getObjCIdDecl();
Douglas Gregor79d67262011-08-12 05:59:41 +00004693
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004694 case PREDEF_DECL_OBJC_SEL_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004695 return Context.getObjCSelDecl();
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004696
Douglas Gregor79d67262011-08-12 05:59:41 +00004697 case PREDEF_DECL_OBJC_CLASS_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004698 return Context.getObjCClassDecl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004699
Douglas Gregora6ea10e2012-01-17 18:09:05 +00004700 case PREDEF_DECL_OBJC_PROTOCOL_ID:
4701 return Context.getObjCProtocolDecl();
4702
Douglas Gregor772eeae2011-08-12 06:49:56 +00004703 case PREDEF_DECL_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004704 return Context.getInt128Decl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004705
4706 case PREDEF_DECL_UNSIGNED_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004707 return Context.getUInt128Decl();
Douglas Gregore97179c2011-09-08 01:46:34 +00004708
4709 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004710 return Context.getObjCInstanceTypeDecl();
Meador Ingec5613b22012-06-16 03:34:49 +00004711
4712 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
4713 return Context.getBuiltinVaListDecl();
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004714 }
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004715 }
4716
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004717 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4718
Richard Smith2fbf3732011-12-20 04:39:57 +00004719 if (Index >= DeclsLoaded.size()) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004720 assert(0 && "declaration ID out-of-range for AST file");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004721 Error("declaration ID out-of-range for AST file");
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004722 return 0;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004723 }
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004724
Douglas Gregorfd002a72011-12-16 22:37:11 +00004725 if (!DeclsLoaded[Index]) {
Douglas Gregor496c7092011-08-03 15:48:04 +00004726 ReadDeclRecord(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00004727 if (DeserializationListener)
4728 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
4729 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004730
4731 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004732}
4733
Douglas Gregora1be2782011-12-17 23:38:30 +00004734DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
4735 DeclID GlobalID) {
4736 if (GlobalID < NUM_PREDEF_DECL_IDS)
4737 return GlobalID;
4738
4739 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
4740 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4741 ModuleFile *Owner = I->second;
4742
4743 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
4744 = M.GlobalToLocalDeclIDs.find(Owner);
4745 if (Pos == M.GlobalToLocalDeclIDs.end())
4746 return 0;
4747
4748 return GlobalID - Owner->BaseDeclID + Pos->second;
4749}
4750
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004751serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00004752 const RecordData &Record,
4753 unsigned &Idx) {
4754 if (Idx >= Record.size()) {
4755 Error("Corrupted AST file");
4756 return 0;
4757 }
4758
4759 return getGlobalDeclID(F, Record[Idx++]);
4760}
4761
Chris Lattner887e2b32009-04-27 05:46:25 +00004762/// \brief Resolve the offset of a statement into a statement.
4763///
4764/// This operation will read a new statement from the external
4765/// source each time it is called, and is meant to be used via a
4766/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004767Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004768 // Switch case IDs are per Decl.
4769 ClearSwitchCaseIDs();
4770
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004771 // Offset here is a global offset across the entire chain.
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004772 RecordLocation Loc = getLocalBitOffset(Offset);
4773 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
4774 return ReadStmtFromStream(*Loc.F);
Douglas Gregor250fc9c2009-04-18 00:07:54 +00004775}
4776
Douglas Gregor851c75a2011-08-24 21:27:34 +00004777namespace {
4778 class FindExternalLexicalDeclsVisitor {
4779 ASTReader &Reader;
4780 const DeclContext *DC;
4781 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004782
Douglas Gregor851c75a2011-08-24 21:27:34 +00004783 SmallVectorImpl<Decl*> &Decls;
4784 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
4785
4786 public:
4787 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
4788 bool (*isKindWeWant)(Decl::Kind),
4789 SmallVectorImpl<Decl*> &Decls)
4790 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
4791 {
4792 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
4793 PredefsVisited[I] = false;
4794 }
4795
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004796 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
Douglas Gregor851c75a2011-08-24 21:27:34 +00004797 if (Preorder)
4798 return false;
4799
4800 FindExternalLexicalDeclsVisitor *This
4801 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
4802
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004803 ModuleFile::DeclContextInfosMap::iterator Info
Douglas Gregor851c75a2011-08-24 21:27:34 +00004804 = M.DeclContextInfos.find(This->DC);
4805 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
4806 return false;
4807
4808 // Load all of the declaration IDs
4809 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
4810 *IDE = ID + Info->second.NumLexicalDecls;
4811 ID != IDE; ++ID) {
4812 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
4813 continue;
4814
4815 // Don't add predefined declarations to the lexical context more
4816 // than once.
4817 if (ID->second < NUM_PREDEF_DECL_IDS) {
4818 if (This->PredefsVisited[ID->second])
4819 continue;
4820
4821 This->PredefsVisited[ID->second] = true;
4822 }
4823
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004824 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
4825 if (!This->DC->isDeclInLexicalTraversal(D))
4826 This->Decls.push_back(D);
4827 }
Douglas Gregor851c75a2011-08-24 21:27:34 +00004828 }
4829
4830 return false;
4831 }
4832 };
4833}
4834
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004835ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00004836 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner5f9e2722011-07-23 10:55:15 +00004837 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00004838 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor851c75a2011-08-24 21:27:34 +00004839 // least. Walk all of the modules in the order they were loaded.
4840 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
4841 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00004842 ++NumLexicalDeclContextsRead;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004843 return ELR_Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004844}
4845
Douglas Gregor0d95f772011-08-24 19:03:07 +00004846namespace {
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004847
4848class DeclIDComp {
4849 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004850 ModuleFile &Mod;
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004851
4852public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004853 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004854
4855 bool operator()(LocalDeclID L, LocalDeclID R) const {
4856 SourceLocation LHS = getLocation(L);
4857 SourceLocation RHS = getLocation(R);
4858 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4859 }
4860
4861 bool operator()(SourceLocation LHS, LocalDeclID R) const {
4862 SourceLocation RHS = getLocation(R);
4863 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4864 }
4865
4866 bool operator()(LocalDeclID L, SourceLocation RHS) const {
4867 SourceLocation LHS = getLocation(L);
4868 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4869 }
4870
4871 SourceLocation getLocation(LocalDeclID ID) const {
4872 return Reader.getSourceManager().getFileLoc(
4873 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
4874 }
4875};
4876
4877}
4878
4879void ASTReader::FindFileRegionDecls(FileID File,
4880 unsigned Offset, unsigned Length,
4881 SmallVectorImpl<Decl *> &Decls) {
4882 SourceManager &SM = getSourceManager();
4883
4884 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
4885 if (I == FileDeclIDs.end())
4886 return;
4887
4888 FileDeclsInfo &DInfo = I->second;
4889 if (DInfo.Decls.empty())
4890 return;
4891
4892 SourceLocation
4893 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
4894 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
4895
4896 DeclIDComp DIDComp(*this, *DInfo.Mod);
4897 ArrayRef<serialization::LocalDeclID>::iterator
4898 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
4899 BeginLoc, DIDComp);
4900 if (BeginIt != DInfo.Decls.begin())
4901 --BeginIt;
4902
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00004903 // If we are pointing at a top-level decl inside an objc container, we need
4904 // to backtrack until we find it otherwise we will fail to report that the
4905 // region overlaps with an objc container.
4906 while (BeginIt != DInfo.Decls.begin() &&
4907 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
4908 ->isTopLevelDeclInObjCContainer())
4909 --BeginIt;
4910
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004911 ArrayRef<serialization::LocalDeclID>::iterator
4912 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
4913 EndLoc, DIDComp);
4914 if (EndIt != DInfo.Decls.end())
4915 ++EndIt;
4916
4917 for (ArrayRef<serialization::LocalDeclID>::iterator
4918 DIt = BeginIt; DIt != EndIt; ++DIt)
4919 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
4920}
4921
4922namespace {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004923 /// \brief ModuleFile visitor used to perform name lookup into a
Douglas Gregor0d95f772011-08-24 19:03:07 +00004924 /// declaration context.
4925 class DeclContextNameLookupVisitor {
4926 ASTReader &Reader;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004927 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Douglas Gregor0d95f772011-08-24 19:03:07 +00004928 DeclarationName Name;
4929 SmallVectorImpl<NamedDecl *> &Decls;
4930
4931 public:
4932 DeclContextNameLookupVisitor(ASTReader &Reader,
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004933 SmallVectorImpl<const DeclContext *> &Contexts,
4934 DeclarationName Name,
Douglas Gregor0d95f772011-08-24 19:03:07 +00004935 SmallVectorImpl<NamedDecl *> &Decls)
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004936 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
Douglas Gregor0d95f772011-08-24 19:03:07 +00004937
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004938 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00004939 DeclContextNameLookupVisitor *This
4940 = static_cast<DeclContextNameLookupVisitor *>(UserData);
4941
4942 // Check whether we have any visible declaration information for
4943 // this context in this module.
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004944 ModuleFile::DeclContextInfosMap::iterator Info;
4945 bool FoundInfo = false;
4946 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
4947 Info = M.DeclContextInfos.find(This->Contexts[I]);
4948 if (Info != M.DeclContextInfos.end() &&
4949 Info->second.NameLookupTableData) {
4950 FoundInfo = true;
4951 break;
4952 }
4953 }
Douglas Gregor0d95f772011-08-24 19:03:07 +00004954
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004955 if (!FoundInfo)
4956 return false;
4957
Douglas Gregor0d95f772011-08-24 19:03:07 +00004958 // Look for this name within this module.
4959 ASTDeclContextNameLookupTable *LookupTable =
Benjamin Kramerb1758c62012-04-15 12:36:49 +00004960 Info->second.NameLookupTableData;
Douglas Gregor0d95f772011-08-24 19:03:07 +00004961 ASTDeclContextNameLookupTable::iterator Pos
4962 = LookupTable->find(This->Name);
4963 if (Pos == LookupTable->end())
4964 return false;
4965
4966 bool FoundAnything = false;
4967 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
4968 for (; Data.first != Data.second; ++Data.first) {
4969 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
4970 if (!ND)
4971 continue;
4972
4973 if (ND->getDeclName() != This->Name) {
Axel Naumann3dd82f72012-10-01 09:51:27 +00004974 // A name might be null because the decl's redeclarable part is
4975 // currently read before reading its name. The lookup is triggered by
4976 // building that decl (likely indirectly), and so it is later in the
4977 // sense of "already existing" and can be ignored here.
Douglas Gregor0d95f772011-08-24 19:03:07 +00004978 continue;
4979 }
4980
4981 // Record this declaration.
4982 FoundAnything = true;
4983 This->Decls.push_back(ND);
4984 }
4985
4986 return FoundAnything;
4987 }
4988 };
4989}
4990
John McCall76bd1f32010-06-01 09:23:16 +00004991DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004992ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00004993 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00004994 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00004995 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00004996 if (!Name)
4997 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
4998 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004999
Chris Lattner5f9e2722011-07-23 10:55:15 +00005000 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005001
5002 // Compute the declaration contexts we need to look into. Multiple such
5003 // declaration contexts occur when two declaration contexts from disjoint
5004 // modules get merged, e.g., when two namespaces with the same name are
5005 // independently defined in separate modules.
5006 SmallVector<const DeclContext *, 2> Contexts;
5007 Contexts.push_back(DC);
5008
5009 if (DC->isNamespace()) {
5010 MergedDeclsMap::iterator Merged
5011 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5012 if (Merged != MergedDecls.end()) {
5013 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5014 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5015 }
5016 }
5017
5018 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor0d95f772011-08-24 19:03:07 +00005019 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00005020 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005021 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00005022 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00005023}
5024
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005025namespace {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005026 /// \brief ModuleFile visitor used to retrieve all visible names in a
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005027 /// declaration context.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005028 class DeclContextAllNamesVisitor {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005029 ASTReader &Reader;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005030 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005031 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005032
5033 public:
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005034 DeclContextAllNamesVisitor(ASTReader &Reader,
5035 SmallVectorImpl<const DeclContext *> &Contexts,
5036 llvm::DenseMap<DeclarationName,
5037 SmallVector<NamedDecl *, 8> > &Decls)
5038 : Reader(Reader), Contexts(Contexts), Decls(Decls) { }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005039
5040 static bool visit(ModuleFile &M, void *UserData) {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005041 DeclContextAllNamesVisitor *This
5042 = static_cast<DeclContextAllNamesVisitor *>(UserData);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005043
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005044 // Check whether we have any visible declaration information for
5045 // this context in this module.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005046 ModuleFile::DeclContextInfosMap::iterator Info;
5047 bool FoundInfo = false;
5048 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5049 Info = M.DeclContextInfos.find(This->Contexts[I]);
5050 if (Info != M.DeclContextInfos.end() &&
5051 Info->second.NameLookupTableData) {
5052 FoundInfo = true;
5053 break;
5054 }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005055 }
5056
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005057 if (!FoundInfo)
5058 return false;
5059
5060 ASTDeclContextNameLookupTable *LookupTable =
5061 Info->second.NameLookupTableData;
5062 bool FoundAnything = false;
5063 for (ASTDeclContextNameLookupTable::data_iterator
5064 I = LookupTable->data_begin(), E = LookupTable->data_end();
5065 I != E; ++I) {
5066 ASTDeclContextNameLookupTrait::data_type Data = *I;
5067 for (; Data.first != Data.second; ++Data.first) {
5068 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5069 *Data.first);
5070 if (!ND)
5071 continue;
5072
5073 // Record this declaration.
5074 FoundAnything = true;
5075 This->Decls[ND->getDeclName()].push_back(ND);
5076 }
5077 }
5078
5079 return FoundAnything;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005080 }
5081 };
5082}
5083
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005084void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005085 if (!DC->hasExternalVisibleStorage())
5086 return;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005087 llvm::DenseMap<DeclarationName, llvm::SmallVector<NamedDecl*, 8> > Decls;
5088
5089 // Compute the declaration contexts we need to look into. Multiple such
5090 // declaration contexts occur when two declaration contexts from disjoint
5091 // modules get merged, e.g., when two namespaces with the same name are
5092 // independently defined in separate modules.
5093 SmallVector<const DeclContext *, 2> Contexts;
5094 Contexts.push_back(DC);
5095
5096 if (DC->isNamespace()) {
5097 MergedDeclsMap::iterator Merged
5098 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5099 if (Merged != MergedDecls.end()) {
5100 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5101 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5102 }
5103 }
5104
5105 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls);
5106 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5107 ++NumVisibleDeclContextsRead;
5108
5109 for (llvm::DenseMap<DeclarationName,
5110 llvm::SmallVector<NamedDecl*, 8> >::iterator
5111 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5112 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5113 }
Argyrios Kyrtzidis394e5392012-04-26 18:34:14 +00005114 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005115}
5116
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005117/// \brief Under non-PCH compilation the consumer receives the objc methods
5118/// before receiving the implementation, and codegen depends on this.
5119/// We simulate this by deserializing and passing to consumer the methods of the
5120/// implementation before passing the deserialized implementation decl.
5121static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5122 ASTConsumer *Consumer) {
5123 assert(ImplD && Consumer);
5124
5125 for (ObjCImplDecl::method_iterator
5126 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00005127 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005128
5129 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5130}
5131
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005132void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005133 assert(Consumer);
5134 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005135 Decl *D = InterestingDecls.front();
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005136 InterestingDecls.pop_front();
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005137
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005138 PassInterestingDeclToConsumer(D);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005139 }
5140}
5141
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005142void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5143 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5144 PassObjCImplDeclToConsumer(ImplD, Consumer);
5145 else
5146 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5147}
5148
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005149void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00005150 this->Consumer = Consumer;
5151
Douglas Gregorfdd01722009-04-14 00:24:19 +00005152 if (!Consumer)
5153 return;
5154
5155 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005156 // Force deserialization of this decl, which will cause it to be queued for
5157 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00005158 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00005159 }
Douglas Gregor1a995dd2011-09-15 18:47:32 +00005160 ExternalDefinitions.clear();
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00005161
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005162 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00005163}
5164
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005165void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005166 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00005167
Mike Stump1eb44332009-09-09 15:08:12 +00005168 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005169 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00005170 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005171 unsigned NumDeclsLoaded
5172 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5173 (Decl *)0);
5174 unsigned NumIdentifiersLoaded
5175 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5176 IdentifiersLoaded.end(),
5177 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00005178 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005179 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5180 SelectorsLoaded.end(),
5181 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00005182
Douglas Gregor4fed3f42009-04-27 18:38:38 +00005183 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
5184 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor0cdd7982011-07-21 18:46:38 +00005185 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005186 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5187 NumSLocEntriesRead, TotalNumSLocEntries,
5188 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005189 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005190 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005191 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5192 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5193 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005194 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005195 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5196 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005197 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005198 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005199 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5200 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00005201 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005202 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00005203 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5204 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00005205 if (TotalNumStatements)
5206 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5207 NumStatementsRead, TotalNumStatements,
5208 ((float)NumStatementsRead/TotalNumStatements * 100));
5209 if (TotalNumMacros)
5210 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5211 NumMacrosRead, TotalNumMacros,
5212 ((float)NumMacrosRead/TotalNumMacros * 100));
5213 if (TotalLexicalDeclContexts)
5214 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5215 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5216 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5217 * 100));
5218 if (TotalVisibleDeclContexts)
5219 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5220 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5221 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5222 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005223 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005224 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005225 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5226 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00005227 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005228 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00005229 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00005230 std::fprintf(stderr, "\n");
Douglas Gregor23d7df52011-07-21 19:50:14 +00005231 dump();
5232 std::fprintf(stderr, "\n");
5233}
5234
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005235template<typename Key, typename ModuleFile, unsigned InitialCapacity>
Douglas Gregor23d7df52011-07-21 19:50:14 +00005236static void
Chris Lattner5f9e2722011-07-23 10:55:15 +00005237dumpModuleIDMap(StringRef Name,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005238 const ContinuousRangeMap<Key, ModuleFile *,
Douglas Gregor23d7df52011-07-21 19:50:14 +00005239 InitialCapacity> &Map) {
5240 if (Map.begin() == Map.end())
5241 return;
5242
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005243 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
Douglas Gregor23d7df52011-07-21 19:50:14 +00005244 llvm::errs() << Name << ":\n";
5245 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5246 I != IEnd; ++I) {
5247 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5248 << "\n";
5249 }
5250}
5251
Douglas Gregor23d7df52011-07-21 19:50:14 +00005252void ASTReader::dump() {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005253 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005254 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor23d7df52011-07-21 19:50:14 +00005255 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor1e849b62011-07-29 00:21:44 +00005256 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005257 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005258 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
Douglas Gregor26ced122011-12-01 00:59:36 +00005259 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005260 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005261 dumpModuleIDMap("Global preprocessed entity map",
5262 GlobalPreprocessedEntityMap);
Douglas Gregor8df5c9b2011-08-02 11:12:41 +00005263
5264 llvm::errs() << "\n*** PCH/Modules Loaded:";
5265 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5266 MEnd = ModuleMgr.end();
5267 M != MEnd; ++M)
5268 (*M)->dump();
Douglas Gregor2cf26342009-04-09 22:27:44 +00005269}
5270
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005271/// Return the amount of memory used by memory buffers, breaking down
5272/// by heap-backed versus mmap'ed memory.
5273void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005274 for (ModuleConstIterator I = ModuleMgr.begin(),
5275 E = ModuleMgr.end(); I != E; ++I) {
5276 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005277 size_t bytes = buf->getBufferSize();
5278 switch (buf->getBufferKind()) {
5279 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5280 sizes.malloc_bytes += bytes;
5281 break;
5282 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5283 sizes.mmap_bytes += bytes;
5284 break;
5285 }
5286 }
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005287 }
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005288}
5289
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005290void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005291 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005292 S.ExternalSource = this;
5293
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005294 // Makes sure any declarations that were deserialized "too early"
5295 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00005296 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005297 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
5298 PreloadedDecls[I]->getDeclName());
Douglas Gregor668c1a42009-04-21 22:25:48 +00005299 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005300 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00005301
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005302 // Load the offsets of the declarations that Sema references.
5303 // They will be lazily deserialized when needed.
5304 if (!SemaDeclRefs.empty()) {
5305 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregor1e5b6f62011-07-28 00:57:24 +00005306 if (!SemaObj->StdNamespace)
5307 SemaObj->StdNamespace = SemaDeclRefs[0];
5308 if (!SemaObj->StdBadAlloc)
5309 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005310 }
5311
Peter Collingbourne84bccea2011-02-15 19:46:30 +00005312 if (!FPPragmaOptions.empty()) {
5313 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
5314 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
5315 }
5316
5317 if (!OpenCLExtensions.empty()) {
5318 unsigned I = 0;
5319#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
5320#include "clang/Basic/OpenCLExtensions.def"
5321
5322 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
5323 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00005324}
5325
Douglas Gregor211f6e82011-08-20 04:39:52 +00005326IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregor057df202012-01-18 20:56:22 +00005327 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart),
5328 /*PriorGeneration=*/0);
Douglas Gregor211f6e82011-08-20 04:39:52 +00005329 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005330 IdentifierInfo *II = Visitor.getIdentifierInfo();
Douglas Gregor057df202012-01-18 20:56:22 +00005331 markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005332 return II;
Douglas Gregor668c1a42009-04-21 22:25:48 +00005333}
5334
Douglas Gregor95f42922010-10-14 22:11:03 +00005335namespace clang {
5336 /// \brief An identifier-lookup iterator that enumerates all of the
5337 /// identifiers stored within a set of AST files.
5338 class ASTIdentifierIterator : public IdentifierIterator {
5339 /// \brief The AST reader whose identifiers are being enumerated.
5340 const ASTReader &Reader;
5341
5342 /// \brief The current index into the chain of AST files stored in
5343 /// the AST reader.
5344 unsigned Index;
5345
5346 /// \brief The current position within the identifier lookup table
5347 /// of the current AST file.
5348 ASTIdentifierLookupTable::key_iterator Current;
5349
5350 /// \brief The end position within the identifier lookup table of
5351 /// the current AST file.
5352 ASTIdentifierLookupTable::key_iterator End;
5353
5354 public:
5355 explicit ASTIdentifierIterator(const ASTReader &Reader);
5356
Chris Lattner5f9e2722011-07-23 10:55:15 +00005357 virtual StringRef Next();
Douglas Gregor95f42922010-10-14 22:11:03 +00005358 };
5359}
5360
5361ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005362 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor95f42922010-10-14 22:11:03 +00005363 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005364 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005365 Current = IdTable->key_begin();
5366 End = IdTable->key_end();
5367}
5368
Chris Lattner5f9e2722011-07-23 10:55:15 +00005369StringRef ASTIdentifierIterator::Next() {
Douglas Gregor95f42922010-10-14 22:11:03 +00005370 while (Current == End) {
5371 // If we have exhausted all of our AST files, we're done.
5372 if (Index == 0)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005373 return StringRef();
Douglas Gregor95f42922010-10-14 22:11:03 +00005374
5375 --Index;
5376 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005377 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
5378 IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005379 Current = IdTable->key_begin();
5380 End = IdTable->key_end();
5381 }
5382
5383 // We have any identifiers remaining in the current AST file; return
5384 // the next one.
5385 std::pair<const char*, unsigned> Key = *Current;
5386 ++Current;
Chris Lattner5f9e2722011-07-23 10:55:15 +00005387 return StringRef(Key.first, Key.second);
Douglas Gregor95f42922010-10-14 22:11:03 +00005388}
5389
5390IdentifierIterator *ASTReader::getIdentifiers() const {
5391 return new ASTIdentifierIterator(*this);
5392}
5393
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005394namespace clang { namespace serialization {
5395 class ReadMethodPoolVisitor {
5396 ASTReader &Reader;
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005397 Selector Sel;
5398 unsigned PriorGeneration;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005399 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
5400 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005401
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005402 public:
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005403 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
5404 unsigned PriorGeneration)
5405 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005406
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005407 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005408 ReadMethodPoolVisitor *This
5409 = static_cast<ReadMethodPoolVisitor *>(UserData);
5410
5411 if (!M.SelectorLookupTable)
5412 return false;
5413
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005414 // If we've already searched this module file, skip it now.
5415 if (M.Generation <= This->PriorGeneration)
5416 return true;
5417
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005418 ASTSelectorLookupTable *PoolTable
5419 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
5420 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
5421 if (Pos == PoolTable->end())
5422 return false;
5423
5424 ++This->Reader.NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005425 // FIXME: Not quite happy with the statistics here. We probably should
5426 // disable this tracking when called via LoadSelector.
5427 // Also, should entries without methods count as misses?
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005428 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005429 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005430 if (This->Reader.DeserializationListener)
5431 This->Reader.DeserializationListener->SelectorRead(Data.ID,
5432 This->Sel);
5433
5434 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
5435 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
5436 return true;
Sebastian Redl725cd962010-08-04 20:40:17 +00005437 }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005438
5439 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005440 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
5441 return InstanceMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005442 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005443
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005444 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005445 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
5446 return FactoryMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005447 }
5448 };
5449} } // end namespace clang::serialization
5450
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005451/// \brief Add the given set of methods to the method list.
5452static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
5453 ObjCMethodList &List) {
5454 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
5455 S.addMethodToGlobalList(&List, Methods[I]);
5456 }
5457}
5458
5459void ASTReader::ReadMethodPool(Selector Sel) {
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005460 // Get the selector generation and update it to the current generation.
5461 unsigned &Generation = SelectorGeneration[Sel];
5462 unsigned PriorGeneration = Generation;
5463 Generation = CurrentGeneration;
5464
5465 // Search for methods defined with this selector.
5466 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005467 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005468
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005469 if (Visitor.getInstanceMethods().empty() &&
5470 Visitor.getFactoryMethods().empty()) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005471 ++NumMethodPoolMisses;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005472 return;
5473 }
5474
5475 if (!getSema())
5476 return;
5477
5478 Sema &S = *getSema();
5479 Sema::GlobalMethodPool::iterator Pos
5480 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
5481
5482 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
5483 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005484}
5485
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005486void ASTReader::ReadKnownNamespaces(
Chris Lattner5f9e2722011-07-23 10:55:15 +00005487 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005488 Namespaces.clear();
5489
5490 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
5491 if (NamespaceDecl *Namespace
5492 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
5493 Namespaces.push_back(Namespace);
5494 }
5495}
5496
Douglas Gregora8623202011-07-27 20:58:46 +00005497void ASTReader::ReadTentativeDefinitions(
5498 SmallVectorImpl<VarDecl *> &TentativeDefs) {
5499 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
5500 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
5501 if (Var)
5502 TentativeDefs.push_back(Var);
5503 }
5504 TentativeDefinitions.clear();
5505}
5506
Douglas Gregora2ee20a2011-07-27 21:45:57 +00005507void ASTReader::ReadUnusedFileScopedDecls(
5508 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
5509 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
5510 DeclaratorDecl *D
5511 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
5512 if (D)
5513 Decls.push_back(D);
5514 }
5515 UnusedFileScopedDecls.clear();
5516}
5517
Douglas Gregor0129b562011-07-27 21:57:17 +00005518void ASTReader::ReadDelegatingConstructors(
5519 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
5520 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
5521 CXXConstructorDecl *D
5522 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
5523 if (D)
5524 Decls.push_back(D);
5525 }
5526 DelegatingCtorDecls.clear();
5527}
5528
Douglas Gregord58a0a52011-07-28 00:39:29 +00005529void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
5530 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
5531 TypedefNameDecl *D
5532 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
5533 if (D)
5534 Decls.push_back(D);
5535 }
5536 ExtVectorDecls.clear();
5537}
5538
Douglas Gregora126f172011-07-28 00:53:40 +00005539void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
5540 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
5541 CXXRecordDecl *D
5542 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
5543 if (D)
5544 Decls.push_back(D);
5545 }
5546 DynamicClasses.clear();
5547}
5548
Douglas Gregorec12ce22011-07-28 14:20:37 +00005549void
5550ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
5551 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
5552 NamedDecl *D
5553 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
5554 if (D)
5555 Decls.push_back(D);
5556 }
5557 LocallyScopedExternalDecls.clear();
5558}
5559
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00005560void ASTReader::ReadReferencedSelectors(
5561 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
5562 if (ReferencedSelectorsData.empty())
5563 return;
5564
5565 // If there are @selector references added them to its pool. This is for
5566 // implementation of -Wselector.
5567 unsigned int DataSize = ReferencedSelectorsData.size()-1;
5568 unsigned I = 0;
5569 while (I < DataSize) {
5570 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
5571 SourceLocation SelLoc
5572 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
5573 Sels.push_back(std::make_pair(Sel, SelLoc));
5574 }
5575 ReferencedSelectorsData.clear();
5576}
5577
Douglas Gregor31e37b22011-07-28 18:09:57 +00005578void ASTReader::ReadWeakUndeclaredIdentifiers(
5579 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
5580 if (WeakUndeclaredIdentifiers.empty())
5581 return;
5582
5583 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
5584 IdentifierInfo *WeakId
5585 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5586 IdentifierInfo *AliasId
5587 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5588 SourceLocation Loc
5589 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
5590 bool Used = WeakUndeclaredIdentifiers[I++];
5591 WeakInfo WI(AliasId, Loc);
5592 WI.setUsed(Used);
5593 WeakIDs.push_back(std::make_pair(WeakId, WI));
5594 }
5595 WeakUndeclaredIdentifiers.clear();
5596}
5597
Douglas Gregordfe65432011-07-28 19:11:31 +00005598void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
5599 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
5600 ExternalVTableUse VT;
5601 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
5602 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
5603 VT.DefinitionRequired = VTableUses[Idx++];
5604 VTables.push_back(VT);
5605 }
5606
5607 VTableUses.clear();
5608}
5609
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00005610void ASTReader::ReadPendingInstantiations(
5611 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
5612 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
5613 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
5614 SourceLocation Loc
5615 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
Axel Naumann39d26c32012-10-02 09:09:43 +00005616
5617 // For modules, find out whether an instantiation already exists
5618 if (!getContext().getLangOpts().Modules
5619 || needPendingInstantiation(D))
5620 Pending.push_back(std::make_pair(D, Loc));
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00005621 }
5622 PendingInstantiations.clear();
5623}
5624
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005625void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00005626 // It would be complicated to avoid reading the methods anyway. So don't.
5627 ReadMethodPool(Sel);
5628}
5629
Douglas Gregor95eab172011-07-28 20:55:49 +00005630void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005631 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00005632 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005633 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005634 if (DeserializationListener)
5635 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00005636}
5637
Douglas Gregord89275b2009-07-06 18:54:52 +00005638/// \brief Set the globally-visible declarations associated with the given
5639/// identifier.
5640///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005641/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00005642/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00005643/// them.
5644///
5645/// \param II an IdentifierInfo that refers to one or more globally-visible
5646/// declarations.
5647///
5648/// \param DeclIDs the set of declaration IDs with the name @p II that are
5649/// visible at global scope.
5650///
5651/// \param Nonrecursive should be true to indicate that the caller knows that
5652/// this call is non-recursive, and therefore the globally-visible declarations
5653/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00005654void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005655ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner5f9e2722011-07-23 10:55:15 +00005656 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregord89275b2009-07-06 18:54:52 +00005657 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005658 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00005659 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
5660 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
5661 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00005662 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00005663 return;
5664 }
Mike Stump1eb44332009-09-09 15:08:12 +00005665
Douglas Gregord89275b2009-07-06 18:54:52 +00005666 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
5667 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
5668 if (SemaObj) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005669 // Introduce this declaration into the translation-unit scope
5670 // and add it to the declaration chain for this identifier, so
5671 // that (unqualified) name lookup will find it.
5672 SemaObj->pushExternalDeclIntoScope(D, II);
Douglas Gregord89275b2009-07-06 18:54:52 +00005673 } else {
5674 // Queue this declaration so that it will be added to the
5675 // translation unit scope and identifier's declaration chain
5676 // once a Sema object is known.
5677 PreloadedDecls.push_back(D);
5678 }
5679 }
5680}
5681
Douglas Gregor95eab172011-07-28 20:55:49 +00005682IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00005683 if (ID == 0)
5684 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00005685
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005686 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005687 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00005688 return 0;
5689 }
Mike Stump1eb44332009-09-09 15:08:12 +00005690
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005691 ID -= 1;
5692 if (!IdentifiersLoaded[ID]) {
Douglas Gregor67268d02011-07-20 00:59:32 +00005693 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
5694 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005695 ModuleFile *M = I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00005696 unsigned Index = ID - M->BaseIdentifierID;
5697 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregord6595a42009-04-25 21:04:17 +00005698
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005699 // All of the strings in the AST file are preceded by a 16-bit length.
5700 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00005701 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
5702 // unsigned integers. This is important to avoid integer overflow when
5703 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00005704 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00005705 unsigned StrLen = (((unsigned) StrLenPtr[0])
5706 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005707 IdentifiersLoaded[ID]
Douglas Gregor712f2fc2011-09-09 22:02:16 +00005708 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005709 if (DeserializationListener)
5710 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00005711 }
Mike Stump1eb44332009-09-09 15:08:12 +00005712
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005713 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00005714}
5715
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005716IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
Douglas Gregor95eab172011-07-28 20:55:49 +00005717 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
5718}
5719
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005720IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +00005721 if (LocalID < NUM_PREDEF_IDENT_IDS)
5722 return LocalID;
5723
5724 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5725 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
5726 assert(I != M.IdentifierRemap.end()
5727 && "Invalid index into identifier index remap");
5728
5729 return LocalID + I->second;
Douglas Gregor95eab172011-07-28 20:55:49 +00005730}
5731
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005732bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregore23ac652011-04-20 00:21:03 +00005733 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005734}
5735
Douglas Gregor26ced122011-12-01 00:59:36 +00005736serialization::SubmoduleID
5737ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
5738 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
5739 return LocalID;
5740
5741 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5742 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
5743 assert(I != M.SubmoduleRemap.end()
5744 && "Invalid index into identifier index remap");
5745
5746 return LocalID + I->second;
5747}
5748
5749Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
5750 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
5751 assert(GlobalID == 0 && "Unhandled global submodule ID");
5752 return 0;
5753 }
5754
5755 if (GlobalID > SubmodulesLoaded.size()) {
5756 Error("submodule ID out of range in AST file");
5757 return 0;
5758 }
5759
5760 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
5761}
5762
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005763Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
Douglas Gregor2d2689a2011-07-28 21:16:51 +00005764 return DecodeSelector(getGlobalSelectorID(M, LocalID));
5765}
5766
5767Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005768 if (ID == 0)
5769 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00005770
Sebastian Redl725cd962010-08-04 20:40:17 +00005771 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005772 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005773 return Selector();
5774 }
Douglas Gregor83941df2009-04-25 17:48:32 +00005775
Sebastian Redl725cd962010-08-04 20:40:17 +00005776 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005777 // Load this selector from the selector table.
Douglas Gregor96958cb2011-07-20 01:10:58 +00005778 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
5779 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005780 ModuleFile &M = *I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00005781 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005782 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor96958cb2011-07-20 01:10:58 +00005783 SelectorsLoaded[ID - 1] =
Douglas Gregor9827a802011-07-29 00:56:45 +00005784 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor96958cb2011-07-20 01:10:58 +00005785 if (DeserializationListener)
5786 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor83941df2009-04-25 17:48:32 +00005787 }
5788
Sebastian Redl725cd962010-08-04 20:40:17 +00005789 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005790}
5791
Douglas Gregor8451ec72011-07-28 14:41:43 +00005792Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00005793 return DecodeSelector(ID);
5794}
5795
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005796uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00005797 // ID 0 (the null selector) is considered an external selector.
5798 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00005799}
5800
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005801serialization::SelectorID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005802ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005803 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
5804 return LocalID;
5805
5806 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5807 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
5808 assert(I != M.SelectorRemap.end()
5809 && "Invalid index into identifier index remap");
5810
5811 return LocalID + I->second;
Douglas Gregor8451ec72011-07-28 14:41:43 +00005812}
5813
Mike Stump1eb44332009-09-09 15:08:12 +00005814DeclarationName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005815ASTReader::ReadDeclarationName(ModuleFile &F,
Douglas Gregor393f2492011-07-22 00:38:23 +00005816 const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00005817 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
5818 switch (Kind) {
5819 case DeclarationName::Identifier:
Douglas Gregor95eab172011-07-28 20:55:49 +00005820 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005821
5822 case DeclarationName::ObjCZeroArgSelector:
5823 case DeclarationName::ObjCOneArgSelector:
5824 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor2d2689a2011-07-28 21:16:51 +00005825 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005826
5827 case DeclarationName::CXXConstructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005828 return Context.DeclarationNames.getCXXConstructorName(
5829 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005830
5831 case DeclarationName::CXXDestructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005832 return Context.DeclarationNames.getCXXDestructorName(
5833 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005834
5835 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor35942772011-09-09 21:34:22 +00005836 return Context.DeclarationNames.getCXXConversionFunctionName(
5837 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005838
5839 case DeclarationName::CXXOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005840 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00005841 (OverloadedOperatorKind)Record[Idx++]);
5842
Sean Hunt3e518bd2009-11-29 07:34:05 +00005843 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005844 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregor95eab172011-07-28 20:55:49 +00005845 GetIdentifierInfo(F, Record, Idx));
Sean Hunt3e518bd2009-11-29 07:34:05 +00005846
Douglas Gregor2cf26342009-04-09 22:27:44 +00005847 case DeclarationName::CXXUsingDirective:
5848 return DeclarationName::getUsingDirectiveName();
5849 }
5850
David Blaikie7530c032012-01-17 06:56:22 +00005851 llvm_unreachable("Invalid NameKind!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00005852}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00005853
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005854void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005855 DeclarationNameLoc &DNLoc,
5856 DeclarationName Name,
5857 const RecordData &Record, unsigned &Idx) {
5858 switch (Name.getNameKind()) {
5859 case DeclarationName::CXXConstructorName:
5860 case DeclarationName::CXXDestructorName:
5861 case DeclarationName::CXXConversionFunctionName:
5862 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
5863 break;
5864
5865 case DeclarationName::CXXOperatorName:
5866 DNLoc.CXXOperatorName.BeginOpNameLoc
5867 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5868 DNLoc.CXXOperatorName.EndOpNameLoc
5869 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5870 break;
5871
5872 case DeclarationName::CXXLiteralOperatorName:
5873 DNLoc.CXXLiteralOperatorName.OpNameLoc
5874 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5875 break;
5876
5877 case DeclarationName::Identifier:
5878 case DeclarationName::ObjCZeroArgSelector:
5879 case DeclarationName::ObjCOneArgSelector:
5880 case DeclarationName::ObjCMultiArgSelector:
5881 case DeclarationName::CXXUsingDirective:
5882 break;
5883 }
5884}
5885
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005886void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005887 DeclarationNameInfo &NameInfo,
5888 const RecordData &Record, unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00005889 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005890 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
5891 DeclarationNameLoc DNLoc;
5892 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
5893 NameInfo.setInfo(DNLoc);
5894}
5895
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005896void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005897 const RecordData &Record, unsigned &Idx) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005898 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005899 unsigned NumTPLists = Record[Idx++];
5900 Info.NumTemplParamLists = NumTPLists;
5901 if (NumTPLists) {
Douglas Gregor35942772011-09-09 21:34:22 +00005902 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005903 for (unsigned i=0; i != NumTPLists; ++i)
5904 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
5905 }
5906}
5907
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005908TemplateName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005909ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005910 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00005911 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005912 switch (Kind) {
5913 case TemplateName::Template:
Douglas Gregor409448c2011-07-21 22:35:25 +00005914 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005915
5916 case TemplateName::OverloadedTemplate: {
5917 unsigned size = Record[Idx++];
5918 UnresolvedSet<8> Decls;
5919 while (size--)
Douglas Gregor409448c2011-07-21 22:35:25 +00005920 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005921
Douglas Gregor35942772011-09-09 21:34:22 +00005922 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005923 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005924
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005925 case TemplateName::QualifiedTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005926 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005927 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00005928 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005929 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005930 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005931
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005932 case TemplateName::DependentTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005933 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005934 if (Record[Idx++]) // isIdentifier
Douglas Gregor35942772011-09-09 21:34:22 +00005935 return Context.getDependentTemplateName(NNS,
Douglas Gregor95eab172011-07-28 20:55:49 +00005936 GetIdentifierInfo(F, Record,
5937 Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00005938 return Context.getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00005939 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005940 }
John McCall14606042011-06-30 08:33:18 +00005941
5942 case TemplateName::SubstTemplateTemplateParm: {
5943 TemplateTemplateParmDecl *param
Douglas Gregor409448c2011-07-21 22:35:25 +00005944 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCall14606042011-06-30 08:33:18 +00005945 if (!param) return TemplateName();
5946 TemplateName replacement = ReadTemplateName(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005947 return Context.getSubstTemplateTemplateParm(param, replacement);
John McCall14606042011-06-30 08:33:18 +00005948 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005949
5950 case TemplateName::SubstTemplateTemplateParmPack: {
5951 TemplateTemplateParmDecl *Param
Douglas Gregor409448c2011-07-21 22:35:25 +00005952 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005953 if (!Param)
5954 return TemplateName();
5955
5956 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
5957 if (ArgPack.getKind() != TemplateArgument::Pack)
5958 return TemplateName();
5959
Douglas Gregor35942772011-09-09 21:34:22 +00005960 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005961 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005962 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005963
David Blaikieb219cfc2011-09-23 05:06:16 +00005964 llvm_unreachable("Unhandled template name kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005965}
5966
5967TemplateArgument
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005968ASTReader::ReadTemplateArgument(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00005969 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00005970 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
5971 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005972 case TemplateArgument::Null:
5973 return TemplateArgument();
5974 case TemplateArgument::Type:
Douglas Gregor393f2492011-07-22 00:38:23 +00005975 return TemplateArgument(readType(F, Record, Idx));
Eli Friedmand7a6b162012-09-26 02:36:12 +00005976 case TemplateArgument::Declaration: {
5977 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
5978 bool ForReferenceParam = Record[Idx++];
5979 return TemplateArgument(D, ForReferenceParam);
5980 }
5981 case TemplateArgument::NullPtr:
5982 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00005983 case TemplateArgument::Integral: {
5984 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00005985 QualType T = readType(F, Record, Idx);
Benjamin Kramer85524372012-06-07 15:09:51 +00005986 return TemplateArgument(Context, Value, T);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00005987 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00005988 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005989 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00005990 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005991 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00005992 llvm::Optional<unsigned> NumTemplateExpansions;
5993 if (unsigned NumExpansions = Record[Idx++])
5994 NumTemplateExpansions = NumExpansions - 1;
5995 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00005996 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005997 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00005998 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005999 case TemplateArgument::Pack: {
6000 unsigned NumArgs = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006001 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
Douglas Gregor910f8002010-11-07 23:05:16 +00006002 for (unsigned I = 0; I != NumArgs; ++I)
6003 Args[I] = ReadTemplateArgument(F, Record, Idx);
6004 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006005 }
6006 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006007
David Blaikieb219cfc2011-09-23 05:06:16 +00006008 llvm_unreachable("Unhandled template argument kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006009}
6010
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006011TemplateParameterList *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006012ASTReader::ReadTemplateParameterList(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00006013 const RecordData &Record, unsigned &Idx) {
6014 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6015 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6016 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006017
6018 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00006019 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006020 Params.reserve(NumParams);
6021 while (NumParams--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006022 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer20249a12010-10-21 03:16:25 +00006023
6024 TemplateParameterList* TemplateParams =
Douglas Gregor35942772011-09-09 21:34:22 +00006025 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006026 Params.data(), Params.size(), RAngleLoc);
6027 return TemplateParams;
6028}
6029
6030void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006031ASTReader::
Chris Lattner5f9e2722011-07-23 10:55:15 +00006032ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006033 ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006034 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006035 unsigned NumTemplateArgs = Record[Idx++];
6036 TemplArgs.reserve(NumTemplateArgs);
6037 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00006038 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006039}
6040
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006041/// \brief Read a UnresolvedSet structure.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006042void ASTReader::ReadUnresolvedSet(ModuleFile &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006043 const RecordData &Record, unsigned &Idx) {
6044 unsigned NumDecls = Record[Idx++];
6045 while (NumDecls--) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006046 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006047 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6048 Set.addDecl(D, AS);
6049 }
6050}
6051
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006052CXXBaseSpecifier
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006053ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
Nick Lewycky56062202010-07-26 16:56:01 +00006054 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006055 bool isVirtual = static_cast<bool>(Record[Idx++]);
6056 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6057 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006058 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00006059 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6060 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006061 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006062 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006063 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006064 Result.setInheritConstructors(inheritConstructors);
6065 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006066}
6067
Sean Huntcbb67482011-01-08 20:30:50 +00006068std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006069ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
Sean Huntcbb67482011-01-08 20:30:50 +00006070 unsigned &Idx) {
6071 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006072 unsigned NumInitializers = Record[Idx++];
6073 if (NumInitializers) {
Sean Huntcbb67482011-01-08 20:30:50 +00006074 CtorInitializers
Douglas Gregor35942772011-09-09 21:34:22 +00006075 = new (Context) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006076 for (unsigned i=0; i != NumInitializers; ++i) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006077 TypeSourceInfo *TInfo = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006078 bool IsBaseVirtual = false;
6079 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00006080 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00006081
Sean Hunt156b6402011-05-04 01:19:08 +00006082 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6083 switch (Type) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006084 case CTOR_INITIALIZER_BASE:
6085 TInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006086 IsBaseVirtual = Record[Idx++];
Sean Hunt156b6402011-05-04 01:19:08 +00006087 break;
Douglas Gregor76852c22011-11-01 01:16:03 +00006088
6089 case CTOR_INITIALIZER_DELEGATING:
6090 TInfo = GetTypeSourceInfo(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006091 break;
6092
6093 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006094 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006095 break;
6096
6097 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006098 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006099 break;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006100 }
Sean Hunt156b6402011-05-04 01:19:08 +00006101
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00006102 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00006103 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00006104 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6105 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006106 bool IsWritten = Record[Idx++];
6107 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner5f9e2722011-07-23 10:55:15 +00006108 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006109 if (IsWritten) {
6110 SourceOrderOrNumArrayIndices = Record[Idx++];
6111 } else {
6112 SourceOrderOrNumArrayIndices = Record[Idx++];
6113 Indices.reserve(SourceOrderOrNumArrayIndices);
6114 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor409448c2011-07-21 22:35:25 +00006115 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006116 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006117
Sean Huntcbb67482011-01-08 20:30:50 +00006118 CXXCtorInitializer *BOMInit;
Sean Hunt156b6402011-05-04 01:19:08 +00006119 if (Type == CTOR_INITIALIZER_BASE) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006120 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
Sean Huntcbb67482011-01-08 20:30:50 +00006121 LParenLoc, Init, RParenLoc,
6122 MemberOrEllipsisLoc);
Sean Hunt156b6402011-05-04 01:19:08 +00006123 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006124 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6125 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006126 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00006127 if (Member)
Douglas Gregor35942772011-09-09 21:34:22 +00006128 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006129 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00006130 else
Douglas Gregor35942772011-09-09 21:34:22 +00006131 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
Sean Huntcbb67482011-01-08 20:30:50 +00006132 MemberOrEllipsisLoc, LParenLoc,
6133 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006134 } else {
Douglas Gregor35942772011-09-09 21:34:22 +00006135 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006136 LParenLoc, Init, RParenLoc,
6137 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006138 }
6139
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00006140 if (IsWritten)
6141 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00006142 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006143 }
6144 }
6145
Sean Huntcbb67482011-01-08 20:30:50 +00006146 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006147}
6148
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006149NestedNameSpecifier *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006150ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006151 const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006152 unsigned N = Record[Idx++];
6153 NestedNameSpecifier *NNS = 0, *Prev = 0;
6154 for (unsigned I = 0; I != N; ++I) {
6155 NestedNameSpecifier::SpecifierKind Kind
6156 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6157 switch (Kind) {
6158 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006159 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006160 NNS = NestedNameSpecifier::Create(Context, Prev, II);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006161 break;
6162 }
6163
6164 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006165 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006166 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006167 break;
6168 }
6169
Douglas Gregor14aba762011-02-24 02:36:08 +00006170 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006171 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006172 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
Douglas Gregor14aba762011-02-24 02:36:08 +00006173 break;
6174 }
6175
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006176 case NestedNameSpecifier::TypeSpec:
6177 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor393f2492011-07-22 00:38:23 +00006178 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00006179 if (!T)
6180 return 0;
6181
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006182 bool Template = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006183 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006184 break;
6185 }
6186
6187 case NestedNameSpecifier::Global: {
Douglas Gregor35942772011-09-09 21:34:22 +00006188 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006189 // No associated value, and there can't be a prefix.
6190 break;
6191 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006192 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00006193 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006194 }
6195 return NNS;
6196}
6197
Douglas Gregordc355712011-02-25 00:36:19 +00006198NestedNameSpecifierLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006199ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
Douglas Gregordc355712011-02-25 00:36:19 +00006200 unsigned &Idx) {
6201 unsigned N = Record[Idx++];
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006202 NestedNameSpecifierLocBuilder Builder;
Douglas Gregordc355712011-02-25 00:36:19 +00006203 for (unsigned I = 0; I != N; ++I) {
6204 NestedNameSpecifier::SpecifierKind Kind
6205 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6206 switch (Kind) {
6207 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006208 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006209 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006210 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006211 break;
6212 }
6213
6214 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006215 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006216 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006217 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006218 break;
6219 }
6220
6221 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006222 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006223 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006224 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006225 break;
6226 }
6227
6228 case NestedNameSpecifier::TypeSpec:
6229 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregordc355712011-02-25 00:36:19 +00006230 bool Template = Record[Idx++];
6231 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
6232 if (!T)
6233 return NestedNameSpecifierLoc();
Douglas Gregordc355712011-02-25 00:36:19 +00006234 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006235
6236 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
Douglas Gregor35942772011-09-09 21:34:22 +00006237 Builder.Extend(Context,
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006238 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
6239 T->getTypeLoc(), ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006240 break;
6241 }
6242
6243 case NestedNameSpecifier::Global: {
Douglas Gregordc355712011-02-25 00:36:19 +00006244 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006245 Builder.MakeGlobal(Context, ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006246 break;
6247 }
6248 }
Douglas Gregordc355712011-02-25 00:36:19 +00006249 }
6250
Douglas Gregor35942772011-09-09 21:34:22 +00006251 return Builder.getWithLocInContext(Context);
Douglas Gregordc355712011-02-25 00:36:19 +00006252}
6253
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006254SourceRange
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006255ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006256 unsigned &Idx) {
6257 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
6258 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00006259 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006260}
6261
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006262/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006263llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006264 unsigned BitWidth = Record[Idx++];
6265 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
6266 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
6267 Idx += NumWords;
6268 return Result;
6269}
6270
6271/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006272llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006273 bool isUnsigned = Record[Idx++];
6274 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
6275}
6276
Douglas Gregor17fc2232009-04-14 21:55:33 +00006277/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006278llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00006279 return llvm::APFloat(ReadAPInt(Record, Idx));
6280}
6281
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006282// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006283std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006284 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00006285 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006286 Idx += Len;
6287 return Result;
6288}
6289
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00006290VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
6291 unsigned &Idx) {
6292 unsigned Major = Record[Idx++];
6293 unsigned Minor = Record[Idx++];
6294 unsigned Subminor = Record[Idx++];
6295 if (Minor == 0)
6296 return VersionTuple(Major);
6297 if (Subminor == 0)
6298 return VersionTuple(Major, Minor - 1);
6299 return VersionTuple(Major, Minor - 1, Subminor - 1);
6300}
6301
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006302CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006303 const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00006304 unsigned &Idx) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006305 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006306 return CXXTemporary::Create(Context, Decl);
Chris Lattnerd2598362010-05-10 00:25:06 +00006307}
6308
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006309DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00006310 return Diag(SourceLocation(), DiagID);
6311}
6312
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006313DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00006314 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00006315}
Douglas Gregor025452f2009-04-17 00:04:06 +00006316
Douglas Gregor668c1a42009-04-21 22:25:48 +00006317/// \brief Retrieve the identifier table associated with the
6318/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006319IdentifierTable &ASTReader::getIdentifierTable() {
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006320 return PP.getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00006321}
6322
Douglas Gregor025452f2009-04-17 00:04:06 +00006323/// \brief Record that the given ID maps to the given switch-case
6324/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006325void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006326 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
6327 "Already have a SwitchCase with this ID");
6328 (*CurrSwitchCaseStmts)[ID] = SC;
Douglas Gregor025452f2009-04-17 00:04:06 +00006329}
6330
6331/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006332SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006333 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
6334 return (*CurrSwitchCaseStmts)[ID];
Douglas Gregor025452f2009-04-17 00:04:06 +00006335}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00006336
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006337void ASTReader::ClearSwitchCaseIDs() {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006338 CurrSwitchCaseStmts->clear();
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006339}
6340
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006341void ASTReader::ReadComments() {
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006342 std::vector<RawComment *> Comments;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006343 for (SmallVectorImpl<std::pair<llvm::BitstreamCursor,
6344 serialization::ModuleFile *> >::iterator
6345 I = CommentsCursors.begin(),
6346 E = CommentsCursors.end();
6347 I != E; ++I) {
6348 llvm::BitstreamCursor &Cursor = I->first;
6349 serialization::ModuleFile &F = *I->second;
6350 SavedStreamPosition SavedPosition(Cursor);
6351
6352 RecordData Record;
6353 while (true) {
6354 unsigned Code = Cursor.ReadCode();
6355 if (Code == llvm::bitc::END_BLOCK)
6356 break;
6357
6358 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
6359 // No known subblocks, always skip them.
6360 Cursor.ReadSubBlockID();
6361 if (Cursor.SkipBlock()) {
6362 Error("malformed block record in AST file");
6363 return;
6364 }
6365 continue;
6366 }
6367
6368 if (Code == llvm::bitc::DEFINE_ABBREV) {
6369 Cursor.ReadAbbrevRecord();
6370 continue;
6371 }
6372
6373 // Read a record.
6374 Record.clear();
6375 switch ((CommentRecordTypes) Cursor.ReadRecord(Code, Record)) {
Chandler Carruth13691bb2012-06-20 06:47:54 +00006376 case COMMENTS_RAW_COMMENT: {
6377 unsigned Idx = 0;
6378 SourceRange SR = ReadSourceRange(F, Record, Idx);
6379 RawComment::CommentKind Kind =
6380 (RawComment::CommentKind) Record[Idx++];
6381 bool IsTrailingComment = Record[Idx++];
6382 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006383 Comments.push_back(new (Context) RawComment(SR, Kind,
6384 IsTrailingComment,
6385 IsAlmostTrailingComment));
Chandler Carruth13691bb2012-06-20 06:47:54 +00006386 break;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006387 }
6388 }
6389 }
6390 }
6391 Context.Comments.addCommentsToFront(Comments);
6392}
6393
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006394void ASTReader::finishPendingActions() {
Douglas Gregorcff9f262012-01-27 01:47:08 +00006395 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty()) {
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006396 // If any identifiers with corresponding top-level declarations have
6397 // been loaded, load those declarations now.
6398 while (!PendingIdentifierInfos.empty()) {
6399 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
6400 PendingIdentifierInfos.front().DeclIDs, true);
6401 PendingIdentifierInfos.pop_front();
6402 }
6403
Douglas Gregora1be2782011-12-17 23:38:30 +00006404 // Load pending declaration chains.
6405 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
6406 loadPendingDeclChain(PendingDeclChains[I]);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006407 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
Douglas Gregora1be2782011-12-17 23:38:30 +00006408 }
6409 PendingDeclChains.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006410 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006411
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006412 // If we deserialized any C++ or Objective-C class definitions, any
6413 // Objective-C protocol definitions, or any redeclarable templates, make sure
6414 // that all redeclarations point to the definitions. Note that this can only
6415 // happen now, after the redeclaration chains have been fully wired.
Douglas Gregorfc529f72011-12-19 19:00:47 +00006416 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
6417 DEnd = PendingDefinitions.end();
6418 D != DEnd; ++D) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006419 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
6420 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
6421 // Make sure that the TagType points at the definition.
6422 const_cast<TagType*>(TagT)->decl = TD;
6423 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006424
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006425 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
6426 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
6427 REnd = RD->redecls_end();
6428 R != REnd; ++R)
6429 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
6430
6431 }
6432
Douglas Gregorfc529f72011-12-19 19:00:47 +00006433 continue;
6434 }
6435
Douglas Gregor1d784b22012-01-01 19:51:50 +00006436 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006437 // Make sure that the ObjCInterfaceType points at the definition.
6438 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
6439 ->Decl = ID;
6440
Douglas Gregor1d784b22012-01-01 19:51:50 +00006441 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
6442 REnd = ID->redecls_end();
6443 R != REnd; ++R)
6444 R->Data = ID->Data;
6445
6446 continue;
6447 }
6448
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006449 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
6450 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
6451 REnd = PD->redecls_end();
6452 R != REnd; ++R)
6453 R->Data = PD->Data;
6454
6455 continue;
6456 }
6457
6458 RedeclarableTemplateDecl *RTD
6459 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
6460 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
6461 REnd = RTD->redecls_end();
Douglas Gregorfc529f72011-12-19 19:00:47 +00006462 R != REnd; ++R)
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006463 R->Common = RTD->Common;
Douglas Gregorfc529f72011-12-19 19:00:47 +00006464 }
6465 PendingDefinitions.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006466}
6467
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006468void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00006469 assert(NumCurrentElementsDeserializing &&
6470 "FinishedDeserializing not paired with StartedDeserializing");
6471 if (NumCurrentElementsDeserializing == 1) {
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006472 // We decrease NumCurrentElementsDeserializing only after pending actions
6473 // are finished, to avoid recursively re-calling finishPendingActions().
6474 finishPendingActions();
6475 }
6476 --NumCurrentElementsDeserializing;
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006477
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006478 if (NumCurrentElementsDeserializing == 0 &&
6479 Consumer && !PassingDeclsToConsumer) {
6480 // Guard variable to avoid recursively redoing the process of passing
6481 // decls to consumer.
6482 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6483 true);
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006484
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006485 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00006486 // We are not in recursive loading, so it's safe to pass the "interesting"
6487 // decls to the consumer.
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006488 Decl *D = InterestingDecls.front();
6489 InterestingDecls.pop_front();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006490 PassInterestingDeclToConsumer(D);
6491 }
Douglas Gregord89275b2009-07-06 18:54:52 +00006492 }
Douglas Gregord89275b2009-07-06 18:54:52 +00006493}
Douglas Gregor501c1032010-08-19 00:28:17 +00006494
Douglas Gregorf8a1e512011-09-02 00:26:20 +00006495ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
Douglas Gregor832d6202011-07-22 16:35:34 +00006496 StringRef isysroot, bool DisableValidation,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00006497 bool DisableStatCache, bool AllowASTWithCompilerErrors)
Sebastian Redle1dde812010-08-24 00:50:04 +00006498 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
6499 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006500 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00006501 Consumer(0), ModuleMgr(PP.getFileManager()),
Jonathan D. Turner1afb6612011-07-28 17:20:23 +00006502 RelocatablePCH(false), isysroot(isysroot),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006503 DisableValidation(DisableValidation),
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00006504 DisableStatCache(DisableStatCache),
6505 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006506 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
6507 NumStatHits(0), NumStatMisses(0),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006508 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00006509 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
6510 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
6511 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
6512 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00006513 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
6514 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006515 PassingDeclsToConsumer(false),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00006516 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00006517{
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006518 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redle1dde812010-08-24 00:50:04 +00006519}
6520
Sebastian Redle1dde812010-08-24 00:50:04 +00006521ASTReader::~ASTReader() {
Sebastian Redle1dde812010-08-24 00:50:04 +00006522 for (DeclContextVisibleUpdatesPending::iterator
6523 I = PendingVisibleUpdates.begin(),
6524 E = PendingVisibleUpdates.end();
6525 I != E; ++I) {
6526 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
6527 F = I->second.end();
6528 J != F; ++J)
Benjamin Kramerb1758c62012-04-15 12:36:49 +00006529 delete J->first;
Sebastian Redle1dde812010-08-24 00:50:04 +00006530 }
Axel Naumann38c3bb42012-10-02 12:18:46 +00006531 assert(RedeclsAddedToAST.empty() && "RedeclsAddedToAST not empty!");
Sebastian Redle1dde812010-08-24 00:50:04 +00006532}