blob: b7266ebbba5b6f69ef07cb7b8cf251d76df6be56 [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);
Douglas Gregor3644d972012-10-09 16:01:50 +00001463 } else {
1464 II->setHadMacroDefinition(true);
Douglas Gregor13292642011-12-02 15:45:10 +00001465 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001466
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001467 // Adjust the offset to a global offset.
Douglas Gregor2d2689a2011-07-28 21:16:51 +00001468 UnreadMacroRecordOffsets[II] = F.GlobalBitOffset + LocalOffset;
Douglas Gregor295a2a62010-10-30 00:23:06 +00001469}
1470
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001471void ASTReader::ReadDefinedMacros() {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001472 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1473 E = ModuleMgr.rend(); I != E; ++I) {
1474 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001475
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001476 // If there was no preprocessor block, skip this file.
1477 if (!MacroCursor.getBitStreamReader())
1478 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001479
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001480 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001481 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001482
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001483 RecordData Record;
1484 while (true) {
1485 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001486 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001487 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001488
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001489 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1490 // No known subblocks, always skip them.
1491 Cursor.ReadSubBlockID();
1492 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001493 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001494 return;
1495 }
1496 continue;
1497 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001498
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001499 if (Code == llvm::bitc::DEFINE_ABBREV) {
1500 Cursor.ReadAbbrevRecord();
1501 continue;
1502 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001503
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001504 // Read a record.
1505 const char *BlobStart;
1506 unsigned BlobLen;
1507 Record.clear();
1508 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1509 default: // Default behavior: ignore.
1510 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001511
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001512 case PP_MACRO_OBJECT_LIKE:
1513 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregor95eab172011-07-28 20:55:49 +00001514 getLocalIdentifier(**I, Record[0]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001515 break;
1516
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001517 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001518 // Ignore tokens.
1519 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001520 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001521 }
1522 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001523
1524 // Drain the unread macro-record offsets map.
1525 while (!UnreadMacroRecordOffsets.empty())
1526 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1527}
1528
1529void ASTReader::LoadMacroDefinition(
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001530 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
Douglas Gregor295a2a62010-10-30 00:23:06 +00001531 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
Douglas Gregor295a2a62010-10-30 00:23:06 +00001532 uint64_t Offset = Pos->second;
1533 UnreadMacroRecordOffsets.erase(Pos);
1534
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001535 RecordLocation Loc = getLocalBitOffset(Offset);
1536 ReadMacroRecord(*Loc.F, Loc.Offset);
Douglas Gregor295a2a62010-10-30 00:23:06 +00001537}
1538
1539void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1540 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1541 = UnreadMacroRecordOffsets.find(II);
1542 LoadMacroDefinition(Pos);
Douglas Gregor88a35862010-01-04 19:18:44 +00001543}
1544
Douglas Gregoreee242f2011-10-27 09:33:13 +00001545namespace {
1546 /// \brief Visitor class used to look up identifirs in an AST file.
1547 class IdentifierLookupVisitor {
1548 StringRef Name;
Douglas Gregor057df202012-01-18 20:56:22 +00001549 unsigned PriorGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001550 IdentifierInfo *Found;
1551 public:
Douglas Gregor057df202012-01-18 20:56:22 +00001552 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration)
1553 : Name(Name), PriorGeneration(PriorGeneration), Found() { }
Douglas Gregoreee242f2011-10-27 09:33:13 +00001554
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001555 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00001556 IdentifierLookupVisitor *This
1557 = static_cast<IdentifierLookupVisitor *>(UserData);
1558
Douglas Gregor057df202012-01-18 20:56:22 +00001559 // If we've already searched this module file, skip it now.
1560 if (M.Generation <= This->PriorGeneration)
1561 return true;
1562
Douglas Gregoreee242f2011-10-27 09:33:13 +00001563 ASTIdentifierLookupTable *IdTable
1564 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1565 if (!IdTable)
1566 return false;
1567
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001568 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1569 M, This->Found);
1570
Douglas Gregoreee242f2011-10-27 09:33:13 +00001571 std::pair<const char*, unsigned> Key(This->Name.begin(),
1572 This->Name.size());
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001573 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Trait);
Douglas Gregoreee242f2011-10-27 09:33:13 +00001574 if (Pos == IdTable->end())
1575 return false;
1576
1577 // Dereferencing the iterator has the effect of building the
1578 // IdentifierInfo node and populating it with the various
1579 // declarations it needs.
1580 This->Found = *Pos;
1581 return true;
1582 }
1583
1584 // \brief Retrieve the identifier info found within the module
1585 // files.
1586 IdentifierInfo *getIdentifierInfo() const { return Found; }
1587 };
1588}
1589
1590void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
Douglas Gregor057df202012-01-18 20:56:22 +00001591 unsigned PriorGeneration = 0;
David Blaikie4e4d0842012-03-11 07:00:24 +00001592 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001593 PriorGeneration = IdentifierGeneration[&II];
1594
1595 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration);
1596 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
1597 markIdentifierUpToDate(&II);
1598}
1599
1600void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1601 if (!II)
1602 return;
1603
1604 II->setOutOfDate(false);
1605
1606 // Update the generation for this identifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001607 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001608 IdentifierGeneration[II] = CurrentGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001609}
1610
Chris Lattner5f9e2722011-07-23 10:55:15 +00001611const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001612 std::string Filename = filenameStrRef;
1613 MaybeAddSystemRootToFilename(Filename);
1614 const FileEntry *File = FileMgr.getFile(Filename);
1615 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1616 OriginalDir != CurrentDir) {
1617 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1618 OriginalDir,
1619 CurrentDir);
1620 if (!resolved.empty())
1621 File = FileMgr.getFile(resolved);
1622 }
1623
1624 return File;
1625}
1626
Douglas Gregore650c8c2009-07-07 00:12:59 +00001627/// \brief If we are loading a relocatable PCH file, and the filename is
1628/// not an absolute path, add the system root to the beginning of the file
1629/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001630void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001631 // If this is not a relocatable PCH file, there's nothing to do.
1632 if (!RelocatablePCH)
1633 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001634
Michael J. Spencer256053b2010-12-17 21:22:22 +00001635 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregore650c8c2009-07-07 00:12:59 +00001636 return;
1637
Douglas Gregor832d6202011-07-22 16:35:34 +00001638 if (isysroot.empty()) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001639 // If no system root was given, default to '/'
1640 Filename.insert(Filename.begin(), '/');
1641 return;
1642 }
Mike Stump1eb44332009-09-09 15:08:12 +00001643
Douglas Gregor832d6202011-07-22 16:35:34 +00001644 unsigned Length = isysroot.size();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001645 if (isysroot[Length - 1] != '/')
1646 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Douglas Gregor832d6202011-07-22 16:35:34 +00001648 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregore650c8c2009-07-07 00:12:59 +00001649}
1650
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001651ASTReader::ASTReadResult
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001652ASTReader::ReadASTBlock(ModuleFile &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001653 llvm::BitstreamCursor &Stream = F.Stream;
1654
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001655 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001656 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001657 return Failure;
1658 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001659
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001660 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001661 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001662 while (!Stream.AtEndOfStream()) {
1663 unsigned Code = Stream.ReadCode();
1664 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001665 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001666 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001667 return Failure;
1668 }
Chris Lattner7356a312009-04-11 21:15:38 +00001669
Argyrios Kyrtzidis1f941242012-09-21 01:30:00 +00001670 DeclContext *DC = Context.getTranslationUnitDecl();
1671 if (!DC->hasExternalVisibleStorage() && DC->hasExternalLexicalStorage())
1672 DC->setMustBuildLookupTable();
1673
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001674 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001675 }
1676
1677 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1678 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001679 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001680 // We lazily load the decls block, but we want to set up the
1681 // DeclsCursor cursor to point into it. Clone our current bitcode
1682 // cursor to it, enter the block and read the abbrevs in that block.
1683 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001684 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001685 if (Stream.SkipBlock() || // Skip with the main cursor.
1686 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001687 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001688 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001689 return Failure;
1690 }
1691 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001692
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001693 case DECL_UPDATES_BLOCK_ID:
1694 if (Stream.SkipBlock()) {
1695 Error("malformed block record in AST file");
1696 return Failure;
1697 }
1698 break;
1699
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001700 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001701 F.MacroCursor = Stream;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001702 if (!PP.getExternalSource())
1703 PP.setExternalSource(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00001704
Douglas Gregorecdcb882010-10-20 22:00:55 +00001705 if (Stream.SkipBlock() ||
1706 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001707 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001708 return Failure;
1709 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001710 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001711 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001712
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001713 case PREPROCESSOR_DETAIL_BLOCK_ID:
1714 F.PreprocessorDetailCursor = Stream;
1715 if (Stream.SkipBlock() ||
1716 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1717 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1718 Error("malformed preprocessor detail record in AST file");
1719 return Failure;
1720 }
1721 F.PreprocessorDetailStartOffset
1722 = F.PreprocessorDetailCursor.GetCurrentBitNo();
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001723
1724 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00001725 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001726 if (!PP.getPreprocessingRecord()->getExternalSource())
1727 PP.getPreprocessingRecord()->SetExternalSource(*this);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001728 break;
1729
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001730 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001731 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001732 case Success:
1733 break;
1734
1735 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001736 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001737 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001738
1739 case IgnorePCH:
1740 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001741 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001742 break;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001743
1744 case SUBMODULE_BLOCK_ID:
1745 switch (ReadSubmoduleBlock(F)) {
1746 case Success:
1747 break;
1748
1749 case Failure:
1750 Error("malformed submodule block in AST file");
1751 return Failure;
1752
1753 case IgnorePCH:
1754 return IgnorePCH;
1755 }
1756 break;
1757
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00001758 case COMMENTS_BLOCK_ID: {
1759 llvm::BitstreamCursor C = Stream;
1760 if (Stream.SkipBlock() ||
1761 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
1762 Error("malformed comments block in AST file");
1763 return Failure;
1764 }
1765 CommentsCursors.push_back(std::make_pair(C, &F));
1766 break;
1767 }
1768
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001769 default:
1770 if (!Stream.SkipBlock())
1771 break;
1772 Error("malformed block record in AST file");
1773 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001774 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001775 continue;
1776 }
1777
1778 if (Code == llvm::bitc::DEFINE_ABBREV) {
1779 Stream.ReadAbbrevRecord();
1780 continue;
1781 }
1782
1783 // Read and process a record.
1784 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001785 const char *BlobStart = 0;
1786 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001787 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001788 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001789 default: // Default behavior: ignore.
1790 break;
1791
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001792 case METADATA: {
1793 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1794 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001795 : diag::warn_pch_version_too_new);
1796 return IgnorePCH;
1797 }
1798
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001799 bool hasErrors = Record[5];
1800 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1801 Diag(diag::err_pch_with_compiler_errors);
1802 return IgnorePCH;
1803 }
1804
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001805 RelocatablePCH = Record[4];
1806 if (Listener) {
1807 std::string TargetTriple(BlobStart, BlobLen);
1808 if (Listener->ReadTargetTriple(TargetTriple))
1809 return IgnorePCH;
1810 }
1811 break;
1812 }
1813
Douglas Gregore95b9192011-08-17 21:07:30 +00001814 case IMPORTS: {
1815 // Load each of the imported PCH files.
1816 unsigned Idx = 0, N = Record.size();
1817 while (Idx < N) {
1818 // Read information about the AST file.
1819 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1820 unsigned Length = Record[Idx++];
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001821 SmallString<128> ImportedFile(Record.begin() + Idx,
Douglas Gregore95b9192011-08-17 21:07:30 +00001822 Record.begin() + Idx + Length);
1823 Idx += Length;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001824
Douglas Gregore95b9192011-08-17 21:07:30 +00001825 // Load the AST file.
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001826 switch(ReadASTCore(ImportedFile, ImportedKind, &F)) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001827 case Failure: return Failure;
1828 // If we have to ignore the dependency, we'll have to ignore this too.
1829 case IgnorePCH: return IgnorePCH;
1830 case Success: break;
1831 }
1832 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001833 break;
1834 }
1835
Douglas Gregora119da02011-08-02 16:26:37 +00001836 case TYPE_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001837 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001838 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001839 return Failure;
1840 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001841 F.TypeOffsets = (const uint32_t *)BlobStart;
1842 F.LocalNumTypes = Record[0];
Douglas Gregore3605012011-08-02 18:32:54 +00001843 unsigned LocalBaseTypeIndex = Record[1];
1844 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor1e849b62011-07-29 00:21:44 +00001845
Douglas Gregora119da02011-08-02 16:26:37 +00001846 if (F.LocalNumTypes > 0) {
1847 // Introduce the global -> local mapping for types within this module.
1848 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
1849
1850 // Introduce the local -> global mapping for types within this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001851 F.TypeRemap.insertOrReplace(
1852 std::make_pair(LocalBaseTypeIndex,
1853 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregora119da02011-08-02 16:26:37 +00001854
1855 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
1856 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001857 break;
Douglas Gregora119da02011-08-02 16:26:37 +00001858 }
1859
Douglas Gregor496c7092011-08-03 15:48:04 +00001860 case DECL_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001861 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001862 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001863 return Failure;
1864 }
Argyrios Kyrtzidis9d31fa72011-10-27 18:47:35 +00001865 F.DeclOffsets = (const DeclOffset *)BlobStart;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001866 F.LocalNumDecls = Record[0];
Douglas Gregor496c7092011-08-03 15:48:04 +00001867 unsigned LocalBaseDeclID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001868 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor96e973f2011-07-20 00:27:43 +00001869
Douglas Gregor496c7092011-08-03 15:48:04 +00001870 if (F.LocalNumDecls > 0) {
1871 // Introduce the global -> local mapping for declarations within this
1872 // module.
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001873 GlobalDeclMap.insert(
1874 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregor496c7092011-08-03 15:48:04 +00001875
1876 // Introduce the local -> global mapping for declarations within this
1877 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001878 F.DeclRemap.insertOrReplace(
1879 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
Douglas Gregor496c7092011-08-03 15:48:04 +00001880
Douglas Gregora1be2782011-12-17 23:38:30 +00001881 // Introduce the global -> local mapping for declarations within this
1882 // module.
1883 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
1884
Douglas Gregor496c7092011-08-03 15:48:04 +00001885 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
1886 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001887 break;
Douglas Gregor496c7092011-08-03 15:48:04 +00001888 }
1889
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001890 case TU_UPDATE_LEXICAL: {
Douglas Gregor35942772011-09-09 21:34:22 +00001891 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001892 DeclContextInfo &Info = F.DeclContextInfos[TU];
1893 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
1894 Info.NumLexicalDecls
1895 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregor35942772011-09-09 21:34:22 +00001896 TU->setHasExternalLexicalStorage(true);
Sebastian Redld692af72010-07-27 18:24:41 +00001897 break;
1898 }
1899
Sebastian Redle1dde812010-08-24 00:50:04 +00001900 case UPDATE_VISIBLE: {
Douglas Gregor496c7092011-08-03 15:48:04 +00001901 unsigned Idx = 0;
1902 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Benjamin Kramerb1758c62012-04-15 12:36:49 +00001903 ASTDeclContextNameLookupTable *Table =
1904 ASTDeclContextNameLookupTable::Create(
Douglas Gregor496c7092011-08-03 15:48:04 +00001905 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redle1dde812010-08-24 00:50:04 +00001906 (const unsigned char *)BlobStart,
Douglas Gregor393f2492011-07-22 00:38:23 +00001907 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregor35942772011-09-09 21:34:22 +00001908 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
1909 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001910 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001911 TU->setHasExternalVisibleStorage(true);
Sebastian Redle1dde812010-08-24 00:50:04 +00001912 } else
Douglas Gregor496c7092011-08-03 15:48:04 +00001913 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redle1dde812010-08-24 00:50:04 +00001914 break;
1915 }
1916
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001917 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001918 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001919 return IgnorePCH;
1920 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001921
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001922 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001923 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001924 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001925 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001926 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001927 (const unsigned char *)F.IdentifierTableData + Record[0],
1928 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00001929 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001930
1931 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001932 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001933 break;
1934
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001935 case IDENTIFIER_OFFSET: {
Sebastian Redl2da08f92010-07-19 22:28:42 +00001936 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001937 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001938 return Failure;
1939 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001940 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1941 F.LocalNumIdentifiers = Record[0];
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001942 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001943 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor67268d02011-07-20 00:59:32 +00001944
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001945 if (F.LocalNumIdentifiers > 0) {
1946 // Introduce the global -> local mapping for identifiers within this
1947 // module.
1948 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
1949 &F));
1950
1951 // Introduce the local -> global mapping for identifiers within this
1952 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001953 F.IdentifierRemap.insertOrReplace(
1954 std::make_pair(LocalBaseIdentifierID,
1955 F.BaseIdentifierID - LocalBaseIdentifierID));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001956
1957 IdentifiersLoaded.resize(IdentifiersLoaded.size()
1958 + F.LocalNumIdentifiers);
1959 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001960 break;
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001961 }
1962
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001963 case EXTERNAL_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001964 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1965 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorfdd01722009-04-14 00:24:19 +00001966 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001967
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001968 case SPECIAL_TYPES:
Douglas Gregor393f2492011-07-22 00:38:23 +00001969 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1970 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregorad1de002009-04-18 05:55:16 +00001971 break;
1972
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001973 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001974 TotalNumStatements += Record[0];
1975 TotalNumMacros += Record[1];
1976 TotalLexicalDeclContexts += Record[2];
1977 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001978 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001979
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001980 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001981 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1982 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001983 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001984
Sean Huntebcbe1d2011-05-04 23:29:54 +00001985 case DELEGATING_CTORS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001986 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1987 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00001988 break;
1989
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001990 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor31e37b22011-07-28 18:09:57 +00001991 if (Record.size() % 4 != 0) {
1992 Error("invalid weak identifiers record");
1993 return Failure;
1994 }
1995
1996 // FIXME: Ignore weak undeclared identifiers from non-original PCH
1997 // files. This isn't the way to do it :)
1998 WeakUndeclaredIdentifiers.clear();
1999
2000 // Translate the weak, undeclared identifiers into global IDs.
2001 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2002 WeakUndeclaredIdentifiers.push_back(
2003 getGlobalIdentifierID(F, Record[I++]));
2004 WeakUndeclaredIdentifiers.push_back(
2005 getGlobalIdentifierID(F, Record[I++]));
2006 WeakUndeclaredIdentifiers.push_back(
2007 ReadSourceLocation(F, Record, I).getRawEncoding());
2008 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2009 }
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002010 break;
2011
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002012 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002013 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2014 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor14c22f22009-04-22 22:18:58 +00002015 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002016
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002017 case SELECTOR_OFFSETS: {
Sebastian Redl059612d2010-08-03 21:58:15 +00002018 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00002019 F.LocalNumSelectors = Record[0];
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002020 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002021 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor96958cb2011-07-20 01:10:58 +00002022
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002023 if (F.LocalNumSelectors > 0) {
2024 // Introduce the global -> local mapping for selectors within this
2025 // module.
2026 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2027
2028 // Introduce the local -> global mapping for selectors within this
2029 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002030 F.SelectorRemap.insertOrReplace(
2031 std::make_pair(LocalBaseSelectorID,
2032 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor83941df2009-04-25 17:48:32 +00002033
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002034 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2035 }
2036 break;
2037 }
2038
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002039 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002040 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002041 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002042 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002043 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002044 F.SelectorLookupTableData + Record[0],
2045 F.SelectorLookupTableData,
Douglas Gregor409448c2011-07-21 22:35:25 +00002046 ASTSelectorLookupTrait(*this, F));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002047 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002048 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002049
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002050 case REFERENCED_SELECTOR_POOL:
Douglas Gregor8451ec72011-07-28 14:41:43 +00002051 if (!Record.empty()) {
2052 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2053 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2054 Record[Idx++]));
2055 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2056 getRawEncoding());
2057 }
2058 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00002059 break;
2060
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002061 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002062 if (!Record.empty() && Listener)
2063 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002064 break;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002065
2066 case FILE_SORTED_DECLS:
2067 F.FileSortedDecls = (const DeclID *)BlobStart;
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002068 F.NumFileSortedDecls = Record[0];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002069 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002070
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002071 case SOURCE_LOCATION_OFFSETS: {
2072 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002073 F.LocalNumSLocEntries = Record[0];
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002074 unsigned SLocSpaceSize = Record[1];
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002075 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002076 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2077 SLocSpaceSize);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002078 // Make our entry in the range map. BaseID is negative and growing, so
2079 // we invert it. Because we invert it, though, we need the other end of
2080 // the range.
2081 unsigned RangeStart =
2082 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2083 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2084 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2085
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002086 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2087 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2088 GlobalSLocOffsetMap.insert(
2089 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2090 - SLocSpaceSize,&F));
2091
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002092 // Initialize the remapping table.
2093 // Invalid stays invalid.
2094 F.SLocRemap.insert(std::make_pair(0U, 0));
2095 // This module. Base was 2 when being compiled.
2096 F.SLocRemap.insert(std::make_pair(2U,
2097 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002098
2099 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002100 break;
2101 }
2102
Douglas Gregor5d51a1d2011-08-01 16:01:55 +00002103 case MODULE_OFFSET_MAP: {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002104 // Additional remapping information.
2105 const unsigned char *Data = (const unsigned char*)BlobStart;
2106 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregorf33740e2011-08-02 10:56:51 +00002107
2108 // Continuous range maps we may be updating in our module.
2109 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002110 ContinuousRangeMap<uint32_t, int, 2>::Builder
2111 IdentifierRemap(F.IdentifierRemap);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002112 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002113 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2114 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor26ced122011-12-01 00:59:36 +00002115 SubmoduleRemap(F.SubmoduleRemap);
2116 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002117 SelectorRemap(F.SelectorRemap);
Douglas Gregor496c7092011-08-03 15:48:04 +00002118 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregora119da02011-08-02 16:26:37 +00002119 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2120
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002121 while(Data < DataEnd) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002122 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002123 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002124 Data += Len;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002125 ModuleFile *OM = ModuleMgr.lookup(Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002126 if (!OM) {
2127 Error("SourceLocation remap refers to unknown module");
2128 return Failure;
2129 }
Douglas Gregorf33740e2011-08-02 10:56:51 +00002130
2131 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2132 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2133 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor26ced122011-12-01 00:59:36 +00002134 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002135 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2136 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora119da02011-08-02 16:26:37 +00002137 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002138
2139 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2140 SLocRemap.insert(std::make_pair(SLocOffset,
2141 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002142 IdentifierRemap.insert(
2143 std::make_pair(IdentifierIDOffset,
2144 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002145 PreprocessedEntityRemap.insert(
2146 std::make_pair(PreprocessedEntityIDOffset,
2147 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregor26ced122011-12-01 00:59:36 +00002148 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2149 OM->BaseSubmoduleID - SubmoduleIDOffset));
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002150 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2151 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregor496c7092011-08-03 15:48:04 +00002152 DeclRemap.insert(std::make_pair(DeclIDOffset,
2153 OM->BaseDeclID - DeclIDOffset));
2154
Douglas Gregora119da02011-08-02 16:26:37 +00002155 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregore3605012011-08-02 18:32:54 +00002156 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregora1be2782011-12-17 23:38:30 +00002157
2158 // Global -> local mappings.
2159 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002160 }
2161 break;
2162 }
2163
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002164 case SOURCE_MANAGER_LINE_TABLE:
2165 if (ParseLineTable(F, Record))
2166 return Failure;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002167 break;
2168
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00002169 case FILE_SOURCE_LOCATION_OFFSETS:
2170 F.SLocFileOffsets = (const uint32_t *)BlobStart;
2171 F.LocalNumSLocFileEntries = Record[0];
2172 break;
2173
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002174 case SOURCE_LOCATION_PRELOADS: {
2175 // Need to transform from the local view (1-based IDs) to the global view,
2176 // which is based off F.SLocEntryBaseID.
Douglas Gregorf249bf32011-08-25 21:09:44 +00002177 if (!F.PreloadSLocEntries.empty()) {
2178 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2179 return Failure;
2180 }
2181
2182 F.PreloadSLocEntries.swap(Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002183 break;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002184 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002185
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002186 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002187 if (!DisableStatCache) {
2188 ASTStatCache *MyStatCache =
2189 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2190 (const unsigned char *)BlobStart,
2191 NumStatHits, NumStatMisses);
2192 FileMgr.addStatCache(MyStatCache);
2193 F.StatCache = MyStatCache;
2194 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002195 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002196 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002197
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002198 case EXT_VECTOR_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002199 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2200 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorb81c1702009-04-27 20:06:05 +00002201 break;
2202
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002203 case VTABLE_USES:
Douglas Gregordfe65432011-07-28 19:11:31 +00002204 if (Record.size() % 3 != 0) {
2205 Error("Invalid VTABLE_USES record");
2206 return Failure;
2207 }
2208
Sebastian Redl40566802010-08-05 18:21:25 +00002209 // Later tables overwrite earlier ones.
Douglas Gregordfe65432011-07-28 19:11:31 +00002210 // FIXME: Modules will have some trouble with this. This is clearly not
2211 // the right way to do this.
Douglas Gregor409448c2011-07-21 22:35:25 +00002212 VTableUses.clear();
Douglas Gregordfe65432011-07-28 19:11:31 +00002213
2214 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2215 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2216 VTableUses.push_back(
2217 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2218 VTableUses.push_back(Record[Idx++]);
2219 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002220 break;
2221
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002222 case DYNAMIC_CLASSES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002223 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2224 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002225 break;
2226
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002227 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorf2abb522011-07-28 19:26:52 +00002228 if (PendingInstantiations.size() % 2 != 0) {
Axel Naumann39d26c32012-10-02 09:09:43 +00002229 Error("Invalid existing PendingInstantiations");
2230 return Failure;
2231 }
2232
2233 if (Record.size() % 2 != 0) {
Douglas Gregorf2abb522011-07-28 19:26:52 +00002234 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2235 return Failure;
2236 }
Axel Naumann39d26c32012-10-02 09:09:43 +00002237
Douglas Gregorf2abb522011-07-28 19:26:52 +00002238 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2239 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2240 PendingInstantiations.push_back(
2241 ReadSourceLocation(F, Record, I).getRawEncoding());
2242 }
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002243 break;
2244
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002245 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002246 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002247 // FIXME: Modules will have some trouble with this.
2248 SemaDeclRefs.clear();
2249 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2250 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002251 break;
2252
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002253 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002254 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002255 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002256 ActualOriginalFileName.assign(BlobStart, BlobLen);
2257 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002258 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002259 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002260
Douglas Gregor31d375f2011-05-06 21:43:30 +00002261 case ORIGINAL_FILE_ID:
2262 OriginalFileID = FileID::get(Record[0]);
2263 break;
2264
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002265 case ORIGINAL_PCH_DIR:
2266 // The primary AST will be the last to get here, so it will be the one
2267 // that's used.
2268 OriginalDir.assign(BlobStart, BlobLen);
2269 break;
2270
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002271 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002272 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002273 StringRef ASTBranch(BlobStart, BlobLen);
2274 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002275 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002276 return IgnorePCH;
2277 }
2278 break;
2279 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002280
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002281 case PPD_ENTITIES_OFFSETS: {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002282 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart;
2283 assert(BlobLen % sizeof(PPEntityOffset) == 0);
2284 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002285
2286 unsigned LocalBasePreprocessedEntityID = Record[0];
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002287
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002288 unsigned StartingID;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002289 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00002290 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002291 if (!PP.getPreprocessingRecord()->getExternalSource())
2292 PP.getPreprocessingRecord()->SetExternalSource(*this);
2293 StartingID
2294 = PP.getPreprocessingRecord()
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002295 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Douglas Gregor9827a802011-07-29 00:56:45 +00002296 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002297
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002298 if (F.NumPreprocessedEntities > 0) {
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002299 // Introduce the global -> local mapping for preprocessed entities in
2300 // this module.
2301 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2302
2303 // Introduce the local -> global mapping for preprocessed entities in
2304 // this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002305 F.PreprocessedEntityRemap.insertOrReplace(
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002306 std::make_pair(LocalBasePreprocessedEntityID,
2307 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2308 }
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002309
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002310 break;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002311 }
2312
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002313 case DECL_UPDATE_OFFSETS: {
2314 if (Record.size() % 2 != 0) {
2315 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2316 return Failure;
2317 }
2318 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregor496c7092011-08-03 15:48:04 +00002319 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2320 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002321 break;
2322 }
2323
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002324 case DECL_REPLACEMENTS: {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002325 if (Record.size() % 3 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002326 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002327 return Failure;
2328 }
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002329 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
Douglas Gregor496c7092011-08-03 15:48:04 +00002330 ReplacedDecls[getGlobalDeclID(F, Record[I])]
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002331 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002332 break;
2333 }
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002334
Douglas Gregorcff9f262012-01-27 01:47:08 +00002335 case OBJC_CATEGORIES_MAP: {
2336 if (F.LocalNumObjCCategoriesInMap != 0) {
2337 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002338 return Failure;
2339 }
Douglas Gregorcff9f262012-01-27 01:47:08 +00002340
2341 F.LocalNumObjCCategoriesInMap = Record[0];
2342 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)BlobStart;
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002343 break;
2344 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002345
Douglas Gregorcff9f262012-01-27 01:47:08 +00002346 case OBJC_CATEGORIES:
2347 F.ObjCCategories.swap(Record);
2348 break;
2349
Douglas Gregor7c789c12010-10-29 22:39:52 +00002350 case CXX_BASE_SPECIFIER_OFFSETS: {
2351 if (F.LocalNumCXXBaseSpecifiers != 0) {
2352 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2353 return Failure;
2354 }
2355
2356 F.LocalNumCXXBaseSpecifiers = Record[0];
2357 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner1da90142011-07-21 21:15:19 +00002358 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002359 break;
2360 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002361
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002362 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002363 if (Record.size() % 2 != 0) {
2364 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2365 return Failure;
2366 }
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002367
2368 if (F.PragmaDiagMappings.empty())
2369 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002370 else
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002371 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2372 Record.begin(), Record.end());
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002373 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002374
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002375 case CUDA_SPECIAL_DECL_REFS:
2376 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002377 // FIXME: Modules will have trouble with this.
2378 CUDASpecialDeclRefs.clear();
2379 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2380 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002381 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002382
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002383 case HEADER_SEARCH_TABLE: {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002384 F.HeaderFileInfoTableData = BlobStart;
2385 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002386 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002387 if (Record[0]) {
2388 F.HeaderFileInfoTable
2389 = HeaderFileInfoLookupTable::Create(
2390 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002391 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregor95eab172011-07-28 20:55:49 +00002392 HeaderFileInfoTrait(*this, F,
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002393 &PP.getHeaderSearchInfo(),
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002394 BlobStart + Record[2]));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002395
2396 PP.getHeaderSearchInfo().SetExternalSource(this);
2397 if (!PP.getHeaderSearchInfo().getExternalLookup())
2398 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002399 }
2400 break;
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002401 }
2402
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002403 case FP_PRAGMA_OPTIONS:
2404 // Later tables overwrite earlier ones.
2405 FPPragmaOptions.swap(Record);
2406 break;
2407
2408 case OPENCL_EXTENSIONS:
2409 // Later tables overwrite earlier ones.
2410 OpenCLExtensions.swap(Record);
2411 break;
Sean Huntebcbe1d2011-05-04 23:29:54 +00002412
2413 case TENTATIVE_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002414 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2415 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002416 break;
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002417
2418 case KNOWN_NAMESPACES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002419 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2420 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002421 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002422
2423 case IMPORTED_MODULES: {
2424 if (F.Kind != MK_Module) {
2425 // If we aren't loading a module (which has its own exports), make
2426 // all of the imported modules visible.
2427 // FIXME: Deal with macros-only imports.
2428 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2429 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2430 ImportedModules.push_back(GlobalID);
2431 }
2432 }
2433 break;
Douglas Gregora1be2782011-12-17 23:38:30 +00002434 }
Douglas Gregor2171bf12012-01-15 16:58:34 +00002435
Douglas Gregora1be2782011-12-17 23:38:30 +00002436 case LOCAL_REDECLARATIONS: {
Douglas Gregor2171bf12012-01-15 16:58:34 +00002437 F.RedeclarationChains.swap(Record);
2438 break;
2439 }
2440
2441 case LOCAL_REDECLARATIONS_MAP: {
2442 if (F.LocalNumRedeclarationsInMap != 0) {
2443 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
Douglas Gregora1be2782011-12-17 23:38:30 +00002444 return Failure;
2445 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00002446
Douglas Gregor2171bf12012-01-15 16:58:34 +00002447 F.LocalNumRedeclarationsInMap = Record[0];
2448 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)BlobStart;
Douglas Gregora1be2782011-12-17 23:38:30 +00002449 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002450 }
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00002451
2452 case MERGED_DECLARATIONS: {
2453 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2454 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2455 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2456 for (unsigned N = Record[Idx++]; N > 0; --N)
2457 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2458 }
2459 break;
2460 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002461 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002462 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002463 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002464 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002465}
2466
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002467ASTReader::ASTReadResult ASTReader::validateFileEntries(ModuleFile &M) {
Douglas Gregorc69a2922011-08-25 20:58:51 +00002468 llvm::BitstreamCursor &SLocEntryCursor = M.SLocEntryCursor;
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002469
Douglas Gregorc69a2922011-08-25 20:58:51 +00002470 for (unsigned i = 0, e = M.LocalNumSLocFileEntries; i != e; ++i) {
2471 SLocEntryCursor.JumpToBit(M.SLocFileOffsets[i]);
2472 unsigned Code = SLocEntryCursor.ReadCode();
2473 if (Code == llvm::bitc::END_BLOCK ||
2474 Code == llvm::bitc::ENTER_SUBBLOCK ||
2475 Code == llvm::bitc::DEFINE_ABBREV) {
2476 Error("incorrectly-formatted source location entry in AST file");
2477 return Failure;
2478 }
2479
2480 RecordData Record;
2481 const char *BlobStart;
2482 unsigned BlobLen;
2483 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2484 default:
2485 Error("incorrectly-formatted source location entry in AST file");
2486 return Failure;
2487
2488 case SM_SLOC_FILE_ENTRY: {
Douglas Gregora081da52011-11-16 20:05:18 +00002489 // If the buffer was overridden, the file need not exist.
2490 if (Record[6])
2491 break;
2492
Douglas Gregorc69a2922011-08-25 20:58:51 +00002493 StringRef Filename(BlobStart, BlobLen);
2494 const FileEntry *File = getFileEntry(Filename);
2495
2496 if (File == 0) {
2497 std::string ErrorStr = "could not find file '";
2498 ErrorStr += Filename;
2499 ErrorStr += "' referenced by AST file";
2500 Error(ErrorStr.c_str());
2501 return IgnorePCH;
2502 }
2503
Douglas Gregora081da52011-11-16 20:05:18 +00002504 if (Record.size() < 7) {
Douglas Gregorc69a2922011-08-25 20:58:51 +00002505 Error("source location entry is incorrect");
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002506 return Failure;
2507 }
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002508
2509 off_t StoredSize = (off_t)Record[4];
2510 time_t StoredTime = (time_t)Record[5];
2511
2512 // Check if there was a request to override the contents of the file
2513 // that was part of the precompiled header. Overridding such a file
2514 // can lead to problems when lexing using the source locations from the
2515 // PCH.
2516 SourceManager &SM = getSourceManager();
2517 if (SM.isFileOverridden(File)) {
2518 Error(diag::err_fe_pch_file_overridden, Filename);
2519 // After emitting the diagnostic, recover by disabling the override so
2520 // that the original file will be used.
2521 SM.disableFileContentsOverride(File);
2522 // The FileEntry is a virtual file entry with the size of the contents
2523 // that would override the original contents. Set it to the original's
2524 // size/time.
2525 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2526 StoredSize, StoredTime);
2527 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002528
Douglas Gregorc69a2922011-08-25 20:58:51 +00002529 // The stat info from the FileEntry came from the cached stat
2530 // info of the PCH, so we cannot trust it.
2531 struct stat StatBuf;
2532 if (::stat(File->getName(), &StatBuf) != 0) {
2533 StatBuf.st_size = File->getSize();
2534 StatBuf.st_mtime = File->getModificationTime();
2535 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002536
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002537 if ((StoredSize != StatBuf.st_size
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002538#if !defined(LLVM_ON_WIN32)
Douglas Gregorc69a2922011-08-25 20:58:51 +00002539 // In our regression testing, the Windows file system seems to
2540 // have inconsistent modification times that sometimes
2541 // erroneously trigger this error-handling path.
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002542 || StoredTime != StatBuf.st_mtime
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002543#endif
Douglas Gregorc69a2922011-08-25 20:58:51 +00002544 )) {
2545 Error(diag::err_fe_pch_file_modified, Filename);
2546 return IgnorePCH;
2547 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002548
Douglas Gregorc69a2922011-08-25 20:58:51 +00002549 break;
2550 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002551 }
2552 }
2553
2554 return Success;
2555}
2556
Douglas Gregorecc2c092011-12-01 22:20:10 +00002557void ASTReader::makeNamesVisible(const HiddenNames &Names) {
Douglas Gregor13292642011-12-02 15:45:10 +00002558 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
2559 if (Decl *D = Names[I].dyn_cast<Decl *>())
Douglas Gregorf143ffc2012-01-06 16:22:39 +00002560 D->Hidden = false;
Douglas Gregor1d4c1132011-12-20 22:06:13 +00002561 else {
2562 IdentifierInfo *II = Names[I].get<IdentifierInfo *>();
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002563 // FIXME: Check if this works correctly with macro history.
Douglas Gregor1d4c1132011-12-20 22:06:13 +00002564 if (!II->hasMacroDefinition()) {
Douglas Gregorc12906e2012-09-24 19:56:18 +00002565 // Make sure that this macro hasn't been #undef'd in the mean-time.
2566 llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator Known
2567 = PP.Macros.find(II);
2568 if (Known == PP.Macros.end() ||
2569 Known->second->getUndefLoc().isInvalid()) {
2570 II->setHasMacroDefinition(true);
2571 if (DeserializationListener)
2572 DeserializationListener->MacroVisible(II);
2573 }
Douglas Gregor1d4c1132011-12-20 22:06:13 +00002574 }
2575 }
Douglas Gregor13292642011-12-02 15:45:10 +00002576 }
Douglas Gregorecc2c092011-12-01 22:20:10 +00002577}
2578
Douglas Gregor5e356932011-12-01 17:11:21 +00002579void ASTReader::makeModuleVisible(Module *Mod,
2580 Module::NameVisibilityKind NameVisibility) {
2581 llvm::SmallPtrSet<Module *, 4> Visited;
2582 llvm::SmallVector<Module *, 4> Stack;
2583 Stack.push_back(Mod);
2584 while (!Stack.empty()) {
2585 Mod = Stack.back();
2586 Stack.pop_back();
2587
2588 if (NameVisibility <= Mod->NameVisibility) {
2589 // This module already has this level of visibility (or greater), so
2590 // there is nothing more to do.
2591 continue;
2592 }
2593
Douglas Gregor51f564f2011-12-31 04:05:44 +00002594 if (!Mod->isAvailable()) {
2595 // Modules that aren't available cannot be made visible.
2596 continue;
2597 }
2598
Douglas Gregor5e356932011-12-01 17:11:21 +00002599 // Update the module's name visibility.
2600 Mod->NameVisibility = NameVisibility;
2601
Douglas Gregorecc2c092011-12-01 22:20:10 +00002602 // If we've already deserialized any names from this module,
Douglas Gregor5e356932011-12-01 17:11:21 +00002603 // mark them as visible.
Douglas Gregorecc2c092011-12-01 22:20:10 +00002604 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2605 if (Hidden != HiddenNamesMap.end()) {
2606 makeNamesVisible(Hidden->second);
2607 HiddenNamesMap.erase(Hidden);
2608 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002609
2610 // Push any non-explicit submodules onto the stack to be marked as
2611 // visible.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002612 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2613 SubEnd = Mod->submodule_end();
Douglas Gregor5e356932011-12-01 17:11:21 +00002614 Sub != SubEnd; ++Sub) {
Douglas Gregorb7a78192012-01-04 23:32:19 +00002615 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2616 Stack.push_back(*Sub);
Douglas Gregor5e356932011-12-01 17:11:21 +00002617 }
Douglas Gregor07165b92011-12-02 19:11:09 +00002618
2619 // Push any exported modules onto the stack to be marked as visible.
Douglas Gregor0adaa882011-12-05 17:28:06 +00002620 bool AnyWildcard = false;
2621 bool UnrestrictedWildcard = false;
2622 llvm::SmallVector<Module *, 4> WildcardRestrictions;
Douglas Gregor07165b92011-12-02 19:11:09 +00002623 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
2624 Module *Exported = Mod->Exports[I].getPointer();
Douglas Gregor0adaa882011-12-05 17:28:06 +00002625 if (!Mod->Exports[I].getInt()) {
2626 // Export a named module directly; no wildcards involved.
2627 if (Visited.insert(Exported))
Douglas Gregor07165b92011-12-02 19:11:09 +00002628 Stack.push_back(Exported);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002629
2630 continue;
Douglas Gregor07165b92011-12-02 19:11:09 +00002631 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002632
2633 // Wildcard export: export all of the imported modules that match
2634 // the given pattern.
2635 AnyWildcard = true;
2636 if (UnrestrictedWildcard)
2637 continue;
2638
2639 if (Module *Restriction = Mod->Exports[I].getPointer())
2640 WildcardRestrictions.push_back(Restriction);
2641 else {
2642 WildcardRestrictions.clear();
2643 UnrestrictedWildcard = true;
2644 }
2645 }
2646
2647 // If there were any wildcards, push any imported modules that were
2648 // re-exported by the wildcard restriction.
2649 if (!AnyWildcard)
2650 continue;
2651
2652 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
2653 Module *Imported = Mod->Imports[I];
Benjamin Kramerd48bcb22012-08-22 15:37:55 +00002654 if (!Visited.insert(Imported))
Douglas Gregor0adaa882011-12-05 17:28:06 +00002655 continue;
2656
2657 bool Acceptable = UnrestrictedWildcard;
2658 if (!Acceptable) {
2659 // Check whether this module meets one of the restrictions.
2660 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
2661 Module *Restriction = WildcardRestrictions[R];
2662 if (Imported == Restriction || Imported->isSubModuleOf(Restriction)) {
2663 Acceptable = true;
2664 break;
2665 }
2666 }
2667 }
2668
2669 if (!Acceptable)
2670 continue;
2671
Douglas Gregor0adaa882011-12-05 17:28:06 +00002672 Stack.push_back(Imported);
Douglas Gregor07165b92011-12-02 19:11:09 +00002673 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002674 }
2675}
2676
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002677ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregor72a9ae12011-07-22 16:00:58 +00002678 ModuleKind Type) {
Douglas Gregor057df202012-01-18 20:56:22 +00002679 // Bump the generation number.
Douglas Gregorcff9f262012-01-27 01:47:08 +00002680 unsigned PreviousGeneration = CurrentGeneration++;
Douglas Gregor057df202012-01-18 20:56:22 +00002681
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002682 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002683 case Failure: return Failure;
2684 case IgnorePCH: return IgnorePCH;
2685 case Success: break;
2686 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002687
2688 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor057df202012-01-18 20:56:22 +00002689
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002690 // Check the predefines buffers.
Douglas Gregor6236a292011-12-02 21:56:05 +00002691 if (!DisableValidation && Type == MK_PCH &&
Argyrios Kyrtzidis26d43cd2011-09-12 18:09:38 +00002692 // FIXME: CheckPredefinesBuffers also sets the SuggestedPredefines;
2693 // if DisableValidation is true, defines that were set on command-line
2694 // but not in the PCH file will not be added to SuggestedPredefines.
2695 CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002696 return IgnorePCH;
2697
Douglas Gregoreee242f2011-10-27 09:33:13 +00002698 // Mark all of the identifiers in the identifier table as being out of date,
2699 // so that various accessors know to check the loaded modules when the
2700 // identifier is used.
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002701 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2702 IdEnd = PP.getIdentifierTable().end();
2703 Id != IdEnd; ++Id)
Douglas Gregoreee242f2011-10-27 09:33:13 +00002704 Id->second->setOutOfDate(true);
Douglas Gregor057df202012-01-18 20:56:22 +00002705
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002706 // Resolve any unresolved module exports.
Douglas Gregor55988682011-12-05 16:33:54 +00002707 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) {
2708 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I];
2709 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002710 Module *ResolvedMod = getSubmodule(GlobalID);
2711
2712 if (Unresolved.IsImport) {
2713 if (ResolvedMod)
Douglas Gregor55988682011-12-05 16:33:54 +00002714 Unresolved.Mod->Imports.push_back(ResolvedMod);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002715 continue;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002716 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002717
2718 if (ResolvedMod || Unresolved.IsWildcard)
2719 Unresolved.Mod->Exports.push_back(
2720 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002721 }
Douglas Gregor55988682011-12-05 16:33:54 +00002722 UnresolvedModuleImportExports.clear();
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002723
Douglas Gregor35942772011-09-09 21:34:22 +00002724 InitializeContext();
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002725
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002726 if (DeserializationListener)
2727 DeserializationListener->ReaderInitialized(this);
2728
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002729 if (!OriginalFileID.isInvalid()) {
2730 OriginalFileID = FileID::get(ModuleMgr.getPrimaryModule().SLocEntryBaseID
2731 + OriginalFileID.getOpaqueValue() - 1);
2732
2733 // If this AST file is a precompiled preamble, then set the preamble file ID
2734 // of the source manager to the file source file from which the preamble was
2735 // built.
2736 if (Type == MK_Preamble) {
Argyrios Kyrtzidis507097e2011-09-19 20:40:35 +00002737 SourceMgr.setPreambleFileID(OriginalFileID);
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002738 } else if (Type == MK_MainFile) {
2739 SourceMgr.setMainFileID(OriginalFileID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002740 }
Douglas Gregor414cb642010-11-30 05:23:00 +00002741 }
2742
Douglas Gregorcff9f262012-01-27 01:47:08 +00002743 // For any Objective-C class definitions we have already loaded, make sure
2744 // that we load any additional categories.
2745 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
2746 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
2747 ObjCClassesLoaded[I],
2748 PreviousGeneration);
2749 }
2750
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002751 return Success;
2752}
2753
Chris Lattner5f9e2722011-07-23 10:55:15 +00002754ASTReader::ASTReadResult ASTReader::ReadASTCore(StringRef FileName,
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002755 ModuleKind Type,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002756 ModuleFile *ImportedBy) {
2757 ModuleFile *M;
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002758 bool NewModule;
2759 std::string ErrorStr;
2760 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
Douglas Gregor057df202012-01-18 20:56:22 +00002761 CurrentGeneration, ErrorStr);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002762
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002763 if (!M) {
2764 // We couldn't load the module.
2765 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2766 + ErrorStr;
2767 Error(Msg);
2768 return Failure;
2769 }
2770
2771 if (!NewModule) {
2772 // We've already loaded this module.
2773 return Success;
2774 }
2775
2776 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2777 // module?
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002778 if (FileName != "-") {
2779 CurrentDir = llvm::sys::path::parent_path(FileName);
2780 if (CurrentDir.empty()) CurrentDir = ".";
2781 }
2782
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002783 ModuleFile &F = *M;
Sebastian Redl9137a522010-07-16 17:50:48 +00002784 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002785 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002786 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002787
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002788 // Sniff for the signature.
2789 if (Stream.Read(8) != 'C' ||
2790 Stream.Read(8) != 'P' ||
2791 Stream.Read(8) != 'C' ||
2792 Stream.Read(8) != 'H') {
2793 Diag(diag::err_not_a_pch_file) << FileName;
2794 return Failure;
2795 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002796
Douglas Gregor2cf26342009-04-09 22:27:44 +00002797 while (!Stream.AtEndOfStream()) {
2798 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002799
Douglas Gregore1d918e2009-04-10 23:10:45 +00002800 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002801 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002802 return Failure;
2803 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002804
2805 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002806
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002807 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002808 switch (BlockID) {
2809 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002810 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002811 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002812 return Failure;
2813 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002814 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002815 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002816 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002817 case Success:
2818 break;
2819
2820 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002821 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002822
2823 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002824 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002825 // AST block, skipping subblocks, to see if there are other
2826 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002827
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002828 // FIXME: We can't clear loaded slocentries anymore.
2829 //SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002830
2831 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002832 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002833 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002834
Douglas Gregore1d918e2009-04-10 23:10:45 +00002835 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002836 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002837 break;
2838 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002839 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002840 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002841 return Failure;
2842 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002843 break;
2844 }
Mike Stump1eb44332009-09-09 15:08:12 +00002845 }
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002846
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002847 // Once read, set the ModuleFile bit base offset and update the size in
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002848 // bits of all files we've seen.
2849 F.GlobalBitOffset = TotalModulesSizeInBits;
2850 TotalModulesSizeInBits += F.SizeInBits;
2851 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
Douglas Gregorc69a2922011-08-25 20:58:51 +00002852
2853 // Make sure that the files this module was built against are still available.
2854 if (!DisableValidation) {
2855 switch(validateFileEntries(*M)) {
2856 case Failure: return Failure;
2857 case IgnorePCH: return IgnorePCH;
2858 case Success: break;
2859 }
2860 }
Douglas Gregorf249bf32011-08-25 21:09:44 +00002861
2862 // Preload SLocEntries.
2863 for (unsigned I = 0, N = M->PreloadSLocEntries.size(); I != N; ++I) {
2864 int Index = int(M->PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Argyrios Kyrtzidisac1ffcc2011-09-19 20:39:54 +00002865 // Load it through the SourceManager and don't call ReadSLocEntryRecord()
2866 // directly because the entry may have already been loaded in which case
2867 // calling ReadSLocEntryRecord() directly would trigger an assertion in
2868 // SourceManager.
2869 SourceMgr.getLoadedSLocEntryByID(Index);
Douglas Gregorf249bf32011-08-25 21:09:44 +00002870 }
2871
Douglas Gregorc69a2922011-08-25 20:58:51 +00002872
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002873 return Success;
2874}
2875
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002876void ASTReader::InitializeContext() {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002877 // If there's a listener, notify them that we "read" the translation unit.
2878 if (DeserializationListener)
Douglas Gregor35942772011-09-09 21:34:22 +00002879 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
2880 Context.getTranslationUnitDecl());
Douglas Gregor3747ee72010-10-01 01:18:02 +00002881
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002882 // Make sure we load the declaration update records for the translation unit,
2883 // if there are any.
Douglas Gregor35942772011-09-09 21:34:22 +00002884 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
2885 Context.getTranslationUnitDecl());
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002886
Douglas Gregor5f957282011-08-11 22:18:49 +00002887 // FIXME: Find a better way to deal with collisions between these
2888 // built-in types. Right now, we just ignore the problem.
2889
2890 // Load the special types.
Douglas Gregora6ea10e2012-01-17 18:09:05 +00002891 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002892 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2893 if (!Context.CFConstantStringTypeDecl)
2894 Context.setCFConstantStringType(GetType(String));
2895 }
2896
2897 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2898 QualType FileType = GetType(File);
2899 if (FileType.isNull()) {
2900 Error("FILE type is NULL");
2901 return;
2902 }
2903
2904 if (!Context.FILEDecl) {
2905 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2906 Context.setFILEDecl(Typedef->getDecl());
2907 else {
2908 const TagType *Tag = FileType->getAs<TagType>();
2909 if (!Tag) {
2910 Error("Invalid FILE type in AST file");
2911 return;
2912 }
2913 Context.setFILEDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002914 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002915 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002916 }
Douglas Gregor5f957282011-08-11 22:18:49 +00002917
Douglas Gregor72cd7a02011-11-11 19:13:12 +00002918 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002919 QualType Jmp_bufType = GetType(Jmp_buf);
2920 if (Jmp_bufType.isNull()) {
2921 Error("jmp_buf type is NULL");
2922 return;
2923 }
2924
2925 if (!Context.jmp_bufDecl) {
2926 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2927 Context.setjmp_bufDecl(Typedef->getDecl());
2928 else {
2929 const TagType *Tag = Jmp_bufType->getAs<TagType>();
2930 if (!Tag) {
2931 Error("Invalid jmp_buf type in AST file");
2932 return;
2933 }
2934 Context.setjmp_bufDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002935 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002936 }
Mike Stump782fa302009-07-28 02:25:19 +00002937 }
Douglas Gregor02a5e872011-09-10 00:30:18 +00002938
Douglas Gregor72cd7a02011-11-11 19:13:12 +00002939 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002940 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2941 if (Sigjmp_bufType.isNull()) {
2942 Error("sigjmp_buf type is NULL");
2943 return;
2944 }
2945
2946 if (!Context.sigjmp_bufDecl) {
2947 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2948 Context.setsigjmp_bufDecl(Typedef->getDecl());
2949 else {
2950 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
2951 assert(Tag && "Invalid sigjmp_buf type in AST file");
2952 Context.setsigjmp_bufDecl(Tag->getDecl());
2953 }
2954 }
2955 }
2956
2957 if (unsigned ObjCIdRedef
2958 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
2959 if (Context.ObjCIdRedefinitionType.isNull())
2960 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
2961 }
2962
2963 if (unsigned ObjCClassRedef
2964 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
2965 if (Context.ObjCClassRedefinitionType.isNull())
2966 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
2967 }
2968
2969 if (unsigned ObjCSelRedef
2970 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
2971 if (Context.ObjCSelRedefinitionType.isNull())
2972 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
2973 }
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00002974
2975 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
2976 QualType Ucontext_tType = GetType(Ucontext_t);
2977 if (Ucontext_tType.isNull()) {
2978 Error("ucontext_t type is NULL");
2979 return;
2980 }
2981
2982 if (!Context.ucontext_tDecl) {
2983 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
2984 Context.setucontext_tDecl(Typedef->getDecl());
2985 else {
2986 const TagType *Tag = Ucontext_tType->getAs<TagType>();
2987 assert(Tag && "Invalid ucontext_t type in AST file");
2988 Context.setucontext_tDecl(Tag->getDecl());
2989 }
2990 }
2991 }
Douglas Gregor5f957282011-08-11 22:18:49 +00002992 }
2993
Douglas Gregor35942772011-09-09 21:34:22 +00002994 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002995
2996 // If there were any CUDA special declarations, deserialize them.
2997 if (!CUDASpecialDeclRefs.empty()) {
2998 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
Douglas Gregor35942772011-09-09 21:34:22 +00002999 Context.setcudaConfigureCallDecl(
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003000 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3001 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003002
3003 // Re-export any modules that were imported by a non-module AST file.
3004 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3005 if (Module *Imported = getSubmodule(ImportedModules[I]))
3006 makeModuleVisible(Imported, Module::AllVisible);
3007 }
3008 ImportedModules.clear();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003009}
3010
Douglas Gregorecc2c092011-12-01 22:20:10 +00003011void ASTReader::finalizeForWriting() {
3012 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3013 HiddenEnd = HiddenNamesMap.end();
3014 Hidden != HiddenEnd; ++Hidden) {
3015 makeNamesVisible(Hidden->second);
3016 }
3017 HiddenNamesMap.clear();
3018}
3019
Douglas Gregorb64c1932009-05-12 01:31:05 +00003020/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003021/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00003022/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003023std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00003024 FileManager &FileMgr,
David Blaikied6471f72011-09-25 23:23:43 +00003025 DiagnosticsEngine &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003026 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003027 std::string ErrStr;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00003028 OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00003029 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00003030 if (!Buffer) {
Kaelyn Uhrainda01f622012-06-20 00:36:03 +00003031 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003032 return std::string();
3033 }
3034
3035 // Initialize the stream
3036 llvm::BitstreamReader StreamFile;
3037 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00003038 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00003039 (const unsigned char *)Buffer->getBufferEnd());
3040 Stream.init(StreamFile);
3041
3042 // Sniff for the signature.
3043 if (Stream.Read(8) != 'C' ||
3044 Stream.Read(8) != 'P' ||
3045 Stream.Read(8) != 'C' ||
3046 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003047 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003048 return std::string();
3049 }
3050
3051 RecordData Record;
3052 while (!Stream.AtEndOfStream()) {
3053 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00003054
Douglas Gregorb64c1932009-05-12 01:31:05 +00003055 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3056 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00003057
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003058 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003059 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003060 case AST_BLOCK_ID:
3061 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003062 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003063 return std::string();
3064 }
3065 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003066
Douglas Gregorb64c1932009-05-12 01:31:05 +00003067 default:
3068 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003069 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003070 return std::string();
3071 }
3072 break;
3073 }
3074 continue;
3075 }
3076
3077 if (Code == llvm::bitc::END_BLOCK) {
3078 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003079 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003080 return std::string();
3081 }
3082 continue;
3083 }
3084
3085 if (Code == llvm::bitc::DEFINE_ABBREV) {
3086 Stream.ReadAbbrevRecord();
3087 continue;
3088 }
3089
3090 Record.clear();
3091 const char *BlobStart = 0;
3092 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003093 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003094 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00003095 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00003096 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00003097
3098 return std::string();
3099}
3100
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003101ASTReader::ASTReadResult ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003102 // Enter the submodule block.
3103 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3104 Error("malformed submodule block record in AST file");
3105 return Failure;
3106 }
3107
3108 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
Douglas Gregor26ced122011-12-01 00:59:36 +00003109 bool First = true;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003110 Module *CurrentModule = 0;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003111 RecordData Record;
3112 while (true) {
3113 unsigned Code = F.Stream.ReadCode();
3114 if (Code == llvm::bitc::END_BLOCK) {
3115 if (F.Stream.ReadBlockEnd()) {
3116 Error("error at end of submodule block in AST file");
3117 return Failure;
3118 }
3119 return Success;
3120 }
3121
3122 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3123 // No known subblocks, always skip them.
3124 F.Stream.ReadSubBlockID();
3125 if (F.Stream.SkipBlock()) {
3126 Error("malformed block record in AST file");
3127 return Failure;
3128 }
3129 continue;
3130 }
3131
3132 if (Code == llvm::bitc::DEFINE_ABBREV) {
3133 F.Stream.ReadAbbrevRecord();
3134 continue;
3135 }
3136
3137 // Read a record.
3138 const char *BlobStart;
3139 unsigned BlobLen;
3140 Record.clear();
3141 switch (F.Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
3142 default: // Default behavior: ignore.
3143 break;
3144
3145 case SUBMODULE_DEFINITION: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003146 if (First) {
3147 Error("missing submodule metadata record at beginning of block");
3148 return Failure;
3149 }
3150
Douglas Gregore209e502011-12-06 01:10:29 +00003151 if (Record.size() < 7) {
Douglas Gregor1e123682011-12-05 22:27:44 +00003152 Error("malformed module definition");
3153 return Failure;
3154 }
3155
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003156 StringRef Name(BlobStart, BlobLen);
Douglas Gregore209e502011-12-06 01:10:29 +00003157 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3158 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3159 bool IsFramework = Record[2];
3160 bool IsExplicit = Record[3];
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003161 bool IsSystem = Record[4];
3162 bool InferSubmodules = Record[5];
3163 bool InferExplicitSubmodules = Record[6];
3164 bool InferExportWildcard = Record[7];
Douglas Gregor1e123682011-12-05 22:27:44 +00003165
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003166 Module *ParentModule = 0;
Douglas Gregor26ced122011-12-01 00:59:36 +00003167 if (Parent)
3168 ParentModule = getSubmodule(Parent);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003169
3170 // Retrieve this (sub)module from the module map, creating it if
3171 // necessary.
3172 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3173 IsFramework,
3174 IsExplicit).first;
Douglas Gregore209e502011-12-06 01:10:29 +00003175 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3176 if (GlobalIndex >= SubmodulesLoaded.size() ||
3177 SubmodulesLoaded[GlobalIndex]) {
Douglas Gregor26ced122011-12-01 00:59:36 +00003178 Error("too many submodules");
3179 return Failure;
3180 }
Douglas Gregora015cab2011-12-02 17:30:13 +00003181
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00003182 CurrentModule->setASTFile(F.File);
Douglas Gregor305dc3e2011-12-20 00:28:52 +00003183 CurrentModule->IsFromModuleFile = true;
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003184 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Douglas Gregor1e123682011-12-05 22:27:44 +00003185 CurrentModule->InferSubmodules = InferSubmodules;
3186 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3187 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregora015cab2011-12-02 17:30:13 +00003188 if (DeserializationListener)
Douglas Gregore209e502011-12-06 01:10:29 +00003189 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
Douglas Gregora015cab2011-12-02 17:30:13 +00003190
Douglas Gregore209e502011-12-06 01:10:29 +00003191 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003192 break;
3193 }
3194
Douglas Gregor77d029f2011-12-08 19:11:24 +00003195 case SUBMODULE_UMBRELLA_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003196 if (First) {
3197 Error("missing submodule metadata record at beginning of block");
3198 return Failure;
3199 }
3200
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003201 if (!CurrentModule)
3202 break;
3203
3204 StringRef FileName(BlobStart, BlobLen);
3205 if (const FileEntry *Umbrella = PP.getFileManager().getFile(FileName)) {
Douglas Gregor10694ce2011-12-08 17:39:04 +00003206 if (!CurrentModule->getUmbrellaHeader())
Douglas Gregore209e502011-12-06 01:10:29 +00003207 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
Douglas Gregor10694ce2011-12-08 17:39:04 +00003208 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003209 Error("mismatched umbrella headers in submodule");
3210 return Failure;
3211 }
3212 }
3213 break;
3214 }
3215
3216 case SUBMODULE_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003217 if (First) {
3218 Error("missing submodule metadata record at beginning of block");
3219 return Failure;
3220 }
3221
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003222 if (!CurrentModule)
3223 break;
3224
3225 // FIXME: Be more lazy about this!
3226 StringRef FileName(BlobStart, BlobLen);
3227 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3228 if (std::find(CurrentModule->Headers.begin(),
3229 CurrentModule->Headers.end(),
3230 File) == CurrentModule->Headers.end())
Douglas Gregore209e502011-12-06 01:10:29 +00003231 ModMap.addHeader(CurrentModule, File);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003232 }
3233 break;
3234 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00003235
3236 case SUBMODULE_TOPHEADER: {
3237 if (First) {
3238 Error("missing submodule metadata record at beginning of block");
3239 return Failure;
3240 }
3241
3242 if (!CurrentModule)
3243 break;
3244
3245 // FIXME: Be more lazy about this!
3246 StringRef FileName(BlobStart, BlobLen);
3247 if (const FileEntry *File = PP.getFileManager().getFile(FileName))
3248 CurrentModule->TopHeaders.insert(File);
3249 break;
3250 }
3251
Douglas Gregor77d029f2011-12-08 19:11:24 +00003252 case SUBMODULE_UMBRELLA_DIR: {
3253 if (First) {
3254 Error("missing submodule metadata record at beginning of block");
3255 return Failure;
3256 }
3257
3258 if (!CurrentModule)
3259 break;
3260
3261 StringRef DirName(BlobStart, BlobLen);
3262 if (const DirectoryEntry *Umbrella
3263 = PP.getFileManager().getDirectory(DirName)) {
3264 if (!CurrentModule->getUmbrellaDir())
3265 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3266 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3267 Error("mismatched umbrella directories in submodule");
3268 return Failure;
3269 }
3270 }
3271 break;
3272 }
3273
Douglas Gregor26ced122011-12-01 00:59:36 +00003274 case SUBMODULE_METADATA: {
3275 if (!First) {
3276 Error("submodule metadata record not at beginning of block");
3277 return Failure;
3278 }
3279 First = false;
3280
3281 F.BaseSubmoduleID = getTotalNumSubmodules();
Douglas Gregor26ced122011-12-01 00:59:36 +00003282 F.LocalNumSubmodules = Record[0];
3283 unsigned LocalBaseSubmoduleID = Record[1];
3284 if (F.LocalNumSubmodules > 0) {
3285 // Introduce the global -> local mapping for submodules within this
3286 // module.
3287 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3288
3289 // Introduce the local -> global mapping for submodules within this
3290 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00003291 F.SubmoduleRemap.insertOrReplace(
Douglas Gregor26ced122011-12-01 00:59:36 +00003292 std::make_pair(LocalBaseSubmoduleID,
3293 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3294
3295 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3296 }
3297 break;
3298 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003299
Douglas Gregor55988682011-12-05 16:33:54 +00003300 case SUBMODULE_IMPORTS: {
3301 if (First) {
3302 Error("missing submodule metadata record at beginning of block");
3303 return Failure;
3304 }
3305
3306 if (!CurrentModule)
3307 break;
3308
3309 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
3310 UnresolvedModuleImportExport Unresolved;
3311 Unresolved.File = &F;
3312 Unresolved.Mod = CurrentModule;
3313 Unresolved.ID = Record[Idx];
3314 Unresolved.IsImport = true;
3315 Unresolved.IsWildcard = false;
3316 UnresolvedModuleImportExports.push_back(Unresolved);
3317 }
3318 break;
3319 }
3320
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003321 case SUBMODULE_EXPORTS: {
3322 if (First) {
3323 Error("missing submodule metadata record at beginning of block");
3324 return Failure;
3325 }
3326
3327 if (!CurrentModule)
3328 break;
3329
3330 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor55988682011-12-05 16:33:54 +00003331 UnresolvedModuleImportExport Unresolved;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003332 Unresolved.File = &F;
Douglas Gregor55988682011-12-05 16:33:54 +00003333 Unresolved.Mod = CurrentModule;
3334 Unresolved.ID = Record[Idx];
3335 Unresolved.IsImport = false;
3336 Unresolved.IsWildcard = Record[Idx + 1];
3337 UnresolvedModuleImportExports.push_back(Unresolved);
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003338 }
3339
3340 // Once we've loaded the set of exports, there's no reason to keep
3341 // the parsed, unresolved exports around.
3342 CurrentModule->UnresolvedExports.clear();
3343 break;
3344 }
Douglas Gregor51f564f2011-12-31 04:05:44 +00003345 case SUBMODULE_REQUIRES: {
3346 if (First) {
3347 Error("missing submodule metadata record at beginning of block");
3348 return Failure;
3349 }
3350
3351 if (!CurrentModule)
3352 break;
3353
3354 CurrentModule->addRequirement(StringRef(BlobStart, BlobLen),
David Blaikie4e4d0842012-03-11 07:00:24 +00003355 Context.getLangOpts(),
Douglas Gregordc58aa72012-01-30 06:01:29 +00003356 Context.getTargetInfo());
Douglas Gregor51f564f2011-12-31 04:05:44 +00003357 break;
3358 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003359 }
3360 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003361}
3362
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003363/// \brief Parse the record that corresponds to a LangOptions data
3364/// structure.
3365///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003366/// This routine parses the language options from the AST file and then gives
3367/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003368///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003369/// \returns true if the listener deems the file unacceptable, false otherwise.
John McCall260611a2012-06-20 06:18:46 +00003370bool ASTReader::ParseLanguageOptions(const RecordData &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003371 if (Listener) {
3372 LangOptions LangOpts;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003373 unsigned Idx = 0;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003374#define LANGOPT(Name, Bits, Default, Description) \
3375 LangOpts.Name = Record[Idx++];
3376#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3377 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3378#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +00003379
3380 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3381 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3382 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003383
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00003384 unsigned Length = Record[Idx++];
3385 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3386 Record.begin() + Idx + Length);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003387 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003388 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003389
3390 return false;
3391}
3392
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003393std::pair<ModuleFile *, unsigned>
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003394ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003395 GlobalPreprocessedEntityMapType::iterator
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003396 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003397 assert(I != GlobalPreprocessedEntityMap.end() &&
3398 "Corrupted global preprocessed entity map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003399 ModuleFile *M = I->second;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003400 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
3401 return std::make_pair(M, LocalIndex);
3402}
3403
Argyrios Kyrtzidis632dcc92012-10-02 16:10:51 +00003404std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
3405ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
3406 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
3407 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
3408 Mod.NumPreprocessedEntities);
3409
3410 return std::make_pair(PreprocessingRecord::iterator(),
3411 PreprocessingRecord::iterator());
3412}
3413
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00003414std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
3415ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
3416 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
3417 ModuleDeclIterator(this, &Mod,
3418 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
3419}
3420
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003421PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
3422 PreprocessedEntityID PPID = Index+1;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003423 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3424 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003425 unsigned LocalIndex = PPInfo.second;
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003426 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
Douglas Gregor4800a5c2011-02-08 21:58:10 +00003427
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003428 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003429 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003430
3431 unsigned Code = M.PreprocessorDetailCursor.ReadCode();
3432 switch (Code) {
3433 case llvm::bitc::END_BLOCK:
3434 return 0;
3435
3436 case llvm::bitc::ENTER_SUBBLOCK:
3437 Error("unexpected subblock record in preprocessor detail block");
3438 return 0;
3439
3440 case llvm::bitc::DEFINE_ABBREV:
3441 Error("unexpected abbrevation record in preprocessor detail block");
3442 return 0;
3443
3444 default:
3445 break;
3446 }
3447
3448 if (!PP.getPreprocessingRecord()) {
3449 Error("no preprocessing record");
3450 return 0;
3451 }
3452
3453 // Read the record.
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003454 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
3455 ReadSourceLocation(M, PPOffs.End));
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003456 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
3457 const char *BlobStart = 0;
3458 unsigned BlobLen = 0;
3459 RecordData Record;
3460 PreprocessorDetailRecordTypes RecType =
3461 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord(
3462 Code, Record, BlobStart, BlobLen);
3463 switch (RecType) {
3464 case PPD_MACRO_EXPANSION: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003465 bool isBuiltin = Record[0];
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003466 IdentifierInfo *Name = 0;
3467 MacroDefinition *Def = 0;
3468 if (isBuiltin)
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003469 Name = getLocalIdentifier(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003470 else {
3471 PreprocessedEntityID
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003472 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003473 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
3474 }
3475
3476 MacroExpansion *ME;
3477 if (isBuiltin)
3478 ME = new (PPRec) MacroExpansion(Name, Range);
3479 else
3480 ME = new (PPRec) MacroExpansion(Def, Range);
3481
3482 return ME;
3483 }
3484
3485 case PPD_MACRO_DEFINITION: {
3486 // Decode the identifier info and then check again; if the macro is
3487 // still defined and associated with the identifier,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003488 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003489 MacroDefinition *MD
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003490 = new (PPRec) MacroDefinition(II, Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003491
3492 if (DeserializationListener)
3493 DeserializationListener->MacroDefinitionRead(PPID, MD);
3494
3495 return MD;
3496 }
3497
3498 case PPD_INCLUSION_DIRECTIVE: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003499 const char *FullFileNameStart = BlobStart + Record[0];
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00003500 StringRef FullFileName(FullFileNameStart, BlobLen - Record[0]);
3501 const FileEntry *File = 0;
3502 if (!FullFileName.empty())
3503 File = PP.getFileManager().getFile(FullFileName);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003504
3505 // FIXME: Stable encoding
3506 InclusionDirective::InclusionKind Kind
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003507 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003508 InclusionDirective *ID
3509 = new (PPRec) InclusionDirective(PPRec, Kind,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003510 StringRef(BlobStart, Record[0]),
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00003511 Record[1], Record[3],
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003512 File,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003513 Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003514 return ID;
3515 }
3516 }
David Blaikie7530c032012-01-17 06:56:22 +00003517
3518 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003519}
3520
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003521/// \brief \arg SLocMapI points at a chunk of a module that contains no
3522/// preprocessed entities or the entities it contains are not the ones we are
3523/// looking for. Find the next module that contains entities and return the ID
3524/// of the first entry.
3525PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
3526 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
3527 ++SLocMapI;
3528 for (GlobalSLocOffsetMapType::const_iterator
3529 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003530 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003531 if (M.NumPreprocessedEntities)
3532 return getGlobalPreprocessedEntityID(M, M.BasePreprocessedEntityID);
3533 }
3534
3535 return getTotalNumPreprocessedEntities();
3536}
3537
3538namespace {
3539
3540template <unsigned PPEntityOffset::*PPLoc>
3541struct PPEntityComp {
3542 const ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003543 ModuleFile &M;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003544
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003545 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003546
Benjamin Kramer88df1252011-09-21 06:42:26 +00003547 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
3548 SourceLocation LHS = getLoc(L);
3549 SourceLocation RHS = getLoc(R);
3550 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3551 }
3552
3553 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003554 SourceLocation LHS = getLoc(L);
3555 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3556 }
3557
Benjamin Kramer88df1252011-09-21 06:42:26 +00003558 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003559 SourceLocation RHS = getLoc(R);
3560 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3561 }
3562
3563 SourceLocation getLoc(const PPEntityOffset &PPE) const {
3564 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
3565 }
3566};
3567
3568}
3569
3570/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
3571PreprocessedEntityID
3572ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
3573 if (SourceMgr.isLocalSourceLocation(BLoc))
3574 return getTotalNumPreprocessedEntities();
3575
3576 GlobalSLocOffsetMapType::const_iterator
3577 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3578 BLoc.getOffset());
3579 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3580 "Corrupted global sloc offset map");
3581
3582 if (SLocMapI->second->NumPreprocessedEntities == 0)
3583 return findNextPreprocessedEntity(SLocMapI);
3584
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003585 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003586 typedef const PPEntityOffset *pp_iterator;
3587 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3588 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
Argyrios Kyrtzidis4cd06342011-09-22 21:17:02 +00003589
3590 size_t Count = M.NumPreprocessedEntities;
3591 size_t Half;
3592 pp_iterator First = pp_begin;
3593 pp_iterator PPI;
3594
3595 // Do a binary search manually instead of using std::lower_bound because
3596 // The end locations of entities may be unordered (when a macro expansion
3597 // is inside another macro argument), but for this case it is not important
3598 // whether we get the first macro expansion or its containing macro.
3599 while (Count > 0) {
3600 Half = Count/2;
3601 PPI = First;
3602 std::advance(PPI, Half);
3603 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
3604 BLoc)){
3605 First = PPI;
3606 ++First;
3607 Count = Count - Half - 1;
3608 } else
3609 Count = Half;
3610 }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003611
3612 if (PPI == pp_end)
3613 return findNextPreprocessedEntity(SLocMapI);
3614
3615 return getGlobalPreprocessedEntityID(M,
3616 M.BasePreprocessedEntityID + (PPI - pp_begin));
3617}
3618
3619/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
3620PreprocessedEntityID
3621ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
3622 if (SourceMgr.isLocalSourceLocation(ELoc))
3623 return getTotalNumPreprocessedEntities();
3624
3625 GlobalSLocOffsetMapType::const_iterator
3626 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3627 ELoc.getOffset());
3628 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3629 "Corrupted global sloc offset map");
3630
3631 if (SLocMapI->second->NumPreprocessedEntities == 0)
3632 return findNextPreprocessedEntity(SLocMapI);
3633
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003634 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003635 typedef const PPEntityOffset *pp_iterator;
3636 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3637 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
3638 pp_iterator PPI =
3639 std::upper_bound(pp_begin, pp_end, ELoc,
3640 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
3641
3642 if (PPI == pp_end)
3643 return findNextPreprocessedEntity(SLocMapI);
3644
3645 return getGlobalPreprocessedEntityID(M,
3646 M.BasePreprocessedEntityID + (PPI - pp_begin));
3647}
3648
3649/// \brief Returns a pair of [Begin, End) indices of preallocated
3650/// preprocessed entities that \arg Range encompasses.
3651std::pair<unsigned, unsigned>
3652 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
3653 if (Range.isInvalid())
3654 return std::make_pair(0,0);
3655 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
3656
3657 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
3658 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
3659 return std::make_pair(BeginID, EndID);
3660}
3661
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003662/// \brief Optionally returns true or false if the preallocated preprocessed
3663/// entity with index \arg Index came from file \arg FID.
3664llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
3665 FileID FID) {
3666 if (FID.isInvalid())
3667 return false;
3668
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003669 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3670 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003671 unsigned LocalIndex = PPInfo.second;
3672 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
3673
3674 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
3675 if (Loc.isInvalid())
3676 return false;
3677
3678 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
3679 return true;
3680 else
3681 return false;
3682}
3683
Douglas Gregord10a3812011-08-25 18:14:34 +00003684namespace {
3685 /// \brief Visitor used to search for information about a header file.
3686 class HeaderFileInfoVisitor {
3687 ASTReader &Reader;
3688 const FileEntry *FE;
3689
3690 llvm::Optional<HeaderFileInfo> HFI;
3691
3692 public:
3693 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
3694 : Reader(Reader), FE(FE) { }
3695
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003696 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003697 HeaderFileInfoVisitor *This
3698 = static_cast<HeaderFileInfoVisitor *>(UserData);
3699
3700 HeaderFileInfoTrait Trait(This->Reader, M,
3701 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
3702 M.HeaderFileFrameworkStrings,
3703 This->FE->getName());
3704
3705 HeaderFileInfoLookupTable *Table
3706 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
3707 if (!Table)
3708 return false;
3709
3710 // Look in the on-disk hash table for an entry for this file name.
3711 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
3712 &Trait);
3713 if (Pos == Table->end())
3714 return false;
3715
3716 This->HFI = *Pos;
3717 return true;
3718 }
3719
3720 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
3721 };
3722}
3723
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003724HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003725 HeaderFileInfoVisitor Visitor(*this, FE);
3726 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
3727 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003728 if (Listener)
Douglas Gregord10a3812011-08-25 18:14:34 +00003729 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
3730 return *HFI;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003731 }
3732
3733 return HeaderFileInfo();
3734}
3735
David Blaikied6471f72011-09-25 23:23:43 +00003736void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00003737 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003738 ModuleFile &F = *(*I);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003739 unsigned Idx = 0;
3740 while (Idx < F.PragmaDiagMappings.size()) {
3741 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00003742 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
3743 Diag.DiagStatePoints.push_back(
3744 DiagnosticsEngine::DiagStatePoint(&Diag.DiagStates.back(),
3745 FullSourceLoc(Loc, SourceMgr)));
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003746 while (1) {
3747 assert(Idx < F.PragmaDiagMappings.size() &&
3748 "Invalid data, didn't find '-1' marking end of diag/map pairs");
3749 if (Idx >= F.PragmaDiagMappings.size()) {
3750 break; // Something is messed up but at least avoid infinite loop in
3751 // release build.
3752 }
3753 unsigned DiagID = F.PragmaDiagMappings[Idx++];
3754 if (DiagID == (unsigned)-1) {
3755 break; // no more diag/map pairs for this location.
3756 }
3757 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00003758 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
3759 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003760 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003761 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00003762 }
3763}
3764
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003765/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003766ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregora119da02011-08-02 16:26:37 +00003767 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turnere9b76c12011-07-20 21:31:32 +00003768 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003769 ModuleFile *M = I->second;
Douglas Gregore3605012011-08-02 18:32:54 +00003770 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003771}
3772
3773/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00003774///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003775/// The index is the type ID, shifted and minus the number of predefs. This
3776/// routine actually reads the record corresponding to the type at the given
3777/// location. It is a helper routine for GetType, which deals with reading type
3778/// IDs.
Douglas Gregor393f2492011-07-22 00:38:23 +00003779QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003780 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00003781 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00003782
Douglas Gregor0b748912009-04-14 21:18:50 +00003783 // Keep track of where we are in the stream, then jump back there
3784 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003785 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00003786
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003787 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00003788
Douglas Gregord89275b2009-07-06 18:54:52 +00003789 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003790 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00003791
Douglas Gregor393f2492011-07-22 00:38:23 +00003792 unsigned Idx = 0;
Sebastian Redlc3632732010-10-05 15:59:54 +00003793 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003794 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003795 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003796 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3797 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003798 if (Record.size() != 2) {
3799 Error("Incorrect encoding of extended qualifier type");
3800 return QualType();
3801 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003802 QualType Base = readType(*Loc.F, Record, Idx);
3803 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
Douglas Gregor35942772011-09-09 21:34:22 +00003804 return Context.getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00003805 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003806
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003807 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003808 if (Record.size() != 1) {
3809 Error("Incorrect encoding of complex type");
3810 return QualType();
3811 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003812 QualType ElemType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003813 return Context.getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003814 }
3815
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003816 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003817 if (Record.size() != 1) {
3818 Error("Incorrect encoding of pointer 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.getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003823 }
3824
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003825 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003826 if (Record.size() != 1) {
3827 Error("Incorrect encoding of block pointer 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.getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003832 }
3833
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003834 case TYPE_LVALUE_REFERENCE: {
Richard Smithdf1550f2011-04-12 10:38:03 +00003835 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003836 Error("Incorrect encoding of lvalue reference type");
3837 return QualType();
3838 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003839 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003840 return Context.getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003841 }
3842
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003843 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003844 if (Record.size() != 1) {
3845 Error("Incorrect encoding of rvalue reference type");
3846 return QualType();
3847 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003848 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003849 return Context.getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003850 }
3851
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003852 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00003853 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003854 Error("Incorrect encoding of member pointer type");
3855 return QualType();
3856 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003857 QualType PointeeType = readType(*Loc.F, Record, Idx);
3858 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00003859 if (PointeeType.isNull() || ClassType.isNull())
3860 return QualType();
3861
Douglas Gregor35942772011-09-09 21:34:22 +00003862 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003863 }
3864
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003865 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003866 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003867 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3868 unsigned IndexTypeQuals = Record[2];
3869 unsigned Idx = 3;
3870 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003871 return Context.getConstantArrayType(ElementType, Size,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003872 ASM, IndexTypeQuals);
3873 }
3874
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003875 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003876 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003877 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3878 unsigned IndexTypeQuals = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003879 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003880 }
3881
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003882 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003883 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00003884 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3885 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00003886 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3887 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
Douglas Gregor35942772011-09-09 21:34:22 +00003888 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003889 ASM, IndexTypeQuals,
3890 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003891 }
3892
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003893 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003894 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003895 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003896 return QualType();
3897 }
3898
Douglas Gregor393f2492011-07-22 00:38:23 +00003899 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003900 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00003901 unsigned VecKind = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003902 return Context.getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00003903 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003904 }
3905
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003906 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003907 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003908 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003909 return QualType();
3910 }
3911
Douglas Gregor393f2492011-07-22 00:38:23 +00003912 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003913 unsigned NumElements = Record[1];
Douglas Gregor35942772011-09-09 21:34:22 +00003914 return Context.getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003915 }
3916
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003917 case TYPE_FUNCTION_NO_PROTO: {
John McCallf85e1932011-06-15 23:02:42 +00003918 if (Record.size() != 6) {
Douglas Gregora02b1472009-04-28 21:53:25 +00003919 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003920 return QualType();
3921 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003922 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCallf85e1932011-06-15 23:02:42 +00003923 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
3924 (CallingConv)Record[4], Record[5]);
Douglas Gregor35942772011-09-09 21:34:22 +00003925 return Context.getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003926 }
3927
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003928 case TYPE_FUNCTION_PROTO: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003929 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalle23cf432010-12-14 08:05:40 +00003930
3931 FunctionProtoType::ExtProtoInfo EPI;
3932 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmana49218e2011-04-09 08:18:08 +00003933 /*hasregparm*/ Record[2],
3934 /*regparm*/ Record[3],
John McCallf85e1932011-06-15 23:02:42 +00003935 static_cast<CallingConv>(Record[4]),
3936 /*produces*/ Record[5]);
John McCalle23cf432010-12-14 08:05:40 +00003937
John McCallf85e1932011-06-15 23:02:42 +00003938 unsigned Idx = 6;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003939 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003940 SmallVector<QualType, 16> ParamTypes;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003941 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00003942 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalle23cf432010-12-14 08:05:40 +00003943
3944 EPI.Variadic = Record[Idx++];
Richard Smitheefb3d52012-02-10 09:58:53 +00003945 EPI.HasTrailingReturn = Record[Idx++];
John McCalle23cf432010-12-14 08:05:40 +00003946 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00003947 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redl60618fa2011-03-12 11:50:43 +00003948 ExceptionSpecificationType EST =
3949 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3950 EPI.ExceptionSpecType = EST;
Douglas Gregorb0d06e22012-04-04 00:34:49 +00003951 SmallVector<QualType, 2> Exceptions;
Sebastian Redl60618fa2011-03-12 11:50:43 +00003952 if (EST == EST_Dynamic) {
3953 EPI.NumExceptions = Record[Idx++];
Sebastian Redl60618fa2011-03-12 11:50:43 +00003954 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00003955 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redl60618fa2011-03-12 11:50:43 +00003956 EPI.Exceptions = Exceptions.data();
3957 } else if (EST == EST_ComputedNoexcept) {
3958 EPI.NoexceptExpr = ReadExpr(*Loc.F);
Richard Smith7bb698a2012-04-21 17:47:47 +00003959 } else if (EST == EST_Uninstantiated) {
3960 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
3961 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Richard Smithb9d0b762012-07-27 04:22:15 +00003962 } else if (EST == EST_Unevaluated) {
3963 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Sebastian Redl60618fa2011-03-12 11:50:43 +00003964 }
Douglas Gregor35942772011-09-09 21:34:22 +00003965 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00003966 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003967 }
3968
Douglas Gregor409448c2011-07-21 22:35:25 +00003969 case TYPE_UNRESOLVED_USING: {
3970 unsigned Idx = 0;
Douglas Gregor35942772011-09-09 21:34:22 +00003971 return Context.getTypeDeclType(
Douglas Gregor409448c2011-07-21 22:35:25 +00003972 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
3973 }
3974
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003975 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003976 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003977 Error("incorrect encoding of typedef type");
3978 return QualType();
3979 }
Douglas Gregor409448c2011-07-21 22:35:25 +00003980 unsigned Idx = 0;
3981 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003982 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003983 if (!Canonical.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00003984 Canonical = Context.getCanonicalType(Canonical);
3985 return Context.getTypedefType(Decl, Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003986 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003987
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003988 case TYPE_TYPEOF_EXPR:
Douglas Gregor35942772011-09-09 21:34:22 +00003989 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003990
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003991 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003992 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003993 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003994 return QualType();
3995 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003996 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003997 return Context.getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003998 }
Mike Stump1eb44332009-09-09 15:08:12 +00003999
Douglas Gregorf8af9822012-02-12 18:42:33 +00004000 case TYPE_DECLTYPE: {
4001 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4002 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4003 }
Anders Carlsson395b4752009-06-24 19:06:50 +00004004
Sean Huntca63c202011-05-24 22:41:36 +00004005 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004006 QualType BaseType = readType(*Loc.F, Record, Idx);
4007 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Sean Huntca63c202011-05-24 22:41:36 +00004008 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004009 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
Sean Huntca63c202011-05-24 22:41:36 +00004010 }
4011
Richard Smith34b41d92011-02-20 03:19:35 +00004012 case TYPE_AUTO:
Douglas Gregor35942772011-09-09 21:34:22 +00004013 return Context.getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith34b41d92011-02-20 03:19:35 +00004014
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004015 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004016 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004017 Error("incorrect encoding of record type");
4018 return QualType();
4019 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004020 unsigned Idx = 0;
4021 bool IsDependent = Record[Idx++];
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004022 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4023 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4024 QualType T = Context.getRecordType(RD);
John McCallf4c73712011-01-19 06:33:43 +00004025 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004026 return T;
4027 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004028
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004029 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004030 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004031 Error("incorrect encoding of enum type");
4032 return QualType();
4033 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004034 unsigned Idx = 0;
4035 bool IsDependent = Record[Idx++];
4036 QualType T
Douglas Gregor35942772011-09-09 21:34:22 +00004037 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00004038 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004039 return T;
4040 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004041
John McCall9d156a72011-01-06 01:58:22 +00004042 case TYPE_ATTRIBUTED: {
4043 if (Record.size() != 3) {
4044 Error("incorrect encoding of attributed type");
4045 return QualType();
4046 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004047 QualType modifiedType = readType(*Loc.F, Record, Idx);
4048 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall9d156a72011-01-06 01:58:22 +00004049 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
Douglas Gregor35942772011-09-09 21:34:22 +00004050 return Context.getAttributedType(kind, modifiedType, equivalentType);
John McCall9d156a72011-01-06 01:58:22 +00004051 }
4052
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004053 case TYPE_PAREN: {
4054 if (Record.size() != 1) {
4055 Error("incorrect encoding of paren type");
4056 return QualType();
4057 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004058 QualType InnerType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004059 return Context.getParenType(InnerType);
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004060 }
4061
Douglas Gregor7536dd52010-12-20 02:24:11 +00004062 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00004063 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00004064 Error("incorrect encoding of pack expansion type");
4065 return QualType();
4066 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004067 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004068 if (Pattern.isNull())
4069 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00004070 llvm::Optional<unsigned> NumExpansions;
4071 if (Record[1])
4072 NumExpansions = Record[1] - 1;
Douglas Gregor35942772011-09-09 21:34:22 +00004073 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004074 }
4075
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004076 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004077 unsigned Idx = 0;
4078 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004079 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004080 QualType NamedType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004081 return Context.getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00004082 }
4083
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004084 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004085 unsigned Idx = 0;
Douglas Gregor409448c2011-07-21 22:35:25 +00004086 ObjCInterfaceDecl *ItfD
4087 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004088 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
John McCallc12c5bb2010-05-15 11:32:37 +00004089 }
4090
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004091 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00004092 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004093 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004094 unsigned NumProtos = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004095 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004096 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor409448c2011-07-21 22:35:25 +00004097 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004098 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004099 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004100
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004101 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004102 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004103 QualType Pointee = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004104 return Context.getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004105 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00004106
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004107 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00004108 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004109 QualType Parm = readType(*Loc.F, Record, Idx);
4110 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCall49a832b2009-10-18 09:09:24 +00004111 return
Douglas Gregor35942772011-09-09 21:34:22 +00004112 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
John McCall49a832b2009-10-18 09:09:24 +00004113 Replacement);
4114 }
John McCall3cb0ebd2010-03-10 03:28:59 +00004115
Douglas Gregorc3069d62011-01-14 02:55:32 +00004116 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4117 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004118 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorc3069d62011-01-14 02:55:32 +00004119 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004120 return Context.getSubstTemplateTypeParmPackType(
Douglas Gregorc3069d62011-01-14 02:55:32 +00004121 cast<TemplateTypeParmType>(Parm),
4122 ArgPack);
4123 }
4124
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004125 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor409448c2011-07-21 22:35:25 +00004126 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004127 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004128 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004129 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004130 return
Douglas Gregor35942772011-09-09 21:34:22 +00004131 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00004132 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004133
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004134 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004135 unsigned Idx = 0;
4136 unsigned Depth = Record[Idx++];
4137 unsigned Index = Record[Idx++];
4138 bool Pack = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004139 TemplateTypeParmDecl *D
4140 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004141 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004142 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004143
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004144 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004145 unsigned Idx = 0;
4146 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004147 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004148 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004149 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004150 if (!Canon.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004151 Canon = Context.getCanonicalType(Canon);
4152 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004153 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004154
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004155 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004156 unsigned Idx = 0;
4157 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004158 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004159 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004160 unsigned NumArgs = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004161 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004162 Args.reserve(NumArgs);
4163 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004164 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004165 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004166 Args.size(), Args.data());
4167 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004168
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004169 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004170 unsigned Idx = 0;
4171
4172 // ArrayType
Douglas Gregor393f2492011-07-22 00:38:23 +00004173 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004174 ArrayType::ArraySizeModifier ASM
4175 = (ArrayType::ArraySizeModifier)Record[Idx++];
4176 unsigned IndexTypeQuals = Record[Idx++];
4177
4178 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00004179 Expr *NumElts = ReadExpr(*Loc.F);
4180 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004181
Douglas Gregor35942772011-09-09 21:34:22 +00004182 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004183 IndexTypeQuals, Brackets);
4184 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004185
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004186 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004187 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004188 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004189 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner5f9e2722011-07-23 10:55:15 +00004190 SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00004191 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004192 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004193 QualType T;
Richard Smith3e4c6c42011-05-05 21:57:07 +00004194 if (Underlying.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004195 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004196 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004197 else
Douglas Gregor35942772011-09-09 21:34:22 +00004198 T = Context.getTemplateSpecializationType(Name, Args.data(),
Richard Smith3e4c6c42011-05-05 21:57:07 +00004199 Args.size(), Underlying);
John McCallf4c73712011-01-19 06:33:43 +00004200 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004201 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004202 }
Eli Friedmanb001de72011-10-06 23:00:33 +00004203
4204 case TYPE_ATOMIC: {
4205 if (Record.size() != 1) {
4206 Error("Incorrect encoding of atomic type");
4207 return QualType();
4208 }
4209 QualType ValueType = readType(*Loc.F, Record, Idx);
4210 return Context.getAtomicType(ValueType);
4211 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004212 }
David Blaikie7530c032012-01-17 06:56:22 +00004213 llvm_unreachable("Invalid TypeCode!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00004214}
4215
Sebastian Redlc3632732010-10-05 15:59:54 +00004216class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004217 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004218 ModuleFile &F;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004219 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00004220 unsigned &Idx;
4221
Sebastian Redlc3632732010-10-05 15:59:54 +00004222 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4223 unsigned &I) {
4224 return Reader.ReadSourceLocation(F, R, I);
4225 }
4226
Douglas Gregor409448c2011-07-21 22:35:25 +00004227 template<typename T>
4228 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4229 return Reader.ReadDeclAs<T>(F, Record, Idx);
4230 }
4231
John McCalla1ee0c52009-10-16 21:56:05 +00004232public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004233 TypeLocReader(ASTReader &Reader, ModuleFile &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004234 const ASTReader::RecordData &Record, unsigned &Idx)
Benjamin Kramerfacde172012-06-06 17:32:50 +00004235 : Reader(Reader), F(F), Record(Record), Idx(Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00004236 { }
John McCalla1ee0c52009-10-16 21:56:05 +00004237
John McCall51bd8032009-10-18 01:05:36 +00004238 // We want compile-time assurance that we've enumerated all of
4239 // these, so unfortunately we have to declare them first, then
4240 // define them out-of-line.
4241#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00004242#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00004243 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004244#include "clang/AST/TypeLocNodes.def"
4245
John McCall51bd8032009-10-18 01:05:36 +00004246 void VisitFunctionTypeLoc(FunctionTypeLoc);
4247 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004248};
4249
John McCall51bd8032009-10-18 01:05:36 +00004250void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00004251 // nothing to do
4252}
John McCall51bd8032009-10-18 01:05:36 +00004253void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004254 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00004255 if (TL.needsExtraLocalData()) {
4256 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4257 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4258 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4259 TL.setModeAttr(Record[Idx++]);
4260 }
John McCalla1ee0c52009-10-16 21:56:05 +00004261}
John McCall51bd8032009-10-18 01:05:36 +00004262void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004263 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004264}
John McCall51bd8032009-10-18 01:05:36 +00004265void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004266 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004267}
John McCall51bd8032009-10-18 01:05:36 +00004268void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004269 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004270}
John McCall51bd8032009-10-18 01:05:36 +00004271void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004272 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004273}
John McCall51bd8032009-10-18 01:05:36 +00004274void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004275 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004276}
John McCall51bd8032009-10-18 01:05:36 +00004277void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004278 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00004279 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004280}
John McCall51bd8032009-10-18 01:05:36 +00004281void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004282 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4283 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004284 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00004285 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004286 else
John McCall51bd8032009-10-18 01:05:36 +00004287 TL.setSizeExpr(0);
4288}
4289void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4290 VisitArrayTypeLoc(TL);
4291}
4292void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4293 VisitArrayTypeLoc(TL);
4294}
4295void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4296 VisitArrayTypeLoc(TL);
4297}
4298void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4299 DependentSizedArrayTypeLoc TL) {
4300 VisitArrayTypeLoc(TL);
4301}
4302void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4303 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004304 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004305}
4306void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004307 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004308}
4309void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004310 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004311}
4312void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +00004313 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004314 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4315 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara796aa442011-03-12 11:17:06 +00004316 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004317 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor409448c2011-07-21 22:35:25 +00004318 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004319 }
4320}
4321void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4322 VisitFunctionTypeLoc(TL);
4323}
4324void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4325 VisitFunctionTypeLoc(TL);
4326}
John McCalled976492009-12-04 22:46:56 +00004327void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004328 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00004329}
John McCall51bd8032009-10-18 01:05:36 +00004330void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004331 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004332}
4333void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004334 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4335 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4336 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004337}
4338void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004339 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4340 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4341 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4342 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004343}
4344void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004345 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004346}
Sean Huntca63c202011-05-24 22:41:36 +00004347void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4348 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4349 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4350 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4351 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4352}
Richard Smith34b41d92011-02-20 03:19:35 +00004353void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4354 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4355}
John McCall51bd8032009-10-18 01:05:36 +00004356void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004357 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004358}
4359void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004360 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004361}
John McCall9d156a72011-01-06 01:58:22 +00004362void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4363 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4364 if (TL.hasAttrOperand()) {
4365 SourceRange range;
4366 range.setBegin(ReadSourceLocation(Record, Idx));
4367 range.setEnd(ReadSourceLocation(Record, Idx));
4368 TL.setAttrOperandParensRange(range);
4369 }
4370 if (TL.hasAttrExprOperand()) {
4371 if (Record[Idx++])
4372 TL.setAttrExprOperand(Reader.ReadExpr(F));
4373 else
4374 TL.setAttrExprOperand(0);
4375 } else if (TL.hasAttrEnumOperand())
4376 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4377}
John McCall51bd8032009-10-18 01:05:36 +00004378void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004379 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004380}
John McCall49a832b2009-10-18 09:09:24 +00004381void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4382 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004383 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00004384}
Douglas Gregorc3069d62011-01-14 02:55:32 +00004385void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
4386 SubstTemplateTypeParmPackTypeLoc TL) {
4387 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4388}
John McCall51bd8032009-10-18 01:05:36 +00004389void TypeLocReader::VisitTemplateSpecializationTypeLoc(
4390 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004391 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004392 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4393 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4394 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00004395 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
4396 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00004397 Reader.GetTemplateArgumentLocInfo(F,
4398 TL.getTypePtr()->getArg(i).getKind(),
4399 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004400}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004401void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
4402 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4403 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4404}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004405void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004406 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor9e876872011-03-01 18:12:44 +00004407 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004408}
John McCall3cb0ebd2010-03-10 03:28:59 +00004409void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004410 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00004411}
Douglas Gregor4714c122010-03-31 17:34:00 +00004412void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004413 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor2494dd02011-03-01 01:34:45 +00004414 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004415 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004416}
John McCall33500952010-06-11 00:33:02 +00004417void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
4418 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004419 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004420 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Abramo Bagnara66581d42012-02-06 22:45:07 +00004421 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004422 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004423 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4424 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004425 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
4426 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00004427 Reader.GetTemplateArgumentLocInfo(F,
4428 TL.getTypePtr()->getArg(I).getKind(),
4429 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004430}
Douglas Gregor7536dd52010-12-20 02:24:11 +00004431void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
4432 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
4433}
John McCall51bd8032009-10-18 01:05:36 +00004434void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004435 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00004436}
4437void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
4438 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004439 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4440 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004441 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00004442 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004443}
John McCall54e14c42009-10-22 22:37:11 +00004444void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004445 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00004446}
Eli Friedmanb001de72011-10-06 23:00:33 +00004447void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
4448 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4449 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4450 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4451}
John McCalla1ee0c52009-10-16 21:56:05 +00004452
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004453TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004454 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00004455 unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004456 QualType InfoTy = readType(F, Record, Idx);
John McCalla1ee0c52009-10-16 21:56:05 +00004457 if (InfoTy.isNull())
4458 return 0;
4459
Douglas Gregor35942772011-09-09 21:34:22 +00004460 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00004461 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00004462 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00004463 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00004464 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00004465}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004466
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004467QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00004468 unsigned FastQuals = ID & Qualifiers::FastMask;
4469 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004470
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004471 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004472 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004473 switch ((PredefinedTypeIDs)Index) {
4474 case PREDEF_TYPE_NULL_ID: return QualType();
Douglas Gregor35942772011-09-09 21:34:22 +00004475 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
4476 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004477
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004478 case PREDEF_TYPE_CHAR_U_ID:
4479 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00004480 // FIXME: Check that the signedness of CharTy is correct!
Douglas Gregor35942772011-09-09 21:34:22 +00004481 T = Context.CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004482 break;
4483
Douglas Gregor35942772011-09-09 21:34:22 +00004484 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
4485 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
4486 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
4487 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
4488 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
4489 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
4490 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
4491 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
4492 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
4493 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
4494 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
4495 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
4496 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00004497 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004498 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
4499 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
4500 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
4501 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
4502 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
John McCall3c3b7f92011-10-25 17:37:35 +00004503 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004504 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
4505 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
4506 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
4507 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
4508 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
4509 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
4510 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
4511 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
4512 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004513
4514 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
Douglas Gregor35942772011-09-09 21:34:22 +00004515 T = Context.getAutoRRefDeductType();
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004516 break;
John McCall0ddaeb92011-10-17 18:09:15 +00004517
4518 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
4519 T = Context.ARCUnbridgedCastTy;
4520 break;
4521
Meador Ingefb40e3f2012-07-01 15:57:25 +00004522 case PREDEF_TYPE_VA_LIST_TAG:
4523 T = Context.getVaListTagType();
4524 break;
Eli Friedmana6c66ce2012-08-31 00:14:07 +00004525
4526 case PREDEF_TYPE_BUILTIN_FN:
4527 T = Context.BuiltinFnTy;
4528 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004529 }
4530
4531 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00004532 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004533 }
4534
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004535 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004536 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00004537 if (TypesLoaded[Index].isNull()) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004538 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00004539 if (TypesLoaded[Index].isNull())
4540 return QualType();
4541
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004542 TypesLoaded[Index]->setFromAST();
Sebastian Redl30c514c2010-07-14 23:45:08 +00004543 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004544 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00004545 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00004546 }
Mike Stump1eb44332009-09-09 15:08:12 +00004547
John McCall0953e762009-09-24 19:53:00 +00004548 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004549}
4550
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004551QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004552 return GetType(getGlobalTypeID(F, LocalID));
4553}
4554
4555serialization::TypeID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004556ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
Douglas Gregora119da02011-08-02 16:26:37 +00004557 unsigned FastQuals = LocalID & Qualifiers::FastMask;
4558 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
4559
4560 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
4561 return LocalID;
4562
4563 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4564 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
4565 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
4566
4567 unsigned GlobalIndex = LocalIndex + I->second;
4568 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
4569}
4570
John McCall833ca992009-10-29 08:12:44 +00004571TemplateArgumentLocInfo
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004572ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00004573 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00004574 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004575 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00004576 switch (Kind) {
4577 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004578 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00004579 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00004580 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00004581 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004582 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4583 Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00004584 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004585 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregora7fc9012011-01-05 18:58:31 +00004586 SourceLocation());
4587 }
4588 case TemplateArgument::TemplateExpansion: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004589 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4590 Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004591 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004592 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004593 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregorba68eca2011-01-05 17:40:24 +00004594 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00004595 }
John McCall833ca992009-10-29 08:12:44 +00004596 case TemplateArgument::Null:
4597 case TemplateArgument::Integral:
4598 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004599 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004600 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004601 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004602 return TemplateArgumentLocInfo();
4603 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00004604 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00004605}
4606
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004607TemplateArgumentLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004608ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004609 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004610 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004611
4612 if (Arg.getKind() == TemplateArgument::Expression) {
4613 if (Record[Index++]) // bool InfoHasSameExpr.
4614 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4615 }
Sebastian Redlc3632732010-10-05 15:59:54 +00004616 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004617 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004618}
4619
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004620Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00004621 return GetDecl(ID);
4622}
4623
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004624uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
Douglas Gregore92b8a12011-08-04 00:01:48 +00004625 unsigned &Idx){
4626 if (Idx >= Record.size())
Douglas Gregor7c789c12010-10-29 22:39:52 +00004627 return 0;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004628
Douglas Gregore92b8a12011-08-04 00:01:48 +00004629 unsigned LocalID = Record[Idx++];
4630 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004631}
4632
4633CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004634 RecordLocation Loc = getLocalBitOffset(Offset);
4635 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004636 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004637 Cursor.JumpToBit(Loc.Offset);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004638 ReadingKindTracker ReadingKind(Read_Decl, *this);
4639 RecordData Record;
4640 unsigned Code = Cursor.ReadCode();
4641 unsigned RecCode = Cursor.ReadRecord(Code, Record);
4642 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4643 Error("Malformed AST file: missing C++ base specifiers");
4644 return 0;
4645 }
4646
4647 unsigned Idx = 0;
4648 unsigned NumBases = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00004649 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004650 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4651 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004652 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004653 return Bases;
4654}
4655
Douglas Gregor409448c2011-07-21 22:35:25 +00004656serialization::DeclID
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00004657ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004658 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregor496c7092011-08-03 15:48:04 +00004659 return LocalID;
4660
4661 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004662 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregor496c7092011-08-03 15:48:04 +00004663 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
4664
4665 return LocalID + I->second;
Douglas Gregor409448c2011-07-21 22:35:25 +00004666}
4667
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004668bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004669 ModuleFile &M) const {
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004670 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
4671 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4672 return &M == I->second;
4673}
4674
Douglas Gregorcff9f262012-01-27 01:47:08 +00004675ModuleFile *ASTReader::getOwningModuleFile(Decl *D) {
4676 if (!D->isFromASTFile())
4677 return 0;
4678 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
4679 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4680 return I->second;
4681}
4682
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004683SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
4684 if (ID < NUM_PREDEF_DECL_IDS)
4685 return SourceLocation();
4686
4687 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4688
4689 if (Index > DeclsLoaded.size()) {
4690 Error("declaration ID out-of-range for AST file");
4691 return SourceLocation();
4692 }
4693
4694 if (Decl *D = DeclsLoaded[Index])
4695 return D->getLocation();
4696
4697 unsigned RawLocation = 0;
4698 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
4699 return ReadSourceLocation(*Rec.F, RawLocation);
4700}
4701
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004702Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004703 if (ID < NUM_PREDEF_DECL_IDS) {
4704 switch ((PredefinedDeclIDs)ID) {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004705 case PREDEF_DECL_NULL_ID:
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004706 return 0;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004707
4708 case PREDEF_DECL_TRANSLATION_UNIT_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004709 return Context.getTranslationUnitDecl();
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00004710
4711 case PREDEF_DECL_OBJC_ID_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004712 return Context.getObjCIdDecl();
Douglas Gregor79d67262011-08-12 05:59:41 +00004713
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004714 case PREDEF_DECL_OBJC_SEL_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004715 return Context.getObjCSelDecl();
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004716
Douglas Gregor79d67262011-08-12 05:59:41 +00004717 case PREDEF_DECL_OBJC_CLASS_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004718 return Context.getObjCClassDecl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004719
Douglas Gregora6ea10e2012-01-17 18:09:05 +00004720 case PREDEF_DECL_OBJC_PROTOCOL_ID:
4721 return Context.getObjCProtocolDecl();
4722
Douglas Gregor772eeae2011-08-12 06:49:56 +00004723 case PREDEF_DECL_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004724 return Context.getInt128Decl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004725
4726 case PREDEF_DECL_UNSIGNED_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004727 return Context.getUInt128Decl();
Douglas Gregore97179c2011-09-08 01:46:34 +00004728
4729 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004730 return Context.getObjCInstanceTypeDecl();
Meador Ingec5613b22012-06-16 03:34:49 +00004731
4732 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
4733 return Context.getBuiltinVaListDecl();
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004734 }
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004735 }
4736
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004737 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4738
Richard Smith2fbf3732011-12-20 04:39:57 +00004739 if (Index >= DeclsLoaded.size()) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004740 assert(0 && "declaration ID out-of-range for AST file");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004741 Error("declaration ID out-of-range for AST file");
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004742 return 0;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004743 }
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004744
Douglas Gregorfd002a72011-12-16 22:37:11 +00004745 if (!DeclsLoaded[Index]) {
Douglas Gregor496c7092011-08-03 15:48:04 +00004746 ReadDeclRecord(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00004747 if (DeserializationListener)
4748 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
4749 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004750
4751 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004752}
4753
Douglas Gregora1be2782011-12-17 23:38:30 +00004754DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
4755 DeclID GlobalID) {
4756 if (GlobalID < NUM_PREDEF_DECL_IDS)
4757 return GlobalID;
4758
4759 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
4760 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4761 ModuleFile *Owner = I->second;
4762
4763 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
4764 = M.GlobalToLocalDeclIDs.find(Owner);
4765 if (Pos == M.GlobalToLocalDeclIDs.end())
4766 return 0;
4767
4768 return GlobalID - Owner->BaseDeclID + Pos->second;
4769}
4770
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004771serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00004772 const RecordData &Record,
4773 unsigned &Idx) {
4774 if (Idx >= Record.size()) {
4775 Error("Corrupted AST file");
4776 return 0;
4777 }
4778
4779 return getGlobalDeclID(F, Record[Idx++]);
4780}
4781
Chris Lattner887e2b32009-04-27 05:46:25 +00004782/// \brief Resolve the offset of a statement into a statement.
4783///
4784/// This operation will read a new statement from the external
4785/// source each time it is called, and is meant to be used via a
4786/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004787Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004788 // Switch case IDs are per Decl.
4789 ClearSwitchCaseIDs();
4790
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004791 // Offset here is a global offset across the entire chain.
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004792 RecordLocation Loc = getLocalBitOffset(Offset);
4793 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
4794 return ReadStmtFromStream(*Loc.F);
Douglas Gregor250fc9c2009-04-18 00:07:54 +00004795}
4796
Douglas Gregor851c75a2011-08-24 21:27:34 +00004797namespace {
4798 class FindExternalLexicalDeclsVisitor {
4799 ASTReader &Reader;
4800 const DeclContext *DC;
4801 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004802
Douglas Gregor851c75a2011-08-24 21:27:34 +00004803 SmallVectorImpl<Decl*> &Decls;
4804 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
4805
4806 public:
4807 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
4808 bool (*isKindWeWant)(Decl::Kind),
4809 SmallVectorImpl<Decl*> &Decls)
4810 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
4811 {
4812 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
4813 PredefsVisited[I] = false;
4814 }
4815
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004816 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
Douglas Gregor851c75a2011-08-24 21:27:34 +00004817 if (Preorder)
4818 return false;
4819
4820 FindExternalLexicalDeclsVisitor *This
4821 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
4822
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004823 ModuleFile::DeclContextInfosMap::iterator Info
Douglas Gregor851c75a2011-08-24 21:27:34 +00004824 = M.DeclContextInfos.find(This->DC);
4825 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
4826 return false;
4827
4828 // Load all of the declaration IDs
4829 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
4830 *IDE = ID + Info->second.NumLexicalDecls;
4831 ID != IDE; ++ID) {
4832 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
4833 continue;
4834
4835 // Don't add predefined declarations to the lexical context more
4836 // than once.
4837 if (ID->second < NUM_PREDEF_DECL_IDS) {
4838 if (This->PredefsVisited[ID->second])
4839 continue;
4840
4841 This->PredefsVisited[ID->second] = true;
4842 }
4843
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004844 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
4845 if (!This->DC->isDeclInLexicalTraversal(D))
4846 This->Decls.push_back(D);
4847 }
Douglas Gregor851c75a2011-08-24 21:27:34 +00004848 }
4849
4850 return false;
4851 }
4852 };
4853}
4854
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004855ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00004856 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner5f9e2722011-07-23 10:55:15 +00004857 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00004858 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor851c75a2011-08-24 21:27:34 +00004859 // least. Walk all of the modules in the order they were loaded.
4860 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
4861 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00004862 ++NumLexicalDeclContextsRead;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004863 return ELR_Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004864}
4865
Douglas Gregor0d95f772011-08-24 19:03:07 +00004866namespace {
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004867
4868class DeclIDComp {
4869 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004870 ModuleFile &Mod;
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004871
4872public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004873 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004874
4875 bool operator()(LocalDeclID L, LocalDeclID R) const {
4876 SourceLocation LHS = getLocation(L);
4877 SourceLocation RHS = getLocation(R);
4878 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4879 }
4880
4881 bool operator()(SourceLocation LHS, LocalDeclID R) const {
4882 SourceLocation RHS = getLocation(R);
4883 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4884 }
4885
4886 bool operator()(LocalDeclID L, SourceLocation RHS) const {
4887 SourceLocation LHS = getLocation(L);
4888 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4889 }
4890
4891 SourceLocation getLocation(LocalDeclID ID) const {
4892 return Reader.getSourceManager().getFileLoc(
4893 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
4894 }
4895};
4896
4897}
4898
4899void ASTReader::FindFileRegionDecls(FileID File,
4900 unsigned Offset, unsigned Length,
4901 SmallVectorImpl<Decl *> &Decls) {
4902 SourceManager &SM = getSourceManager();
4903
4904 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
4905 if (I == FileDeclIDs.end())
4906 return;
4907
4908 FileDeclsInfo &DInfo = I->second;
4909 if (DInfo.Decls.empty())
4910 return;
4911
4912 SourceLocation
4913 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
4914 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
4915
4916 DeclIDComp DIDComp(*this, *DInfo.Mod);
4917 ArrayRef<serialization::LocalDeclID>::iterator
4918 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
4919 BeginLoc, DIDComp);
4920 if (BeginIt != DInfo.Decls.begin())
4921 --BeginIt;
4922
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00004923 // If we are pointing at a top-level decl inside an objc container, we need
4924 // to backtrack until we find it otherwise we will fail to report that the
4925 // region overlaps with an objc container.
4926 while (BeginIt != DInfo.Decls.begin() &&
4927 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
4928 ->isTopLevelDeclInObjCContainer())
4929 --BeginIt;
4930
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004931 ArrayRef<serialization::LocalDeclID>::iterator
4932 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
4933 EndLoc, DIDComp);
4934 if (EndIt != DInfo.Decls.end())
4935 ++EndIt;
4936
4937 for (ArrayRef<serialization::LocalDeclID>::iterator
4938 DIt = BeginIt; DIt != EndIt; ++DIt)
4939 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
4940}
4941
4942namespace {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004943 /// \brief ModuleFile visitor used to perform name lookup into a
Douglas Gregor0d95f772011-08-24 19:03:07 +00004944 /// declaration context.
4945 class DeclContextNameLookupVisitor {
4946 ASTReader &Reader;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004947 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Douglas Gregor0d95f772011-08-24 19:03:07 +00004948 DeclarationName Name;
4949 SmallVectorImpl<NamedDecl *> &Decls;
4950
4951 public:
4952 DeclContextNameLookupVisitor(ASTReader &Reader,
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004953 SmallVectorImpl<const DeclContext *> &Contexts,
4954 DeclarationName Name,
Douglas Gregor0d95f772011-08-24 19:03:07 +00004955 SmallVectorImpl<NamedDecl *> &Decls)
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004956 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
Douglas Gregor0d95f772011-08-24 19:03:07 +00004957
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004958 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00004959 DeclContextNameLookupVisitor *This
4960 = static_cast<DeclContextNameLookupVisitor *>(UserData);
4961
4962 // Check whether we have any visible declaration information for
4963 // this context in this module.
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004964 ModuleFile::DeclContextInfosMap::iterator Info;
4965 bool FoundInfo = false;
4966 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
4967 Info = M.DeclContextInfos.find(This->Contexts[I]);
4968 if (Info != M.DeclContextInfos.end() &&
4969 Info->second.NameLookupTableData) {
4970 FoundInfo = true;
4971 break;
4972 }
4973 }
Douglas Gregor0d95f772011-08-24 19:03:07 +00004974
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004975 if (!FoundInfo)
4976 return false;
4977
Douglas Gregor0d95f772011-08-24 19:03:07 +00004978 // Look for this name within this module.
4979 ASTDeclContextNameLookupTable *LookupTable =
Benjamin Kramerb1758c62012-04-15 12:36:49 +00004980 Info->second.NameLookupTableData;
Douglas Gregor0d95f772011-08-24 19:03:07 +00004981 ASTDeclContextNameLookupTable::iterator Pos
4982 = LookupTable->find(This->Name);
4983 if (Pos == LookupTable->end())
4984 return false;
4985
4986 bool FoundAnything = false;
4987 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
4988 for (; Data.first != Data.second; ++Data.first) {
4989 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
4990 if (!ND)
4991 continue;
4992
4993 if (ND->getDeclName() != This->Name) {
Axel Naumann3dd82f72012-10-01 09:51:27 +00004994 // A name might be null because the decl's redeclarable part is
4995 // currently read before reading its name. The lookup is triggered by
4996 // building that decl (likely indirectly), and so it is later in the
4997 // sense of "already existing" and can be ignored here.
Douglas Gregor0d95f772011-08-24 19:03:07 +00004998 continue;
4999 }
5000
5001 // Record this declaration.
5002 FoundAnything = true;
5003 This->Decls.push_back(ND);
5004 }
5005
5006 return FoundAnything;
5007 }
5008 };
5009}
5010
John McCall76bd1f32010-06-01 09:23:16 +00005011DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005012ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00005013 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00005014 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00005015 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005016 if (!Name)
5017 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
5018 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00005019
Chris Lattner5f9e2722011-07-23 10:55:15 +00005020 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005021
5022 // Compute the declaration contexts we need to look into. Multiple such
5023 // declaration contexts occur when two declaration contexts from disjoint
5024 // modules get merged, e.g., when two namespaces with the same name are
5025 // independently defined in separate modules.
5026 SmallVector<const DeclContext *, 2> Contexts;
5027 Contexts.push_back(DC);
5028
5029 if (DC->isNamespace()) {
5030 MergedDeclsMap::iterator Merged
5031 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5032 if (Merged != MergedDecls.end()) {
5033 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5034 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5035 }
5036 }
5037
5038 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor0d95f772011-08-24 19:03:07 +00005039 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00005040 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005041 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00005042 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00005043}
5044
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005045namespace {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005046 /// \brief ModuleFile visitor used to retrieve all visible names in a
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005047 /// declaration context.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005048 class DeclContextAllNamesVisitor {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005049 ASTReader &Reader;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005050 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005051 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005052
5053 public:
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005054 DeclContextAllNamesVisitor(ASTReader &Reader,
5055 SmallVectorImpl<const DeclContext *> &Contexts,
5056 llvm::DenseMap<DeclarationName,
5057 SmallVector<NamedDecl *, 8> > &Decls)
5058 : Reader(Reader), Contexts(Contexts), Decls(Decls) { }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005059
5060 static bool visit(ModuleFile &M, void *UserData) {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005061 DeclContextAllNamesVisitor *This
5062 = static_cast<DeclContextAllNamesVisitor *>(UserData);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005063
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005064 // Check whether we have any visible declaration information for
5065 // this context in this module.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005066 ModuleFile::DeclContextInfosMap::iterator Info;
5067 bool FoundInfo = false;
5068 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5069 Info = M.DeclContextInfos.find(This->Contexts[I]);
5070 if (Info != M.DeclContextInfos.end() &&
5071 Info->second.NameLookupTableData) {
5072 FoundInfo = true;
5073 break;
5074 }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005075 }
5076
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005077 if (!FoundInfo)
5078 return false;
5079
5080 ASTDeclContextNameLookupTable *LookupTable =
5081 Info->second.NameLookupTableData;
5082 bool FoundAnything = false;
5083 for (ASTDeclContextNameLookupTable::data_iterator
5084 I = LookupTable->data_begin(), E = LookupTable->data_end();
5085 I != E; ++I) {
5086 ASTDeclContextNameLookupTrait::data_type Data = *I;
5087 for (; Data.first != Data.second; ++Data.first) {
5088 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5089 *Data.first);
5090 if (!ND)
5091 continue;
5092
5093 // Record this declaration.
5094 FoundAnything = true;
5095 This->Decls[ND->getDeclName()].push_back(ND);
5096 }
5097 }
5098
5099 return FoundAnything;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005100 }
5101 };
5102}
5103
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005104void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005105 if (!DC->hasExternalVisibleStorage())
5106 return;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005107 llvm::DenseMap<DeclarationName, llvm::SmallVector<NamedDecl*, 8> > Decls;
5108
5109 // Compute the declaration contexts we need to look into. Multiple such
5110 // declaration contexts occur when two declaration contexts from disjoint
5111 // modules get merged, e.g., when two namespaces with the same name are
5112 // independently defined in separate modules.
5113 SmallVector<const DeclContext *, 2> Contexts;
5114 Contexts.push_back(DC);
5115
5116 if (DC->isNamespace()) {
5117 MergedDeclsMap::iterator Merged
5118 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5119 if (Merged != MergedDecls.end()) {
5120 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5121 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5122 }
5123 }
5124
5125 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls);
5126 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5127 ++NumVisibleDeclContextsRead;
5128
5129 for (llvm::DenseMap<DeclarationName,
5130 llvm::SmallVector<NamedDecl*, 8> >::iterator
5131 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5132 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5133 }
Argyrios Kyrtzidis394e5392012-04-26 18:34:14 +00005134 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005135}
5136
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005137/// \brief Under non-PCH compilation the consumer receives the objc methods
5138/// before receiving the implementation, and codegen depends on this.
5139/// We simulate this by deserializing and passing to consumer the methods of the
5140/// implementation before passing the deserialized implementation decl.
5141static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5142 ASTConsumer *Consumer) {
5143 assert(ImplD && Consumer);
5144
5145 for (ObjCImplDecl::method_iterator
5146 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00005147 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005148
5149 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5150}
5151
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005152void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005153 assert(Consumer);
5154 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005155 Decl *D = InterestingDecls.front();
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005156 InterestingDecls.pop_front();
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005157
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005158 PassInterestingDeclToConsumer(D);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005159 }
5160}
5161
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005162void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5163 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5164 PassObjCImplDeclToConsumer(ImplD, Consumer);
5165 else
5166 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5167}
5168
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005169void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00005170 this->Consumer = Consumer;
5171
Douglas Gregorfdd01722009-04-14 00:24:19 +00005172 if (!Consumer)
5173 return;
5174
5175 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005176 // Force deserialization of this decl, which will cause it to be queued for
5177 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00005178 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00005179 }
Douglas Gregor1a995dd2011-09-15 18:47:32 +00005180 ExternalDefinitions.clear();
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00005181
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005182 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00005183}
5184
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005185void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005186 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00005187
Mike Stump1eb44332009-09-09 15:08:12 +00005188 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005189 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00005190 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005191 unsigned NumDeclsLoaded
5192 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5193 (Decl *)0);
5194 unsigned NumIdentifiersLoaded
5195 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5196 IdentifiersLoaded.end(),
5197 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00005198 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005199 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5200 SelectorsLoaded.end(),
5201 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00005202
Douglas Gregor4fed3f42009-04-27 18:38:38 +00005203 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
5204 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor0cdd7982011-07-21 18:46:38 +00005205 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005206 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5207 NumSLocEntriesRead, TotalNumSLocEntries,
5208 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005209 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005210 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005211 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5212 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5213 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005214 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005215 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5216 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005217 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005218 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005219 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5220 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00005221 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005222 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00005223 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5224 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00005225 if (TotalNumStatements)
5226 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5227 NumStatementsRead, TotalNumStatements,
5228 ((float)NumStatementsRead/TotalNumStatements * 100));
5229 if (TotalNumMacros)
5230 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5231 NumMacrosRead, TotalNumMacros,
5232 ((float)NumMacrosRead/TotalNumMacros * 100));
5233 if (TotalLexicalDeclContexts)
5234 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5235 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5236 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5237 * 100));
5238 if (TotalVisibleDeclContexts)
5239 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5240 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5241 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5242 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005243 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005244 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005245 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5246 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00005247 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005248 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00005249 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00005250 std::fprintf(stderr, "\n");
Douglas Gregor23d7df52011-07-21 19:50:14 +00005251 dump();
5252 std::fprintf(stderr, "\n");
5253}
5254
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005255template<typename Key, typename ModuleFile, unsigned InitialCapacity>
Douglas Gregor23d7df52011-07-21 19:50:14 +00005256static void
Chris Lattner5f9e2722011-07-23 10:55:15 +00005257dumpModuleIDMap(StringRef Name,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005258 const ContinuousRangeMap<Key, ModuleFile *,
Douglas Gregor23d7df52011-07-21 19:50:14 +00005259 InitialCapacity> &Map) {
5260 if (Map.begin() == Map.end())
5261 return;
5262
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005263 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
Douglas Gregor23d7df52011-07-21 19:50:14 +00005264 llvm::errs() << Name << ":\n";
5265 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5266 I != IEnd; ++I) {
5267 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5268 << "\n";
5269 }
5270}
5271
Douglas Gregor23d7df52011-07-21 19:50:14 +00005272void ASTReader::dump() {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005273 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005274 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor23d7df52011-07-21 19:50:14 +00005275 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor1e849b62011-07-29 00:21:44 +00005276 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005277 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005278 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
Douglas Gregor26ced122011-12-01 00:59:36 +00005279 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005280 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005281 dumpModuleIDMap("Global preprocessed entity map",
5282 GlobalPreprocessedEntityMap);
Douglas Gregor8df5c9b2011-08-02 11:12:41 +00005283
5284 llvm::errs() << "\n*** PCH/Modules Loaded:";
5285 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5286 MEnd = ModuleMgr.end();
5287 M != MEnd; ++M)
5288 (*M)->dump();
Douglas Gregor2cf26342009-04-09 22:27:44 +00005289}
5290
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005291/// Return the amount of memory used by memory buffers, breaking down
5292/// by heap-backed versus mmap'ed memory.
5293void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005294 for (ModuleConstIterator I = ModuleMgr.begin(),
5295 E = ModuleMgr.end(); I != E; ++I) {
5296 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005297 size_t bytes = buf->getBufferSize();
5298 switch (buf->getBufferKind()) {
5299 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5300 sizes.malloc_bytes += bytes;
5301 break;
5302 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5303 sizes.mmap_bytes += bytes;
5304 break;
5305 }
5306 }
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005307 }
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005308}
5309
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005310void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005311 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005312 S.ExternalSource = this;
5313
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005314 // Makes sure any declarations that were deserialized "too early"
5315 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00005316 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005317 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
5318 PreloadedDecls[I]->getDeclName());
Douglas Gregor668c1a42009-04-21 22:25:48 +00005319 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005320 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00005321
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005322 // Load the offsets of the declarations that Sema references.
5323 // They will be lazily deserialized when needed.
5324 if (!SemaDeclRefs.empty()) {
5325 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregor1e5b6f62011-07-28 00:57:24 +00005326 if (!SemaObj->StdNamespace)
5327 SemaObj->StdNamespace = SemaDeclRefs[0];
5328 if (!SemaObj->StdBadAlloc)
5329 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005330 }
5331
Peter Collingbourne84bccea2011-02-15 19:46:30 +00005332 if (!FPPragmaOptions.empty()) {
5333 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
5334 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
5335 }
5336
5337 if (!OpenCLExtensions.empty()) {
5338 unsigned I = 0;
5339#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
5340#include "clang/Basic/OpenCLExtensions.def"
5341
5342 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
5343 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00005344}
5345
Douglas Gregor211f6e82011-08-20 04:39:52 +00005346IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregor057df202012-01-18 20:56:22 +00005347 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart),
5348 /*PriorGeneration=*/0);
Douglas Gregor211f6e82011-08-20 04:39:52 +00005349 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005350 IdentifierInfo *II = Visitor.getIdentifierInfo();
Douglas Gregor057df202012-01-18 20:56:22 +00005351 markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005352 return II;
Douglas Gregor668c1a42009-04-21 22:25:48 +00005353}
5354
Douglas Gregor95f42922010-10-14 22:11:03 +00005355namespace clang {
5356 /// \brief An identifier-lookup iterator that enumerates all of the
5357 /// identifiers stored within a set of AST files.
5358 class ASTIdentifierIterator : public IdentifierIterator {
5359 /// \brief The AST reader whose identifiers are being enumerated.
5360 const ASTReader &Reader;
5361
5362 /// \brief The current index into the chain of AST files stored in
5363 /// the AST reader.
5364 unsigned Index;
5365
5366 /// \brief The current position within the identifier lookup table
5367 /// of the current AST file.
5368 ASTIdentifierLookupTable::key_iterator Current;
5369
5370 /// \brief The end position within the identifier lookup table of
5371 /// the current AST file.
5372 ASTIdentifierLookupTable::key_iterator End;
5373
5374 public:
5375 explicit ASTIdentifierIterator(const ASTReader &Reader);
5376
Chris Lattner5f9e2722011-07-23 10:55:15 +00005377 virtual StringRef Next();
Douglas Gregor95f42922010-10-14 22:11:03 +00005378 };
5379}
5380
5381ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005382 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor95f42922010-10-14 22:11:03 +00005383 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005384 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005385 Current = IdTable->key_begin();
5386 End = IdTable->key_end();
5387}
5388
Chris Lattner5f9e2722011-07-23 10:55:15 +00005389StringRef ASTIdentifierIterator::Next() {
Douglas Gregor95f42922010-10-14 22:11:03 +00005390 while (Current == End) {
5391 // If we have exhausted all of our AST files, we're done.
5392 if (Index == 0)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005393 return StringRef();
Douglas Gregor95f42922010-10-14 22:11:03 +00005394
5395 --Index;
5396 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005397 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
5398 IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005399 Current = IdTable->key_begin();
5400 End = IdTable->key_end();
5401 }
5402
5403 // We have any identifiers remaining in the current AST file; return
5404 // the next one.
5405 std::pair<const char*, unsigned> Key = *Current;
5406 ++Current;
Chris Lattner5f9e2722011-07-23 10:55:15 +00005407 return StringRef(Key.first, Key.second);
Douglas Gregor95f42922010-10-14 22:11:03 +00005408}
5409
5410IdentifierIterator *ASTReader::getIdentifiers() const {
5411 return new ASTIdentifierIterator(*this);
5412}
5413
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005414namespace clang { namespace serialization {
5415 class ReadMethodPoolVisitor {
5416 ASTReader &Reader;
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005417 Selector Sel;
5418 unsigned PriorGeneration;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005419 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
5420 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005421
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005422 public:
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005423 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
5424 unsigned PriorGeneration)
5425 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005426
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005427 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005428 ReadMethodPoolVisitor *This
5429 = static_cast<ReadMethodPoolVisitor *>(UserData);
5430
5431 if (!M.SelectorLookupTable)
5432 return false;
5433
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005434 // If we've already searched this module file, skip it now.
5435 if (M.Generation <= This->PriorGeneration)
5436 return true;
5437
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005438 ASTSelectorLookupTable *PoolTable
5439 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
5440 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
5441 if (Pos == PoolTable->end())
5442 return false;
5443
5444 ++This->Reader.NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005445 // FIXME: Not quite happy with the statistics here. We probably should
5446 // disable this tracking when called via LoadSelector.
5447 // Also, should entries without methods count as misses?
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005448 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005449 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005450 if (This->Reader.DeserializationListener)
5451 This->Reader.DeserializationListener->SelectorRead(Data.ID,
5452 This->Sel);
5453
5454 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
5455 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
5456 return true;
Sebastian Redl725cd962010-08-04 20:40:17 +00005457 }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005458
5459 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005460 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
5461 return InstanceMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005462 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005463
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005464 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005465 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
5466 return FactoryMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005467 }
5468 };
5469} } // end namespace clang::serialization
5470
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005471/// \brief Add the given set of methods to the method list.
5472static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
5473 ObjCMethodList &List) {
5474 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
5475 S.addMethodToGlobalList(&List, Methods[I]);
5476 }
5477}
5478
5479void ASTReader::ReadMethodPool(Selector Sel) {
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005480 // Get the selector generation and update it to the current generation.
5481 unsigned &Generation = SelectorGeneration[Sel];
5482 unsigned PriorGeneration = Generation;
5483 Generation = CurrentGeneration;
5484
5485 // Search for methods defined with this selector.
5486 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005487 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005488
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005489 if (Visitor.getInstanceMethods().empty() &&
5490 Visitor.getFactoryMethods().empty()) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005491 ++NumMethodPoolMisses;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005492 return;
5493 }
5494
5495 if (!getSema())
5496 return;
5497
5498 Sema &S = *getSema();
5499 Sema::GlobalMethodPool::iterator Pos
5500 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
5501
5502 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
5503 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005504}
5505
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005506void ASTReader::ReadKnownNamespaces(
Chris Lattner5f9e2722011-07-23 10:55:15 +00005507 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005508 Namespaces.clear();
5509
5510 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
5511 if (NamespaceDecl *Namespace
5512 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
5513 Namespaces.push_back(Namespace);
5514 }
5515}
5516
Douglas Gregora8623202011-07-27 20:58:46 +00005517void ASTReader::ReadTentativeDefinitions(
5518 SmallVectorImpl<VarDecl *> &TentativeDefs) {
5519 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
5520 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
5521 if (Var)
5522 TentativeDefs.push_back(Var);
5523 }
5524 TentativeDefinitions.clear();
5525}
5526
Douglas Gregora2ee20a2011-07-27 21:45:57 +00005527void ASTReader::ReadUnusedFileScopedDecls(
5528 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
5529 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
5530 DeclaratorDecl *D
5531 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
5532 if (D)
5533 Decls.push_back(D);
5534 }
5535 UnusedFileScopedDecls.clear();
5536}
5537
Douglas Gregor0129b562011-07-27 21:57:17 +00005538void ASTReader::ReadDelegatingConstructors(
5539 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
5540 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
5541 CXXConstructorDecl *D
5542 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
5543 if (D)
5544 Decls.push_back(D);
5545 }
5546 DelegatingCtorDecls.clear();
5547}
5548
Douglas Gregord58a0a52011-07-28 00:39:29 +00005549void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
5550 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
5551 TypedefNameDecl *D
5552 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
5553 if (D)
5554 Decls.push_back(D);
5555 }
5556 ExtVectorDecls.clear();
5557}
5558
Douglas Gregora126f172011-07-28 00:53:40 +00005559void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
5560 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
5561 CXXRecordDecl *D
5562 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
5563 if (D)
5564 Decls.push_back(D);
5565 }
5566 DynamicClasses.clear();
5567}
5568
Douglas Gregorec12ce22011-07-28 14:20:37 +00005569void
5570ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
5571 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
5572 NamedDecl *D
5573 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
5574 if (D)
5575 Decls.push_back(D);
5576 }
5577 LocallyScopedExternalDecls.clear();
5578}
5579
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00005580void ASTReader::ReadReferencedSelectors(
5581 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
5582 if (ReferencedSelectorsData.empty())
5583 return;
5584
5585 // If there are @selector references added them to its pool. This is for
5586 // implementation of -Wselector.
5587 unsigned int DataSize = ReferencedSelectorsData.size()-1;
5588 unsigned I = 0;
5589 while (I < DataSize) {
5590 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
5591 SourceLocation SelLoc
5592 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
5593 Sels.push_back(std::make_pair(Sel, SelLoc));
5594 }
5595 ReferencedSelectorsData.clear();
5596}
5597
Douglas Gregor31e37b22011-07-28 18:09:57 +00005598void ASTReader::ReadWeakUndeclaredIdentifiers(
5599 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
5600 if (WeakUndeclaredIdentifiers.empty())
5601 return;
5602
5603 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
5604 IdentifierInfo *WeakId
5605 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5606 IdentifierInfo *AliasId
5607 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5608 SourceLocation Loc
5609 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
5610 bool Used = WeakUndeclaredIdentifiers[I++];
5611 WeakInfo WI(AliasId, Loc);
5612 WI.setUsed(Used);
5613 WeakIDs.push_back(std::make_pair(WeakId, WI));
5614 }
5615 WeakUndeclaredIdentifiers.clear();
5616}
5617
Douglas Gregordfe65432011-07-28 19:11:31 +00005618void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
5619 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
5620 ExternalVTableUse VT;
5621 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
5622 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
5623 VT.DefinitionRequired = VTableUses[Idx++];
5624 VTables.push_back(VT);
5625 }
5626
5627 VTableUses.clear();
5628}
5629
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00005630void ASTReader::ReadPendingInstantiations(
5631 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
5632 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
5633 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
5634 SourceLocation Loc
5635 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
Axel Naumann39d26c32012-10-02 09:09:43 +00005636
Douglas Gregore5fa3c22012-10-03 18:34:48 +00005637 Pending.push_back(std::make_pair(D, Loc));
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00005638 }
5639 PendingInstantiations.clear();
5640}
5641
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005642void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00005643 // It would be complicated to avoid reading the methods anyway. So don't.
5644 ReadMethodPool(Sel);
5645}
5646
Douglas Gregor95eab172011-07-28 20:55:49 +00005647void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005648 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00005649 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005650 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005651 if (DeserializationListener)
5652 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00005653}
5654
Douglas Gregord89275b2009-07-06 18:54:52 +00005655/// \brief Set the globally-visible declarations associated with the given
5656/// identifier.
5657///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005658/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00005659/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00005660/// them.
5661///
5662/// \param II an IdentifierInfo that refers to one or more globally-visible
5663/// declarations.
5664///
5665/// \param DeclIDs the set of declaration IDs with the name @p II that are
5666/// visible at global scope.
5667///
5668/// \param Nonrecursive should be true to indicate that the caller knows that
5669/// this call is non-recursive, and therefore the globally-visible declarations
5670/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00005671void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005672ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner5f9e2722011-07-23 10:55:15 +00005673 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregord89275b2009-07-06 18:54:52 +00005674 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005675 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00005676 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
5677 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
5678 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00005679 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00005680 return;
5681 }
Mike Stump1eb44332009-09-09 15:08:12 +00005682
Douglas Gregord89275b2009-07-06 18:54:52 +00005683 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
5684 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
5685 if (SemaObj) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005686 // Introduce this declaration into the translation-unit scope
5687 // and add it to the declaration chain for this identifier, so
5688 // that (unqualified) name lookup will find it.
5689 SemaObj->pushExternalDeclIntoScope(D, II);
Douglas Gregord89275b2009-07-06 18:54:52 +00005690 } else {
5691 // Queue this declaration so that it will be added to the
5692 // translation unit scope and identifier's declaration chain
5693 // once a Sema object is known.
5694 PreloadedDecls.push_back(D);
5695 }
5696 }
5697}
5698
Douglas Gregor95eab172011-07-28 20:55:49 +00005699IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00005700 if (ID == 0)
5701 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00005702
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005703 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005704 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00005705 return 0;
5706 }
Mike Stump1eb44332009-09-09 15:08:12 +00005707
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005708 ID -= 1;
5709 if (!IdentifiersLoaded[ID]) {
Douglas Gregor67268d02011-07-20 00:59:32 +00005710 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
5711 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005712 ModuleFile *M = I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00005713 unsigned Index = ID - M->BaseIdentifierID;
5714 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregord6595a42009-04-25 21:04:17 +00005715
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005716 // All of the strings in the AST file are preceded by a 16-bit length.
5717 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00005718 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
5719 // unsigned integers. This is important to avoid integer overflow when
5720 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00005721 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00005722 unsigned StrLen = (((unsigned) StrLenPtr[0])
5723 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005724 IdentifiersLoaded[ID]
Douglas Gregor712f2fc2011-09-09 22:02:16 +00005725 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005726 if (DeserializationListener)
5727 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00005728 }
Mike Stump1eb44332009-09-09 15:08:12 +00005729
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005730 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00005731}
5732
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005733IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
Douglas Gregor95eab172011-07-28 20:55:49 +00005734 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
5735}
5736
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005737IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +00005738 if (LocalID < NUM_PREDEF_IDENT_IDS)
5739 return LocalID;
5740
5741 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5742 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
5743 assert(I != M.IdentifierRemap.end()
5744 && "Invalid index into identifier index remap");
5745
5746 return LocalID + I->second;
Douglas Gregor95eab172011-07-28 20:55:49 +00005747}
5748
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005749bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregore23ac652011-04-20 00:21:03 +00005750 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005751}
5752
Douglas Gregor26ced122011-12-01 00:59:36 +00005753serialization::SubmoduleID
5754ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
5755 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
5756 return LocalID;
5757
5758 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5759 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
5760 assert(I != M.SubmoduleRemap.end()
5761 && "Invalid index into identifier index remap");
5762
5763 return LocalID + I->second;
5764}
5765
5766Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
5767 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
5768 assert(GlobalID == 0 && "Unhandled global submodule ID");
5769 return 0;
5770 }
5771
5772 if (GlobalID > SubmodulesLoaded.size()) {
5773 Error("submodule ID out of range in AST file");
5774 return 0;
5775 }
5776
5777 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
5778}
5779
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005780Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
Douglas Gregor2d2689a2011-07-28 21:16:51 +00005781 return DecodeSelector(getGlobalSelectorID(M, LocalID));
5782}
5783
5784Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005785 if (ID == 0)
5786 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00005787
Sebastian Redl725cd962010-08-04 20:40:17 +00005788 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005789 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005790 return Selector();
5791 }
Douglas Gregor83941df2009-04-25 17:48:32 +00005792
Sebastian Redl725cd962010-08-04 20:40:17 +00005793 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005794 // Load this selector from the selector table.
Douglas Gregor96958cb2011-07-20 01:10:58 +00005795 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
5796 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005797 ModuleFile &M = *I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00005798 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005799 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor96958cb2011-07-20 01:10:58 +00005800 SelectorsLoaded[ID - 1] =
Douglas Gregor9827a802011-07-29 00:56:45 +00005801 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor96958cb2011-07-20 01:10:58 +00005802 if (DeserializationListener)
5803 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor83941df2009-04-25 17:48:32 +00005804 }
5805
Sebastian Redl725cd962010-08-04 20:40:17 +00005806 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005807}
5808
Douglas Gregor8451ec72011-07-28 14:41:43 +00005809Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00005810 return DecodeSelector(ID);
5811}
5812
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005813uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00005814 // ID 0 (the null selector) is considered an external selector.
5815 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00005816}
5817
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005818serialization::SelectorID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005819ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005820 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
5821 return LocalID;
5822
5823 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5824 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
5825 assert(I != M.SelectorRemap.end()
5826 && "Invalid index into identifier index remap");
5827
5828 return LocalID + I->second;
Douglas Gregor8451ec72011-07-28 14:41:43 +00005829}
5830
Mike Stump1eb44332009-09-09 15:08:12 +00005831DeclarationName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005832ASTReader::ReadDeclarationName(ModuleFile &F,
Douglas Gregor393f2492011-07-22 00:38:23 +00005833 const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00005834 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
5835 switch (Kind) {
5836 case DeclarationName::Identifier:
Douglas Gregor95eab172011-07-28 20:55:49 +00005837 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005838
5839 case DeclarationName::ObjCZeroArgSelector:
5840 case DeclarationName::ObjCOneArgSelector:
5841 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor2d2689a2011-07-28 21:16:51 +00005842 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005843
5844 case DeclarationName::CXXConstructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005845 return Context.DeclarationNames.getCXXConstructorName(
5846 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005847
5848 case DeclarationName::CXXDestructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005849 return Context.DeclarationNames.getCXXDestructorName(
5850 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005851
5852 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor35942772011-09-09 21:34:22 +00005853 return Context.DeclarationNames.getCXXConversionFunctionName(
5854 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005855
5856 case DeclarationName::CXXOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005857 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00005858 (OverloadedOperatorKind)Record[Idx++]);
5859
Sean Hunt3e518bd2009-11-29 07:34:05 +00005860 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005861 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregor95eab172011-07-28 20:55:49 +00005862 GetIdentifierInfo(F, Record, Idx));
Sean Hunt3e518bd2009-11-29 07:34:05 +00005863
Douglas Gregor2cf26342009-04-09 22:27:44 +00005864 case DeclarationName::CXXUsingDirective:
5865 return DeclarationName::getUsingDirectiveName();
5866 }
5867
David Blaikie7530c032012-01-17 06:56:22 +00005868 llvm_unreachable("Invalid NameKind!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00005869}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00005870
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005871void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005872 DeclarationNameLoc &DNLoc,
5873 DeclarationName Name,
5874 const RecordData &Record, unsigned &Idx) {
5875 switch (Name.getNameKind()) {
5876 case DeclarationName::CXXConstructorName:
5877 case DeclarationName::CXXDestructorName:
5878 case DeclarationName::CXXConversionFunctionName:
5879 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
5880 break;
5881
5882 case DeclarationName::CXXOperatorName:
5883 DNLoc.CXXOperatorName.BeginOpNameLoc
5884 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5885 DNLoc.CXXOperatorName.EndOpNameLoc
5886 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5887 break;
5888
5889 case DeclarationName::CXXLiteralOperatorName:
5890 DNLoc.CXXLiteralOperatorName.OpNameLoc
5891 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5892 break;
5893
5894 case DeclarationName::Identifier:
5895 case DeclarationName::ObjCZeroArgSelector:
5896 case DeclarationName::ObjCOneArgSelector:
5897 case DeclarationName::ObjCMultiArgSelector:
5898 case DeclarationName::CXXUsingDirective:
5899 break;
5900 }
5901}
5902
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005903void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005904 DeclarationNameInfo &NameInfo,
5905 const RecordData &Record, unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00005906 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005907 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
5908 DeclarationNameLoc DNLoc;
5909 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
5910 NameInfo.setInfo(DNLoc);
5911}
5912
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005913void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005914 const RecordData &Record, unsigned &Idx) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005915 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005916 unsigned NumTPLists = Record[Idx++];
5917 Info.NumTemplParamLists = NumTPLists;
5918 if (NumTPLists) {
Douglas Gregor35942772011-09-09 21:34:22 +00005919 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005920 for (unsigned i=0; i != NumTPLists; ++i)
5921 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
5922 }
5923}
5924
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005925TemplateName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005926ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005927 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00005928 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005929 switch (Kind) {
5930 case TemplateName::Template:
Douglas Gregor409448c2011-07-21 22:35:25 +00005931 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005932
5933 case TemplateName::OverloadedTemplate: {
5934 unsigned size = Record[Idx++];
5935 UnresolvedSet<8> Decls;
5936 while (size--)
Douglas Gregor409448c2011-07-21 22:35:25 +00005937 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005938
Douglas Gregor35942772011-09-09 21:34:22 +00005939 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005940 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005941
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005942 case TemplateName::QualifiedTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005943 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005944 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00005945 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005946 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005947 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005948
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005949 case TemplateName::DependentTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005950 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005951 if (Record[Idx++]) // isIdentifier
Douglas Gregor35942772011-09-09 21:34:22 +00005952 return Context.getDependentTemplateName(NNS,
Douglas Gregor95eab172011-07-28 20:55:49 +00005953 GetIdentifierInfo(F, Record,
5954 Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00005955 return Context.getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00005956 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005957 }
John McCall14606042011-06-30 08:33:18 +00005958
5959 case TemplateName::SubstTemplateTemplateParm: {
5960 TemplateTemplateParmDecl *param
Douglas Gregor409448c2011-07-21 22:35:25 +00005961 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCall14606042011-06-30 08:33:18 +00005962 if (!param) return TemplateName();
5963 TemplateName replacement = ReadTemplateName(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005964 return Context.getSubstTemplateTemplateParm(param, replacement);
John McCall14606042011-06-30 08:33:18 +00005965 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005966
5967 case TemplateName::SubstTemplateTemplateParmPack: {
5968 TemplateTemplateParmDecl *Param
Douglas Gregor409448c2011-07-21 22:35:25 +00005969 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005970 if (!Param)
5971 return TemplateName();
5972
5973 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
5974 if (ArgPack.getKind() != TemplateArgument::Pack)
5975 return TemplateName();
5976
Douglas Gregor35942772011-09-09 21:34:22 +00005977 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005978 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005979 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005980
David Blaikieb219cfc2011-09-23 05:06:16 +00005981 llvm_unreachable("Unhandled template name kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005982}
5983
5984TemplateArgument
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005985ASTReader::ReadTemplateArgument(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00005986 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00005987 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
5988 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005989 case TemplateArgument::Null:
5990 return TemplateArgument();
5991 case TemplateArgument::Type:
Douglas Gregor393f2492011-07-22 00:38:23 +00005992 return TemplateArgument(readType(F, Record, Idx));
Eli Friedmand7a6b162012-09-26 02:36:12 +00005993 case TemplateArgument::Declaration: {
5994 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
5995 bool ForReferenceParam = Record[Idx++];
5996 return TemplateArgument(D, ForReferenceParam);
5997 }
5998 case TemplateArgument::NullPtr:
5999 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006000 case TemplateArgument::Integral: {
6001 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00006002 QualType T = readType(F, Record, Idx);
Benjamin Kramer85524372012-06-07 15:09:51 +00006003 return TemplateArgument(Context, Value, T);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006004 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00006005 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006006 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00006007 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006008 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00006009 llvm::Optional<unsigned> NumTemplateExpansions;
6010 if (unsigned NumExpansions = Record[Idx++])
6011 NumTemplateExpansions = NumExpansions - 1;
6012 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00006013 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006014 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00006015 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006016 case TemplateArgument::Pack: {
6017 unsigned NumArgs = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006018 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
Douglas Gregor910f8002010-11-07 23:05:16 +00006019 for (unsigned I = 0; I != NumArgs; ++I)
6020 Args[I] = ReadTemplateArgument(F, Record, Idx);
6021 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006022 }
6023 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006024
David Blaikieb219cfc2011-09-23 05:06:16 +00006025 llvm_unreachable("Unhandled template argument kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006026}
6027
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006028TemplateParameterList *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006029ASTReader::ReadTemplateParameterList(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00006030 const RecordData &Record, unsigned &Idx) {
6031 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6032 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6033 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006034
6035 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00006036 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006037 Params.reserve(NumParams);
6038 while (NumParams--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006039 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer20249a12010-10-21 03:16:25 +00006040
6041 TemplateParameterList* TemplateParams =
Douglas Gregor35942772011-09-09 21:34:22 +00006042 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006043 Params.data(), Params.size(), RAngleLoc);
6044 return TemplateParams;
6045}
6046
6047void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006048ASTReader::
Chris Lattner5f9e2722011-07-23 10:55:15 +00006049ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006050 ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006051 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006052 unsigned NumTemplateArgs = Record[Idx++];
6053 TemplArgs.reserve(NumTemplateArgs);
6054 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00006055 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006056}
6057
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006058/// \brief Read a UnresolvedSet structure.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006059void ASTReader::ReadUnresolvedSet(ModuleFile &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006060 const RecordData &Record, unsigned &Idx) {
6061 unsigned NumDecls = Record[Idx++];
6062 while (NumDecls--) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006063 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006064 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6065 Set.addDecl(D, AS);
6066 }
6067}
6068
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006069CXXBaseSpecifier
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006070ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
Nick Lewycky56062202010-07-26 16:56:01 +00006071 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006072 bool isVirtual = static_cast<bool>(Record[Idx++]);
6073 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6074 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006075 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00006076 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6077 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006078 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006079 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006080 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006081 Result.setInheritConstructors(inheritConstructors);
6082 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006083}
6084
Sean Huntcbb67482011-01-08 20:30:50 +00006085std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006086ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
Sean Huntcbb67482011-01-08 20:30:50 +00006087 unsigned &Idx) {
6088 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006089 unsigned NumInitializers = Record[Idx++];
6090 if (NumInitializers) {
Sean Huntcbb67482011-01-08 20:30:50 +00006091 CtorInitializers
Douglas Gregor35942772011-09-09 21:34:22 +00006092 = new (Context) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006093 for (unsigned i=0; i != NumInitializers; ++i) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006094 TypeSourceInfo *TInfo = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006095 bool IsBaseVirtual = false;
6096 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00006097 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00006098
Sean Hunt156b6402011-05-04 01:19:08 +00006099 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6100 switch (Type) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006101 case CTOR_INITIALIZER_BASE:
6102 TInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006103 IsBaseVirtual = Record[Idx++];
Sean Hunt156b6402011-05-04 01:19:08 +00006104 break;
Douglas Gregor76852c22011-11-01 01:16:03 +00006105
6106 case CTOR_INITIALIZER_DELEGATING:
6107 TInfo = GetTypeSourceInfo(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006108 break;
6109
6110 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006111 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006112 break;
6113
6114 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006115 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006116 break;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006117 }
Sean Hunt156b6402011-05-04 01:19:08 +00006118
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00006119 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00006120 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00006121 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6122 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006123 bool IsWritten = Record[Idx++];
6124 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner5f9e2722011-07-23 10:55:15 +00006125 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006126 if (IsWritten) {
6127 SourceOrderOrNumArrayIndices = Record[Idx++];
6128 } else {
6129 SourceOrderOrNumArrayIndices = Record[Idx++];
6130 Indices.reserve(SourceOrderOrNumArrayIndices);
6131 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor409448c2011-07-21 22:35:25 +00006132 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006133 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006134
Sean Huntcbb67482011-01-08 20:30:50 +00006135 CXXCtorInitializer *BOMInit;
Sean Hunt156b6402011-05-04 01:19:08 +00006136 if (Type == CTOR_INITIALIZER_BASE) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006137 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
Sean Huntcbb67482011-01-08 20:30:50 +00006138 LParenLoc, Init, RParenLoc,
6139 MemberOrEllipsisLoc);
Sean Hunt156b6402011-05-04 01:19:08 +00006140 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006141 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6142 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006143 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00006144 if (Member)
Douglas Gregor35942772011-09-09 21:34:22 +00006145 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006146 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00006147 else
Douglas Gregor35942772011-09-09 21:34:22 +00006148 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
Sean Huntcbb67482011-01-08 20:30:50 +00006149 MemberOrEllipsisLoc, LParenLoc,
6150 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006151 } else {
Douglas Gregor35942772011-09-09 21:34:22 +00006152 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006153 LParenLoc, Init, RParenLoc,
6154 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006155 }
6156
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00006157 if (IsWritten)
6158 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00006159 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006160 }
6161 }
6162
Sean Huntcbb67482011-01-08 20:30:50 +00006163 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006164}
6165
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006166NestedNameSpecifier *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006167ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006168 const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006169 unsigned N = Record[Idx++];
6170 NestedNameSpecifier *NNS = 0, *Prev = 0;
6171 for (unsigned I = 0; I != N; ++I) {
6172 NestedNameSpecifier::SpecifierKind Kind
6173 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6174 switch (Kind) {
6175 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006176 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006177 NNS = NestedNameSpecifier::Create(Context, Prev, II);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006178 break;
6179 }
6180
6181 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006182 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006183 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006184 break;
6185 }
6186
Douglas Gregor14aba762011-02-24 02:36:08 +00006187 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006188 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006189 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
Douglas Gregor14aba762011-02-24 02:36:08 +00006190 break;
6191 }
6192
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006193 case NestedNameSpecifier::TypeSpec:
6194 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor393f2492011-07-22 00:38:23 +00006195 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00006196 if (!T)
6197 return 0;
6198
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006199 bool Template = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006200 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006201 break;
6202 }
6203
6204 case NestedNameSpecifier::Global: {
Douglas Gregor35942772011-09-09 21:34:22 +00006205 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006206 // No associated value, and there can't be a prefix.
6207 break;
6208 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006209 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00006210 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006211 }
6212 return NNS;
6213}
6214
Douglas Gregordc355712011-02-25 00:36:19 +00006215NestedNameSpecifierLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006216ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
Douglas Gregordc355712011-02-25 00:36:19 +00006217 unsigned &Idx) {
6218 unsigned N = Record[Idx++];
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006219 NestedNameSpecifierLocBuilder Builder;
Douglas Gregordc355712011-02-25 00:36:19 +00006220 for (unsigned I = 0; I != N; ++I) {
6221 NestedNameSpecifier::SpecifierKind Kind
6222 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6223 switch (Kind) {
6224 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006225 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006226 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006227 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006228 break;
6229 }
6230
6231 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006232 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006233 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006234 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006235 break;
6236 }
6237
6238 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006239 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006240 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006241 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006242 break;
6243 }
6244
6245 case NestedNameSpecifier::TypeSpec:
6246 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregordc355712011-02-25 00:36:19 +00006247 bool Template = Record[Idx++];
6248 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
6249 if (!T)
6250 return NestedNameSpecifierLoc();
Douglas Gregordc355712011-02-25 00:36:19 +00006251 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006252
6253 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
Douglas Gregor35942772011-09-09 21:34:22 +00006254 Builder.Extend(Context,
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006255 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
6256 T->getTypeLoc(), ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006257 break;
6258 }
6259
6260 case NestedNameSpecifier::Global: {
Douglas Gregordc355712011-02-25 00:36:19 +00006261 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006262 Builder.MakeGlobal(Context, ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006263 break;
6264 }
6265 }
Douglas Gregordc355712011-02-25 00:36:19 +00006266 }
6267
Douglas Gregor35942772011-09-09 21:34:22 +00006268 return Builder.getWithLocInContext(Context);
Douglas Gregordc355712011-02-25 00:36:19 +00006269}
6270
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006271SourceRange
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006272ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006273 unsigned &Idx) {
6274 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
6275 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00006276 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006277}
6278
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006279/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006280llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006281 unsigned BitWidth = Record[Idx++];
6282 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
6283 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
6284 Idx += NumWords;
6285 return Result;
6286}
6287
6288/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006289llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006290 bool isUnsigned = Record[Idx++];
6291 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
6292}
6293
Douglas Gregor17fc2232009-04-14 21:55:33 +00006294/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006295llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00006296 return llvm::APFloat(ReadAPInt(Record, Idx));
6297}
6298
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006299// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006300std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006301 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00006302 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006303 Idx += Len;
6304 return Result;
6305}
6306
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00006307VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
6308 unsigned &Idx) {
6309 unsigned Major = Record[Idx++];
6310 unsigned Minor = Record[Idx++];
6311 unsigned Subminor = Record[Idx++];
6312 if (Minor == 0)
6313 return VersionTuple(Major);
6314 if (Subminor == 0)
6315 return VersionTuple(Major, Minor - 1);
6316 return VersionTuple(Major, Minor - 1, Subminor - 1);
6317}
6318
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006319CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006320 const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00006321 unsigned &Idx) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006322 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006323 return CXXTemporary::Create(Context, Decl);
Chris Lattnerd2598362010-05-10 00:25:06 +00006324}
6325
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006326DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00006327 return Diag(SourceLocation(), DiagID);
6328}
6329
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006330DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00006331 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00006332}
Douglas Gregor025452f2009-04-17 00:04:06 +00006333
Douglas Gregor668c1a42009-04-21 22:25:48 +00006334/// \brief Retrieve the identifier table associated with the
6335/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006336IdentifierTable &ASTReader::getIdentifierTable() {
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006337 return PP.getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00006338}
6339
Douglas Gregor025452f2009-04-17 00:04:06 +00006340/// \brief Record that the given ID maps to the given switch-case
6341/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006342void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006343 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
6344 "Already have a SwitchCase with this ID");
6345 (*CurrSwitchCaseStmts)[ID] = SC;
Douglas Gregor025452f2009-04-17 00:04:06 +00006346}
6347
6348/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006349SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006350 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
6351 return (*CurrSwitchCaseStmts)[ID];
Douglas Gregor025452f2009-04-17 00:04:06 +00006352}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00006353
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006354void ASTReader::ClearSwitchCaseIDs() {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006355 CurrSwitchCaseStmts->clear();
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006356}
6357
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006358void ASTReader::ReadComments() {
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006359 std::vector<RawComment *> Comments;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006360 for (SmallVectorImpl<std::pair<llvm::BitstreamCursor,
6361 serialization::ModuleFile *> >::iterator
6362 I = CommentsCursors.begin(),
6363 E = CommentsCursors.end();
6364 I != E; ++I) {
6365 llvm::BitstreamCursor &Cursor = I->first;
6366 serialization::ModuleFile &F = *I->second;
6367 SavedStreamPosition SavedPosition(Cursor);
6368
6369 RecordData Record;
6370 while (true) {
6371 unsigned Code = Cursor.ReadCode();
6372 if (Code == llvm::bitc::END_BLOCK)
6373 break;
6374
6375 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
6376 // No known subblocks, always skip them.
6377 Cursor.ReadSubBlockID();
6378 if (Cursor.SkipBlock()) {
6379 Error("malformed block record in AST file");
6380 return;
6381 }
6382 continue;
6383 }
6384
6385 if (Code == llvm::bitc::DEFINE_ABBREV) {
6386 Cursor.ReadAbbrevRecord();
6387 continue;
6388 }
6389
6390 // Read a record.
6391 Record.clear();
6392 switch ((CommentRecordTypes) Cursor.ReadRecord(Code, Record)) {
Chandler Carruth13691bb2012-06-20 06:47:54 +00006393 case COMMENTS_RAW_COMMENT: {
6394 unsigned Idx = 0;
6395 SourceRange SR = ReadSourceRange(F, Record, Idx);
6396 RawComment::CommentKind Kind =
6397 (RawComment::CommentKind) Record[Idx++];
6398 bool IsTrailingComment = Record[Idx++];
6399 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006400 Comments.push_back(new (Context) RawComment(SR, Kind,
6401 IsTrailingComment,
6402 IsAlmostTrailingComment));
Chandler Carruth13691bb2012-06-20 06:47:54 +00006403 break;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006404 }
6405 }
6406 }
6407 }
6408 Context.Comments.addCommentsToFront(Comments);
6409}
6410
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006411void ASTReader::finishPendingActions() {
Douglas Gregorcff9f262012-01-27 01:47:08 +00006412 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty()) {
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006413 // If any identifiers with corresponding top-level declarations have
6414 // been loaded, load those declarations now.
6415 while (!PendingIdentifierInfos.empty()) {
6416 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
6417 PendingIdentifierInfos.front().DeclIDs, true);
6418 PendingIdentifierInfos.pop_front();
6419 }
6420
Douglas Gregora1be2782011-12-17 23:38:30 +00006421 // Load pending declaration chains.
6422 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
6423 loadPendingDeclChain(PendingDeclChains[I]);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006424 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
Douglas Gregora1be2782011-12-17 23:38:30 +00006425 }
6426 PendingDeclChains.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006427 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006428
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006429 // If we deserialized any C++ or Objective-C class definitions, any
6430 // Objective-C protocol definitions, or any redeclarable templates, make sure
6431 // that all redeclarations point to the definitions. Note that this can only
6432 // happen now, after the redeclaration chains have been fully wired.
Douglas Gregorfc529f72011-12-19 19:00:47 +00006433 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
6434 DEnd = PendingDefinitions.end();
6435 D != DEnd; ++D) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006436 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
6437 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
6438 // Make sure that the TagType points at the definition.
6439 const_cast<TagType*>(TagT)->decl = TD;
6440 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006441
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006442 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
6443 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
6444 REnd = RD->redecls_end();
6445 R != REnd; ++R)
6446 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
6447
6448 }
6449
Douglas Gregorfc529f72011-12-19 19:00:47 +00006450 continue;
6451 }
6452
Douglas Gregor1d784b22012-01-01 19:51:50 +00006453 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006454 // Make sure that the ObjCInterfaceType points at the definition.
6455 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
6456 ->Decl = ID;
6457
Douglas Gregor1d784b22012-01-01 19:51:50 +00006458 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
6459 REnd = ID->redecls_end();
6460 R != REnd; ++R)
6461 R->Data = ID->Data;
6462
6463 continue;
6464 }
6465
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006466 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
6467 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
6468 REnd = PD->redecls_end();
6469 R != REnd; ++R)
6470 R->Data = PD->Data;
6471
6472 continue;
6473 }
6474
6475 RedeclarableTemplateDecl *RTD
6476 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
6477 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
6478 REnd = RTD->redecls_end();
Douglas Gregorfc529f72011-12-19 19:00:47 +00006479 R != REnd; ++R)
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006480 R->Common = RTD->Common;
Douglas Gregorfc529f72011-12-19 19:00:47 +00006481 }
6482 PendingDefinitions.clear();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006483
6484 // Load the bodies of any functions or methods we've encountered. We do
6485 // this now (delayed) so that we can be sure that the declaration chains
6486 // have been fully wired up.
Douglas Gregorce12d2f2012-10-09 17:50:23 +00006487 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
6488 PBEnd = PendingBodies.end();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006489 PB != PBEnd; ++PB) {
6490 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
6491 // FIXME: Check for =delete/=default?
6492 // FIXME: Complain about ODR violations here?
6493 if (!getContext().getLangOpts().Modules || !FD->hasBody())
6494 FD->setLazyBody(PB->second);
6495 continue;
6496 }
6497
6498 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
6499 if (!getContext().getLangOpts().Modules || !MD->hasBody())
6500 MD->setLazyBody(PB->second);
6501 }
6502 PendingBodies.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006503}
6504
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006505void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00006506 assert(NumCurrentElementsDeserializing &&
6507 "FinishedDeserializing not paired with StartedDeserializing");
6508 if (NumCurrentElementsDeserializing == 1) {
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006509 // We decrease NumCurrentElementsDeserializing only after pending actions
6510 // are finished, to avoid recursively re-calling finishPendingActions().
6511 finishPendingActions();
6512 }
6513 --NumCurrentElementsDeserializing;
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006514
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006515 if (NumCurrentElementsDeserializing == 0 &&
6516 Consumer && !PassingDeclsToConsumer) {
6517 // Guard variable to avoid recursively redoing the process of passing
6518 // decls to consumer.
6519 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6520 true);
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006521
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006522 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00006523 // We are not in recursive loading, so it's safe to pass the "interesting"
6524 // decls to the consumer.
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006525 Decl *D = InterestingDecls.front();
6526 InterestingDecls.pop_front();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006527 PassInterestingDeclToConsumer(D);
6528 }
Douglas Gregord89275b2009-07-06 18:54:52 +00006529 }
Douglas Gregord89275b2009-07-06 18:54:52 +00006530}
Douglas Gregor501c1032010-08-19 00:28:17 +00006531
Douglas Gregorf8a1e512011-09-02 00:26:20 +00006532ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
Douglas Gregor832d6202011-07-22 16:35:34 +00006533 StringRef isysroot, bool DisableValidation,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00006534 bool DisableStatCache, bool AllowASTWithCompilerErrors)
Sebastian Redle1dde812010-08-24 00:50:04 +00006535 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
6536 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006537 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00006538 Consumer(0), ModuleMgr(PP.getFileManager()),
Jonathan D. Turner1afb6612011-07-28 17:20:23 +00006539 RelocatablePCH(false), isysroot(isysroot),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006540 DisableValidation(DisableValidation),
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00006541 DisableStatCache(DisableStatCache),
6542 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006543 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
6544 NumStatHits(0), NumStatMisses(0),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006545 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00006546 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
6547 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
6548 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
6549 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00006550 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
6551 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006552 PassingDeclsToConsumer(false),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00006553 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00006554{
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006555 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redle1dde812010-08-24 00:50:04 +00006556}
6557
Sebastian Redle1dde812010-08-24 00:50:04 +00006558ASTReader::~ASTReader() {
Sebastian Redle1dde812010-08-24 00:50:04 +00006559 for (DeclContextVisibleUpdatesPending::iterator
6560 I = PendingVisibleUpdates.begin(),
6561 E = PendingVisibleUpdates.end();
6562 I != E; ++I) {
6563 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
6564 F = I->second.end();
6565 J != F; ++J)
Benjamin Kramerb1758c62012-04-15 12:36:49 +00006566 delete J->first;
Sebastian Redle1dde812010-08-24 00:50:04 +00006567 }
6568}