blob: 6de0c65ba30a5e62ec80fd5b5340c7a9cd766b6f [file] [log] [blame]
Sebastian Redl904c9c82010-08-18 23:57:11 +00001//===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redlc43b54c2010-08-18 23:56:43 +000010// This file defines the ASTReader class, which reads AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner4c6f9522009-04-27 05:14:47 +000013
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000014#include "clang/Serialization/ASTReader.h"
15#include "clang/Serialization/ASTDeserializationListener.h"
Douglas Gregor98339b92011-08-25 20:47:51 +000016#include "clang/Serialization/ModuleManager.h"
Chandler Carrutha2398d72011-12-09 00:02:23 +000017#include "clang/Serialization/SerializationDiagnostic.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000018#include "ASTCommon.h"
Douglas Gregor98339b92011-08-25 20:47:51 +000019#include "ASTReaderInternals.h"
Douglas Gregore737f502010-08-12 20:07:10 +000020#include "clang/Sema/Sema.h"
John McCall5f1e0942010-08-24 08:50:51 +000021#include "clang/Sema/Scope.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000022#include "clang/AST/ASTConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include "clang/AST/ASTContext.h"
John McCall2a7fb272010-08-25 05:32:35 +000024#include "clang/AST/DeclTemplate.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000025#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000026#include "clang/AST/ExprCXX.h"
Douglas Gregor5f791bb2011-02-28 23:58:31 +000027#include "clang/AST/NestedNameSpecifier.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000028#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000029#include "clang/AST/TypeLocVisitor.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000030#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000031#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000032#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000033#include "clang/Lex/HeaderSearch.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000034#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000035#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000036#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000037#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000038#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000039#include "clang/Basic/TargetInfo.h"
Douglas Gregor445e23e2009-10-05 21:07:28 +000040#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000041#include "clang/Basic/VersionTuple.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000044#include "llvm/Support/MemoryBuffer.h"
John McCall833ca992009-10-29 08:12:44 +000045#include "llvm/Support/ErrorHandling.h"
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000046#include "llvm/Support/FileSystem.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000047#include "llvm/Support/Path.h"
Nick Lewyckyb346d2f2012-04-16 02:51:46 +000048#include "llvm/Support/SaveAndRestore.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000049#include "llvm/Support/system_error.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000050#include <algorithm>
Douglas Gregore721f952009-04-28 18:58:38 +000051#include <iterator>
Douglas Gregor2cf26342009-04-09 22:27:44 +000052#include <cstdio>
Douglas Gregor4fed3f42009-04-27 18:38:38 +000053#include <sys/stat.h>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000054
Douglas Gregor2cf26342009-04-09 22:27:44 +000055using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000056using namespace clang::serialization;
Douglas Gregor98339b92011-08-25 20:47:51 +000057using namespace clang::serialization::reader;
Douglas Gregor2cf26342009-04-09 22:27:44 +000058
59//===----------------------------------------------------------------------===//
Sebastian Redl3c7f4132010-08-18 23:57:06 +000060// PCH validator implementation
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000061//===----------------------------------------------------------------------===//
62
Sebastian Redl571db7f2010-08-18 23:56:56 +000063ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000064
65bool
66PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
David Blaikie4e4d0842012-03-11 07:00:24 +000067 const LangOptions &PPLangOpts = PP.getLangOpts();
Douglas Gregor7d5e81b2011-09-13 18:26:39 +000068
69#define LANGOPT(Name, Bits, Default, Description) \
70 if (PPLangOpts.Name != LangOpts.Name) { \
71 Reader.Diag(diag::err_pch_langopt_mismatch) \
72 << Description << LangOpts.Name << PPLangOpts.Name; \
73 return true; \
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000074 }
75
Douglas Gregor7d5e81b2011-09-13 18:26:39 +000076#define VALUE_LANGOPT(Name, Bits, Default, Description) \
77 if (PPLangOpts.Name != LangOpts.Name) { \
78 Reader.Diag(diag::err_pch_langopt_value_mismatch) \
79 << Description; \
80 return true; \
81}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000082
Douglas Gregor7d5e81b2011-09-13 18:26:39 +000083#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
84 if (PPLangOpts.get##Name() != LangOpts.get##Name()) { \
85 Reader.Diag(diag::err_pch_langopt_value_mismatch) \
86 << Description; \
87 return true; \
88 }
89
90#define BENIGN_LANGOPT(Name, Bits, Default, Description)
91#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
92#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +000093
94 if (PPLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
95 Reader.Diag(diag::err_pch_langopt_value_mismatch)
96 << "target Objective-C runtime";
97 return true;
98 }
Douglas Gregor7d5e81b2011-09-13 18:26:39 +000099
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000100 return false;
101}
102
Chris Lattner5f9e2722011-07-23 10:55:15 +0000103bool PCHValidator::ReadTargetTriple(StringRef Triple) {
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000104 if (Triple == PP.getTargetInfo().getTriple().str())
105 return false;
106
107 Reader.Diag(diag::warn_pch_target_triple)
108 << Triple << PP.getTargetInfo().getTriple().str();
109 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000110}
111
Benjamin Kramer54353f42010-11-25 18:29:30 +0000112namespace {
113 struct EmptyStringRef {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000114 bool operator ()(StringRef r) const { return r.empty(); }
Benjamin Kramer54353f42010-11-25 18:29:30 +0000115 };
116 struct EmptyBlock {
117 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
118 };
119}
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000120
Chris Lattner5f9e2722011-07-23 10:55:15 +0000121static bool EqualConcatenations(SmallVector<StringRef, 2> L,
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000122 PCHPredefinesBlocks R) {
123 // First, sum up the lengths.
124 unsigned LL = 0, RL = 0;
125 for (unsigned I = 0, N = L.size(); I != N; ++I) {
126 LL += L[I].size();
127 }
128 for (unsigned I = 0, N = R.size(); I != N; ++I) {
129 RL += R[I].Data.size();
130 }
131 if (LL != RL)
132 return false;
133 if (LL == 0 && RL == 0)
134 return true;
135
136 // Kick out empty parts, they confuse the algorithm below.
137 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
138 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
139
140 // Do it the hard way. At this point, both vectors must be non-empty.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000141 StringRef LR = L[0], RR = R[0].Data;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000142 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000143 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000144 for (;;) {
145 // Compare the current pieces.
146 if (LR.size() == RR.size()) {
147 // If they're the same length, it's pretty easy.
148 if (LR != RR)
149 return false;
150 // Both pieces are done, advance.
151 ++LI;
152 ++RI;
153 // If either string is done, they're both done, since they're the same
154 // length.
155 if (LI == LN) {
156 assert(RI == RN && "Strings not the same length after all?");
157 return true;
158 }
159 LR = L[LI];
160 RR = R[RI].Data;
161 } else if (LR.size() < RR.size()) {
162 // Right piece is longer.
163 if (!RR.startswith(LR))
164 return false;
165 ++LI;
166 assert(LI != LN && "Strings not the same length after all?");
167 RR = RR.substr(LR.size());
168 LR = L[LI];
169 } else {
170 // Left piece is longer.
171 if (!LR.startswith(RR))
172 return false;
173 ++RI;
174 assert(RI != RN && "Strings not the same length after all?");
175 LR = LR.substr(RR.size());
176 RR = R[RI].Data;
177 }
178 }
179}
180
Chris Lattner5f9e2722011-07-23 10:55:15 +0000181static std::pair<FileID, StringRef::size_type>
182FindMacro(const PCHPredefinesBlocks &Buffers, StringRef MacroDef) {
183 std::pair<FileID, StringRef::size_type> Res;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000184 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
185 Res.second = Buffers[I].Data.find(MacroDef);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000186 if (Res.second != StringRef::npos) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000187 Res.first = Buffers[I].BufferID;
188 break;
189 }
190 }
191 return Res;
192}
193
194bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000195 StringRef OriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000196 std::string &SuggestedPredefines,
197 FileManager &FileMgr) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000198 // We are in the context of an implicit include, so the predefines buffer will
199 // have a #include entry for the PCH file itself (as normalized by the
200 // preprocessor initialization). Find it and skip over it in the checking
201 // below.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000202 SmallString<256> PCHInclude;
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000203 PCHInclude += "#include \"";
Chandler Carruthcb381ea2011-12-09 01:33:57 +0000204 PCHInclude += HeaderSearch::NormalizeDashIncludePath(OriginalFileName,
205 FileMgr);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000206 PCHInclude += "\"\n";
Chris Lattner5f9e2722011-07-23 10:55:15 +0000207 std::pair<StringRef,StringRef> Split =
208 StringRef(PP.getPredefines()).split(PCHInclude.str());
209 StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000210 if (Left == PP.getPredefines()) {
211 Error("Missing PCH include entry!");
212 return true;
213 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000214
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000215 // If the concatenation of all the PCH buffers is equal to the adjusted
216 // command line, we're done.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000217 SmallVector<StringRef, 2> CommandLine;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000218 CommandLine.push_back(Left);
219 CommandLine.push_back(Right);
220 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000221 return false;
222
223 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000225 // The predefines buffers are different. Determine what the differences are,
226 // and whether they require us to reject the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000227 SmallVector<StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000228 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
229 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000230
Chris Lattner5f9e2722011-07-23 10:55:15 +0000231 SmallVector<StringRef, 8> CmdLineLines;
Daniel Dunbare6750492009-11-13 16:46:11 +0000232 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000233
234 // Pick out implicit #includes after the PCH and don't consider them for
235 // validation; we will insert them into SuggestedPredefines so that the
236 // preprocessor includes them.
237 std::string IncludesAfterPCH;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000238 SmallVector<StringRef, 8> AfterPCHLines;
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000239 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
240 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
241 if (AfterPCHLines[i].startswith("#include ")) {
242 IncludesAfterPCH += AfterPCHLines[i];
243 IncludesAfterPCH += '\n';
244 } else {
245 CmdLineLines.push_back(AfterPCHLines[i]);
246 }
247 }
248
249 // Make sure we add the includes last into SuggestedPredefines before we
250 // exit this function.
251 struct AddIncludesRAII {
252 std::string &SuggestedPredefines;
253 std::string &IncludesAfterPCH;
254
255 AddIncludesRAII(std::string &SuggestedPredefines,
256 std::string &IncludesAfterPCH)
257 : SuggestedPredefines(SuggestedPredefines),
258 IncludesAfterPCH(IncludesAfterPCH) { }
259 ~AddIncludesRAII() {
260 SuggestedPredefines += IncludesAfterPCH;
261 }
262 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000263
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000264 // Sort both sets of predefined buffer lines, since we allow some extra
265 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000266 std::sort(CmdLineLines.begin(), CmdLineLines.end());
267 std::sort(PCHLines.begin(), PCHLines.end());
268
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000269 // Determine which predefines that were used to build the PCH file are missing
270 // from the command line.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000271 std::vector<StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000272 std::set_difference(PCHLines.begin(), PCHLines.end(),
273 CmdLineLines.begin(), CmdLineLines.end(),
274 std::back_inserter(MissingPredefines));
275
276 bool MissingDefines = false;
277 bool ConflictingDefines = false;
278 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000279 StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000280 if (Missing.startswith("#include ")) {
281 // An -include was specified when generating the PCH; it is included in
282 // the PCH, just ignore it.
283 continue;
284 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000285 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000286 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
287 return true;
288 }
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000290 // This is a macro definition. Determine the name of the macro we're
291 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000292 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000293 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000294 = Missing.find_first_of("( \n\r", StartOfMacroName);
295 assert(EndOfMacroName != std::string::npos &&
296 "Couldn't find the end of the macro name");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000297 StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000298
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000299 // Determine whether this macro was given a different definition on the
300 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000301 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000302 std::string::size_type MacroDefLen = MacroDefStart.size();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000303 SmallVector<StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000304 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
305 MacroDefStart);
306 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000307 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000308 // Different macro; we're done.
309 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000310 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000311 }
Mike Stump1eb44332009-09-09 15:08:12 +0000312
313 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000314 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000315 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000316 (*ConflictPos)[MacroDefLen] != '(')
317 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000319 // We found a conflicting macro definition.
320 break;
321 }
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000323 if (ConflictPos != CmdLineLines.end()) {
324 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
325 << MacroName;
326
327 // Show the definition of this macro within the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000328 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000329 FindMacro(Buffers, Missing);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000330 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000331 SourceLocation PCHMissingLoc =
332 SourceMgr.getLocForStartOfFile(MacroLoc.first)
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000333 .getLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000334 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000335
336 ConflictingDefines = true;
337 continue;
338 }
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000340 // If the macro doesn't conflict, then we'll just pick up the macro
341 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000342 if (ConflictingDefines)
343 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000345 if (!MissingDefines) {
346 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
347 MissingDefines = true;
348 }
349
350 // Show the definition of this macro within the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000351 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000352 FindMacro(Buffers, Missing);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000353 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000354 SourceLocation PCHMissingLoc =
355 SourceMgr.getLocForStartOfFile(MacroLoc.first)
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000356 .getLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000357 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
358 }
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000360 if (ConflictingDefines)
361 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000363 // Determine what predefines were introduced based on command-line
364 // parameters that were not present when building the PCH
365 // file. Extra #defines are okay, so long as the identifiers being
366 // defined were not used within the precompiled header.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000367 std::vector<StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000368 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
369 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000370 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000371 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000372 StringRef &Extra = ExtraPredefines[I];
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000373 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000374 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
375 return true;
376 }
377
378 // This is an extra macro definition. Determine the name of the
379 // macro we're defining.
380 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000381 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000382 = Extra.find_first_of("( \n\r", StartOfMacroName);
383 assert(EndOfMacroName != std::string::npos &&
384 "Couldn't find the end of the macro name");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000385 StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000386
387 // Check whether this name was used somewhere in the PCH file. If
388 // so, defining it as a macro could change behavior, so we reject
389 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000390 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000391 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000392 return true;
393 }
394
395 // Add this definition to the suggested predefines buffer.
396 SuggestedPredefines += Extra;
397 SuggestedPredefines += '\n';
398 }
399
400 // If we get here, it's because the predefines buffer had compatible
401 // contents. Accept the PCH file.
402 return false;
403}
404
Douglas Gregor12fab312010-03-16 16:35:32 +0000405void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
406 unsigned ID) {
407 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
408 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000409}
410
411void PCHValidator::ReadCounter(unsigned Value) {
412 PP.setCounterValue(Value);
413}
414
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000415//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000416// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000417//===----------------------------------------------------------------------===//
418
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000419void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000420ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000421 DeserializationListener = Listener;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000422}
423
Chris Lattner4c6f9522009-04-27 05:14:47 +0000424
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000425
Douglas Gregor98339b92011-08-25 20:47:51 +0000426unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
427 return serialization::ComputeHash(Sel);
428}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000429
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Douglas Gregor98339b92011-08-25 20:47:51 +0000431std::pair<unsigned, unsigned>
432ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
433 using namespace clang::io;
434 unsigned KeyLen = ReadUnalignedLE16(d);
435 unsigned DataLen = ReadUnalignedLE16(d);
436 return std::make_pair(KeyLen, DataLen);
437}
438
439ASTSelectorLookupTrait::internal_key_type
440ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
441 using namespace clang::io;
Douglas Gregor35942772011-09-09 21:34:22 +0000442 SelectorTable &SelTable = Reader.getContext().Selectors;
Douglas Gregor98339b92011-08-25 20:47:51 +0000443 unsigned N = ReadUnalignedLE16(d);
444 IdentifierInfo *FirstII
445 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
446 if (N == 0)
447 return SelTable.getNullarySelector(FirstII);
448 else if (N == 1)
449 return SelTable.getUnarySelector(FirstII);
450
451 SmallVector<IdentifierInfo *, 16> Args;
452 Args.push_back(FirstII);
453 for (unsigned I = 1; I != N; ++I)
454 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
455
456 return SelTable.getSelector(N, Args.data());
457}
458
459ASTSelectorLookupTrait::data_type
460ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
461 unsigned DataLen) {
462 using namespace clang::io;
463
464 data_type Result;
465
466 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
467 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
468 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
469
470 // Load instance methods
Douglas Gregor98339b92011-08-25 20:47:51 +0000471 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
472 if (ObjCMethodDecl *Method
473 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
474 Result.Instance.push_back(Method);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000475 }
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Douglas Gregor98339b92011-08-25 20:47:51 +0000477 // Load factory methods
Douglas Gregor98339b92011-08-25 20:47:51 +0000478 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
479 if (ObjCMethodDecl *Method
480 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
481 Result.Factory.push_back(Method);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000482 }
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Douglas Gregor98339b92011-08-25 20:47:51 +0000484 return Result;
485}
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Douglas Gregor98339b92011-08-25 20:47:51 +0000487unsigned ASTIdentifierLookupTrait::ComputeHash(const internal_key_type& a) {
488 return llvm::HashString(StringRef(a.first, a.second));
489}
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Douglas Gregor98339b92011-08-25 20:47:51 +0000491std::pair<unsigned, unsigned>
492ASTIdentifierLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
493 using namespace clang::io;
494 unsigned DataLen = ReadUnalignedLE16(d);
495 unsigned KeyLen = ReadUnalignedLE16(d);
496 return std::make_pair(KeyLen, DataLen);
497}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000498
Douglas Gregor98339b92011-08-25 20:47:51 +0000499std::pair<const char*, unsigned>
500ASTIdentifierLookupTrait::ReadKey(const unsigned char* d, unsigned n) {
501 assert(n >= 2 && d[n-1] == '\0');
502 return std::make_pair((const char*) d, n-1);
503}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000504
Douglas Gregor98339b92011-08-25 20:47:51 +0000505IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
506 const unsigned char* d,
507 unsigned DataLen) {
508 using namespace clang::io;
509 unsigned RawID = ReadUnalignedLE32(d);
510 bool IsInteresting = RawID & 0x01;
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Douglas Gregor98339b92011-08-25 20:47:51 +0000512 // Wipe out the "is interesting" bit.
513 RawID = RawID >> 1;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000514
Douglas Gregor98339b92011-08-25 20:47:51 +0000515 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
516 if (!IsInteresting) {
517 // For uninteresting identifiers, just build the IdentifierInfo
518 // and associate it with the persistent ID.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000519 IdentifierInfo *II = KnownII;
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000520 if (!II) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +0000521 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000522 KnownII = II;
523 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000524 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000525 II->setIsFromAST();
Douglas Gregor057df202012-01-18 20:56:22 +0000526 Reader.markIdentifierUpToDate(II);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000527 return II;
528 }
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Douglas Gregor98339b92011-08-25 20:47:51 +0000530 unsigned Bits = ReadUnalignedLE16(d);
531 bool CPlusPlusOperatorKeyword = Bits & 0x01;
532 Bits >>= 1;
533 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
534 Bits >>= 1;
535 bool Poisoned = Bits & 0x01;
536 Bits >>= 1;
537 bool ExtensionToken = Bits & 0x01;
538 Bits >>= 1;
539 bool hasMacroDefinition = Bits & 0x01;
540 Bits >>= 1;
Craig Topper925be542011-12-19 05:04:33 +0000541 unsigned ObjCOrBuiltinID = Bits & 0x7FF;
542 Bits >>= 11;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000543
Douglas Gregor98339b92011-08-25 20:47:51 +0000544 assert(Bits == 0 && "Extra bits in the identifier?");
545 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000546
Douglas Gregor98339b92011-08-25 20:47:51 +0000547 // Build the IdentifierInfo itself and link the identifier ID with
548 // the new IdentifierInfo.
549 IdentifierInfo *II = KnownII;
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000550 if (!II) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000551 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000552 KnownII = II;
553 }
Douglas Gregor057df202012-01-18 20:56:22 +0000554 Reader.markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +0000555 II->setIsFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000556
Douglas Gregor98339b92011-08-25 20:47:51 +0000557 // Set or check the various bits in the IdentifierInfo structure.
558 // Token IDs are read-only.
559 if (HasRevertedTokenIDToIdentifier)
560 II->RevertTokenIDToIdentifier();
561 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
562 assert(II->isExtensionToken() == ExtensionToken &&
563 "Incorrect extension token flag");
564 (void)ExtensionToken;
565 if (Poisoned)
566 II->setIsPoisoned(true);
567 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
568 "Incorrect C++ operator keyword flag");
569 (void)CPlusPlusOperatorKeyword;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000570
Douglas Gregor98339b92011-08-25 20:47:51 +0000571 // If this identifier is a macro, deserialize the macro
572 // definition.
573 if (hasMacroDefinition) {
574 // FIXME: Check for conflicts?
575 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor13292642011-12-02 15:45:10 +0000576 unsigned LocalSubmoduleID = ReadUnalignedLE32(d);
577
578 // Determine whether this macro definition should be visible now, or
579 // whether it is in a hidden submodule.
580 bool Visible = true;
581 if (SubmoduleID GlobalSubmoduleID
582 = Reader.getGlobalSubmoduleID(F, LocalSubmoduleID)) {
583 if (Module *Owner = Reader.getSubmodule(GlobalSubmoduleID)) {
584 if (Owner->NameVisibility == Module::Hidden) {
585 // The owning module is not visible, and this macro definition should
586 // not be, either.
587 Visible = false;
588
589 // Note that this macro definition was hidden because its owning
590 // module is not yet visible.
591 Reader.HiddenNamesMap[Owner].push_back(II);
592 }
593 }
594 }
595
596 Reader.setIdentifierIsMacro(II, F, Offset, Visible);
597 DataLen -= 8;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000598 }
599
Douglas Gregoreee242f2011-10-27 09:33:13 +0000600 Reader.SetIdentifierInfo(ID, II);
601
Douglas Gregor98339b92011-08-25 20:47:51 +0000602 // Read all of the declarations visible at global scope with this
603 // name.
Douglas Gregor98339b92011-08-25 20:47:51 +0000604 if (DataLen > 0) {
605 SmallVector<uint32_t, 4> DeclIDs;
606 for (; DataLen > 0; DataLen -= 4)
607 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
608 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000609 }
610
Douglas Gregor98339b92011-08-25 20:47:51 +0000611 return II;
612}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000613
Douglas Gregor98339b92011-08-25 20:47:51 +0000614unsigned
615ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
616 llvm::FoldingSetNodeID ID;
617 ID.AddInteger(Key.Kind);
618
619 switch (Key.Kind) {
620 case DeclarationName::Identifier:
621 case DeclarationName::CXXLiteralOperatorName:
622 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
623 break;
624 case DeclarationName::ObjCZeroArgSelector:
625 case DeclarationName::ObjCOneArgSelector:
626 case DeclarationName::ObjCMultiArgSelector:
627 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
628 break;
629 case DeclarationName::CXXOperatorName:
630 ID.AddInteger((OverloadedOperatorKind)Key.Data);
631 break;
632 case DeclarationName::CXXConstructorName:
633 case DeclarationName::CXXDestructorName:
634 case DeclarationName::CXXConversionFunctionName:
635 case DeclarationName::CXXUsingDirective:
636 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000637 }
638
Douglas Gregor98339b92011-08-25 20:47:51 +0000639 return ID.ComputeHash();
640}
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000641
Douglas Gregor98339b92011-08-25 20:47:51 +0000642ASTDeclContextNameLookupTrait::internal_key_type
643ASTDeclContextNameLookupTrait::GetInternalKey(
644 const external_key_type& Name) const {
645 DeclNameKey Key;
646 Key.Kind = Name.getNameKind();
647 switch (Name.getNameKind()) {
648 case DeclarationName::Identifier:
649 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
650 break;
651 case DeclarationName::ObjCZeroArgSelector:
652 case DeclarationName::ObjCOneArgSelector:
653 case DeclarationName::ObjCMultiArgSelector:
654 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
655 break;
656 case DeclarationName::CXXOperatorName:
657 Key.Data = Name.getCXXOverloadedOperator();
658 break;
659 case DeclarationName::CXXLiteralOperatorName:
660 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
661 break;
662 case DeclarationName::CXXConstructorName:
663 case DeclarationName::CXXDestructorName:
664 case DeclarationName::CXXConversionFunctionName:
665 case DeclarationName::CXXUsingDirective:
666 Key.Data = 0;
667 break;
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000668 }
669
Douglas Gregor98339b92011-08-25 20:47:51 +0000670 return Key;
671}
672
Douglas Gregor98339b92011-08-25 20:47:51 +0000673std::pair<unsigned, unsigned>
674ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
675 using namespace clang::io;
676 unsigned KeyLen = ReadUnalignedLE16(d);
677 unsigned DataLen = ReadUnalignedLE16(d);
678 return std::make_pair(KeyLen, DataLen);
679}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000680
Douglas Gregor98339b92011-08-25 20:47:51 +0000681ASTDeclContextNameLookupTrait::internal_key_type
682ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
683 using namespace clang::io;
684
685 DeclNameKey Key;
686 Key.Kind = (DeclarationName::NameKind)*d++;
687 switch (Key.Kind) {
688 case DeclarationName::Identifier:
689 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
690 break;
691 case DeclarationName::ObjCZeroArgSelector:
692 case DeclarationName::ObjCOneArgSelector:
693 case DeclarationName::ObjCMultiArgSelector:
694 Key.Data =
695 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
696 .getAsOpaquePtr();
697 break;
698 case DeclarationName::CXXOperatorName:
699 Key.Data = *d++; // OverloadedOperatorKind
700 break;
701 case DeclarationName::CXXLiteralOperatorName:
702 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
703 break;
704 case DeclarationName::CXXConstructorName:
705 case DeclarationName::CXXDestructorName:
706 case DeclarationName::CXXConversionFunctionName:
707 case DeclarationName::CXXUsingDirective:
708 Key.Data = 0;
709 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000710 }
711
Douglas Gregor98339b92011-08-25 20:47:51 +0000712 return Key;
713}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000714
Douglas Gregor98339b92011-08-25 20:47:51 +0000715ASTDeclContextNameLookupTrait::data_type
716ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
717 const unsigned char* d,
Nick Lewyckyb346d2f2012-04-16 02:51:46 +0000718 unsigned DataLen) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000719 using namespace clang::io;
720 unsigned NumDecls = ReadUnalignedLE16(d);
Douglas Gregor9b8b20f2012-01-06 16:09:53 +0000721 LE32DeclID *Start = (LE32DeclID *)d;
Douglas Gregor98339b92011-08-25 20:47:51 +0000722 return std::make_pair(Start, Start + NumDecls);
723}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000724
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000725bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000726 llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000727 const std::pair<uint64_t, uint64_t> &Offsets,
728 DeclContextInfo &Info) {
729 SavedStreamPosition SavedPosition(Cursor);
730 // First the lexical decls.
731 if (Offsets.first != 0) {
732 Cursor.JumpToBit(Offsets.first);
733
734 RecordData Record;
735 const char *Blob;
736 unsigned BlobLen;
737 unsigned Code = Cursor.ReadCode();
738 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
739 if (RecCode != DECL_CONTEXT_LEXICAL) {
740 Error("Expected lexical block");
741 return true;
742 }
743
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000744 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
745 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000746 }
747
748 // Now the lookup table.
749 if (Offsets.second != 0) {
750 Cursor.JumpToBit(Offsets.second);
751
752 RecordData Record;
753 const char *Blob;
754 unsigned BlobLen;
755 unsigned Code = Cursor.ReadCode();
756 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
757 if (RecCode != DECL_CONTEXT_VISIBLE) {
758 Error("Expected visible lookup table block");
759 return true;
760 }
761 Info.NameLookupTableData
762 = ASTDeclContextNameLookupTable::Create(
763 (const unsigned char *)Blob + Record[0],
764 (const unsigned char *)Blob,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000765 ASTDeclContextNameLookupTrait(*this, M));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000766 }
767
768 return false;
769}
770
Chris Lattner5f9e2722011-07-23 10:55:15 +0000771void ASTReader::Error(StringRef Msg) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000772 Error(diag::err_fe_pch_malformed, Msg);
773}
774
775void ASTReader::Error(unsigned DiagID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000776 StringRef Arg1, StringRef Arg2) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000777 if (Diags.isDiagnosticInFlight())
778 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
779 else
780 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000781}
782
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000783/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000784bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000785 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000786 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000787 ActualOriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000788 SuggestedPredefines,
789 FileMgr);
Douglas Gregore721f952009-04-28 18:58:38 +0000790 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000791}
792
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000793//===----------------------------------------------------------------------===//
794// Source Manager Deserialization
795//===----------------------------------------------------------------------===//
796
Douglas Gregorbd945002009-04-13 16:31:14 +0000797/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000798/// \returns true if there was an error.
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000799bool ASTReader::ParseLineTable(ModuleFile &F,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000800 SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000801 unsigned Idx = 0;
802 LineTableInfo &LineTable = SourceMgr.getLineTable();
803
804 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000805 std::map<int, int> FileIDs;
806 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000807 // Extract the file name
808 unsigned FilenameLen = Record[Idx++];
809 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
810 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000811 MaybeAddSystemRootToFilename(Filename);
Jay Foad65aa6882011-06-21 15:13:30 +0000812 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
Douglas Gregorbd945002009-04-13 16:31:14 +0000813 }
814
815 // Parse the line entries
816 std::vector<LineEntry> Entries;
817 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000818 int FID = Record[Idx++];
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000819 assert(FID >= 0 && "Serialized line entries for non-local file.");
820 // Remap FileID from 1-based old view.
821 FID += F.SLocEntryBaseID - 1;
Douglas Gregorbd945002009-04-13 16:31:14 +0000822
823 // Extract the line entries
824 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000825 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000826 Entries.clear();
827 Entries.reserve(NumEntries);
828 for (unsigned I = 0; I != NumEntries; ++I) {
829 unsigned FileOffset = Record[Idx++];
830 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000831 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000832 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000833 = (SrcMgr::CharacteristicKind)Record[Idx++];
834 unsigned IncludeOffset = Record[Idx++];
835 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
836 FileKind, IncludeOffset));
837 }
Douglas Gregor47d9de62012-06-08 16:40:28 +0000838 LineTable.AddEntry(FileID::get(FID), Entries);
Douglas Gregorbd945002009-04-13 16:31:14 +0000839 }
840
841 return false;
842}
843
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000844namespace {
845
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000846class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000847public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000848 const ino_t ino;
849 const dev_t dev;
850 const mode_t mode;
851 const time_t mtime;
852 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000854 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +0000855 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000856};
857
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000858class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000859 public:
860 typedef const char *external_key_type;
861 typedef const char *internal_key_type;
862
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000863 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000864
865 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000866 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000867 }
868
869 static internal_key_type GetInternalKey(const char *path) { return path; }
870
871 static bool EqualKey(internal_key_type a, internal_key_type b) {
872 return strcmp(a, b) == 0;
873 }
874
875 static std::pair<unsigned, unsigned>
876 ReadKeyDataLength(const unsigned char*& d) {
877 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
878 unsigned DataLen = (unsigned) *d++;
879 return std::make_pair(KeyLen + 1, DataLen);
880 }
881
882 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
883 return (const char *)d;
884 }
885
886 static data_type ReadData(const internal_key_type, const unsigned char *d,
887 unsigned /*DataLen*/) {
888 using namespace clang::io;
889
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000890 ino_t ino = (ino_t) ReadUnalignedLE32(d);
891 dev_t dev = (dev_t) ReadUnalignedLE32(d);
892 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000893 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000894 off_t size = (off_t) ReadUnalignedLE64(d);
895 return data_type(ino, dev, mode, mtime, size);
896 }
897};
898
899/// \brief stat() cache for precompiled headers.
900///
901/// This cache is very similar to the stat cache used by pretokenized
902/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +0000903class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000904 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000905 CacheTy *Cache;
906
907 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000908public:
Chris Lattner74e976b2010-11-23 19:28:12 +0000909 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
910 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000911 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
912 Cache = CacheTy::Create(Buckets, Base);
913 }
914
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000915 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Chris Lattner898a0612010-11-23 21:17:56 +0000917 LookupResult getStat(const char *Path, struct stat &StatBuf,
918 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000919 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +0000920 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000921
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000922 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000923 if (I == Cache->end()) {
924 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +0000925 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000926 }
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000928 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000929 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Chris Lattner10e286a2010-11-23 19:19:34 +0000931 StatBuf.st_ino = Data.ino;
932 StatBuf.st_dev = Data.dev;
933 StatBuf.st_mtime = Data.mtime;
934 StatBuf.st_mode = Data.mode;
935 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +0000936 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000937 }
938};
939} // end anonymous namespace
940
941
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000942/// \brief Read a source manager block
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000943ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000944 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000945
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000946 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +0000947
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000948 // Set the source-location entry cursor to the current position in
949 // the stream. This cursor will be used to read the contents of the
950 // source manager block initially, and then lazily read
951 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000952 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000953
954 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000955 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000956 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000957 return Failure;
958 }
959
960 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000961 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000962 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000963 return Failure;
964 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000965
Douglas Gregor14f79002009-04-10 03:52:48 +0000966 RecordData Record;
967 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000968 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +0000969 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000970 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000971 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000972 return Failure;
973 }
Douglas Gregore1d918e2009-04-10 23:10:45 +0000974 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000975 }
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Douglas Gregor14f79002009-04-10 03:52:48 +0000977 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
978 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000979 SLocEntryCursor.ReadSubBlockID();
980 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000981 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000982 return Failure;
983 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000984 continue;
985 }
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Douglas Gregor14f79002009-04-10 03:52:48 +0000987 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000988 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +0000989 continue;
990 }
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Douglas Gregor14f79002009-04-10 03:52:48 +0000992 // Read a record.
993 const char *BlobStart;
994 unsigned BlobLen;
995 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000996 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000997 default: // Default behavior: ignore.
998 break;
999
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001000 case SM_SLOC_FILE_ENTRY:
1001 case SM_SLOC_BUFFER_ENTRY:
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001002 case SM_SLOC_EXPANSION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001003 // Once we hit one of the source location entries, we're done.
1004 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001005 }
1006 }
1007}
1008
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001009/// \brief If a header file is not found at the path that we expect it to be
1010/// and the PCH file was moved from its original location, try to resolve the
1011/// file by assuming that header+PCH were moved together and the header is in
1012/// the same place relative to the PCH.
1013static std::string
1014resolveFileRelativeToOriginalDir(const std::string &Filename,
1015 const std::string &OriginalDir,
1016 const std::string &CurrDir) {
1017 assert(OriginalDir != CurrDir &&
1018 "No point trying to resolve the file if the PCH dir didn't change");
1019 using namespace llvm::sys;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001020 SmallString<128> filePath(Filename);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001021 fs::make_absolute(filePath);
1022 assert(path::is_absolute(OriginalDir));
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001023 SmallString<128> currPCHPath(CurrDir);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001024
1025 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1026 fileDirE = path::end(path::parent_path(filePath));
1027 path::const_iterator origDirI = path::begin(OriginalDir),
1028 origDirE = path::end(OriginalDir);
1029 // Skip the common path components from filePath and OriginalDir.
1030 while (fileDirI != fileDirE && origDirI != origDirE &&
1031 *fileDirI == *origDirI) {
1032 ++fileDirI;
1033 ++origDirI;
1034 }
1035 for (; origDirI != origDirE; ++origDirI)
1036 path::append(currPCHPath, "..");
1037 path::append(currPCHPath, fileDirI, fileDirE);
1038 path::append(currPCHPath, path::filename(Filename));
1039 return currPCHPath.str();
1040}
1041
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001042/// \brief Read in the source location entry with the given ID.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001043ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(int ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001044 if (ID == 0)
1045 return Success;
1046
Douglas Gregor0cdd7982011-07-21 18:46:38 +00001047 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001048 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001049 return Failure;
1050 }
1051
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001052 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001053 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001054 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001055 unsigned BaseOffset = F->SLocEntryBaseOffset;
Sebastian Redl9137a522010-07-16 17:50:48 +00001056
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001057 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001058 unsigned Code = SLocEntryCursor.ReadCode();
1059 if (Code == llvm::bitc::END_BLOCK ||
1060 Code == llvm::bitc::ENTER_SUBBLOCK ||
1061 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001062 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001063 return Failure;
1064 }
1065
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001066 RecordData Record;
1067 const char *BlobStart;
1068 unsigned BlobLen;
1069 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1070 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001071 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001072 return Failure;
1073
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001074 case SM_SLOC_FILE_ENTRY: {
Douglas Gregora081da52011-11-16 20:05:18 +00001075 if (Record.size() < 7) {
1076 Error("source location entry is incorrect");
1077 return Failure;
1078 }
1079
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001080 // We will detect whether a file changed and return 'Failure' for it, but
1081 // we will also try to fail gracefully by setting up the SLocEntry.
1082 ASTReader::ASTReadResult Result = Success;
1083
Douglas Gregora081da52011-11-16 20:05:18 +00001084 bool OverriddenBuffer = Record[6];
1085
1086 std::string OrigFilename(BlobStart, BlobStart + BlobLen);
1087 std::string Filename = OrigFilename;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001088 MaybeAddSystemRootToFilename(Filename);
Douglas Gregora4581a12011-11-17 19:08:51 +00001089 const FileEntry *File =
1090 OverriddenBuffer? FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1091 (time_t)Record[5])
1092 : FileMgr.getFile(Filename, /*OpenFile=*/false);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001093 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1094 OriginalDir != CurrentDir) {
1095 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1096 OriginalDir,
1097 CurrentDir);
1098 if (!resolved.empty())
1099 File = FileMgr.getFile(resolved);
1100 }
Axel Naumann04331162011-01-27 10:55:51 +00001101 if (File == 0)
1102 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1103 (time_t)Record[5]);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001104 if (File == 0) {
1105 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001106 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001107 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001108 Error(ErrorStr.c_str());
1109 return Failure;
1110 }
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001112 if (!DisableValidation &&
1113 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001114#if !defined(LLVM_ON_WIN32)
1115 // In our regression testing, the Windows file system seems to
1116 // have inconsistent modification times that sometimes
1117 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001118 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001119#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001120 )) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +00001121 Error(diag::err_fe_pch_file_modified, Filename);
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001122 Result = Failure;
Douglas Gregor2d52be52010-03-21 22:49:54 +00001123 }
1124
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001125 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001126 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001127 // This is the module's main file.
1128 IncludeLoc = getImportLocation(F);
1129 }
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +00001130 SrcMgr::CharacteristicKind
1131 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1132 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001133 ID, BaseOffset + Record[0]);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001134 SrcMgr::FileInfo &FileInfo =
1135 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
Douglas Gregora081da52011-11-16 20:05:18 +00001136 FileInfo.NumCreatedFIDs = Record[7];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001137 if (Record[3])
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001138 FileInfo.setHasLineDirectives();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001139
Douglas Gregora081da52011-11-16 20:05:18 +00001140 const DeclID *FirstDecl = F->FileSortedDecls + Record[8];
1141 unsigned NumFileDecls = Record[9];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001142 if (NumFileDecls) {
1143 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
Argyrios Kyrtzidis9d128d02011-10-31 07:20:08 +00001144 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1145 NumFileDecls));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001146 }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001147
Douglas Gregor35f9ae62011-11-17 01:44:33 +00001148 const SrcMgr::ContentCache *ContentCache
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +00001149 = SourceMgr.getOrCreateContentCache(File,
1150 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
Douglas Gregor35f9ae62011-11-17 01:44:33 +00001151 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1152 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
Douglas Gregora081da52011-11-16 20:05:18 +00001153 unsigned Code = SLocEntryCursor.ReadCode();
1154 Record.clear();
1155 unsigned RecCode
1156 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1157
1158 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1159 Error("AST record has invalid code");
1160 return Failure;
1161 }
1162
1163 llvm::MemoryBuffer *Buffer
1164 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
1165 Filename);
1166 SourceMgr.overrideFileContents(File, Buffer);
1167 }
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001168
1169 if (Result == Failure)
1170 return Failure;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001171 break;
1172 }
1173
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001174 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001175 const char *Name = BlobStart;
1176 unsigned Offset = Record[0];
1177 unsigned Code = SLocEntryCursor.ReadCode();
1178 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001179 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001180 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001181
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001182 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001183 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001184 return Failure;
1185 }
1186
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001187 llvm::MemoryBuffer *Buffer
Douglas Gregora081da52011-11-16 20:05:18 +00001188 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
1189 Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001190 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID,
1191 BaseOffset + Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001192
Douglas Gregor6236a292011-12-02 21:56:05 +00001193 if (strcmp(Name, "<built-in>") == 0 && F->Kind == MK_PCH) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001194 PCHPredefinesBlock Block = {
1195 BufferID,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001196 StringRef(BlobStart, BlobLen - 1)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001197 };
1198 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001199 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001200
1201 break;
1202 }
1203
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001204 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001205 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruthbf340e42011-07-26 03:03:05 +00001206 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001207 ReadSourceLocation(*F, Record[2]),
1208 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001209 Record[4],
1210 ID,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001211 BaseOffset + Record[0]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001212 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001213 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001214 }
1215
1216 return Success;
1217}
1218
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001219/// \brief Find the location where the module F is imported.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001220SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001221 if (F->ImportLoc.isValid())
1222 return F->ImportLoc;
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001223
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001224 // Otherwise we have a PCH. It's considered to be "imported" at the first
1225 // location of its includer.
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001226 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001227 // Main file is the importer. We assume that it is the first entry in the
1228 // entry table. We can't ask the manager, because at the time of PCH loading
1229 // the main file entry doesn't exist yet.
1230 // The very first entry is the invalid instantiation loc, which takes up
1231 // offsets 0 and 1.
1232 return SourceLocation::getFromRawEncoding(2U);
1233 }
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001234 //return F->Loaders[0]->FirstLoc;
1235 return F->ImportedBy[0]->FirstLoc;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001236}
1237
Chris Lattner6367f6d2009-04-27 01:05:14 +00001238/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1239/// specified cursor. Read the abbreviations that are at the top of the block
1240/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001241bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001242 unsigned BlockID) {
1243 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001244 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001245 return Failure;
1246 }
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Chris Lattner6367f6d2009-04-27 01:05:14 +00001248 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001249 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001250 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001251
Chris Lattner6367f6d2009-04-27 01:05:14 +00001252 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001253 if (Code != llvm::bitc::DEFINE_ABBREV) {
1254 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001255 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001256 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001257 Cursor.ReadAbbrevRecord();
1258 }
1259}
1260
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001261void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001262 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001263
Douglas Gregor37e26842009-04-21 23:56:24 +00001264 // Keep track of where we are in the stream, then jump back there
1265 // after reading this macro.
1266 SavedStreamPosition SavedPosition(Stream);
1267
1268 Stream.JumpToBit(Offset);
1269 RecordData Record;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001270 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001271 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Douglas Gregor37e26842009-04-21 23:56:24 +00001273 while (true) {
1274 unsigned Code = Stream.ReadCode();
1275 switch (Code) {
1276 case llvm::bitc::END_BLOCK:
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001277 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001278
1279 case llvm::bitc::ENTER_SUBBLOCK:
1280 // No known subblocks, always skip them.
1281 Stream.ReadSubBlockID();
1282 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001283 Error("malformed block record in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001284 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001285 }
1286 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001287
Douglas Gregor37e26842009-04-21 23:56:24 +00001288 case llvm::bitc::DEFINE_ABBREV:
1289 Stream.ReadAbbrevRecord();
1290 continue;
1291 default: break;
1292 }
1293
1294 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001295 const char *BlobStart = 0;
1296 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001297 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001298 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001299 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001300 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001301 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001302 case PP_MACRO_OBJECT_LIKE:
1303 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001304 // If we already have a macro, that means that we've hit the end
1305 // of the definition of the macro we were looking for. We're
1306 // done.
1307 if (Macro)
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001308 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001309
Douglas Gregor95eab172011-07-28 20:55:49 +00001310 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001311 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001312 Error("macro must have a name in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001313 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001314 }
Douglas Gregoreee242f2011-10-27 09:33:13 +00001315
Sebastian Redlc3632732010-10-05 15:59:54 +00001316 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001317 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001319 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001320 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001321 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001322
Douglas Gregoraa93a872011-10-17 15:32:29 +00001323 bool IsPublic = Record[3];
1324 unsigned NextIndex = 4;
1325 MI->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex));
Douglas Gregor7143aab2011-09-01 17:04:32 +00001326
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001327 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001328 // Decode function-like macro info.
Douglas Gregor7143aab2011-09-01 17:04:32 +00001329 bool isC99VarArgs = Record[NextIndex++];
1330 bool isGNUVarArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001331 MacroArgs.clear();
Douglas Gregor7143aab2011-09-01 17:04:32 +00001332 unsigned NumArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001333 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregor7143aab2011-09-01 17:04:32 +00001334 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001335
1336 // Install function-like macro info.
1337 MI->setIsFunctionLike();
1338 if (isC99VarArgs) MI->setIsC99Varargs();
1339 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001340 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001341 PP.getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001342 }
1343
1344 // Finally, install the macro.
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001345 PP.setMacroInfo(II, MI, /*LoadedFromAST=*/true);
Douglas Gregor37e26842009-04-21 23:56:24 +00001346
1347 // Remember that we saw this macro last so that we add the tokens that
1348 // form its body to it.
1349 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001350
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001351 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1352 Record[NextIndex]) {
1353 // We have a macro definition. Register the association
1354 PreprocessedEntityID
1355 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1356 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1357 PPRec.RegisterMacroDefinition(Macro,
1358 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001359 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001360
Douglas Gregor37e26842009-04-21 23:56:24 +00001361 ++NumMacrosRead;
1362 break;
1363 }
Mike Stump1eb44332009-09-09 15:08:12 +00001364
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001365 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001366 // If we see a TOKEN before a PP_MACRO_*, then the file is
1367 // erroneous, just pretend we didn't see this.
1368 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001369
Douglas Gregor37e26842009-04-21 23:56:24 +00001370 Token Tok;
1371 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001372 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001373 Tok.setLength(Record[1]);
Douglas Gregor95eab172011-07-28 20:55:49 +00001374 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregor37e26842009-04-21 23:56:24 +00001375 Tok.setIdentifierInfo(II);
1376 Tok.setKind((tok::TokenKind)Record[3]);
1377 Tok.setFlag((Token::TokenFlags)Record[4]);
1378 Macro->AddTokenToBody(Tok);
1379 break;
1380 }
David Blaikie7530c032012-01-17 06:56:22 +00001381 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001382 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001383}
1384
Douglas Gregor86c67d82011-07-28 22:39:26 +00001385PreprocessedEntityID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001386ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
Argyrios Kyrtzidis1f6d2252011-09-19 20:40:02 +00001387 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001388 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1389 assert(I != M.PreprocessedEntityRemap.end()
1390 && "Invalid index into preprocessed entity index remap");
1391
1392 return LocalID + I->second;
Douglas Gregor86c67d82011-07-28 22:39:26 +00001393}
1394
Douglas Gregor98339b92011-08-25 20:47:51 +00001395unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1396 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001397}
Douglas Gregor98339b92011-08-25 20:47:51 +00001398
1399HeaderFileInfoTrait::internal_key_type
1400HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1401
1402bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1403 if (strcmp(a, b) == 0)
1404 return true;
1405
1406 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1407 return false;
Douglas Gregor99a922b2011-12-09 16:22:07 +00001408
1409 // Determine whether the actual files are equivalent.
1410 bool Result = false;
1411 if (llvm::sys::fs::equivalent(a, b, Result))
Douglas Gregor98339b92011-08-25 20:47:51 +00001412 return false;
1413
Douglas Gregor99a922b2011-12-09 16:22:07 +00001414 return Result;
Douglas Gregor98339b92011-08-25 20:47:51 +00001415}
1416
1417std::pair<unsigned, unsigned>
1418HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1419 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1420 unsigned DataLen = (unsigned) *d++;
1421 return std::make_pair(KeyLen + 1, DataLen);
1422}
1423
1424HeaderFileInfoTrait::data_type
1425HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1426 unsigned DataLen) {
1427 const unsigned char *End = d + DataLen;
1428 using namespace clang::io;
1429 HeaderFileInfo HFI;
1430 unsigned Flags = *d++;
1431 HFI.isImport = (Flags >> 5) & 0x01;
1432 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1433 HFI.DirInfo = (Flags >> 2) & 0x03;
1434 HFI.Resolved = (Flags >> 1) & 0x01;
1435 HFI.IndexHeaderMapHeader = Flags & 0x01;
1436 HFI.NumIncludes = ReadUnalignedLE16(d);
Douglas Gregor541ba162011-10-17 18:53:12 +00001437 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1438 ReadUnalignedLE32(d));
Douglas Gregor98339b92011-08-25 20:47:51 +00001439 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1440 // The framework offset is 1 greater than the actual offset,
1441 // since 0 is used as an indicator for "no framework name".
1442 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1443 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1444 }
1445
1446 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1447 (void)End;
1448
1449 // This HeaderFileInfo was externally loaded.
1450 HFI.External = true;
1451 return HFI;
1452}
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001453
Douglas Gregor13292642011-12-02 15:45:10 +00001454void ASTReader::setIdentifierIsMacro(IdentifierInfo *II, ModuleFile &F,
1455 uint64_t LocalOffset, bool Visible) {
1456 if (Visible) {
1457 // Note that this identifier has a macro definition.
1458 II->setHasMacroDefinition(true);
1459 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001460
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001461 // Adjust the offset to a global offset.
Douglas Gregor2d2689a2011-07-28 21:16:51 +00001462 UnreadMacroRecordOffsets[II] = F.GlobalBitOffset + LocalOffset;
Douglas Gregor295a2a62010-10-30 00:23:06 +00001463}
1464
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001465void ASTReader::ReadDefinedMacros() {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001466 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1467 E = ModuleMgr.rend(); I != E; ++I) {
1468 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001469
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001470 // If there was no preprocessor block, skip this file.
1471 if (!MacroCursor.getBitStreamReader())
1472 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001473
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001474 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001475 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001476
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001477 RecordData Record;
1478 while (true) {
1479 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001480 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001481 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001482
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001483 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1484 // No known subblocks, always skip them.
1485 Cursor.ReadSubBlockID();
1486 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001487 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001488 return;
1489 }
1490 continue;
1491 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001492
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001493 if (Code == llvm::bitc::DEFINE_ABBREV) {
1494 Cursor.ReadAbbrevRecord();
1495 continue;
1496 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001497
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001498 // Read a record.
1499 const char *BlobStart;
1500 unsigned BlobLen;
1501 Record.clear();
1502 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1503 default: // Default behavior: ignore.
1504 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001505
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001506 case PP_MACRO_OBJECT_LIKE:
1507 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregor95eab172011-07-28 20:55:49 +00001508 getLocalIdentifier(**I, Record[0]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001509 break;
1510
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001511 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001512 // Ignore tokens.
1513 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001514 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001515 }
1516 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001517
1518 // Drain the unread macro-record offsets map.
1519 while (!UnreadMacroRecordOffsets.empty())
1520 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1521}
1522
1523void ASTReader::LoadMacroDefinition(
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001524 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
Douglas Gregor295a2a62010-10-30 00:23:06 +00001525 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
Douglas Gregor295a2a62010-10-30 00:23:06 +00001526 uint64_t Offset = Pos->second;
1527 UnreadMacroRecordOffsets.erase(Pos);
1528
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001529 RecordLocation Loc = getLocalBitOffset(Offset);
1530 ReadMacroRecord(*Loc.F, Loc.Offset);
Douglas Gregor295a2a62010-10-30 00:23:06 +00001531}
1532
1533void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1534 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1535 = UnreadMacroRecordOffsets.find(II);
1536 LoadMacroDefinition(Pos);
Douglas Gregor88a35862010-01-04 19:18:44 +00001537}
1538
Douglas Gregoreee242f2011-10-27 09:33:13 +00001539namespace {
1540 /// \brief Visitor class used to look up identifirs in an AST file.
1541 class IdentifierLookupVisitor {
1542 StringRef Name;
Douglas Gregor057df202012-01-18 20:56:22 +00001543 unsigned PriorGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001544 IdentifierInfo *Found;
1545 public:
Douglas Gregor057df202012-01-18 20:56:22 +00001546 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration)
1547 : Name(Name), PriorGeneration(PriorGeneration), Found() { }
Douglas Gregoreee242f2011-10-27 09:33:13 +00001548
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001549 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00001550 IdentifierLookupVisitor *This
1551 = static_cast<IdentifierLookupVisitor *>(UserData);
1552
Douglas Gregor057df202012-01-18 20:56:22 +00001553 // If we've already searched this module file, skip it now.
1554 if (M.Generation <= This->PriorGeneration)
1555 return true;
1556
Douglas Gregoreee242f2011-10-27 09:33:13 +00001557 ASTIdentifierLookupTable *IdTable
1558 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1559 if (!IdTable)
1560 return false;
1561
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001562 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1563 M, This->Found);
1564
Douglas Gregoreee242f2011-10-27 09:33:13 +00001565 std::pair<const char*, unsigned> Key(This->Name.begin(),
1566 This->Name.size());
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001567 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Trait);
Douglas Gregoreee242f2011-10-27 09:33:13 +00001568 if (Pos == IdTable->end())
1569 return false;
1570
1571 // Dereferencing the iterator has the effect of building the
1572 // IdentifierInfo node and populating it with the various
1573 // declarations it needs.
1574 This->Found = *Pos;
1575 return true;
1576 }
1577
1578 // \brief Retrieve the identifier info found within the module
1579 // files.
1580 IdentifierInfo *getIdentifierInfo() const { return Found; }
1581 };
1582}
1583
1584void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
Douglas Gregor057df202012-01-18 20:56:22 +00001585 unsigned PriorGeneration = 0;
David Blaikie4e4d0842012-03-11 07:00:24 +00001586 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001587 PriorGeneration = IdentifierGeneration[&II];
1588
1589 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration);
1590 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
1591 markIdentifierUpToDate(&II);
1592}
1593
1594void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1595 if (!II)
1596 return;
1597
1598 II->setOutOfDate(false);
1599
1600 // Update the generation for this identifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001601 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001602 IdentifierGeneration[II] = CurrentGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001603}
1604
Chris Lattner5f9e2722011-07-23 10:55:15 +00001605const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001606 std::string Filename = filenameStrRef;
1607 MaybeAddSystemRootToFilename(Filename);
1608 const FileEntry *File = FileMgr.getFile(Filename);
1609 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1610 OriginalDir != CurrentDir) {
1611 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1612 OriginalDir,
1613 CurrentDir);
1614 if (!resolved.empty())
1615 File = FileMgr.getFile(resolved);
1616 }
1617
1618 return File;
1619}
1620
Douglas Gregore650c8c2009-07-07 00:12:59 +00001621/// \brief If we are loading a relocatable PCH file, and the filename is
1622/// not an absolute path, add the system root to the beginning of the file
1623/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001624void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001625 // If this is not a relocatable PCH file, there's nothing to do.
1626 if (!RelocatablePCH)
1627 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Michael J. Spencer256053b2010-12-17 21:22:22 +00001629 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregore650c8c2009-07-07 00:12:59 +00001630 return;
1631
Douglas Gregor832d6202011-07-22 16:35:34 +00001632 if (isysroot.empty()) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001633 // If no system root was given, default to '/'
1634 Filename.insert(Filename.begin(), '/');
1635 return;
1636 }
Mike Stump1eb44332009-09-09 15:08:12 +00001637
Douglas Gregor832d6202011-07-22 16:35:34 +00001638 unsigned Length = isysroot.size();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001639 if (isysroot[Length - 1] != '/')
1640 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001641
Douglas Gregor832d6202011-07-22 16:35:34 +00001642 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregore650c8c2009-07-07 00:12:59 +00001643}
1644
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001645ASTReader::ASTReadResult
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001646ASTReader::ReadASTBlock(ModuleFile &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001647 llvm::BitstreamCursor &Stream = F.Stream;
1648
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001649 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001650 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001651 return Failure;
1652 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001653
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001654 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001655 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001656 while (!Stream.AtEndOfStream()) {
1657 unsigned Code = Stream.ReadCode();
1658 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001659 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001660 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001661 return Failure;
1662 }
Chris Lattner7356a312009-04-11 21:15:38 +00001663
Argyrios Kyrtzidis1f941242012-09-21 01:30:00 +00001664 DeclContext *DC = Context.getTranslationUnitDecl();
1665 if (!DC->hasExternalVisibleStorage() && DC->hasExternalLexicalStorage())
1666 DC->setMustBuildLookupTable();
1667
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001668 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001669 }
1670
1671 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1672 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001673 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001674 // We lazily load the decls block, but we want to set up the
1675 // DeclsCursor cursor to point into it. Clone our current bitcode
1676 // cursor to it, enter the block and read the abbrevs in that block.
1677 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001678 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001679 if (Stream.SkipBlock() || // Skip with the main cursor.
1680 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001681 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001682 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001683 return Failure;
1684 }
1685 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001686
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001687 case DECL_UPDATES_BLOCK_ID:
1688 if (Stream.SkipBlock()) {
1689 Error("malformed block record in AST file");
1690 return Failure;
1691 }
1692 break;
1693
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001694 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001695 F.MacroCursor = Stream;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001696 if (!PP.getExternalSource())
1697 PP.setExternalSource(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00001698
Douglas Gregorecdcb882010-10-20 22:00:55 +00001699 if (Stream.SkipBlock() ||
1700 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001701 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001702 return Failure;
1703 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001704 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001705 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001706
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001707 case PREPROCESSOR_DETAIL_BLOCK_ID:
1708 F.PreprocessorDetailCursor = Stream;
1709 if (Stream.SkipBlock() ||
1710 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1711 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1712 Error("malformed preprocessor detail record in AST file");
1713 return Failure;
1714 }
1715 F.PreprocessorDetailStartOffset
1716 = F.PreprocessorDetailCursor.GetCurrentBitNo();
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001717
1718 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00001719 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001720 if (!PP.getPreprocessingRecord()->getExternalSource())
1721 PP.getPreprocessingRecord()->SetExternalSource(*this);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001722 break;
1723
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001724 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001725 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001726 case Success:
1727 break;
1728
1729 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001730 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001731 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001732
1733 case IgnorePCH:
1734 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001735 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001736 break;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001737
1738 case SUBMODULE_BLOCK_ID:
1739 switch (ReadSubmoduleBlock(F)) {
1740 case Success:
1741 break;
1742
1743 case Failure:
1744 Error("malformed submodule block in AST file");
1745 return Failure;
1746
1747 case IgnorePCH:
1748 return IgnorePCH;
1749 }
1750 break;
1751
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00001752 case COMMENTS_BLOCK_ID: {
1753 llvm::BitstreamCursor C = Stream;
1754 if (Stream.SkipBlock() ||
1755 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
1756 Error("malformed comments block in AST file");
1757 return Failure;
1758 }
1759 CommentsCursors.push_back(std::make_pair(C, &F));
1760 break;
1761 }
1762
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001763 default:
1764 if (!Stream.SkipBlock())
1765 break;
1766 Error("malformed block record in AST file");
1767 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001768 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001769 continue;
1770 }
1771
1772 if (Code == llvm::bitc::DEFINE_ABBREV) {
1773 Stream.ReadAbbrevRecord();
1774 continue;
1775 }
1776
1777 // Read and process a record.
1778 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001779 const char *BlobStart = 0;
1780 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001781 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001782 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001783 default: // Default behavior: ignore.
1784 break;
1785
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001786 case METADATA: {
1787 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1788 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001789 : diag::warn_pch_version_too_new);
1790 return IgnorePCH;
1791 }
1792
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001793 bool hasErrors = Record[5];
1794 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1795 Diag(diag::err_pch_with_compiler_errors);
1796 return IgnorePCH;
1797 }
1798
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001799 RelocatablePCH = Record[4];
1800 if (Listener) {
1801 std::string TargetTriple(BlobStart, BlobLen);
1802 if (Listener->ReadTargetTriple(TargetTriple))
1803 return IgnorePCH;
1804 }
1805 break;
1806 }
1807
Douglas Gregore95b9192011-08-17 21:07:30 +00001808 case IMPORTS: {
1809 // Load each of the imported PCH files.
1810 unsigned Idx = 0, N = Record.size();
1811 while (Idx < N) {
1812 // Read information about the AST file.
1813 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1814 unsigned Length = Record[Idx++];
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001815 SmallString<128> ImportedFile(Record.begin() + Idx,
Douglas Gregore95b9192011-08-17 21:07:30 +00001816 Record.begin() + Idx + Length);
1817 Idx += Length;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001818
Douglas Gregore95b9192011-08-17 21:07:30 +00001819 // Load the AST file.
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001820 switch(ReadASTCore(ImportedFile, ImportedKind, &F)) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001821 case Failure: return Failure;
1822 // If we have to ignore the dependency, we'll have to ignore this too.
1823 case IgnorePCH: return IgnorePCH;
1824 case Success: break;
1825 }
1826 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001827 break;
1828 }
1829
Douglas Gregora119da02011-08-02 16:26:37 +00001830 case TYPE_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001831 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001832 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001833 return Failure;
1834 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001835 F.TypeOffsets = (const uint32_t *)BlobStart;
1836 F.LocalNumTypes = Record[0];
Douglas Gregore3605012011-08-02 18:32:54 +00001837 unsigned LocalBaseTypeIndex = Record[1];
1838 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor1e849b62011-07-29 00:21:44 +00001839
Douglas Gregora119da02011-08-02 16:26:37 +00001840 if (F.LocalNumTypes > 0) {
1841 // Introduce the global -> local mapping for types within this module.
1842 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
1843
1844 // Introduce the local -> global mapping for types within this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001845 F.TypeRemap.insertOrReplace(
1846 std::make_pair(LocalBaseTypeIndex,
1847 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregora119da02011-08-02 16:26:37 +00001848
1849 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
1850 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001851 break;
Douglas Gregora119da02011-08-02 16:26:37 +00001852 }
1853
Douglas Gregor496c7092011-08-03 15:48:04 +00001854 case DECL_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001855 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001856 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001857 return Failure;
1858 }
Argyrios Kyrtzidis9d31fa72011-10-27 18:47:35 +00001859 F.DeclOffsets = (const DeclOffset *)BlobStart;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001860 F.LocalNumDecls = Record[0];
Douglas Gregor496c7092011-08-03 15:48:04 +00001861 unsigned LocalBaseDeclID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001862 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor96e973f2011-07-20 00:27:43 +00001863
Douglas Gregor496c7092011-08-03 15:48:04 +00001864 if (F.LocalNumDecls > 0) {
1865 // Introduce the global -> local mapping for declarations within this
1866 // module.
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001867 GlobalDeclMap.insert(
1868 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregor496c7092011-08-03 15:48:04 +00001869
1870 // Introduce the local -> global mapping for declarations within this
1871 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001872 F.DeclRemap.insertOrReplace(
1873 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
Douglas Gregor496c7092011-08-03 15:48:04 +00001874
Douglas Gregora1be2782011-12-17 23:38:30 +00001875 // Introduce the global -> local mapping for declarations within this
1876 // module.
1877 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
1878
Douglas Gregor496c7092011-08-03 15:48:04 +00001879 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
1880 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001881 break;
Douglas Gregor496c7092011-08-03 15:48:04 +00001882 }
1883
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001884 case TU_UPDATE_LEXICAL: {
Douglas Gregor35942772011-09-09 21:34:22 +00001885 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001886 DeclContextInfo &Info = F.DeclContextInfos[TU];
1887 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
1888 Info.NumLexicalDecls
1889 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregor35942772011-09-09 21:34:22 +00001890 TU->setHasExternalLexicalStorage(true);
Sebastian Redld692af72010-07-27 18:24:41 +00001891 break;
1892 }
1893
Sebastian Redle1dde812010-08-24 00:50:04 +00001894 case UPDATE_VISIBLE: {
Douglas Gregor496c7092011-08-03 15:48:04 +00001895 unsigned Idx = 0;
1896 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Benjamin Kramerb1758c62012-04-15 12:36:49 +00001897 ASTDeclContextNameLookupTable *Table =
1898 ASTDeclContextNameLookupTable::Create(
Douglas Gregor496c7092011-08-03 15:48:04 +00001899 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redle1dde812010-08-24 00:50:04 +00001900 (const unsigned char *)BlobStart,
Douglas Gregor393f2492011-07-22 00:38:23 +00001901 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregor35942772011-09-09 21:34:22 +00001902 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
1903 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001904 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001905 TU->setHasExternalVisibleStorage(true);
Sebastian Redle1dde812010-08-24 00:50:04 +00001906 } else
Douglas Gregor496c7092011-08-03 15:48:04 +00001907 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redle1dde812010-08-24 00:50:04 +00001908 break;
1909 }
1910
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001911 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001912 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001913 return IgnorePCH;
1914 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001915
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001916 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001917 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001918 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001919 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001920 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001921 (const unsigned char *)F.IdentifierTableData + Record[0],
1922 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00001923 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001924
1925 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001926 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001927 break;
1928
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001929 case IDENTIFIER_OFFSET: {
Sebastian Redl2da08f92010-07-19 22:28:42 +00001930 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001931 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001932 return Failure;
1933 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001934 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1935 F.LocalNumIdentifiers = Record[0];
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001936 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001937 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor67268d02011-07-20 00:59:32 +00001938
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001939 if (F.LocalNumIdentifiers > 0) {
1940 // Introduce the global -> local mapping for identifiers within this
1941 // module.
1942 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
1943 &F));
1944
1945 // Introduce the local -> global mapping for identifiers within this
1946 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001947 F.IdentifierRemap.insertOrReplace(
1948 std::make_pair(LocalBaseIdentifierID,
1949 F.BaseIdentifierID - LocalBaseIdentifierID));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001950
1951 IdentifiersLoaded.resize(IdentifiersLoaded.size()
1952 + F.LocalNumIdentifiers);
1953 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001954 break;
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001955 }
1956
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001957 case EXTERNAL_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001958 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1959 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorfdd01722009-04-14 00:24:19 +00001960 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001961
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001962 case SPECIAL_TYPES:
Douglas Gregor393f2492011-07-22 00:38:23 +00001963 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1964 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregorad1de002009-04-18 05:55:16 +00001965 break;
1966
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001967 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001968 TotalNumStatements += Record[0];
1969 TotalNumMacros += Record[1];
1970 TotalLexicalDeclContexts += Record[2];
1971 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001972 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001973
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001974 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001975 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1976 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001977 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001978
Sean Huntebcbe1d2011-05-04 23:29:54 +00001979 case DELEGATING_CTORS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001980 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1981 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00001982 break;
1983
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001984 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor31e37b22011-07-28 18:09:57 +00001985 if (Record.size() % 4 != 0) {
1986 Error("invalid weak identifiers record");
1987 return Failure;
1988 }
1989
1990 // FIXME: Ignore weak undeclared identifiers from non-original PCH
1991 // files. This isn't the way to do it :)
1992 WeakUndeclaredIdentifiers.clear();
1993
1994 // Translate the weak, undeclared identifiers into global IDs.
1995 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
1996 WeakUndeclaredIdentifiers.push_back(
1997 getGlobalIdentifierID(F, Record[I++]));
1998 WeakUndeclaredIdentifiers.push_back(
1999 getGlobalIdentifierID(F, Record[I++]));
2000 WeakUndeclaredIdentifiers.push_back(
2001 ReadSourceLocation(F, Record, I).getRawEncoding());
2002 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2003 }
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002004 break;
2005
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002006 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002007 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2008 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor14c22f22009-04-22 22:18:58 +00002009 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002010
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002011 case SELECTOR_OFFSETS: {
Sebastian Redl059612d2010-08-03 21:58:15 +00002012 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00002013 F.LocalNumSelectors = Record[0];
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002014 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002015 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor96958cb2011-07-20 01:10:58 +00002016
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002017 if (F.LocalNumSelectors > 0) {
2018 // Introduce the global -> local mapping for selectors within this
2019 // module.
2020 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2021
2022 // Introduce the local -> global mapping for selectors within this
2023 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002024 F.SelectorRemap.insertOrReplace(
2025 std::make_pair(LocalBaseSelectorID,
2026 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor83941df2009-04-25 17:48:32 +00002027
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002028 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2029 }
2030 break;
2031 }
2032
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002033 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002034 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002035 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002036 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002037 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002038 F.SelectorLookupTableData + Record[0],
2039 F.SelectorLookupTableData,
Douglas Gregor409448c2011-07-21 22:35:25 +00002040 ASTSelectorLookupTrait(*this, F));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002041 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002042 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002043
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002044 case REFERENCED_SELECTOR_POOL:
Douglas Gregor8451ec72011-07-28 14:41:43 +00002045 if (!Record.empty()) {
2046 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2047 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2048 Record[Idx++]));
2049 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2050 getRawEncoding());
2051 }
2052 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00002053 break;
2054
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002055 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002056 if (!Record.empty() && Listener)
2057 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002058 break;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002059
2060 case FILE_SORTED_DECLS:
2061 F.FileSortedDecls = (const DeclID *)BlobStart;
2062 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002063
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002064 case SOURCE_LOCATION_OFFSETS: {
2065 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002066 F.LocalNumSLocEntries = Record[0];
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002067 unsigned SLocSpaceSize = Record[1];
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002068 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002069 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2070 SLocSpaceSize);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002071 // Make our entry in the range map. BaseID is negative and growing, so
2072 // we invert it. Because we invert it, though, we need the other end of
2073 // the range.
2074 unsigned RangeStart =
2075 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2076 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2077 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2078
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002079 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2080 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2081 GlobalSLocOffsetMap.insert(
2082 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2083 - SLocSpaceSize,&F));
2084
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002085 // Initialize the remapping table.
2086 // Invalid stays invalid.
2087 F.SLocRemap.insert(std::make_pair(0U, 0));
2088 // This module. Base was 2 when being compiled.
2089 F.SLocRemap.insert(std::make_pair(2U,
2090 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002091
2092 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002093 break;
2094 }
2095
Douglas Gregor5d51a1d2011-08-01 16:01:55 +00002096 case MODULE_OFFSET_MAP: {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002097 // Additional remapping information.
2098 const unsigned char *Data = (const unsigned char*)BlobStart;
2099 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregorf33740e2011-08-02 10:56:51 +00002100
2101 // Continuous range maps we may be updating in our module.
2102 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002103 ContinuousRangeMap<uint32_t, int, 2>::Builder
2104 IdentifierRemap(F.IdentifierRemap);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002105 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002106 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2107 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor26ced122011-12-01 00:59:36 +00002108 SubmoduleRemap(F.SubmoduleRemap);
2109 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002110 SelectorRemap(F.SelectorRemap);
Douglas Gregor496c7092011-08-03 15:48:04 +00002111 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregora119da02011-08-02 16:26:37 +00002112 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2113
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002114 while(Data < DataEnd) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002115 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002116 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002117 Data += Len;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002118 ModuleFile *OM = ModuleMgr.lookup(Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002119 if (!OM) {
2120 Error("SourceLocation remap refers to unknown module");
2121 return Failure;
2122 }
Douglas Gregorf33740e2011-08-02 10:56:51 +00002123
2124 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2125 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2126 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor26ced122011-12-01 00:59:36 +00002127 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002128 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2129 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora119da02011-08-02 16:26:37 +00002130 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002131
2132 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2133 SLocRemap.insert(std::make_pair(SLocOffset,
2134 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002135 IdentifierRemap.insert(
2136 std::make_pair(IdentifierIDOffset,
2137 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002138 PreprocessedEntityRemap.insert(
2139 std::make_pair(PreprocessedEntityIDOffset,
2140 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregor26ced122011-12-01 00:59:36 +00002141 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2142 OM->BaseSubmoduleID - SubmoduleIDOffset));
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002143 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2144 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregor496c7092011-08-03 15:48:04 +00002145 DeclRemap.insert(std::make_pair(DeclIDOffset,
2146 OM->BaseDeclID - DeclIDOffset));
2147
Douglas Gregora119da02011-08-02 16:26:37 +00002148 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregore3605012011-08-02 18:32:54 +00002149 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregora1be2782011-12-17 23:38:30 +00002150
2151 // Global -> local mappings.
2152 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002153 }
2154 break;
2155 }
2156
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002157 case SOURCE_MANAGER_LINE_TABLE:
2158 if (ParseLineTable(F, Record))
2159 return Failure;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002160 break;
2161
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00002162 case FILE_SOURCE_LOCATION_OFFSETS:
2163 F.SLocFileOffsets = (const uint32_t *)BlobStart;
2164 F.LocalNumSLocFileEntries = Record[0];
2165 break;
2166
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002167 case SOURCE_LOCATION_PRELOADS: {
2168 // Need to transform from the local view (1-based IDs) to the global view,
2169 // which is based off F.SLocEntryBaseID.
Douglas Gregorf249bf32011-08-25 21:09:44 +00002170 if (!F.PreloadSLocEntries.empty()) {
2171 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2172 return Failure;
2173 }
2174
2175 F.PreloadSLocEntries.swap(Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002176 break;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002177 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002178
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002179 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002180 if (!DisableStatCache) {
2181 ASTStatCache *MyStatCache =
2182 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2183 (const unsigned char *)BlobStart,
2184 NumStatHits, NumStatMisses);
2185 FileMgr.addStatCache(MyStatCache);
2186 F.StatCache = MyStatCache;
2187 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002188 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002189 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002190
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002191 case EXT_VECTOR_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002192 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2193 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorb81c1702009-04-27 20:06:05 +00002194 break;
2195
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002196 case VTABLE_USES:
Douglas Gregordfe65432011-07-28 19:11:31 +00002197 if (Record.size() % 3 != 0) {
2198 Error("Invalid VTABLE_USES record");
2199 return Failure;
2200 }
2201
Sebastian Redl40566802010-08-05 18:21:25 +00002202 // Later tables overwrite earlier ones.
Douglas Gregordfe65432011-07-28 19:11:31 +00002203 // FIXME: Modules will have some trouble with this. This is clearly not
2204 // the right way to do this.
Douglas Gregor409448c2011-07-21 22:35:25 +00002205 VTableUses.clear();
Douglas Gregordfe65432011-07-28 19:11:31 +00002206
2207 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2208 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2209 VTableUses.push_back(
2210 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2211 VTableUses.push_back(Record[Idx++]);
2212 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002213 break;
2214
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002215 case DYNAMIC_CLASSES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002216 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2217 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002218 break;
2219
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002220 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorf2abb522011-07-28 19:26:52 +00002221 if (PendingInstantiations.size() % 2 != 0) {
2222 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2223 return Failure;
2224 }
2225
2226 // Later lists of pending instantiations overwrite earlier ones.
2227 // FIXME: This is most certainly wrong for modules.
2228 PendingInstantiations.clear();
2229 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2230 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2231 PendingInstantiations.push_back(
2232 ReadSourceLocation(F, Record, I).getRawEncoding());
2233 }
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002234 break;
2235
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002236 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002237 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002238 // FIXME: Modules will have some trouble with this.
2239 SemaDeclRefs.clear();
2240 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2241 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002242 break;
2243
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002244 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002245 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002246 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002247 ActualOriginalFileName.assign(BlobStart, BlobLen);
2248 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002249 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002250 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002251
Douglas Gregor31d375f2011-05-06 21:43:30 +00002252 case ORIGINAL_FILE_ID:
2253 OriginalFileID = FileID::get(Record[0]);
2254 break;
2255
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002256 case ORIGINAL_PCH_DIR:
2257 // The primary AST will be the last to get here, so it will be the one
2258 // that's used.
2259 OriginalDir.assign(BlobStart, BlobLen);
2260 break;
2261
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002262 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002263 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002264 StringRef ASTBranch(BlobStart, BlobLen);
2265 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002266 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002267 return IgnorePCH;
2268 }
2269 break;
2270 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002271
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002272 case PPD_ENTITIES_OFFSETS: {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002273 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart;
2274 assert(BlobLen % sizeof(PPEntityOffset) == 0);
2275 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002276
2277 unsigned LocalBasePreprocessedEntityID = Record[0];
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002278
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002279 unsigned StartingID;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002280 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00002281 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002282 if (!PP.getPreprocessingRecord()->getExternalSource())
2283 PP.getPreprocessingRecord()->SetExternalSource(*this);
2284 StartingID
2285 = PP.getPreprocessingRecord()
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002286 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Douglas Gregor9827a802011-07-29 00:56:45 +00002287 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002288
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002289 if (F.NumPreprocessedEntities > 0) {
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002290 // Introduce the global -> local mapping for preprocessed entities in
2291 // this module.
2292 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2293
2294 // Introduce the local -> global mapping for preprocessed entities in
2295 // this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002296 F.PreprocessedEntityRemap.insertOrReplace(
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002297 std::make_pair(LocalBasePreprocessedEntityID,
2298 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2299 }
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002300
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002301 break;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002302 }
2303
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002304 case DECL_UPDATE_OFFSETS: {
2305 if (Record.size() % 2 != 0) {
2306 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2307 return Failure;
2308 }
2309 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregor496c7092011-08-03 15:48:04 +00002310 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2311 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002312 break;
2313 }
2314
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002315 case DECL_REPLACEMENTS: {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002316 if (Record.size() % 3 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002317 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002318 return Failure;
2319 }
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002320 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
Douglas Gregor496c7092011-08-03 15:48:04 +00002321 ReplacedDecls[getGlobalDeclID(F, Record[I])]
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002322 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002323 break;
2324 }
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002325
Douglas Gregorcff9f262012-01-27 01:47:08 +00002326 case OBJC_CATEGORIES_MAP: {
2327 if (F.LocalNumObjCCategoriesInMap != 0) {
2328 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002329 return Failure;
2330 }
Douglas Gregorcff9f262012-01-27 01:47:08 +00002331
2332 F.LocalNumObjCCategoriesInMap = Record[0];
2333 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)BlobStart;
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002334 break;
2335 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002336
Douglas Gregorcff9f262012-01-27 01:47:08 +00002337 case OBJC_CATEGORIES:
2338 F.ObjCCategories.swap(Record);
2339 break;
2340
Douglas Gregor7c789c12010-10-29 22:39:52 +00002341 case CXX_BASE_SPECIFIER_OFFSETS: {
2342 if (F.LocalNumCXXBaseSpecifiers != 0) {
2343 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2344 return Failure;
2345 }
2346
2347 F.LocalNumCXXBaseSpecifiers = Record[0];
2348 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner1da90142011-07-21 21:15:19 +00002349 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002350 break;
2351 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002352
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002353 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002354 if (Record.size() % 2 != 0) {
2355 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2356 return Failure;
2357 }
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002358
2359 if (F.PragmaDiagMappings.empty())
2360 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002361 else
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002362 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2363 Record.begin(), Record.end());
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002364 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002365
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002366 case CUDA_SPECIAL_DECL_REFS:
2367 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002368 // FIXME: Modules will have trouble with this.
2369 CUDASpecialDeclRefs.clear();
2370 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2371 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002372 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002373
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002374 case HEADER_SEARCH_TABLE: {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002375 F.HeaderFileInfoTableData = BlobStart;
2376 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002377 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002378 if (Record[0]) {
2379 F.HeaderFileInfoTable
2380 = HeaderFileInfoLookupTable::Create(
2381 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002382 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregor95eab172011-07-28 20:55:49 +00002383 HeaderFileInfoTrait(*this, F,
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002384 &PP.getHeaderSearchInfo(),
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002385 BlobStart + Record[2]));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002386
2387 PP.getHeaderSearchInfo().SetExternalSource(this);
2388 if (!PP.getHeaderSearchInfo().getExternalLookup())
2389 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002390 }
2391 break;
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002392 }
2393
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002394 case FP_PRAGMA_OPTIONS:
2395 // Later tables overwrite earlier ones.
2396 FPPragmaOptions.swap(Record);
2397 break;
2398
2399 case OPENCL_EXTENSIONS:
2400 // Later tables overwrite earlier ones.
2401 OpenCLExtensions.swap(Record);
2402 break;
Sean Huntebcbe1d2011-05-04 23:29:54 +00002403
2404 case TENTATIVE_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002405 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2406 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002407 break;
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002408
2409 case KNOWN_NAMESPACES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002410 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2411 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002412 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002413
2414 case IMPORTED_MODULES: {
2415 if (F.Kind != MK_Module) {
2416 // If we aren't loading a module (which has its own exports), make
2417 // all of the imported modules visible.
2418 // FIXME: Deal with macros-only imports.
2419 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2420 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2421 ImportedModules.push_back(GlobalID);
2422 }
2423 }
2424 break;
Douglas Gregora1be2782011-12-17 23:38:30 +00002425 }
Douglas Gregor2171bf12012-01-15 16:58:34 +00002426
Douglas Gregora1be2782011-12-17 23:38:30 +00002427 case LOCAL_REDECLARATIONS: {
Douglas Gregor2171bf12012-01-15 16:58:34 +00002428 F.RedeclarationChains.swap(Record);
2429 break;
2430 }
2431
2432 case LOCAL_REDECLARATIONS_MAP: {
2433 if (F.LocalNumRedeclarationsInMap != 0) {
2434 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
Douglas Gregora1be2782011-12-17 23:38:30 +00002435 return Failure;
2436 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00002437
Douglas Gregor2171bf12012-01-15 16:58:34 +00002438 F.LocalNumRedeclarationsInMap = Record[0];
2439 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)BlobStart;
Douglas Gregora1be2782011-12-17 23:38:30 +00002440 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002441 }
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00002442
2443 case MERGED_DECLARATIONS: {
2444 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2445 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2446 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2447 for (unsigned N = Record[Idx++]; N > 0; --N)
2448 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2449 }
2450 break;
2451 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002452 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002453 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002454 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002455 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002456}
2457
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002458ASTReader::ASTReadResult ASTReader::validateFileEntries(ModuleFile &M) {
Douglas Gregorc69a2922011-08-25 20:58:51 +00002459 llvm::BitstreamCursor &SLocEntryCursor = M.SLocEntryCursor;
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002460
Douglas Gregorc69a2922011-08-25 20:58:51 +00002461 for (unsigned i = 0, e = M.LocalNumSLocFileEntries; i != e; ++i) {
2462 SLocEntryCursor.JumpToBit(M.SLocFileOffsets[i]);
2463 unsigned Code = SLocEntryCursor.ReadCode();
2464 if (Code == llvm::bitc::END_BLOCK ||
2465 Code == llvm::bitc::ENTER_SUBBLOCK ||
2466 Code == llvm::bitc::DEFINE_ABBREV) {
2467 Error("incorrectly-formatted source location entry in AST file");
2468 return Failure;
2469 }
2470
2471 RecordData Record;
2472 const char *BlobStart;
2473 unsigned BlobLen;
2474 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2475 default:
2476 Error("incorrectly-formatted source location entry in AST file");
2477 return Failure;
2478
2479 case SM_SLOC_FILE_ENTRY: {
Douglas Gregora081da52011-11-16 20:05:18 +00002480 // If the buffer was overridden, the file need not exist.
2481 if (Record[6])
2482 break;
2483
Douglas Gregorc69a2922011-08-25 20:58:51 +00002484 StringRef Filename(BlobStart, BlobLen);
2485 const FileEntry *File = getFileEntry(Filename);
2486
2487 if (File == 0) {
2488 std::string ErrorStr = "could not find file '";
2489 ErrorStr += Filename;
2490 ErrorStr += "' referenced by AST file";
2491 Error(ErrorStr.c_str());
2492 return IgnorePCH;
2493 }
2494
Douglas Gregora081da52011-11-16 20:05:18 +00002495 if (Record.size() < 7) {
Douglas Gregorc69a2922011-08-25 20:58:51 +00002496 Error("source location entry is incorrect");
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002497 return Failure;
2498 }
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002499
2500 off_t StoredSize = (off_t)Record[4];
2501 time_t StoredTime = (time_t)Record[5];
2502
2503 // Check if there was a request to override the contents of the file
2504 // that was part of the precompiled header. Overridding such a file
2505 // can lead to problems when lexing using the source locations from the
2506 // PCH.
2507 SourceManager &SM = getSourceManager();
2508 if (SM.isFileOverridden(File)) {
2509 Error(diag::err_fe_pch_file_overridden, Filename);
2510 // After emitting the diagnostic, recover by disabling the override so
2511 // that the original file will be used.
2512 SM.disableFileContentsOverride(File);
2513 // The FileEntry is a virtual file entry with the size of the contents
2514 // that would override the original contents. Set it to the original's
2515 // size/time.
2516 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2517 StoredSize, StoredTime);
2518 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002519
Douglas Gregorc69a2922011-08-25 20:58:51 +00002520 // The stat info from the FileEntry came from the cached stat
2521 // info of the PCH, so we cannot trust it.
2522 struct stat StatBuf;
2523 if (::stat(File->getName(), &StatBuf) != 0) {
2524 StatBuf.st_size = File->getSize();
2525 StatBuf.st_mtime = File->getModificationTime();
2526 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002527
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002528 if ((StoredSize != StatBuf.st_size
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002529#if !defined(LLVM_ON_WIN32)
Douglas Gregorc69a2922011-08-25 20:58:51 +00002530 // In our regression testing, the Windows file system seems to
2531 // have inconsistent modification times that sometimes
2532 // erroneously trigger this error-handling path.
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002533 || StoredTime != StatBuf.st_mtime
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002534#endif
Douglas Gregorc69a2922011-08-25 20:58:51 +00002535 )) {
2536 Error(diag::err_fe_pch_file_modified, Filename);
2537 return IgnorePCH;
2538 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002539
Douglas Gregorc69a2922011-08-25 20:58:51 +00002540 break;
2541 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002542 }
2543 }
2544
2545 return Success;
2546}
2547
Douglas Gregorecc2c092011-12-01 22:20:10 +00002548void ASTReader::makeNamesVisible(const HiddenNames &Names) {
Douglas Gregor13292642011-12-02 15:45:10 +00002549 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
2550 if (Decl *D = Names[I].dyn_cast<Decl *>())
Douglas Gregorf143ffc2012-01-06 16:22:39 +00002551 D->Hidden = false;
Douglas Gregor1d4c1132011-12-20 22:06:13 +00002552 else {
2553 IdentifierInfo *II = Names[I].get<IdentifierInfo *>();
2554 if (!II->hasMacroDefinition()) {
Douglas Gregorc12906e2012-09-24 19:56:18 +00002555 // Make sure that this macro hasn't been #undef'd in the mean-time.
2556 llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator Known
2557 = PP.Macros.find(II);
2558 if (Known == PP.Macros.end() ||
2559 Known->second->getUndefLoc().isInvalid()) {
2560 II->setHasMacroDefinition(true);
2561 if (DeserializationListener)
2562 DeserializationListener->MacroVisible(II);
2563 }
Douglas Gregor1d4c1132011-12-20 22:06:13 +00002564 }
2565 }
Douglas Gregor13292642011-12-02 15:45:10 +00002566 }
Douglas Gregorecc2c092011-12-01 22:20:10 +00002567}
2568
Douglas Gregor5e356932011-12-01 17:11:21 +00002569void ASTReader::makeModuleVisible(Module *Mod,
2570 Module::NameVisibilityKind NameVisibility) {
2571 llvm::SmallPtrSet<Module *, 4> Visited;
2572 llvm::SmallVector<Module *, 4> Stack;
2573 Stack.push_back(Mod);
2574 while (!Stack.empty()) {
2575 Mod = Stack.back();
2576 Stack.pop_back();
2577
2578 if (NameVisibility <= Mod->NameVisibility) {
2579 // This module already has this level of visibility (or greater), so
2580 // there is nothing more to do.
2581 continue;
2582 }
2583
Douglas Gregor51f564f2011-12-31 04:05:44 +00002584 if (!Mod->isAvailable()) {
2585 // Modules that aren't available cannot be made visible.
2586 continue;
2587 }
2588
Douglas Gregor5e356932011-12-01 17:11:21 +00002589 // Update the module's name visibility.
2590 Mod->NameVisibility = NameVisibility;
2591
Douglas Gregorecc2c092011-12-01 22:20:10 +00002592 // If we've already deserialized any names from this module,
Douglas Gregor5e356932011-12-01 17:11:21 +00002593 // mark them as visible.
Douglas Gregorecc2c092011-12-01 22:20:10 +00002594 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2595 if (Hidden != HiddenNamesMap.end()) {
2596 makeNamesVisible(Hidden->second);
2597 HiddenNamesMap.erase(Hidden);
2598 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002599
2600 // Push any non-explicit submodules onto the stack to be marked as
2601 // visible.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002602 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2603 SubEnd = Mod->submodule_end();
Douglas Gregor5e356932011-12-01 17:11:21 +00002604 Sub != SubEnd; ++Sub) {
Douglas Gregorb7a78192012-01-04 23:32:19 +00002605 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2606 Stack.push_back(*Sub);
Douglas Gregor5e356932011-12-01 17:11:21 +00002607 }
Douglas Gregor07165b92011-12-02 19:11:09 +00002608
2609 // Push any exported modules onto the stack to be marked as visible.
Douglas Gregor0adaa882011-12-05 17:28:06 +00002610 bool AnyWildcard = false;
2611 bool UnrestrictedWildcard = false;
2612 llvm::SmallVector<Module *, 4> WildcardRestrictions;
Douglas Gregor07165b92011-12-02 19:11:09 +00002613 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
2614 Module *Exported = Mod->Exports[I].getPointer();
Douglas Gregor0adaa882011-12-05 17:28:06 +00002615 if (!Mod->Exports[I].getInt()) {
2616 // Export a named module directly; no wildcards involved.
2617 if (Visited.insert(Exported))
Douglas Gregor07165b92011-12-02 19:11:09 +00002618 Stack.push_back(Exported);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002619
2620 continue;
Douglas Gregor07165b92011-12-02 19:11:09 +00002621 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002622
2623 // Wildcard export: export all of the imported modules that match
2624 // the given pattern.
2625 AnyWildcard = true;
2626 if (UnrestrictedWildcard)
2627 continue;
2628
2629 if (Module *Restriction = Mod->Exports[I].getPointer())
2630 WildcardRestrictions.push_back(Restriction);
2631 else {
2632 WildcardRestrictions.clear();
2633 UnrestrictedWildcard = true;
2634 }
2635 }
2636
2637 // If there were any wildcards, push any imported modules that were
2638 // re-exported by the wildcard restriction.
2639 if (!AnyWildcard)
2640 continue;
2641
2642 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
2643 Module *Imported = Mod->Imports[I];
Benjamin Kramerd48bcb22012-08-22 15:37:55 +00002644 if (!Visited.insert(Imported))
Douglas Gregor0adaa882011-12-05 17:28:06 +00002645 continue;
2646
2647 bool Acceptable = UnrestrictedWildcard;
2648 if (!Acceptable) {
2649 // Check whether this module meets one of the restrictions.
2650 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
2651 Module *Restriction = WildcardRestrictions[R];
2652 if (Imported == Restriction || Imported->isSubModuleOf(Restriction)) {
2653 Acceptable = true;
2654 break;
2655 }
2656 }
2657 }
2658
2659 if (!Acceptable)
2660 continue;
2661
Douglas Gregor0adaa882011-12-05 17:28:06 +00002662 Stack.push_back(Imported);
Douglas Gregor07165b92011-12-02 19:11:09 +00002663 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002664 }
2665}
2666
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002667ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregor72a9ae12011-07-22 16:00:58 +00002668 ModuleKind Type) {
Douglas Gregor057df202012-01-18 20:56:22 +00002669 // Bump the generation number.
Douglas Gregorcff9f262012-01-27 01:47:08 +00002670 unsigned PreviousGeneration = CurrentGeneration++;
Douglas Gregor057df202012-01-18 20:56:22 +00002671
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002672 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002673 case Failure: return Failure;
2674 case IgnorePCH: return IgnorePCH;
2675 case Success: break;
2676 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002677
2678 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor057df202012-01-18 20:56:22 +00002679
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002680 // Check the predefines buffers.
Douglas Gregor6236a292011-12-02 21:56:05 +00002681 if (!DisableValidation && Type == MK_PCH &&
Argyrios Kyrtzidis26d43cd2011-09-12 18:09:38 +00002682 // FIXME: CheckPredefinesBuffers also sets the SuggestedPredefines;
2683 // if DisableValidation is true, defines that were set on command-line
2684 // but not in the PCH file will not be added to SuggestedPredefines.
2685 CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002686 return IgnorePCH;
2687
Douglas Gregoreee242f2011-10-27 09:33:13 +00002688 // Mark all of the identifiers in the identifier table as being out of date,
2689 // so that various accessors know to check the loaded modules when the
2690 // identifier is used.
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002691 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2692 IdEnd = PP.getIdentifierTable().end();
2693 Id != IdEnd; ++Id)
Douglas Gregoreee242f2011-10-27 09:33:13 +00002694 Id->second->setOutOfDate(true);
Douglas Gregor057df202012-01-18 20:56:22 +00002695
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002696 // Resolve any unresolved module exports.
Douglas Gregor55988682011-12-05 16:33:54 +00002697 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) {
2698 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I];
2699 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002700 Module *ResolvedMod = getSubmodule(GlobalID);
2701
2702 if (Unresolved.IsImport) {
2703 if (ResolvedMod)
Douglas Gregor55988682011-12-05 16:33:54 +00002704 Unresolved.Mod->Imports.push_back(ResolvedMod);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002705 continue;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002706 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002707
2708 if (ResolvedMod || Unresolved.IsWildcard)
2709 Unresolved.Mod->Exports.push_back(
2710 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002711 }
Douglas Gregor55988682011-12-05 16:33:54 +00002712 UnresolvedModuleImportExports.clear();
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002713
Douglas Gregor35942772011-09-09 21:34:22 +00002714 InitializeContext();
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002715
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002716 if (DeserializationListener)
2717 DeserializationListener->ReaderInitialized(this);
2718
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002719 if (!OriginalFileID.isInvalid()) {
2720 OriginalFileID = FileID::get(ModuleMgr.getPrimaryModule().SLocEntryBaseID
2721 + OriginalFileID.getOpaqueValue() - 1);
2722
2723 // If this AST file is a precompiled preamble, then set the preamble file ID
2724 // of the source manager to the file source file from which the preamble was
2725 // built.
2726 if (Type == MK_Preamble) {
Argyrios Kyrtzidis507097e2011-09-19 20:40:35 +00002727 SourceMgr.setPreambleFileID(OriginalFileID);
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002728 } else if (Type == MK_MainFile) {
2729 SourceMgr.setMainFileID(OriginalFileID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002730 }
Douglas Gregor414cb642010-11-30 05:23:00 +00002731 }
2732
Douglas Gregorcff9f262012-01-27 01:47:08 +00002733 // For any Objective-C class definitions we have already loaded, make sure
2734 // that we load any additional categories.
2735 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
2736 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
2737 ObjCClassesLoaded[I],
2738 PreviousGeneration);
2739 }
2740
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002741 return Success;
2742}
2743
Chris Lattner5f9e2722011-07-23 10:55:15 +00002744ASTReader::ASTReadResult ASTReader::ReadASTCore(StringRef FileName,
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002745 ModuleKind Type,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002746 ModuleFile *ImportedBy) {
2747 ModuleFile *M;
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002748 bool NewModule;
2749 std::string ErrorStr;
2750 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
Douglas Gregor057df202012-01-18 20:56:22 +00002751 CurrentGeneration, ErrorStr);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002752
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002753 if (!M) {
2754 // We couldn't load the module.
2755 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2756 + ErrorStr;
2757 Error(Msg);
2758 return Failure;
2759 }
2760
2761 if (!NewModule) {
2762 // We've already loaded this module.
2763 return Success;
2764 }
2765
2766 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2767 // module?
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002768 if (FileName != "-") {
2769 CurrentDir = llvm::sys::path::parent_path(FileName);
2770 if (CurrentDir.empty()) CurrentDir = ".";
2771 }
2772
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002773 ModuleFile &F = *M;
Sebastian Redl9137a522010-07-16 17:50:48 +00002774 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002775 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002776 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002777
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002778 // Sniff for the signature.
2779 if (Stream.Read(8) != 'C' ||
2780 Stream.Read(8) != 'P' ||
2781 Stream.Read(8) != 'C' ||
2782 Stream.Read(8) != 'H') {
2783 Diag(diag::err_not_a_pch_file) << FileName;
2784 return Failure;
2785 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002786
Douglas Gregor2cf26342009-04-09 22:27:44 +00002787 while (!Stream.AtEndOfStream()) {
2788 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002789
Douglas Gregore1d918e2009-04-10 23:10:45 +00002790 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002791 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002792 return Failure;
2793 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002794
2795 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002796
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002797 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002798 switch (BlockID) {
2799 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002800 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002801 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002802 return Failure;
2803 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002804 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002805 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002806 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002807 case Success:
2808 break;
2809
2810 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002811 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002812
2813 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002814 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002815 // AST block, skipping subblocks, to see if there are other
2816 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002817
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002818 // FIXME: We can't clear loaded slocentries anymore.
2819 //SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002820
2821 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002822 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002823 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002824
Douglas Gregore1d918e2009-04-10 23:10:45 +00002825 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002826 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002827 break;
2828 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002829 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002830 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002831 return Failure;
2832 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002833 break;
2834 }
Mike Stump1eb44332009-09-09 15:08:12 +00002835 }
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002836
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002837 // Once read, set the ModuleFile bit base offset and update the size in
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002838 // bits of all files we've seen.
2839 F.GlobalBitOffset = TotalModulesSizeInBits;
2840 TotalModulesSizeInBits += F.SizeInBits;
2841 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
Douglas Gregorc69a2922011-08-25 20:58:51 +00002842
2843 // Make sure that the files this module was built against are still available.
2844 if (!DisableValidation) {
2845 switch(validateFileEntries(*M)) {
2846 case Failure: return Failure;
2847 case IgnorePCH: return IgnorePCH;
2848 case Success: break;
2849 }
2850 }
Douglas Gregorf249bf32011-08-25 21:09:44 +00002851
2852 // Preload SLocEntries.
2853 for (unsigned I = 0, N = M->PreloadSLocEntries.size(); I != N; ++I) {
2854 int Index = int(M->PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Argyrios Kyrtzidisac1ffcc2011-09-19 20:39:54 +00002855 // Load it through the SourceManager and don't call ReadSLocEntryRecord()
2856 // directly because the entry may have already been loaded in which case
2857 // calling ReadSLocEntryRecord() directly would trigger an assertion in
2858 // SourceManager.
2859 SourceMgr.getLoadedSLocEntryByID(Index);
Douglas Gregorf249bf32011-08-25 21:09:44 +00002860 }
2861
Douglas Gregorc69a2922011-08-25 20:58:51 +00002862
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002863 return Success;
2864}
2865
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002866void ASTReader::InitializeContext() {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002867 // If there's a listener, notify them that we "read" the translation unit.
2868 if (DeserializationListener)
Douglas Gregor35942772011-09-09 21:34:22 +00002869 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
2870 Context.getTranslationUnitDecl());
Douglas Gregor3747ee72010-10-01 01:18:02 +00002871
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002872 // Make sure we load the declaration update records for the translation unit,
2873 // if there are any.
Douglas Gregor35942772011-09-09 21:34:22 +00002874 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
2875 Context.getTranslationUnitDecl());
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002876
Douglas Gregor5f957282011-08-11 22:18:49 +00002877 // FIXME: Find a better way to deal with collisions between these
2878 // built-in types. Right now, we just ignore the problem.
2879
2880 // Load the special types.
Douglas Gregora6ea10e2012-01-17 18:09:05 +00002881 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002882 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2883 if (!Context.CFConstantStringTypeDecl)
2884 Context.setCFConstantStringType(GetType(String));
2885 }
2886
2887 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2888 QualType FileType = GetType(File);
2889 if (FileType.isNull()) {
2890 Error("FILE type is NULL");
2891 return;
2892 }
2893
2894 if (!Context.FILEDecl) {
2895 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2896 Context.setFILEDecl(Typedef->getDecl());
2897 else {
2898 const TagType *Tag = FileType->getAs<TagType>();
2899 if (!Tag) {
2900 Error("Invalid FILE type in AST file");
2901 return;
2902 }
2903 Context.setFILEDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002904 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002905 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002906 }
Douglas Gregor5f957282011-08-11 22:18:49 +00002907
Douglas Gregor72cd7a02011-11-11 19:13:12 +00002908 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002909 QualType Jmp_bufType = GetType(Jmp_buf);
2910 if (Jmp_bufType.isNull()) {
2911 Error("jmp_buf type is NULL");
2912 return;
2913 }
2914
2915 if (!Context.jmp_bufDecl) {
2916 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2917 Context.setjmp_bufDecl(Typedef->getDecl());
2918 else {
2919 const TagType *Tag = Jmp_bufType->getAs<TagType>();
2920 if (!Tag) {
2921 Error("Invalid jmp_buf type in AST file");
2922 return;
2923 }
2924 Context.setjmp_bufDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002925 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002926 }
Mike Stump782fa302009-07-28 02:25:19 +00002927 }
Douglas Gregor02a5e872011-09-10 00:30:18 +00002928
Douglas Gregor72cd7a02011-11-11 19:13:12 +00002929 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002930 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2931 if (Sigjmp_bufType.isNull()) {
2932 Error("sigjmp_buf type is NULL");
2933 return;
2934 }
2935
2936 if (!Context.sigjmp_bufDecl) {
2937 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2938 Context.setsigjmp_bufDecl(Typedef->getDecl());
2939 else {
2940 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
2941 assert(Tag && "Invalid sigjmp_buf type in AST file");
2942 Context.setsigjmp_bufDecl(Tag->getDecl());
2943 }
2944 }
2945 }
2946
2947 if (unsigned ObjCIdRedef
2948 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
2949 if (Context.ObjCIdRedefinitionType.isNull())
2950 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
2951 }
2952
2953 if (unsigned ObjCClassRedef
2954 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
2955 if (Context.ObjCClassRedefinitionType.isNull())
2956 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
2957 }
2958
2959 if (unsigned ObjCSelRedef
2960 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
2961 if (Context.ObjCSelRedefinitionType.isNull())
2962 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
2963 }
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00002964
2965 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
2966 QualType Ucontext_tType = GetType(Ucontext_t);
2967 if (Ucontext_tType.isNull()) {
2968 Error("ucontext_t type is NULL");
2969 return;
2970 }
2971
2972 if (!Context.ucontext_tDecl) {
2973 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
2974 Context.setucontext_tDecl(Typedef->getDecl());
2975 else {
2976 const TagType *Tag = Ucontext_tType->getAs<TagType>();
2977 assert(Tag && "Invalid ucontext_t type in AST file");
2978 Context.setucontext_tDecl(Tag->getDecl());
2979 }
2980 }
2981 }
Douglas Gregor5f957282011-08-11 22:18:49 +00002982 }
2983
Douglas Gregor35942772011-09-09 21:34:22 +00002984 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002985
2986 // If there were any CUDA special declarations, deserialize them.
2987 if (!CUDASpecialDeclRefs.empty()) {
2988 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
Douglas Gregor35942772011-09-09 21:34:22 +00002989 Context.setcudaConfigureCallDecl(
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002990 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2991 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00002992
2993 // Re-export any modules that were imported by a non-module AST file.
2994 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
2995 if (Module *Imported = getSubmodule(ImportedModules[I]))
2996 makeModuleVisible(Imported, Module::AllVisible);
2997 }
2998 ImportedModules.clear();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002999}
3000
Douglas Gregorecc2c092011-12-01 22:20:10 +00003001void ASTReader::finalizeForWriting() {
3002 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3003 HiddenEnd = HiddenNamesMap.end();
3004 Hidden != HiddenEnd; ++Hidden) {
3005 makeNamesVisible(Hidden->second);
3006 }
3007 HiddenNamesMap.clear();
3008}
3009
Douglas Gregorb64c1932009-05-12 01:31:05 +00003010/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003011/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00003012/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003013std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00003014 FileManager &FileMgr,
David Blaikied6471f72011-09-25 23:23:43 +00003015 DiagnosticsEngine &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003016 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003017 std::string ErrStr;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00003018 OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00003019 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00003020 if (!Buffer) {
Kaelyn Uhrainda01f622012-06-20 00:36:03 +00003021 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003022 return std::string();
3023 }
3024
3025 // Initialize the stream
3026 llvm::BitstreamReader StreamFile;
3027 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00003028 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00003029 (const unsigned char *)Buffer->getBufferEnd());
3030 Stream.init(StreamFile);
3031
3032 // Sniff for the signature.
3033 if (Stream.Read(8) != 'C' ||
3034 Stream.Read(8) != 'P' ||
3035 Stream.Read(8) != 'C' ||
3036 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003037 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003038 return std::string();
3039 }
3040
3041 RecordData Record;
3042 while (!Stream.AtEndOfStream()) {
3043 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00003044
Douglas Gregorb64c1932009-05-12 01:31:05 +00003045 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3046 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00003047
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003048 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003049 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003050 case AST_BLOCK_ID:
3051 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003052 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003053 return std::string();
3054 }
3055 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003056
Douglas Gregorb64c1932009-05-12 01:31:05 +00003057 default:
3058 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003059 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003060 return std::string();
3061 }
3062 break;
3063 }
3064 continue;
3065 }
3066
3067 if (Code == llvm::bitc::END_BLOCK) {
3068 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003069 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003070 return std::string();
3071 }
3072 continue;
3073 }
3074
3075 if (Code == llvm::bitc::DEFINE_ABBREV) {
3076 Stream.ReadAbbrevRecord();
3077 continue;
3078 }
3079
3080 Record.clear();
3081 const char *BlobStart = 0;
3082 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003083 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003084 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00003085 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00003086 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00003087
3088 return std::string();
3089}
3090
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003091ASTReader::ASTReadResult ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003092 // Enter the submodule block.
3093 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3094 Error("malformed submodule block record in AST file");
3095 return Failure;
3096 }
3097
3098 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
Douglas Gregor26ced122011-12-01 00:59:36 +00003099 bool First = true;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003100 Module *CurrentModule = 0;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003101 RecordData Record;
3102 while (true) {
3103 unsigned Code = F.Stream.ReadCode();
3104 if (Code == llvm::bitc::END_BLOCK) {
3105 if (F.Stream.ReadBlockEnd()) {
3106 Error("error at end of submodule block in AST file");
3107 return Failure;
3108 }
3109 return Success;
3110 }
3111
3112 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3113 // No known subblocks, always skip them.
3114 F.Stream.ReadSubBlockID();
3115 if (F.Stream.SkipBlock()) {
3116 Error("malformed block record in AST file");
3117 return Failure;
3118 }
3119 continue;
3120 }
3121
3122 if (Code == llvm::bitc::DEFINE_ABBREV) {
3123 F.Stream.ReadAbbrevRecord();
3124 continue;
3125 }
3126
3127 // Read a record.
3128 const char *BlobStart;
3129 unsigned BlobLen;
3130 Record.clear();
3131 switch (F.Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
3132 default: // Default behavior: ignore.
3133 break;
3134
3135 case SUBMODULE_DEFINITION: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003136 if (First) {
3137 Error("missing submodule metadata record at beginning of block");
3138 return Failure;
3139 }
3140
Douglas Gregore209e502011-12-06 01:10:29 +00003141 if (Record.size() < 7) {
Douglas Gregor1e123682011-12-05 22:27:44 +00003142 Error("malformed module definition");
3143 return Failure;
3144 }
3145
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003146 StringRef Name(BlobStart, BlobLen);
Douglas Gregore209e502011-12-06 01:10:29 +00003147 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3148 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3149 bool IsFramework = Record[2];
3150 bool IsExplicit = Record[3];
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003151 bool IsSystem = Record[4];
3152 bool InferSubmodules = Record[5];
3153 bool InferExplicitSubmodules = Record[6];
3154 bool InferExportWildcard = Record[7];
Douglas Gregor1e123682011-12-05 22:27:44 +00003155
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003156 Module *ParentModule = 0;
Douglas Gregor26ced122011-12-01 00:59:36 +00003157 if (Parent)
3158 ParentModule = getSubmodule(Parent);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003159
3160 // Retrieve this (sub)module from the module map, creating it if
3161 // necessary.
3162 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3163 IsFramework,
3164 IsExplicit).first;
Douglas Gregore209e502011-12-06 01:10:29 +00003165 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3166 if (GlobalIndex >= SubmodulesLoaded.size() ||
3167 SubmodulesLoaded[GlobalIndex]) {
Douglas Gregor26ced122011-12-01 00:59:36 +00003168 Error("too many submodules");
3169 return Failure;
3170 }
Douglas Gregora015cab2011-12-02 17:30:13 +00003171
Douglas Gregor305dc3e2011-12-20 00:28:52 +00003172 CurrentModule->IsFromModuleFile = true;
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003173 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Douglas Gregor1e123682011-12-05 22:27:44 +00003174 CurrentModule->InferSubmodules = InferSubmodules;
3175 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3176 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregora015cab2011-12-02 17:30:13 +00003177 if (DeserializationListener)
Douglas Gregore209e502011-12-06 01:10:29 +00003178 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
Douglas Gregora015cab2011-12-02 17:30:13 +00003179
Douglas Gregore209e502011-12-06 01:10:29 +00003180 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003181 break;
3182 }
3183
Douglas Gregor77d029f2011-12-08 19:11:24 +00003184 case SUBMODULE_UMBRELLA_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003185 if (First) {
3186 Error("missing submodule metadata record at beginning of block");
3187 return Failure;
3188 }
3189
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003190 if (!CurrentModule)
3191 break;
3192
3193 StringRef FileName(BlobStart, BlobLen);
3194 if (const FileEntry *Umbrella = PP.getFileManager().getFile(FileName)) {
Douglas Gregor10694ce2011-12-08 17:39:04 +00003195 if (!CurrentModule->getUmbrellaHeader())
Douglas Gregore209e502011-12-06 01:10:29 +00003196 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
Douglas Gregor10694ce2011-12-08 17:39:04 +00003197 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003198 Error("mismatched umbrella headers in submodule");
3199 return Failure;
3200 }
3201 }
3202 break;
3203 }
3204
3205 case SUBMODULE_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003206 if (First) {
3207 Error("missing submodule metadata record at beginning of block");
3208 return Failure;
3209 }
3210
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003211 if (!CurrentModule)
3212 break;
3213
3214 // FIXME: Be more lazy about this!
3215 StringRef FileName(BlobStart, BlobLen);
3216 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3217 if (std::find(CurrentModule->Headers.begin(),
3218 CurrentModule->Headers.end(),
3219 File) == CurrentModule->Headers.end())
Douglas Gregore209e502011-12-06 01:10:29 +00003220 ModMap.addHeader(CurrentModule, File);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003221 }
3222 break;
3223 }
Douglas Gregor26ced122011-12-01 00:59:36 +00003224
Douglas Gregor77d029f2011-12-08 19:11:24 +00003225 case SUBMODULE_UMBRELLA_DIR: {
3226 if (First) {
3227 Error("missing submodule metadata record at beginning of block");
3228 return Failure;
3229 }
3230
3231 if (!CurrentModule)
3232 break;
3233
3234 StringRef DirName(BlobStart, BlobLen);
3235 if (const DirectoryEntry *Umbrella
3236 = PP.getFileManager().getDirectory(DirName)) {
3237 if (!CurrentModule->getUmbrellaDir())
3238 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3239 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3240 Error("mismatched umbrella directories in submodule");
3241 return Failure;
3242 }
3243 }
3244 break;
3245 }
3246
Douglas Gregor26ced122011-12-01 00:59:36 +00003247 case SUBMODULE_METADATA: {
3248 if (!First) {
3249 Error("submodule metadata record not at beginning of block");
3250 return Failure;
3251 }
3252 First = false;
3253
3254 F.BaseSubmoduleID = getTotalNumSubmodules();
Douglas Gregor26ced122011-12-01 00:59:36 +00003255 F.LocalNumSubmodules = Record[0];
3256 unsigned LocalBaseSubmoduleID = Record[1];
3257 if (F.LocalNumSubmodules > 0) {
3258 // Introduce the global -> local mapping for submodules within this
3259 // module.
3260 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3261
3262 // Introduce the local -> global mapping for submodules within this
3263 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00003264 F.SubmoduleRemap.insertOrReplace(
Douglas Gregor26ced122011-12-01 00:59:36 +00003265 std::make_pair(LocalBaseSubmoduleID,
3266 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3267
3268 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3269 }
3270 break;
3271 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003272
Douglas Gregor55988682011-12-05 16:33:54 +00003273 case SUBMODULE_IMPORTS: {
3274 if (First) {
3275 Error("missing submodule metadata record at beginning of block");
3276 return Failure;
3277 }
3278
3279 if (!CurrentModule)
3280 break;
3281
3282 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
3283 UnresolvedModuleImportExport Unresolved;
3284 Unresolved.File = &F;
3285 Unresolved.Mod = CurrentModule;
3286 Unresolved.ID = Record[Idx];
3287 Unresolved.IsImport = true;
3288 Unresolved.IsWildcard = false;
3289 UnresolvedModuleImportExports.push_back(Unresolved);
3290 }
3291 break;
3292 }
3293
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003294 case SUBMODULE_EXPORTS: {
3295 if (First) {
3296 Error("missing submodule metadata record at beginning of block");
3297 return Failure;
3298 }
3299
3300 if (!CurrentModule)
3301 break;
3302
3303 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor55988682011-12-05 16:33:54 +00003304 UnresolvedModuleImportExport Unresolved;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003305 Unresolved.File = &F;
Douglas Gregor55988682011-12-05 16:33:54 +00003306 Unresolved.Mod = CurrentModule;
3307 Unresolved.ID = Record[Idx];
3308 Unresolved.IsImport = false;
3309 Unresolved.IsWildcard = Record[Idx + 1];
3310 UnresolvedModuleImportExports.push_back(Unresolved);
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003311 }
3312
3313 // Once we've loaded the set of exports, there's no reason to keep
3314 // the parsed, unresolved exports around.
3315 CurrentModule->UnresolvedExports.clear();
3316 break;
3317 }
Douglas Gregor51f564f2011-12-31 04:05:44 +00003318 case SUBMODULE_REQUIRES: {
3319 if (First) {
3320 Error("missing submodule metadata record at beginning of block");
3321 return Failure;
3322 }
3323
3324 if (!CurrentModule)
3325 break;
3326
3327 CurrentModule->addRequirement(StringRef(BlobStart, BlobLen),
David Blaikie4e4d0842012-03-11 07:00:24 +00003328 Context.getLangOpts(),
Douglas Gregordc58aa72012-01-30 06:01:29 +00003329 Context.getTargetInfo());
Douglas Gregor51f564f2011-12-31 04:05:44 +00003330 break;
3331 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003332 }
3333 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003334}
3335
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003336/// \brief Parse the record that corresponds to a LangOptions data
3337/// structure.
3338///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003339/// This routine parses the language options from the AST file and then gives
3340/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003341///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003342/// \returns true if the listener deems the file unacceptable, false otherwise.
John McCall260611a2012-06-20 06:18:46 +00003343bool ASTReader::ParseLanguageOptions(const RecordData &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003344 if (Listener) {
3345 LangOptions LangOpts;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003346 unsigned Idx = 0;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003347#define LANGOPT(Name, Bits, Default, Description) \
3348 LangOpts.Name = Record[Idx++];
3349#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3350 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3351#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +00003352
3353 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3354 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3355 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003356
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00003357 unsigned Length = Record[Idx++];
3358 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3359 Record.begin() + Idx + Length);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003360 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003361 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003362
3363 return false;
3364}
3365
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003366std::pair<ModuleFile *, unsigned>
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003367ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003368 GlobalPreprocessedEntityMapType::iterator
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003369 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003370 assert(I != GlobalPreprocessedEntityMap.end() &&
3371 "Corrupted global preprocessed entity map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003372 ModuleFile *M = I->second;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003373 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
3374 return std::make_pair(M, LocalIndex);
3375}
3376
3377PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
3378 PreprocessedEntityID PPID = Index+1;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003379 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3380 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003381 unsigned LocalIndex = PPInfo.second;
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003382 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
Douglas Gregor4800a5c2011-02-08 21:58:10 +00003383
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003384 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003385 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003386
3387 unsigned Code = M.PreprocessorDetailCursor.ReadCode();
3388 switch (Code) {
3389 case llvm::bitc::END_BLOCK:
3390 return 0;
3391
3392 case llvm::bitc::ENTER_SUBBLOCK:
3393 Error("unexpected subblock record in preprocessor detail block");
3394 return 0;
3395
3396 case llvm::bitc::DEFINE_ABBREV:
3397 Error("unexpected abbrevation record in preprocessor detail block");
3398 return 0;
3399
3400 default:
3401 break;
3402 }
3403
3404 if (!PP.getPreprocessingRecord()) {
3405 Error("no preprocessing record");
3406 return 0;
3407 }
3408
3409 // Read the record.
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003410 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
3411 ReadSourceLocation(M, PPOffs.End));
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003412 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
3413 const char *BlobStart = 0;
3414 unsigned BlobLen = 0;
3415 RecordData Record;
3416 PreprocessorDetailRecordTypes RecType =
3417 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord(
3418 Code, Record, BlobStart, BlobLen);
3419 switch (RecType) {
3420 case PPD_MACRO_EXPANSION: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003421 bool isBuiltin = Record[0];
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003422 IdentifierInfo *Name = 0;
3423 MacroDefinition *Def = 0;
3424 if (isBuiltin)
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003425 Name = getLocalIdentifier(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003426 else {
3427 PreprocessedEntityID
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003428 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003429 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
3430 }
3431
3432 MacroExpansion *ME;
3433 if (isBuiltin)
3434 ME = new (PPRec) MacroExpansion(Name, Range);
3435 else
3436 ME = new (PPRec) MacroExpansion(Def, Range);
3437
3438 return ME;
3439 }
3440
3441 case PPD_MACRO_DEFINITION: {
3442 // Decode the identifier info and then check again; if the macro is
3443 // still defined and associated with the identifier,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003444 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003445 MacroDefinition *MD
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003446 = new (PPRec) MacroDefinition(II, Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003447
3448 if (DeserializationListener)
3449 DeserializationListener->MacroDefinitionRead(PPID, MD);
3450
3451 return MD;
3452 }
3453
3454 case PPD_INCLUSION_DIRECTIVE: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003455 const char *FullFileNameStart = BlobStart + Record[0];
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00003456 StringRef FullFileName(FullFileNameStart, BlobLen - Record[0]);
3457 const FileEntry *File = 0;
3458 if (!FullFileName.empty())
3459 File = PP.getFileManager().getFile(FullFileName);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003460
3461 // FIXME: Stable encoding
3462 InclusionDirective::InclusionKind Kind
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003463 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003464 InclusionDirective *ID
3465 = new (PPRec) InclusionDirective(PPRec, Kind,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003466 StringRef(BlobStart, Record[0]),
3467 Record[1],
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003468 File,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003469 Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003470 return ID;
3471 }
3472 }
David Blaikie7530c032012-01-17 06:56:22 +00003473
3474 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003475}
3476
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003477/// \brief \arg SLocMapI points at a chunk of a module that contains no
3478/// preprocessed entities or the entities it contains are not the ones we are
3479/// looking for. Find the next module that contains entities and return the ID
3480/// of the first entry.
3481PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
3482 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
3483 ++SLocMapI;
3484 for (GlobalSLocOffsetMapType::const_iterator
3485 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003486 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003487 if (M.NumPreprocessedEntities)
3488 return getGlobalPreprocessedEntityID(M, M.BasePreprocessedEntityID);
3489 }
3490
3491 return getTotalNumPreprocessedEntities();
3492}
3493
3494namespace {
3495
3496template <unsigned PPEntityOffset::*PPLoc>
3497struct PPEntityComp {
3498 const ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003499 ModuleFile &M;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003500
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003501 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003502
Benjamin Kramer88df1252011-09-21 06:42:26 +00003503 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
3504 SourceLocation LHS = getLoc(L);
3505 SourceLocation RHS = getLoc(R);
3506 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3507 }
3508
3509 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003510 SourceLocation LHS = getLoc(L);
3511 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3512 }
3513
Benjamin Kramer88df1252011-09-21 06:42:26 +00003514 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003515 SourceLocation RHS = getLoc(R);
3516 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3517 }
3518
3519 SourceLocation getLoc(const PPEntityOffset &PPE) const {
3520 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
3521 }
3522};
3523
3524}
3525
3526/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
3527PreprocessedEntityID
3528ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
3529 if (SourceMgr.isLocalSourceLocation(BLoc))
3530 return getTotalNumPreprocessedEntities();
3531
3532 GlobalSLocOffsetMapType::const_iterator
3533 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3534 BLoc.getOffset());
3535 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3536 "Corrupted global sloc offset map");
3537
3538 if (SLocMapI->second->NumPreprocessedEntities == 0)
3539 return findNextPreprocessedEntity(SLocMapI);
3540
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003541 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003542 typedef const PPEntityOffset *pp_iterator;
3543 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3544 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
Argyrios Kyrtzidis4cd06342011-09-22 21:17:02 +00003545
3546 size_t Count = M.NumPreprocessedEntities;
3547 size_t Half;
3548 pp_iterator First = pp_begin;
3549 pp_iterator PPI;
3550
3551 // Do a binary search manually instead of using std::lower_bound because
3552 // The end locations of entities may be unordered (when a macro expansion
3553 // is inside another macro argument), but for this case it is not important
3554 // whether we get the first macro expansion or its containing macro.
3555 while (Count > 0) {
3556 Half = Count/2;
3557 PPI = First;
3558 std::advance(PPI, Half);
3559 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
3560 BLoc)){
3561 First = PPI;
3562 ++First;
3563 Count = Count - Half - 1;
3564 } else
3565 Count = Half;
3566 }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003567
3568 if (PPI == pp_end)
3569 return findNextPreprocessedEntity(SLocMapI);
3570
3571 return getGlobalPreprocessedEntityID(M,
3572 M.BasePreprocessedEntityID + (PPI - pp_begin));
3573}
3574
3575/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
3576PreprocessedEntityID
3577ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
3578 if (SourceMgr.isLocalSourceLocation(ELoc))
3579 return getTotalNumPreprocessedEntities();
3580
3581 GlobalSLocOffsetMapType::const_iterator
3582 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3583 ELoc.getOffset());
3584 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3585 "Corrupted global sloc offset map");
3586
3587 if (SLocMapI->second->NumPreprocessedEntities == 0)
3588 return findNextPreprocessedEntity(SLocMapI);
3589
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003590 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003591 typedef const PPEntityOffset *pp_iterator;
3592 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3593 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
3594 pp_iterator PPI =
3595 std::upper_bound(pp_begin, pp_end, ELoc,
3596 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
3597
3598 if (PPI == pp_end)
3599 return findNextPreprocessedEntity(SLocMapI);
3600
3601 return getGlobalPreprocessedEntityID(M,
3602 M.BasePreprocessedEntityID + (PPI - pp_begin));
3603}
3604
3605/// \brief Returns a pair of [Begin, End) indices of preallocated
3606/// preprocessed entities that \arg Range encompasses.
3607std::pair<unsigned, unsigned>
3608 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
3609 if (Range.isInvalid())
3610 return std::make_pair(0,0);
3611 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
3612
3613 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
3614 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
3615 return std::make_pair(BeginID, EndID);
3616}
3617
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003618/// \brief Optionally returns true or false if the preallocated preprocessed
3619/// entity with index \arg Index came from file \arg FID.
3620llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
3621 FileID FID) {
3622 if (FID.isInvalid())
3623 return false;
3624
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003625 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3626 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003627 unsigned LocalIndex = PPInfo.second;
3628 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
3629
3630 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
3631 if (Loc.isInvalid())
3632 return false;
3633
3634 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
3635 return true;
3636 else
3637 return false;
3638}
3639
Douglas Gregord10a3812011-08-25 18:14:34 +00003640namespace {
3641 /// \brief Visitor used to search for information about a header file.
3642 class HeaderFileInfoVisitor {
3643 ASTReader &Reader;
3644 const FileEntry *FE;
3645
3646 llvm::Optional<HeaderFileInfo> HFI;
3647
3648 public:
3649 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
3650 : Reader(Reader), FE(FE) { }
3651
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003652 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003653 HeaderFileInfoVisitor *This
3654 = static_cast<HeaderFileInfoVisitor *>(UserData);
3655
3656 HeaderFileInfoTrait Trait(This->Reader, M,
3657 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
3658 M.HeaderFileFrameworkStrings,
3659 This->FE->getName());
3660
3661 HeaderFileInfoLookupTable *Table
3662 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
3663 if (!Table)
3664 return false;
3665
3666 // Look in the on-disk hash table for an entry for this file name.
3667 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
3668 &Trait);
3669 if (Pos == Table->end())
3670 return false;
3671
3672 This->HFI = *Pos;
3673 return true;
3674 }
3675
3676 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
3677 };
3678}
3679
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003680HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003681 HeaderFileInfoVisitor Visitor(*this, FE);
3682 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
3683 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003684 if (Listener)
Douglas Gregord10a3812011-08-25 18:14:34 +00003685 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
3686 return *HFI;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003687 }
3688
3689 return HeaderFileInfo();
3690}
3691
David Blaikied6471f72011-09-25 23:23:43 +00003692void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00003693 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003694 ModuleFile &F = *(*I);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003695 unsigned Idx = 0;
3696 while (Idx < F.PragmaDiagMappings.size()) {
3697 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00003698 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
3699 Diag.DiagStatePoints.push_back(
3700 DiagnosticsEngine::DiagStatePoint(&Diag.DiagStates.back(),
3701 FullSourceLoc(Loc, SourceMgr)));
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003702 while (1) {
3703 assert(Idx < F.PragmaDiagMappings.size() &&
3704 "Invalid data, didn't find '-1' marking end of diag/map pairs");
3705 if (Idx >= F.PragmaDiagMappings.size()) {
3706 break; // Something is messed up but at least avoid infinite loop in
3707 // release build.
3708 }
3709 unsigned DiagID = F.PragmaDiagMappings[Idx++];
3710 if (DiagID == (unsigned)-1) {
3711 break; // no more diag/map pairs for this location.
3712 }
3713 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00003714 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
3715 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003716 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003717 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00003718 }
3719}
3720
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003721/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003722ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregora119da02011-08-02 16:26:37 +00003723 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turnere9b76c12011-07-20 21:31:32 +00003724 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003725 ModuleFile *M = I->second;
Douglas Gregore3605012011-08-02 18:32:54 +00003726 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003727}
3728
3729/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00003730///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003731/// The index is the type ID, shifted and minus the number of predefs. This
3732/// routine actually reads the record corresponding to the type at the given
3733/// location. It is a helper routine for GetType, which deals with reading type
3734/// IDs.
Douglas Gregor393f2492011-07-22 00:38:23 +00003735QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003736 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00003737 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00003738
Douglas Gregor0b748912009-04-14 21:18:50 +00003739 // Keep track of where we are in the stream, then jump back there
3740 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003741 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00003742
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003743 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00003744
Douglas Gregord89275b2009-07-06 18:54:52 +00003745 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003746 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00003747
Douglas Gregor393f2492011-07-22 00:38:23 +00003748 unsigned Idx = 0;
Sebastian Redlc3632732010-10-05 15:59:54 +00003749 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003750 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003751 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003752 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3753 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003754 if (Record.size() != 2) {
3755 Error("Incorrect encoding of extended qualifier type");
3756 return QualType();
3757 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003758 QualType Base = readType(*Loc.F, Record, Idx);
3759 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
Douglas Gregor35942772011-09-09 21:34:22 +00003760 return Context.getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00003761 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003762
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003763 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003764 if (Record.size() != 1) {
3765 Error("Incorrect encoding of complex type");
3766 return QualType();
3767 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003768 QualType ElemType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003769 return Context.getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003770 }
3771
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003772 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003773 if (Record.size() != 1) {
3774 Error("Incorrect encoding of pointer type");
3775 return QualType();
3776 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003777 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003778 return Context.getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003779 }
3780
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003781 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003782 if (Record.size() != 1) {
3783 Error("Incorrect encoding of block pointer type");
3784 return QualType();
3785 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003786 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003787 return Context.getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003788 }
3789
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003790 case TYPE_LVALUE_REFERENCE: {
Richard Smithdf1550f2011-04-12 10:38:03 +00003791 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003792 Error("Incorrect encoding of lvalue reference type");
3793 return QualType();
3794 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003795 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003796 return Context.getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003797 }
3798
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003799 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003800 if (Record.size() != 1) {
3801 Error("Incorrect encoding of rvalue reference type");
3802 return QualType();
3803 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003804 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003805 return Context.getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003806 }
3807
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003808 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00003809 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003810 Error("Incorrect encoding of member pointer type");
3811 return QualType();
3812 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003813 QualType PointeeType = readType(*Loc.F, Record, Idx);
3814 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00003815 if (PointeeType.isNull() || ClassType.isNull())
3816 return QualType();
3817
Douglas Gregor35942772011-09-09 21:34:22 +00003818 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003819 }
3820
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003821 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003822 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003823 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3824 unsigned IndexTypeQuals = Record[2];
3825 unsigned Idx = 3;
3826 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003827 return Context.getConstantArrayType(ElementType, Size,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003828 ASM, IndexTypeQuals);
3829 }
3830
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003831 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003832 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003833 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3834 unsigned IndexTypeQuals = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003835 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003836 }
3837
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003838 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003839 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00003840 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3841 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00003842 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3843 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
Douglas Gregor35942772011-09-09 21:34:22 +00003844 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003845 ASM, IndexTypeQuals,
3846 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003847 }
3848
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003849 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003850 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003851 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003852 return QualType();
3853 }
3854
Douglas Gregor393f2492011-07-22 00:38:23 +00003855 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003856 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00003857 unsigned VecKind = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003858 return Context.getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00003859 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003860 }
3861
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003862 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003863 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003864 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003865 return QualType();
3866 }
3867
Douglas Gregor393f2492011-07-22 00:38:23 +00003868 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003869 unsigned NumElements = Record[1];
Douglas Gregor35942772011-09-09 21:34:22 +00003870 return Context.getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003871 }
3872
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003873 case TYPE_FUNCTION_NO_PROTO: {
John McCallf85e1932011-06-15 23:02:42 +00003874 if (Record.size() != 6) {
Douglas Gregora02b1472009-04-28 21:53:25 +00003875 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003876 return QualType();
3877 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003878 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCallf85e1932011-06-15 23:02:42 +00003879 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
3880 (CallingConv)Record[4], Record[5]);
Douglas Gregor35942772011-09-09 21:34:22 +00003881 return Context.getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003882 }
3883
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003884 case TYPE_FUNCTION_PROTO: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003885 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalle23cf432010-12-14 08:05:40 +00003886
3887 FunctionProtoType::ExtProtoInfo EPI;
3888 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmana49218e2011-04-09 08:18:08 +00003889 /*hasregparm*/ Record[2],
3890 /*regparm*/ Record[3],
John McCallf85e1932011-06-15 23:02:42 +00003891 static_cast<CallingConv>(Record[4]),
3892 /*produces*/ Record[5]);
John McCalle23cf432010-12-14 08:05:40 +00003893
John McCallf85e1932011-06-15 23:02:42 +00003894 unsigned Idx = 6;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003895 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003896 SmallVector<QualType, 16> ParamTypes;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003897 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00003898 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalle23cf432010-12-14 08:05:40 +00003899
3900 EPI.Variadic = Record[Idx++];
Richard Smitheefb3d52012-02-10 09:58:53 +00003901 EPI.HasTrailingReturn = Record[Idx++];
John McCalle23cf432010-12-14 08:05:40 +00003902 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00003903 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redl60618fa2011-03-12 11:50:43 +00003904 ExceptionSpecificationType EST =
3905 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3906 EPI.ExceptionSpecType = EST;
Douglas Gregorb0d06e22012-04-04 00:34:49 +00003907 SmallVector<QualType, 2> Exceptions;
Sebastian Redl60618fa2011-03-12 11:50:43 +00003908 if (EST == EST_Dynamic) {
3909 EPI.NumExceptions = Record[Idx++];
Sebastian Redl60618fa2011-03-12 11:50:43 +00003910 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00003911 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redl60618fa2011-03-12 11:50:43 +00003912 EPI.Exceptions = Exceptions.data();
3913 } else if (EST == EST_ComputedNoexcept) {
3914 EPI.NoexceptExpr = ReadExpr(*Loc.F);
Richard Smith7bb698a2012-04-21 17:47:47 +00003915 } else if (EST == EST_Uninstantiated) {
3916 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
3917 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Richard Smithb9d0b762012-07-27 04:22:15 +00003918 } else if (EST == EST_Unevaluated) {
3919 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Sebastian Redl60618fa2011-03-12 11:50:43 +00003920 }
Douglas Gregor35942772011-09-09 21:34:22 +00003921 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00003922 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003923 }
3924
Douglas Gregor409448c2011-07-21 22:35:25 +00003925 case TYPE_UNRESOLVED_USING: {
3926 unsigned Idx = 0;
Douglas Gregor35942772011-09-09 21:34:22 +00003927 return Context.getTypeDeclType(
Douglas Gregor409448c2011-07-21 22:35:25 +00003928 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
3929 }
3930
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003931 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003932 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003933 Error("incorrect encoding of typedef type");
3934 return QualType();
3935 }
Douglas Gregor409448c2011-07-21 22:35:25 +00003936 unsigned Idx = 0;
3937 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003938 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003939 if (!Canonical.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00003940 Canonical = Context.getCanonicalType(Canonical);
3941 return Context.getTypedefType(Decl, Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003942 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003943
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003944 case TYPE_TYPEOF_EXPR:
Douglas Gregor35942772011-09-09 21:34:22 +00003945 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003946
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003947 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003948 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003949 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003950 return QualType();
3951 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003952 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003953 return Context.getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003954 }
Mike Stump1eb44332009-09-09 15:08:12 +00003955
Douglas Gregorf8af9822012-02-12 18:42:33 +00003956 case TYPE_DECLTYPE: {
3957 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
3958 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
3959 }
Anders Carlsson395b4752009-06-24 19:06:50 +00003960
Sean Huntca63c202011-05-24 22:41:36 +00003961 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003962 QualType BaseType = readType(*Loc.F, Record, Idx);
3963 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Sean Huntca63c202011-05-24 22:41:36 +00003964 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003965 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
Sean Huntca63c202011-05-24 22:41:36 +00003966 }
3967
Richard Smith34b41d92011-02-20 03:19:35 +00003968 case TYPE_AUTO:
Douglas Gregor35942772011-09-09 21:34:22 +00003969 return Context.getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith34b41d92011-02-20 03:19:35 +00003970
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003971 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003972 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003973 Error("incorrect encoding of record type");
3974 return QualType();
3975 }
Douglas Gregor409448c2011-07-21 22:35:25 +00003976 unsigned Idx = 0;
3977 bool IsDependent = Record[Idx++];
Douglas Gregor56ca8a92012-01-17 19:21:53 +00003978 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
3979 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
3980 QualType T = Context.getRecordType(RD);
John McCallf4c73712011-01-19 06:33:43 +00003981 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003982 return T;
3983 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003984
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003985 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003986 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003987 Error("incorrect encoding of enum type");
3988 return QualType();
3989 }
Douglas Gregor409448c2011-07-21 22:35:25 +00003990 unsigned Idx = 0;
3991 bool IsDependent = Record[Idx++];
3992 QualType T
Douglas Gregor35942772011-09-09 21:34:22 +00003993 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00003994 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003995 return T;
3996 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003997
John McCall9d156a72011-01-06 01:58:22 +00003998 case TYPE_ATTRIBUTED: {
3999 if (Record.size() != 3) {
4000 Error("incorrect encoding of attributed type");
4001 return QualType();
4002 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004003 QualType modifiedType = readType(*Loc.F, Record, Idx);
4004 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall9d156a72011-01-06 01:58:22 +00004005 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
Douglas Gregor35942772011-09-09 21:34:22 +00004006 return Context.getAttributedType(kind, modifiedType, equivalentType);
John McCall9d156a72011-01-06 01:58:22 +00004007 }
4008
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004009 case TYPE_PAREN: {
4010 if (Record.size() != 1) {
4011 Error("incorrect encoding of paren type");
4012 return QualType();
4013 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004014 QualType InnerType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004015 return Context.getParenType(InnerType);
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004016 }
4017
Douglas Gregor7536dd52010-12-20 02:24:11 +00004018 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00004019 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00004020 Error("incorrect encoding of pack expansion type");
4021 return QualType();
4022 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004023 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004024 if (Pattern.isNull())
4025 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00004026 llvm::Optional<unsigned> NumExpansions;
4027 if (Record[1])
4028 NumExpansions = Record[1] - 1;
Douglas Gregor35942772011-09-09 21:34:22 +00004029 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004030 }
4031
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004032 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004033 unsigned Idx = 0;
4034 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004035 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004036 QualType NamedType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004037 return Context.getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00004038 }
4039
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004040 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004041 unsigned Idx = 0;
Douglas Gregor409448c2011-07-21 22:35:25 +00004042 ObjCInterfaceDecl *ItfD
4043 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004044 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
John McCallc12c5bb2010-05-15 11:32:37 +00004045 }
4046
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004047 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00004048 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004049 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004050 unsigned NumProtos = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004051 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004052 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor409448c2011-07-21 22:35:25 +00004053 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004054 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004055 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004056
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004057 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004058 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004059 QualType Pointee = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004060 return Context.getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004061 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00004062
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004063 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00004064 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004065 QualType Parm = readType(*Loc.F, Record, Idx);
4066 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCall49a832b2009-10-18 09:09:24 +00004067 return
Douglas Gregor35942772011-09-09 21:34:22 +00004068 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
John McCall49a832b2009-10-18 09:09:24 +00004069 Replacement);
4070 }
John McCall3cb0ebd2010-03-10 03:28:59 +00004071
Douglas Gregorc3069d62011-01-14 02:55:32 +00004072 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4073 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004074 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorc3069d62011-01-14 02:55:32 +00004075 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004076 return Context.getSubstTemplateTypeParmPackType(
Douglas Gregorc3069d62011-01-14 02:55:32 +00004077 cast<TemplateTypeParmType>(Parm),
4078 ArgPack);
4079 }
4080
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004081 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor409448c2011-07-21 22:35:25 +00004082 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004083 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004084 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004085 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004086 return
Douglas Gregor35942772011-09-09 21:34:22 +00004087 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00004088 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004089
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004090 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004091 unsigned Idx = 0;
4092 unsigned Depth = Record[Idx++];
4093 unsigned Index = Record[Idx++];
4094 bool Pack = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004095 TemplateTypeParmDecl *D
4096 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004097 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004098 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004099
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004100 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004101 unsigned Idx = 0;
4102 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004103 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004104 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004105 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004106 if (!Canon.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004107 Canon = Context.getCanonicalType(Canon);
4108 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004109 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004110
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004111 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004112 unsigned Idx = 0;
4113 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004114 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004115 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004116 unsigned NumArgs = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004117 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004118 Args.reserve(NumArgs);
4119 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004120 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004121 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004122 Args.size(), Args.data());
4123 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004124
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004125 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004126 unsigned Idx = 0;
4127
4128 // ArrayType
Douglas Gregor393f2492011-07-22 00:38:23 +00004129 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004130 ArrayType::ArraySizeModifier ASM
4131 = (ArrayType::ArraySizeModifier)Record[Idx++];
4132 unsigned IndexTypeQuals = Record[Idx++];
4133
4134 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00004135 Expr *NumElts = ReadExpr(*Loc.F);
4136 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004137
Douglas Gregor35942772011-09-09 21:34:22 +00004138 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004139 IndexTypeQuals, Brackets);
4140 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004141
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004142 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004143 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004144 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004145 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner5f9e2722011-07-23 10:55:15 +00004146 SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00004147 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004148 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004149 QualType T;
Richard Smith3e4c6c42011-05-05 21:57:07 +00004150 if (Underlying.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004151 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004152 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004153 else
Douglas Gregor35942772011-09-09 21:34:22 +00004154 T = Context.getTemplateSpecializationType(Name, Args.data(),
Richard Smith3e4c6c42011-05-05 21:57:07 +00004155 Args.size(), Underlying);
John McCallf4c73712011-01-19 06:33:43 +00004156 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004157 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004158 }
Eli Friedmanb001de72011-10-06 23:00:33 +00004159
4160 case TYPE_ATOMIC: {
4161 if (Record.size() != 1) {
4162 Error("Incorrect encoding of atomic type");
4163 return QualType();
4164 }
4165 QualType ValueType = readType(*Loc.F, Record, Idx);
4166 return Context.getAtomicType(ValueType);
4167 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004168 }
David Blaikie7530c032012-01-17 06:56:22 +00004169 llvm_unreachable("Invalid TypeCode!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00004170}
4171
Sebastian Redlc3632732010-10-05 15:59:54 +00004172class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004173 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004174 ModuleFile &F;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004175 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00004176 unsigned &Idx;
4177
Sebastian Redlc3632732010-10-05 15:59:54 +00004178 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4179 unsigned &I) {
4180 return Reader.ReadSourceLocation(F, R, I);
4181 }
4182
Douglas Gregor409448c2011-07-21 22:35:25 +00004183 template<typename T>
4184 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4185 return Reader.ReadDeclAs<T>(F, Record, Idx);
4186 }
4187
John McCalla1ee0c52009-10-16 21:56:05 +00004188public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004189 TypeLocReader(ASTReader &Reader, ModuleFile &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004190 const ASTReader::RecordData &Record, unsigned &Idx)
Benjamin Kramerfacde172012-06-06 17:32:50 +00004191 : Reader(Reader), F(F), Record(Record), Idx(Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00004192 { }
John McCalla1ee0c52009-10-16 21:56:05 +00004193
John McCall51bd8032009-10-18 01:05:36 +00004194 // We want compile-time assurance that we've enumerated all of
4195 // these, so unfortunately we have to declare them first, then
4196 // define them out-of-line.
4197#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00004198#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00004199 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004200#include "clang/AST/TypeLocNodes.def"
4201
John McCall51bd8032009-10-18 01:05:36 +00004202 void VisitFunctionTypeLoc(FunctionTypeLoc);
4203 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004204};
4205
John McCall51bd8032009-10-18 01:05:36 +00004206void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00004207 // nothing to do
4208}
John McCall51bd8032009-10-18 01:05:36 +00004209void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004210 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00004211 if (TL.needsExtraLocalData()) {
4212 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4213 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4214 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4215 TL.setModeAttr(Record[Idx++]);
4216 }
John McCalla1ee0c52009-10-16 21:56:05 +00004217}
John McCall51bd8032009-10-18 01:05:36 +00004218void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004219 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004220}
John McCall51bd8032009-10-18 01:05:36 +00004221void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004222 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004223}
John McCall51bd8032009-10-18 01:05:36 +00004224void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004225 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004226}
John McCall51bd8032009-10-18 01:05:36 +00004227void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004228 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004229}
John McCall51bd8032009-10-18 01:05:36 +00004230void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004231 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004232}
John McCall51bd8032009-10-18 01:05:36 +00004233void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004234 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00004235 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004236}
John McCall51bd8032009-10-18 01:05:36 +00004237void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004238 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4239 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004240 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00004241 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004242 else
John McCall51bd8032009-10-18 01:05:36 +00004243 TL.setSizeExpr(0);
4244}
4245void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4246 VisitArrayTypeLoc(TL);
4247}
4248void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4249 VisitArrayTypeLoc(TL);
4250}
4251void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4252 VisitArrayTypeLoc(TL);
4253}
4254void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4255 DependentSizedArrayTypeLoc TL) {
4256 VisitArrayTypeLoc(TL);
4257}
4258void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4259 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004260 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004261}
4262void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004263 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004264}
4265void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004266 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004267}
4268void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +00004269 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
4270 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004271 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor409448c2011-07-21 22:35:25 +00004272 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004273 }
4274}
4275void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4276 VisitFunctionTypeLoc(TL);
4277}
4278void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4279 VisitFunctionTypeLoc(TL);
4280}
John McCalled976492009-12-04 22:46:56 +00004281void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004282 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00004283}
John McCall51bd8032009-10-18 01:05:36 +00004284void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004285 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004286}
4287void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004288 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4289 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4290 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004291}
4292void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004293 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4294 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4295 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4296 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004297}
4298void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004299 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004300}
Sean Huntca63c202011-05-24 22:41:36 +00004301void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4302 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4303 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4304 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4305 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4306}
Richard Smith34b41d92011-02-20 03:19:35 +00004307void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4308 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4309}
John McCall51bd8032009-10-18 01:05:36 +00004310void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004311 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004312}
4313void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004314 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004315}
John McCall9d156a72011-01-06 01:58:22 +00004316void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4317 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4318 if (TL.hasAttrOperand()) {
4319 SourceRange range;
4320 range.setBegin(ReadSourceLocation(Record, Idx));
4321 range.setEnd(ReadSourceLocation(Record, Idx));
4322 TL.setAttrOperandParensRange(range);
4323 }
4324 if (TL.hasAttrExprOperand()) {
4325 if (Record[Idx++])
4326 TL.setAttrExprOperand(Reader.ReadExpr(F));
4327 else
4328 TL.setAttrExprOperand(0);
4329 } else if (TL.hasAttrEnumOperand())
4330 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4331}
John McCall51bd8032009-10-18 01:05:36 +00004332void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004333 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004334}
John McCall49a832b2009-10-18 09:09:24 +00004335void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4336 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004337 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00004338}
Douglas Gregorc3069d62011-01-14 02:55:32 +00004339void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
4340 SubstTemplateTypeParmPackTypeLoc TL) {
4341 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4342}
John McCall51bd8032009-10-18 01:05:36 +00004343void TypeLocReader::VisitTemplateSpecializationTypeLoc(
4344 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004345 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004346 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4347 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4348 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00004349 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
4350 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00004351 Reader.GetTemplateArgumentLocInfo(F,
4352 TL.getTypePtr()->getArg(i).getKind(),
4353 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004354}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004355void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
4356 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4357 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4358}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004359void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004360 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor9e876872011-03-01 18:12:44 +00004361 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004362}
John McCall3cb0ebd2010-03-10 03:28:59 +00004363void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004364 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00004365}
Douglas Gregor4714c122010-03-31 17:34:00 +00004366void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004367 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor2494dd02011-03-01 01:34:45 +00004368 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004369 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004370}
John McCall33500952010-06-11 00:33:02 +00004371void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
4372 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004373 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004374 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Abramo Bagnara66581d42012-02-06 22:45:07 +00004375 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004376 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004377 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4378 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004379 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
4380 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00004381 Reader.GetTemplateArgumentLocInfo(F,
4382 TL.getTypePtr()->getArg(I).getKind(),
4383 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004384}
Douglas Gregor7536dd52010-12-20 02:24:11 +00004385void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
4386 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
4387}
John McCall51bd8032009-10-18 01:05:36 +00004388void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004389 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00004390}
4391void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
4392 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004393 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4394 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004395 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00004396 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004397}
John McCall54e14c42009-10-22 22:37:11 +00004398void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004399 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00004400}
Eli Friedmanb001de72011-10-06 23:00:33 +00004401void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
4402 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4403 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4404 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4405}
John McCalla1ee0c52009-10-16 21:56:05 +00004406
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004407TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004408 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00004409 unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004410 QualType InfoTy = readType(F, Record, Idx);
John McCalla1ee0c52009-10-16 21:56:05 +00004411 if (InfoTy.isNull())
4412 return 0;
4413
Douglas Gregor35942772011-09-09 21:34:22 +00004414 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00004415 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00004416 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00004417 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00004418 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00004419}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004420
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004421QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00004422 unsigned FastQuals = ID & Qualifiers::FastMask;
4423 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004424
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004425 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004426 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004427 switch ((PredefinedTypeIDs)Index) {
4428 case PREDEF_TYPE_NULL_ID: return QualType();
Douglas Gregor35942772011-09-09 21:34:22 +00004429 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
4430 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004431
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004432 case PREDEF_TYPE_CHAR_U_ID:
4433 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00004434 // FIXME: Check that the signedness of CharTy is correct!
Douglas Gregor35942772011-09-09 21:34:22 +00004435 T = Context.CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004436 break;
4437
Douglas Gregor35942772011-09-09 21:34:22 +00004438 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
4439 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
4440 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
4441 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
4442 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
4443 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
4444 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
4445 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
4446 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
4447 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
4448 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
4449 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
4450 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00004451 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004452 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
4453 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
4454 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
4455 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
4456 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
John McCall3c3b7f92011-10-25 17:37:35 +00004457 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004458 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
4459 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
4460 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
4461 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
4462 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
4463 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
4464 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
4465 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
4466 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004467
4468 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
Douglas Gregor35942772011-09-09 21:34:22 +00004469 T = Context.getAutoRRefDeductType();
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004470 break;
John McCall0ddaeb92011-10-17 18:09:15 +00004471
4472 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
4473 T = Context.ARCUnbridgedCastTy;
4474 break;
4475
Meador Ingefb40e3f2012-07-01 15:57:25 +00004476 case PREDEF_TYPE_VA_LIST_TAG:
4477 T = Context.getVaListTagType();
4478 break;
Eli Friedmana6c66ce2012-08-31 00:14:07 +00004479
4480 case PREDEF_TYPE_BUILTIN_FN:
4481 T = Context.BuiltinFnTy;
4482 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004483 }
4484
4485 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00004486 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004487 }
4488
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004489 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004490 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00004491 if (TypesLoaded[Index].isNull()) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004492 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00004493 if (TypesLoaded[Index].isNull())
4494 return QualType();
4495
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004496 TypesLoaded[Index]->setFromAST();
Sebastian Redl30c514c2010-07-14 23:45:08 +00004497 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004498 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00004499 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00004500 }
Mike Stump1eb44332009-09-09 15:08:12 +00004501
John McCall0953e762009-09-24 19:53:00 +00004502 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004503}
4504
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004505QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004506 return GetType(getGlobalTypeID(F, LocalID));
4507}
4508
4509serialization::TypeID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004510ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
Douglas Gregora119da02011-08-02 16:26:37 +00004511 unsigned FastQuals = LocalID & Qualifiers::FastMask;
4512 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
4513
4514 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
4515 return LocalID;
4516
4517 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4518 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
4519 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
4520
4521 unsigned GlobalIndex = LocalIndex + I->second;
4522 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
4523}
4524
John McCall833ca992009-10-29 08:12:44 +00004525TemplateArgumentLocInfo
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004526ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00004527 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00004528 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004529 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00004530 switch (Kind) {
4531 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004532 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00004533 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00004534 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00004535 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004536 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4537 Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00004538 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004539 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregora7fc9012011-01-05 18:58:31 +00004540 SourceLocation());
4541 }
4542 case TemplateArgument::TemplateExpansion: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004543 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4544 Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004545 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004546 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004547 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregorba68eca2011-01-05 17:40:24 +00004548 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00004549 }
John McCall833ca992009-10-29 08:12:44 +00004550 case TemplateArgument::Null:
4551 case TemplateArgument::Integral:
4552 case TemplateArgument::Declaration:
4553 case TemplateArgument::Pack:
4554 return TemplateArgumentLocInfo();
4555 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00004556 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00004557}
4558
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004559TemplateArgumentLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004560ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004561 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004562 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004563
4564 if (Arg.getKind() == TemplateArgument::Expression) {
4565 if (Record[Index++]) // bool InfoHasSameExpr.
4566 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4567 }
Sebastian Redlc3632732010-10-05 15:59:54 +00004568 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004569 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004570}
4571
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004572Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00004573 return GetDecl(ID);
4574}
4575
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004576uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
Douglas Gregore92b8a12011-08-04 00:01:48 +00004577 unsigned &Idx){
4578 if (Idx >= Record.size())
Douglas Gregor7c789c12010-10-29 22:39:52 +00004579 return 0;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004580
Douglas Gregore92b8a12011-08-04 00:01:48 +00004581 unsigned LocalID = Record[Idx++];
4582 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004583}
4584
4585CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004586 RecordLocation Loc = getLocalBitOffset(Offset);
4587 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004588 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004589 Cursor.JumpToBit(Loc.Offset);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004590 ReadingKindTracker ReadingKind(Read_Decl, *this);
4591 RecordData Record;
4592 unsigned Code = Cursor.ReadCode();
4593 unsigned RecCode = Cursor.ReadRecord(Code, Record);
4594 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4595 Error("Malformed AST file: missing C++ base specifiers");
4596 return 0;
4597 }
4598
4599 unsigned Idx = 0;
4600 unsigned NumBases = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00004601 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004602 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4603 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004604 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004605 return Bases;
4606}
4607
Douglas Gregor409448c2011-07-21 22:35:25 +00004608serialization::DeclID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004609ASTReader::getGlobalDeclID(ModuleFile &F, unsigned LocalID) const {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004610 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregor496c7092011-08-03 15:48:04 +00004611 return LocalID;
4612
4613 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004614 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregor496c7092011-08-03 15:48:04 +00004615 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
4616
4617 return LocalID + I->second;
Douglas Gregor409448c2011-07-21 22:35:25 +00004618}
4619
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004620bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004621 ModuleFile &M) const {
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004622 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
4623 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4624 return &M == I->second;
4625}
4626
Douglas Gregorcff9f262012-01-27 01:47:08 +00004627ModuleFile *ASTReader::getOwningModuleFile(Decl *D) {
4628 if (!D->isFromASTFile())
4629 return 0;
4630 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
4631 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4632 return I->second;
4633}
4634
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004635SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
4636 if (ID < NUM_PREDEF_DECL_IDS)
4637 return SourceLocation();
4638
4639 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4640
4641 if (Index > DeclsLoaded.size()) {
4642 Error("declaration ID out-of-range for AST file");
4643 return SourceLocation();
4644 }
4645
4646 if (Decl *D = DeclsLoaded[Index])
4647 return D->getLocation();
4648
4649 unsigned RawLocation = 0;
4650 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
4651 return ReadSourceLocation(*Rec.F, RawLocation);
4652}
4653
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004654Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004655 if (ID < NUM_PREDEF_DECL_IDS) {
4656 switch ((PredefinedDeclIDs)ID) {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004657 case PREDEF_DECL_NULL_ID:
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004658 return 0;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004659
4660 case PREDEF_DECL_TRANSLATION_UNIT_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004661 return Context.getTranslationUnitDecl();
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00004662
4663 case PREDEF_DECL_OBJC_ID_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004664 return Context.getObjCIdDecl();
Douglas Gregor79d67262011-08-12 05:59:41 +00004665
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004666 case PREDEF_DECL_OBJC_SEL_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004667 return Context.getObjCSelDecl();
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004668
Douglas Gregor79d67262011-08-12 05:59:41 +00004669 case PREDEF_DECL_OBJC_CLASS_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004670 return Context.getObjCClassDecl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004671
Douglas Gregora6ea10e2012-01-17 18:09:05 +00004672 case PREDEF_DECL_OBJC_PROTOCOL_ID:
4673 return Context.getObjCProtocolDecl();
4674
Douglas Gregor772eeae2011-08-12 06:49:56 +00004675 case PREDEF_DECL_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004676 return Context.getInt128Decl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004677
4678 case PREDEF_DECL_UNSIGNED_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004679 return Context.getUInt128Decl();
Douglas Gregore97179c2011-09-08 01:46:34 +00004680
4681 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004682 return Context.getObjCInstanceTypeDecl();
Meador Ingec5613b22012-06-16 03:34:49 +00004683
4684 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
4685 return Context.getBuiltinVaListDecl();
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004686 }
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004687 }
4688
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004689 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4690
Richard Smith2fbf3732011-12-20 04:39:57 +00004691 if (Index >= DeclsLoaded.size()) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004692 assert(0 && "declaration ID out-of-range for AST file");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004693 Error("declaration ID out-of-range for AST file");
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004694 return 0;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004695 }
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004696
Douglas Gregorfd002a72011-12-16 22:37:11 +00004697 if (!DeclsLoaded[Index]) {
Douglas Gregor496c7092011-08-03 15:48:04 +00004698 ReadDeclRecord(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00004699 if (DeserializationListener)
4700 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
4701 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004702
4703 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004704}
4705
Douglas Gregora1be2782011-12-17 23:38:30 +00004706DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
4707 DeclID GlobalID) {
4708 if (GlobalID < NUM_PREDEF_DECL_IDS)
4709 return GlobalID;
4710
4711 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
4712 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4713 ModuleFile *Owner = I->second;
4714
4715 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
4716 = M.GlobalToLocalDeclIDs.find(Owner);
4717 if (Pos == M.GlobalToLocalDeclIDs.end())
4718 return 0;
4719
4720 return GlobalID - Owner->BaseDeclID + Pos->second;
4721}
4722
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004723serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00004724 const RecordData &Record,
4725 unsigned &Idx) {
4726 if (Idx >= Record.size()) {
4727 Error("Corrupted AST file");
4728 return 0;
4729 }
4730
4731 return getGlobalDeclID(F, Record[Idx++]);
4732}
4733
Chris Lattner887e2b32009-04-27 05:46:25 +00004734/// \brief Resolve the offset of a statement into a statement.
4735///
4736/// This operation will read a new statement from the external
4737/// source each time it is called, and is meant to be used via a
4738/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004739Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004740 // Switch case IDs are per Decl.
4741 ClearSwitchCaseIDs();
4742
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004743 // Offset here is a global offset across the entire chain.
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004744 RecordLocation Loc = getLocalBitOffset(Offset);
4745 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
4746 return ReadStmtFromStream(*Loc.F);
Douglas Gregor250fc9c2009-04-18 00:07:54 +00004747}
4748
Douglas Gregor851c75a2011-08-24 21:27:34 +00004749namespace {
4750 class FindExternalLexicalDeclsVisitor {
4751 ASTReader &Reader;
4752 const DeclContext *DC;
4753 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004754
Douglas Gregor851c75a2011-08-24 21:27:34 +00004755 SmallVectorImpl<Decl*> &Decls;
4756 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
4757
4758 public:
4759 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
4760 bool (*isKindWeWant)(Decl::Kind),
4761 SmallVectorImpl<Decl*> &Decls)
4762 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
4763 {
4764 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
4765 PredefsVisited[I] = false;
4766 }
4767
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004768 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
Douglas Gregor851c75a2011-08-24 21:27:34 +00004769 if (Preorder)
4770 return false;
4771
4772 FindExternalLexicalDeclsVisitor *This
4773 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
4774
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004775 ModuleFile::DeclContextInfosMap::iterator Info
Douglas Gregor851c75a2011-08-24 21:27:34 +00004776 = M.DeclContextInfos.find(This->DC);
4777 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
4778 return false;
4779
4780 // Load all of the declaration IDs
4781 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
4782 *IDE = ID + Info->second.NumLexicalDecls;
4783 ID != IDE; ++ID) {
4784 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
4785 continue;
4786
4787 // Don't add predefined declarations to the lexical context more
4788 // than once.
4789 if (ID->second < NUM_PREDEF_DECL_IDS) {
4790 if (This->PredefsVisited[ID->second])
4791 continue;
4792
4793 This->PredefsVisited[ID->second] = true;
4794 }
4795
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004796 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
4797 if (!This->DC->isDeclInLexicalTraversal(D))
4798 This->Decls.push_back(D);
4799 }
Douglas Gregor851c75a2011-08-24 21:27:34 +00004800 }
4801
4802 return false;
4803 }
4804 };
4805}
4806
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004807ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00004808 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner5f9e2722011-07-23 10:55:15 +00004809 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00004810 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor851c75a2011-08-24 21:27:34 +00004811 // least. Walk all of the modules in the order they were loaded.
4812 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
4813 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00004814 ++NumLexicalDeclContextsRead;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004815 return ELR_Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004816}
4817
Douglas Gregor0d95f772011-08-24 19:03:07 +00004818namespace {
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004819
4820class DeclIDComp {
4821 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004822 ModuleFile &Mod;
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004823
4824public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004825 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004826
4827 bool operator()(LocalDeclID L, LocalDeclID R) const {
4828 SourceLocation LHS = getLocation(L);
4829 SourceLocation RHS = getLocation(R);
4830 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4831 }
4832
4833 bool operator()(SourceLocation LHS, LocalDeclID R) const {
4834 SourceLocation RHS = getLocation(R);
4835 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4836 }
4837
4838 bool operator()(LocalDeclID L, SourceLocation RHS) const {
4839 SourceLocation LHS = getLocation(L);
4840 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4841 }
4842
4843 SourceLocation getLocation(LocalDeclID ID) const {
4844 return Reader.getSourceManager().getFileLoc(
4845 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
4846 }
4847};
4848
4849}
4850
4851void ASTReader::FindFileRegionDecls(FileID File,
4852 unsigned Offset, unsigned Length,
4853 SmallVectorImpl<Decl *> &Decls) {
4854 SourceManager &SM = getSourceManager();
4855
4856 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
4857 if (I == FileDeclIDs.end())
4858 return;
4859
4860 FileDeclsInfo &DInfo = I->second;
4861 if (DInfo.Decls.empty())
4862 return;
4863
4864 SourceLocation
4865 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
4866 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
4867
4868 DeclIDComp DIDComp(*this, *DInfo.Mod);
4869 ArrayRef<serialization::LocalDeclID>::iterator
4870 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
4871 BeginLoc, DIDComp);
4872 if (BeginIt != DInfo.Decls.begin())
4873 --BeginIt;
4874
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00004875 // If we are pointing at a top-level decl inside an objc container, we need
4876 // to backtrack until we find it otherwise we will fail to report that the
4877 // region overlaps with an objc container.
4878 while (BeginIt != DInfo.Decls.begin() &&
4879 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
4880 ->isTopLevelDeclInObjCContainer())
4881 --BeginIt;
4882
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004883 ArrayRef<serialization::LocalDeclID>::iterator
4884 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
4885 EndLoc, DIDComp);
4886 if (EndIt != DInfo.Decls.end())
4887 ++EndIt;
4888
4889 for (ArrayRef<serialization::LocalDeclID>::iterator
4890 DIt = BeginIt; DIt != EndIt; ++DIt)
4891 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
4892}
4893
4894namespace {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004895 /// \brief ModuleFile visitor used to perform name lookup into a
Douglas Gregor0d95f772011-08-24 19:03:07 +00004896 /// declaration context.
4897 class DeclContextNameLookupVisitor {
4898 ASTReader &Reader;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004899 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Douglas Gregor0d95f772011-08-24 19:03:07 +00004900 DeclarationName Name;
4901 SmallVectorImpl<NamedDecl *> &Decls;
4902
4903 public:
4904 DeclContextNameLookupVisitor(ASTReader &Reader,
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004905 SmallVectorImpl<const DeclContext *> &Contexts,
4906 DeclarationName Name,
Douglas Gregor0d95f772011-08-24 19:03:07 +00004907 SmallVectorImpl<NamedDecl *> &Decls)
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004908 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
Douglas Gregor0d95f772011-08-24 19:03:07 +00004909
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004910 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00004911 DeclContextNameLookupVisitor *This
4912 = static_cast<DeclContextNameLookupVisitor *>(UserData);
4913
4914 // Check whether we have any visible declaration information for
4915 // this context in this module.
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004916 ModuleFile::DeclContextInfosMap::iterator Info;
4917 bool FoundInfo = false;
4918 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
4919 Info = M.DeclContextInfos.find(This->Contexts[I]);
4920 if (Info != M.DeclContextInfos.end() &&
4921 Info->second.NameLookupTableData) {
4922 FoundInfo = true;
4923 break;
4924 }
4925 }
Douglas Gregor0d95f772011-08-24 19:03:07 +00004926
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004927 if (!FoundInfo)
4928 return false;
4929
Douglas Gregor0d95f772011-08-24 19:03:07 +00004930 // Look for this name within this module.
4931 ASTDeclContextNameLookupTable *LookupTable =
Benjamin Kramerb1758c62012-04-15 12:36:49 +00004932 Info->second.NameLookupTableData;
Douglas Gregor0d95f772011-08-24 19:03:07 +00004933 ASTDeclContextNameLookupTable::iterator Pos
4934 = LookupTable->find(This->Name);
4935 if (Pos == LookupTable->end())
4936 return false;
4937
4938 bool FoundAnything = false;
4939 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
4940 for (; Data.first != Data.second; ++Data.first) {
4941 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
4942 if (!ND)
4943 continue;
4944
4945 if (ND->getDeclName() != This->Name) {
4946 assert(!This->Name.getCXXNameType().isNull() &&
4947 "Name mismatch without a type");
4948 continue;
4949 }
4950
4951 // Record this declaration.
4952 FoundAnything = true;
4953 This->Decls.push_back(ND);
4954 }
4955
4956 return FoundAnything;
4957 }
4958 };
4959}
4960
John McCall76bd1f32010-06-01 09:23:16 +00004961DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004962ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00004963 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00004964 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00004965 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00004966 if (!Name)
4967 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
4968 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004969
Chris Lattner5f9e2722011-07-23 10:55:15 +00004970 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004971
4972 // Compute the declaration contexts we need to look into. Multiple such
4973 // declaration contexts occur when two declaration contexts from disjoint
4974 // modules get merged, e.g., when two namespaces with the same name are
4975 // independently defined in separate modules.
4976 SmallVector<const DeclContext *, 2> Contexts;
4977 Contexts.push_back(DC);
4978
4979 if (DC->isNamespace()) {
4980 MergedDeclsMap::iterator Merged
4981 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
4982 if (Merged != MergedDecls.end()) {
4983 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
4984 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
4985 }
4986 }
4987
4988 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor0d95f772011-08-24 19:03:07 +00004989 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00004990 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00004991 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00004992 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004993}
4994
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00004995namespace {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00004996 /// \brief ModuleFile visitor used to retrieve all visible names in a
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00004997 /// declaration context.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00004998 class DeclContextAllNamesVisitor {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00004999 ASTReader &Reader;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005000 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005001 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005002
5003 public:
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005004 DeclContextAllNamesVisitor(ASTReader &Reader,
5005 SmallVectorImpl<const DeclContext *> &Contexts,
5006 llvm::DenseMap<DeclarationName,
5007 SmallVector<NamedDecl *, 8> > &Decls)
5008 : Reader(Reader), Contexts(Contexts), Decls(Decls) { }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005009
5010 static bool visit(ModuleFile &M, void *UserData) {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005011 DeclContextAllNamesVisitor *This
5012 = static_cast<DeclContextAllNamesVisitor *>(UserData);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005013
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005014 // Check whether we have any visible declaration information for
5015 // this context in this module.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005016 ModuleFile::DeclContextInfosMap::iterator Info;
5017 bool FoundInfo = false;
5018 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5019 Info = M.DeclContextInfos.find(This->Contexts[I]);
5020 if (Info != M.DeclContextInfos.end() &&
5021 Info->second.NameLookupTableData) {
5022 FoundInfo = true;
5023 break;
5024 }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005025 }
5026
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005027 if (!FoundInfo)
5028 return false;
5029
5030 ASTDeclContextNameLookupTable *LookupTable =
5031 Info->second.NameLookupTableData;
5032 bool FoundAnything = false;
5033 for (ASTDeclContextNameLookupTable::data_iterator
5034 I = LookupTable->data_begin(), E = LookupTable->data_end();
5035 I != E; ++I) {
5036 ASTDeclContextNameLookupTrait::data_type Data = *I;
5037 for (; Data.first != Data.second; ++Data.first) {
5038 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5039 *Data.first);
5040 if (!ND)
5041 continue;
5042
5043 // Record this declaration.
5044 FoundAnything = true;
5045 This->Decls[ND->getDeclName()].push_back(ND);
5046 }
5047 }
5048
5049 return FoundAnything;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005050 }
5051 };
5052}
5053
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005054void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005055 if (!DC->hasExternalVisibleStorage())
5056 return;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005057 llvm::DenseMap<DeclarationName, llvm::SmallVector<NamedDecl*, 8> > Decls;
5058
5059 // Compute the declaration contexts we need to look into. Multiple such
5060 // declaration contexts occur when two declaration contexts from disjoint
5061 // modules get merged, e.g., when two namespaces with the same name are
5062 // independently defined in separate modules.
5063 SmallVector<const DeclContext *, 2> Contexts;
5064 Contexts.push_back(DC);
5065
5066 if (DC->isNamespace()) {
5067 MergedDeclsMap::iterator Merged
5068 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5069 if (Merged != MergedDecls.end()) {
5070 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5071 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5072 }
5073 }
5074
5075 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls);
5076 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5077 ++NumVisibleDeclContextsRead;
5078
5079 for (llvm::DenseMap<DeclarationName,
5080 llvm::SmallVector<NamedDecl*, 8> >::iterator
5081 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5082 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5083 }
Argyrios Kyrtzidis394e5392012-04-26 18:34:14 +00005084 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005085}
5086
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005087/// \brief Under non-PCH compilation the consumer receives the objc methods
5088/// before receiving the implementation, and codegen depends on this.
5089/// We simulate this by deserializing and passing to consumer the methods of the
5090/// implementation before passing the deserialized implementation decl.
5091static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5092 ASTConsumer *Consumer) {
5093 assert(ImplD && Consumer);
5094
5095 for (ObjCImplDecl::method_iterator
5096 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00005097 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005098
5099 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5100}
5101
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005102void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005103 assert(Consumer);
5104 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005105 Decl *D = InterestingDecls.front();
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005106 InterestingDecls.pop_front();
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005107
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005108 PassInterestingDeclToConsumer(D);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005109 }
5110}
5111
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005112void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5113 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5114 PassObjCImplDeclToConsumer(ImplD, Consumer);
5115 else
5116 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5117}
5118
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005119void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00005120 this->Consumer = Consumer;
5121
Douglas Gregorfdd01722009-04-14 00:24:19 +00005122 if (!Consumer)
5123 return;
5124
5125 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005126 // Force deserialization of this decl, which will cause it to be queued for
5127 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00005128 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00005129 }
Douglas Gregor1a995dd2011-09-15 18:47:32 +00005130 ExternalDefinitions.clear();
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00005131
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005132 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00005133}
5134
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005135void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005136 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00005137
Mike Stump1eb44332009-09-09 15:08:12 +00005138 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005139 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00005140 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005141 unsigned NumDeclsLoaded
5142 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5143 (Decl *)0);
5144 unsigned NumIdentifiersLoaded
5145 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5146 IdentifiersLoaded.end(),
5147 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00005148 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005149 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5150 SelectorsLoaded.end(),
5151 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00005152
Douglas Gregor4fed3f42009-04-27 18:38:38 +00005153 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
5154 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor0cdd7982011-07-21 18:46:38 +00005155 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005156 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5157 NumSLocEntriesRead, TotalNumSLocEntries,
5158 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005159 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005160 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005161 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5162 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5163 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005164 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005165 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5166 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005167 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005168 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005169 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5170 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00005171 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005172 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00005173 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5174 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00005175 if (TotalNumStatements)
5176 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5177 NumStatementsRead, TotalNumStatements,
5178 ((float)NumStatementsRead/TotalNumStatements * 100));
5179 if (TotalNumMacros)
5180 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5181 NumMacrosRead, TotalNumMacros,
5182 ((float)NumMacrosRead/TotalNumMacros * 100));
5183 if (TotalLexicalDeclContexts)
5184 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5185 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5186 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5187 * 100));
5188 if (TotalVisibleDeclContexts)
5189 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5190 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5191 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5192 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005193 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005194 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005195 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5196 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00005197 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005198 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00005199 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00005200 std::fprintf(stderr, "\n");
Douglas Gregor23d7df52011-07-21 19:50:14 +00005201 dump();
5202 std::fprintf(stderr, "\n");
5203}
5204
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005205template<typename Key, typename ModuleFile, unsigned InitialCapacity>
Douglas Gregor23d7df52011-07-21 19:50:14 +00005206static void
Chris Lattner5f9e2722011-07-23 10:55:15 +00005207dumpModuleIDMap(StringRef Name,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005208 const ContinuousRangeMap<Key, ModuleFile *,
Douglas Gregor23d7df52011-07-21 19:50:14 +00005209 InitialCapacity> &Map) {
5210 if (Map.begin() == Map.end())
5211 return;
5212
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005213 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
Douglas Gregor23d7df52011-07-21 19:50:14 +00005214 llvm::errs() << Name << ":\n";
5215 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5216 I != IEnd; ++I) {
5217 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5218 << "\n";
5219 }
5220}
5221
Douglas Gregor23d7df52011-07-21 19:50:14 +00005222void ASTReader::dump() {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005223 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005224 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor23d7df52011-07-21 19:50:14 +00005225 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor1e849b62011-07-29 00:21:44 +00005226 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005227 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005228 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
Douglas Gregor26ced122011-12-01 00:59:36 +00005229 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005230 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005231 dumpModuleIDMap("Global preprocessed entity map",
5232 GlobalPreprocessedEntityMap);
Douglas Gregor8df5c9b2011-08-02 11:12:41 +00005233
5234 llvm::errs() << "\n*** PCH/Modules Loaded:";
5235 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5236 MEnd = ModuleMgr.end();
5237 M != MEnd; ++M)
5238 (*M)->dump();
Douglas Gregor2cf26342009-04-09 22:27:44 +00005239}
5240
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005241/// Return the amount of memory used by memory buffers, breaking down
5242/// by heap-backed versus mmap'ed memory.
5243void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005244 for (ModuleConstIterator I = ModuleMgr.begin(),
5245 E = ModuleMgr.end(); I != E; ++I) {
5246 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005247 size_t bytes = buf->getBufferSize();
5248 switch (buf->getBufferKind()) {
5249 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5250 sizes.malloc_bytes += bytes;
5251 break;
5252 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5253 sizes.mmap_bytes += bytes;
5254 break;
5255 }
5256 }
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005257 }
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005258}
5259
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005260void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005261 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005262 S.ExternalSource = this;
5263
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005264 // Makes sure any declarations that were deserialized "too early"
5265 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00005266 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005267 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
5268 PreloadedDecls[I]->getDeclName());
Douglas Gregor668c1a42009-04-21 22:25:48 +00005269 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005270 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00005271
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005272 // Load the offsets of the declarations that Sema references.
5273 // They will be lazily deserialized when needed.
5274 if (!SemaDeclRefs.empty()) {
5275 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregor1e5b6f62011-07-28 00:57:24 +00005276 if (!SemaObj->StdNamespace)
5277 SemaObj->StdNamespace = SemaDeclRefs[0];
5278 if (!SemaObj->StdBadAlloc)
5279 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005280 }
5281
Peter Collingbourne84bccea2011-02-15 19:46:30 +00005282 if (!FPPragmaOptions.empty()) {
5283 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
5284 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
5285 }
5286
5287 if (!OpenCLExtensions.empty()) {
5288 unsigned I = 0;
5289#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
5290#include "clang/Basic/OpenCLExtensions.def"
5291
5292 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
5293 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00005294}
5295
Douglas Gregor211f6e82011-08-20 04:39:52 +00005296IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregor057df202012-01-18 20:56:22 +00005297 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart),
5298 /*PriorGeneration=*/0);
Douglas Gregor211f6e82011-08-20 04:39:52 +00005299 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005300 IdentifierInfo *II = Visitor.getIdentifierInfo();
Douglas Gregor057df202012-01-18 20:56:22 +00005301 markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005302 return II;
Douglas Gregor668c1a42009-04-21 22:25:48 +00005303}
5304
Douglas Gregor95f42922010-10-14 22:11:03 +00005305namespace clang {
5306 /// \brief An identifier-lookup iterator that enumerates all of the
5307 /// identifiers stored within a set of AST files.
5308 class ASTIdentifierIterator : public IdentifierIterator {
5309 /// \brief The AST reader whose identifiers are being enumerated.
5310 const ASTReader &Reader;
5311
5312 /// \brief The current index into the chain of AST files stored in
5313 /// the AST reader.
5314 unsigned Index;
5315
5316 /// \brief The current position within the identifier lookup table
5317 /// of the current AST file.
5318 ASTIdentifierLookupTable::key_iterator Current;
5319
5320 /// \brief The end position within the identifier lookup table of
5321 /// the current AST file.
5322 ASTIdentifierLookupTable::key_iterator End;
5323
5324 public:
5325 explicit ASTIdentifierIterator(const ASTReader &Reader);
5326
Chris Lattner5f9e2722011-07-23 10:55:15 +00005327 virtual StringRef Next();
Douglas Gregor95f42922010-10-14 22:11:03 +00005328 };
5329}
5330
5331ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005332 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor95f42922010-10-14 22:11:03 +00005333 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005334 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005335 Current = IdTable->key_begin();
5336 End = IdTable->key_end();
5337}
5338
Chris Lattner5f9e2722011-07-23 10:55:15 +00005339StringRef ASTIdentifierIterator::Next() {
Douglas Gregor95f42922010-10-14 22:11:03 +00005340 while (Current == End) {
5341 // If we have exhausted all of our AST files, we're done.
5342 if (Index == 0)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005343 return StringRef();
Douglas Gregor95f42922010-10-14 22:11:03 +00005344
5345 --Index;
5346 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005347 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
5348 IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005349 Current = IdTable->key_begin();
5350 End = IdTable->key_end();
5351 }
5352
5353 // We have any identifiers remaining in the current AST file; return
5354 // the next one.
5355 std::pair<const char*, unsigned> Key = *Current;
5356 ++Current;
Chris Lattner5f9e2722011-07-23 10:55:15 +00005357 return StringRef(Key.first, Key.second);
Douglas Gregor95f42922010-10-14 22:11:03 +00005358}
5359
5360IdentifierIterator *ASTReader::getIdentifiers() const {
5361 return new ASTIdentifierIterator(*this);
5362}
5363
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005364namespace clang { namespace serialization {
5365 class ReadMethodPoolVisitor {
5366 ASTReader &Reader;
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005367 Selector Sel;
5368 unsigned PriorGeneration;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005369 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
5370 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005371
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005372 public:
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005373 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
5374 unsigned PriorGeneration)
5375 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005376
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005377 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005378 ReadMethodPoolVisitor *This
5379 = static_cast<ReadMethodPoolVisitor *>(UserData);
5380
5381 if (!M.SelectorLookupTable)
5382 return false;
5383
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005384 // If we've already searched this module file, skip it now.
5385 if (M.Generation <= This->PriorGeneration)
5386 return true;
5387
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005388 ASTSelectorLookupTable *PoolTable
5389 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
5390 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
5391 if (Pos == PoolTable->end())
5392 return false;
5393
5394 ++This->Reader.NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005395 // FIXME: Not quite happy with the statistics here. We probably should
5396 // disable this tracking when called via LoadSelector.
5397 // Also, should entries without methods count as misses?
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005398 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005399 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005400 if (This->Reader.DeserializationListener)
5401 This->Reader.DeserializationListener->SelectorRead(Data.ID,
5402 This->Sel);
5403
5404 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
5405 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
5406 return true;
Sebastian Redl725cd962010-08-04 20:40:17 +00005407 }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005408
5409 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005410 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
5411 return InstanceMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005412 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005413
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005414 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005415 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
5416 return FactoryMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005417 }
5418 };
5419} } // end namespace clang::serialization
5420
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005421/// \brief Add the given set of methods to the method list.
5422static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
5423 ObjCMethodList &List) {
5424 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
5425 S.addMethodToGlobalList(&List, Methods[I]);
5426 }
5427}
5428
5429void ASTReader::ReadMethodPool(Selector Sel) {
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005430 // Get the selector generation and update it to the current generation.
5431 unsigned &Generation = SelectorGeneration[Sel];
5432 unsigned PriorGeneration = Generation;
5433 Generation = CurrentGeneration;
5434
5435 // Search for methods defined with this selector.
5436 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005437 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005438
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005439 if (Visitor.getInstanceMethods().empty() &&
5440 Visitor.getFactoryMethods().empty()) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005441 ++NumMethodPoolMisses;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005442 return;
5443 }
5444
5445 if (!getSema())
5446 return;
5447
5448 Sema &S = *getSema();
5449 Sema::GlobalMethodPool::iterator Pos
5450 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
5451
5452 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
5453 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005454}
5455
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005456void ASTReader::ReadKnownNamespaces(
Chris Lattner5f9e2722011-07-23 10:55:15 +00005457 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005458 Namespaces.clear();
5459
5460 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
5461 if (NamespaceDecl *Namespace
5462 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
5463 Namespaces.push_back(Namespace);
5464 }
5465}
5466
Douglas Gregora8623202011-07-27 20:58:46 +00005467void ASTReader::ReadTentativeDefinitions(
5468 SmallVectorImpl<VarDecl *> &TentativeDefs) {
5469 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
5470 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
5471 if (Var)
5472 TentativeDefs.push_back(Var);
5473 }
5474 TentativeDefinitions.clear();
5475}
5476
Douglas Gregora2ee20a2011-07-27 21:45:57 +00005477void ASTReader::ReadUnusedFileScopedDecls(
5478 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
5479 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
5480 DeclaratorDecl *D
5481 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
5482 if (D)
5483 Decls.push_back(D);
5484 }
5485 UnusedFileScopedDecls.clear();
5486}
5487
Douglas Gregor0129b562011-07-27 21:57:17 +00005488void ASTReader::ReadDelegatingConstructors(
5489 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
5490 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
5491 CXXConstructorDecl *D
5492 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
5493 if (D)
5494 Decls.push_back(D);
5495 }
5496 DelegatingCtorDecls.clear();
5497}
5498
Douglas Gregord58a0a52011-07-28 00:39:29 +00005499void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
5500 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
5501 TypedefNameDecl *D
5502 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
5503 if (D)
5504 Decls.push_back(D);
5505 }
5506 ExtVectorDecls.clear();
5507}
5508
Douglas Gregora126f172011-07-28 00:53:40 +00005509void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
5510 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
5511 CXXRecordDecl *D
5512 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
5513 if (D)
5514 Decls.push_back(D);
5515 }
5516 DynamicClasses.clear();
5517}
5518
Douglas Gregorec12ce22011-07-28 14:20:37 +00005519void
5520ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
5521 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
5522 NamedDecl *D
5523 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
5524 if (D)
5525 Decls.push_back(D);
5526 }
5527 LocallyScopedExternalDecls.clear();
5528}
5529
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00005530void ASTReader::ReadReferencedSelectors(
5531 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
5532 if (ReferencedSelectorsData.empty())
5533 return;
5534
5535 // If there are @selector references added them to its pool. This is for
5536 // implementation of -Wselector.
5537 unsigned int DataSize = ReferencedSelectorsData.size()-1;
5538 unsigned I = 0;
5539 while (I < DataSize) {
5540 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
5541 SourceLocation SelLoc
5542 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
5543 Sels.push_back(std::make_pair(Sel, SelLoc));
5544 }
5545 ReferencedSelectorsData.clear();
5546}
5547
Douglas Gregor31e37b22011-07-28 18:09:57 +00005548void ASTReader::ReadWeakUndeclaredIdentifiers(
5549 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
5550 if (WeakUndeclaredIdentifiers.empty())
5551 return;
5552
5553 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
5554 IdentifierInfo *WeakId
5555 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5556 IdentifierInfo *AliasId
5557 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5558 SourceLocation Loc
5559 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
5560 bool Used = WeakUndeclaredIdentifiers[I++];
5561 WeakInfo WI(AliasId, Loc);
5562 WI.setUsed(Used);
5563 WeakIDs.push_back(std::make_pair(WeakId, WI));
5564 }
5565 WeakUndeclaredIdentifiers.clear();
5566}
5567
Douglas Gregordfe65432011-07-28 19:11:31 +00005568void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
5569 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
5570 ExternalVTableUse VT;
5571 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
5572 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
5573 VT.DefinitionRequired = VTableUses[Idx++];
5574 VTables.push_back(VT);
5575 }
5576
5577 VTableUses.clear();
5578}
5579
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00005580void ASTReader::ReadPendingInstantiations(
5581 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
5582 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
5583 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
5584 SourceLocation Loc
5585 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
5586 Pending.push_back(std::make_pair(D, Loc));
5587 }
5588 PendingInstantiations.clear();
5589}
5590
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005591void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00005592 // It would be complicated to avoid reading the methods anyway. So don't.
5593 ReadMethodPool(Sel);
5594}
5595
Douglas Gregor95eab172011-07-28 20:55:49 +00005596void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005597 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00005598 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005599 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005600 if (DeserializationListener)
5601 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00005602}
5603
Douglas Gregord89275b2009-07-06 18:54:52 +00005604/// \brief Set the globally-visible declarations associated with the given
5605/// identifier.
5606///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005607/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00005608/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00005609/// them.
5610///
5611/// \param II an IdentifierInfo that refers to one or more globally-visible
5612/// declarations.
5613///
5614/// \param DeclIDs the set of declaration IDs with the name @p II that are
5615/// visible at global scope.
5616///
5617/// \param Nonrecursive should be true to indicate that the caller knows that
5618/// this call is non-recursive, and therefore the globally-visible declarations
5619/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00005620void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005621ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner5f9e2722011-07-23 10:55:15 +00005622 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregord89275b2009-07-06 18:54:52 +00005623 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005624 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00005625 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
5626 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
5627 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00005628 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00005629 return;
5630 }
Mike Stump1eb44332009-09-09 15:08:12 +00005631
Douglas Gregord89275b2009-07-06 18:54:52 +00005632 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
5633 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
5634 if (SemaObj) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005635 // Introduce this declaration into the translation-unit scope
5636 // and add it to the declaration chain for this identifier, so
5637 // that (unqualified) name lookup will find it.
5638 SemaObj->pushExternalDeclIntoScope(D, II);
Douglas Gregord89275b2009-07-06 18:54:52 +00005639 } else {
5640 // Queue this declaration so that it will be added to the
5641 // translation unit scope and identifier's declaration chain
5642 // once a Sema object is known.
5643 PreloadedDecls.push_back(D);
5644 }
5645 }
5646}
5647
Douglas Gregor95eab172011-07-28 20:55:49 +00005648IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00005649 if (ID == 0)
5650 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00005651
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005652 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005653 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00005654 return 0;
5655 }
Mike Stump1eb44332009-09-09 15:08:12 +00005656
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005657 ID -= 1;
5658 if (!IdentifiersLoaded[ID]) {
Douglas Gregor67268d02011-07-20 00:59:32 +00005659 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
5660 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005661 ModuleFile *M = I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00005662 unsigned Index = ID - M->BaseIdentifierID;
5663 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregord6595a42009-04-25 21:04:17 +00005664
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005665 // All of the strings in the AST file are preceded by a 16-bit length.
5666 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00005667 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
5668 // unsigned integers. This is important to avoid integer overflow when
5669 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00005670 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00005671 unsigned StrLen = (((unsigned) StrLenPtr[0])
5672 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005673 IdentifiersLoaded[ID]
Douglas Gregor712f2fc2011-09-09 22:02:16 +00005674 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005675 if (DeserializationListener)
5676 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00005677 }
Mike Stump1eb44332009-09-09 15:08:12 +00005678
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005679 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00005680}
5681
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005682IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
Douglas Gregor95eab172011-07-28 20:55:49 +00005683 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
5684}
5685
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005686IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +00005687 if (LocalID < NUM_PREDEF_IDENT_IDS)
5688 return LocalID;
5689
5690 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5691 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
5692 assert(I != M.IdentifierRemap.end()
5693 && "Invalid index into identifier index remap");
5694
5695 return LocalID + I->second;
Douglas Gregor95eab172011-07-28 20:55:49 +00005696}
5697
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005698bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregore23ac652011-04-20 00:21:03 +00005699 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005700}
5701
Douglas Gregor26ced122011-12-01 00:59:36 +00005702serialization::SubmoduleID
5703ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
5704 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
5705 return LocalID;
5706
5707 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5708 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
5709 assert(I != M.SubmoduleRemap.end()
5710 && "Invalid index into identifier index remap");
5711
5712 return LocalID + I->second;
5713}
5714
5715Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
5716 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
5717 assert(GlobalID == 0 && "Unhandled global submodule ID");
5718 return 0;
5719 }
5720
5721 if (GlobalID > SubmodulesLoaded.size()) {
5722 Error("submodule ID out of range in AST file");
5723 return 0;
5724 }
5725
5726 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
5727}
5728
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005729Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
Douglas Gregor2d2689a2011-07-28 21:16:51 +00005730 return DecodeSelector(getGlobalSelectorID(M, LocalID));
5731}
5732
5733Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005734 if (ID == 0)
5735 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00005736
Sebastian Redl725cd962010-08-04 20:40:17 +00005737 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005738 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005739 return Selector();
5740 }
Douglas Gregor83941df2009-04-25 17:48:32 +00005741
Sebastian Redl725cd962010-08-04 20:40:17 +00005742 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005743 // Load this selector from the selector table.
Douglas Gregor96958cb2011-07-20 01:10:58 +00005744 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
5745 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005746 ModuleFile &M = *I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00005747 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005748 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor96958cb2011-07-20 01:10:58 +00005749 SelectorsLoaded[ID - 1] =
Douglas Gregor9827a802011-07-29 00:56:45 +00005750 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor96958cb2011-07-20 01:10:58 +00005751 if (DeserializationListener)
5752 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor83941df2009-04-25 17:48:32 +00005753 }
5754
Sebastian Redl725cd962010-08-04 20:40:17 +00005755 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005756}
5757
Douglas Gregor8451ec72011-07-28 14:41:43 +00005758Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00005759 return DecodeSelector(ID);
5760}
5761
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005762uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00005763 // ID 0 (the null selector) is considered an external selector.
5764 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00005765}
5766
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005767serialization::SelectorID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005768ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005769 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
5770 return LocalID;
5771
5772 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5773 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
5774 assert(I != M.SelectorRemap.end()
5775 && "Invalid index into identifier index remap");
5776
5777 return LocalID + I->second;
Douglas Gregor8451ec72011-07-28 14:41:43 +00005778}
5779
Mike Stump1eb44332009-09-09 15:08:12 +00005780DeclarationName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005781ASTReader::ReadDeclarationName(ModuleFile &F,
Douglas Gregor393f2492011-07-22 00:38:23 +00005782 const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00005783 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
5784 switch (Kind) {
5785 case DeclarationName::Identifier:
Douglas Gregor95eab172011-07-28 20:55:49 +00005786 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005787
5788 case DeclarationName::ObjCZeroArgSelector:
5789 case DeclarationName::ObjCOneArgSelector:
5790 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor2d2689a2011-07-28 21:16:51 +00005791 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005792
5793 case DeclarationName::CXXConstructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005794 return Context.DeclarationNames.getCXXConstructorName(
5795 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005796
5797 case DeclarationName::CXXDestructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005798 return Context.DeclarationNames.getCXXDestructorName(
5799 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005800
5801 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor35942772011-09-09 21:34:22 +00005802 return Context.DeclarationNames.getCXXConversionFunctionName(
5803 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005804
5805 case DeclarationName::CXXOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005806 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00005807 (OverloadedOperatorKind)Record[Idx++]);
5808
Sean Hunt3e518bd2009-11-29 07:34:05 +00005809 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005810 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregor95eab172011-07-28 20:55:49 +00005811 GetIdentifierInfo(F, Record, Idx));
Sean Hunt3e518bd2009-11-29 07:34:05 +00005812
Douglas Gregor2cf26342009-04-09 22:27:44 +00005813 case DeclarationName::CXXUsingDirective:
5814 return DeclarationName::getUsingDirectiveName();
5815 }
5816
David Blaikie7530c032012-01-17 06:56:22 +00005817 llvm_unreachable("Invalid NameKind!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00005818}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00005819
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005820void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005821 DeclarationNameLoc &DNLoc,
5822 DeclarationName Name,
5823 const RecordData &Record, unsigned &Idx) {
5824 switch (Name.getNameKind()) {
5825 case DeclarationName::CXXConstructorName:
5826 case DeclarationName::CXXDestructorName:
5827 case DeclarationName::CXXConversionFunctionName:
5828 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
5829 break;
5830
5831 case DeclarationName::CXXOperatorName:
5832 DNLoc.CXXOperatorName.BeginOpNameLoc
5833 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5834 DNLoc.CXXOperatorName.EndOpNameLoc
5835 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5836 break;
5837
5838 case DeclarationName::CXXLiteralOperatorName:
5839 DNLoc.CXXLiteralOperatorName.OpNameLoc
5840 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5841 break;
5842
5843 case DeclarationName::Identifier:
5844 case DeclarationName::ObjCZeroArgSelector:
5845 case DeclarationName::ObjCOneArgSelector:
5846 case DeclarationName::ObjCMultiArgSelector:
5847 case DeclarationName::CXXUsingDirective:
5848 break;
5849 }
5850}
5851
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005852void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005853 DeclarationNameInfo &NameInfo,
5854 const RecordData &Record, unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00005855 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005856 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
5857 DeclarationNameLoc DNLoc;
5858 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
5859 NameInfo.setInfo(DNLoc);
5860}
5861
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005862void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005863 const RecordData &Record, unsigned &Idx) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005864 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005865 unsigned NumTPLists = Record[Idx++];
5866 Info.NumTemplParamLists = NumTPLists;
5867 if (NumTPLists) {
Douglas Gregor35942772011-09-09 21:34:22 +00005868 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005869 for (unsigned i=0; i != NumTPLists; ++i)
5870 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
5871 }
5872}
5873
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005874TemplateName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005875ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005876 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00005877 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005878 switch (Kind) {
5879 case TemplateName::Template:
Douglas Gregor409448c2011-07-21 22:35:25 +00005880 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005881
5882 case TemplateName::OverloadedTemplate: {
5883 unsigned size = Record[Idx++];
5884 UnresolvedSet<8> Decls;
5885 while (size--)
Douglas Gregor409448c2011-07-21 22:35:25 +00005886 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005887
Douglas Gregor35942772011-09-09 21:34:22 +00005888 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005889 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005890
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005891 case TemplateName::QualifiedTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005892 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005893 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00005894 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005895 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005896 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005897
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005898 case TemplateName::DependentTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005899 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005900 if (Record[Idx++]) // isIdentifier
Douglas Gregor35942772011-09-09 21:34:22 +00005901 return Context.getDependentTemplateName(NNS,
Douglas Gregor95eab172011-07-28 20:55:49 +00005902 GetIdentifierInfo(F, Record,
5903 Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00005904 return Context.getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00005905 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005906 }
John McCall14606042011-06-30 08:33:18 +00005907
5908 case TemplateName::SubstTemplateTemplateParm: {
5909 TemplateTemplateParmDecl *param
Douglas Gregor409448c2011-07-21 22:35:25 +00005910 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCall14606042011-06-30 08:33:18 +00005911 if (!param) return TemplateName();
5912 TemplateName replacement = ReadTemplateName(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005913 return Context.getSubstTemplateTemplateParm(param, replacement);
John McCall14606042011-06-30 08:33:18 +00005914 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005915
5916 case TemplateName::SubstTemplateTemplateParmPack: {
5917 TemplateTemplateParmDecl *Param
Douglas Gregor409448c2011-07-21 22:35:25 +00005918 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005919 if (!Param)
5920 return TemplateName();
5921
5922 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
5923 if (ArgPack.getKind() != TemplateArgument::Pack)
5924 return TemplateName();
5925
Douglas Gregor35942772011-09-09 21:34:22 +00005926 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005927 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005928 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005929
David Blaikieb219cfc2011-09-23 05:06:16 +00005930 llvm_unreachable("Unhandled template name kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005931}
5932
5933TemplateArgument
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005934ASTReader::ReadTemplateArgument(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00005935 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00005936 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
5937 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005938 case TemplateArgument::Null:
5939 return TemplateArgument();
5940 case TemplateArgument::Type:
Douglas Gregor393f2492011-07-22 00:38:23 +00005941 return TemplateArgument(readType(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005942 case TemplateArgument::Declaration:
Douglas Gregor409448c2011-07-21 22:35:25 +00005943 return TemplateArgument(ReadDecl(F, Record, Idx));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00005944 case TemplateArgument::Integral: {
5945 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00005946 QualType T = readType(F, Record, Idx);
Benjamin Kramer85524372012-06-07 15:09:51 +00005947 return TemplateArgument(Context, Value, T);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00005948 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00005949 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005950 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00005951 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005952 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00005953 llvm::Optional<unsigned> NumTemplateExpansions;
5954 if (unsigned NumExpansions = Record[Idx++])
5955 NumTemplateExpansions = NumExpansions - 1;
5956 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00005957 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005958 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00005959 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005960 case TemplateArgument::Pack: {
5961 unsigned NumArgs = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00005962 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
Douglas Gregor910f8002010-11-07 23:05:16 +00005963 for (unsigned I = 0; I != NumArgs; ++I)
5964 Args[I] = ReadTemplateArgument(F, Record, Idx);
5965 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005966 }
5967 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005968
David Blaikieb219cfc2011-09-23 05:06:16 +00005969 llvm_unreachable("Unhandled template argument kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005970}
5971
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005972TemplateParameterList *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005973ASTReader::ReadTemplateParameterList(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00005974 const RecordData &Record, unsigned &Idx) {
5975 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
5976 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
5977 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005978
5979 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00005980 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005981 Params.reserve(NumParams);
5982 while (NumParams--)
Douglas Gregor409448c2011-07-21 22:35:25 +00005983 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer20249a12010-10-21 03:16:25 +00005984
5985 TemplateParameterList* TemplateParams =
Douglas Gregor35942772011-09-09 21:34:22 +00005986 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005987 Params.data(), Params.size(), RAngleLoc);
5988 return TemplateParams;
5989}
5990
5991void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005992ASTReader::
Chris Lattner5f9e2722011-07-23 10:55:15 +00005993ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005994 ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00005995 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005996 unsigned NumTemplateArgs = Record[Idx++];
5997 TemplArgs.reserve(NumTemplateArgs);
5998 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00005999 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006000}
6001
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006002/// \brief Read a UnresolvedSet structure.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006003void ASTReader::ReadUnresolvedSet(ModuleFile &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006004 const RecordData &Record, unsigned &Idx) {
6005 unsigned NumDecls = Record[Idx++];
6006 while (NumDecls--) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006007 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006008 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6009 Set.addDecl(D, AS);
6010 }
6011}
6012
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006013CXXBaseSpecifier
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006014ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
Nick Lewycky56062202010-07-26 16:56:01 +00006015 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006016 bool isVirtual = static_cast<bool>(Record[Idx++]);
6017 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6018 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006019 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00006020 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6021 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006022 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006023 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006024 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006025 Result.setInheritConstructors(inheritConstructors);
6026 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006027}
6028
Sean Huntcbb67482011-01-08 20:30:50 +00006029std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006030ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
Sean Huntcbb67482011-01-08 20:30:50 +00006031 unsigned &Idx) {
6032 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006033 unsigned NumInitializers = Record[Idx++];
6034 if (NumInitializers) {
Sean Huntcbb67482011-01-08 20:30:50 +00006035 CtorInitializers
Douglas Gregor35942772011-09-09 21:34:22 +00006036 = new (Context) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006037 for (unsigned i=0; i != NumInitializers; ++i) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006038 TypeSourceInfo *TInfo = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006039 bool IsBaseVirtual = false;
6040 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00006041 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00006042
Sean Hunt156b6402011-05-04 01:19:08 +00006043 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6044 switch (Type) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006045 case CTOR_INITIALIZER_BASE:
6046 TInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006047 IsBaseVirtual = Record[Idx++];
Sean Hunt156b6402011-05-04 01:19:08 +00006048 break;
Douglas Gregor76852c22011-11-01 01:16:03 +00006049
6050 case CTOR_INITIALIZER_DELEGATING:
6051 TInfo = GetTypeSourceInfo(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006052 break;
6053
6054 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006055 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006056 break;
6057
6058 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006059 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006060 break;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006061 }
Sean Hunt156b6402011-05-04 01:19:08 +00006062
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00006063 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00006064 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00006065 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6066 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006067 bool IsWritten = Record[Idx++];
6068 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner5f9e2722011-07-23 10:55:15 +00006069 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006070 if (IsWritten) {
6071 SourceOrderOrNumArrayIndices = Record[Idx++];
6072 } else {
6073 SourceOrderOrNumArrayIndices = Record[Idx++];
6074 Indices.reserve(SourceOrderOrNumArrayIndices);
6075 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor409448c2011-07-21 22:35:25 +00006076 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006077 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006078
Sean Huntcbb67482011-01-08 20:30:50 +00006079 CXXCtorInitializer *BOMInit;
Sean Hunt156b6402011-05-04 01:19:08 +00006080 if (Type == CTOR_INITIALIZER_BASE) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006081 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
Sean Huntcbb67482011-01-08 20:30:50 +00006082 LParenLoc, Init, RParenLoc,
6083 MemberOrEllipsisLoc);
Sean Hunt156b6402011-05-04 01:19:08 +00006084 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006085 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6086 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006087 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00006088 if (Member)
Douglas Gregor35942772011-09-09 21:34:22 +00006089 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006090 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00006091 else
Douglas Gregor35942772011-09-09 21:34:22 +00006092 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
Sean Huntcbb67482011-01-08 20:30:50 +00006093 MemberOrEllipsisLoc, LParenLoc,
6094 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006095 } else {
Douglas Gregor35942772011-09-09 21:34:22 +00006096 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006097 LParenLoc, Init, RParenLoc,
6098 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006099 }
6100
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00006101 if (IsWritten)
6102 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00006103 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006104 }
6105 }
6106
Sean Huntcbb67482011-01-08 20:30:50 +00006107 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006108}
6109
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006110NestedNameSpecifier *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006111ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006112 const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006113 unsigned N = Record[Idx++];
6114 NestedNameSpecifier *NNS = 0, *Prev = 0;
6115 for (unsigned I = 0; I != N; ++I) {
6116 NestedNameSpecifier::SpecifierKind Kind
6117 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6118 switch (Kind) {
6119 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006120 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006121 NNS = NestedNameSpecifier::Create(Context, Prev, II);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006122 break;
6123 }
6124
6125 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006126 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006127 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006128 break;
6129 }
6130
Douglas Gregor14aba762011-02-24 02:36:08 +00006131 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006132 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006133 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
Douglas Gregor14aba762011-02-24 02:36:08 +00006134 break;
6135 }
6136
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006137 case NestedNameSpecifier::TypeSpec:
6138 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor393f2492011-07-22 00:38:23 +00006139 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00006140 if (!T)
6141 return 0;
6142
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006143 bool Template = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006144 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006145 break;
6146 }
6147
6148 case NestedNameSpecifier::Global: {
Douglas Gregor35942772011-09-09 21:34:22 +00006149 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006150 // No associated value, and there can't be a prefix.
6151 break;
6152 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006153 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00006154 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006155 }
6156 return NNS;
6157}
6158
Douglas Gregordc355712011-02-25 00:36:19 +00006159NestedNameSpecifierLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006160ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
Douglas Gregordc355712011-02-25 00:36:19 +00006161 unsigned &Idx) {
6162 unsigned N = Record[Idx++];
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006163 NestedNameSpecifierLocBuilder Builder;
Douglas Gregordc355712011-02-25 00:36:19 +00006164 for (unsigned I = 0; I != N; ++I) {
6165 NestedNameSpecifier::SpecifierKind Kind
6166 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6167 switch (Kind) {
6168 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006169 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006170 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006171 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006172 break;
6173 }
6174
6175 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006176 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006177 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006178 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006179 break;
6180 }
6181
6182 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006183 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006184 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006185 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006186 break;
6187 }
6188
6189 case NestedNameSpecifier::TypeSpec:
6190 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregordc355712011-02-25 00:36:19 +00006191 bool Template = Record[Idx++];
6192 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
6193 if (!T)
6194 return NestedNameSpecifierLoc();
Douglas Gregordc355712011-02-25 00:36:19 +00006195 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006196
6197 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
Douglas Gregor35942772011-09-09 21:34:22 +00006198 Builder.Extend(Context,
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006199 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
6200 T->getTypeLoc(), ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006201 break;
6202 }
6203
6204 case NestedNameSpecifier::Global: {
Douglas Gregordc355712011-02-25 00:36:19 +00006205 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006206 Builder.MakeGlobal(Context, ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006207 break;
6208 }
6209 }
Douglas Gregordc355712011-02-25 00:36:19 +00006210 }
6211
Douglas Gregor35942772011-09-09 21:34:22 +00006212 return Builder.getWithLocInContext(Context);
Douglas Gregordc355712011-02-25 00:36:19 +00006213}
6214
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006215SourceRange
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006216ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006217 unsigned &Idx) {
6218 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
6219 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00006220 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006221}
6222
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006223/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006224llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006225 unsigned BitWidth = Record[Idx++];
6226 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
6227 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
6228 Idx += NumWords;
6229 return Result;
6230}
6231
6232/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006233llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006234 bool isUnsigned = Record[Idx++];
6235 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
6236}
6237
Douglas Gregor17fc2232009-04-14 21:55:33 +00006238/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006239llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00006240 return llvm::APFloat(ReadAPInt(Record, Idx));
6241}
6242
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006243// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006244std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006245 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00006246 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006247 Idx += Len;
6248 return Result;
6249}
6250
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00006251VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
6252 unsigned &Idx) {
6253 unsigned Major = Record[Idx++];
6254 unsigned Minor = Record[Idx++];
6255 unsigned Subminor = Record[Idx++];
6256 if (Minor == 0)
6257 return VersionTuple(Major);
6258 if (Subminor == 0)
6259 return VersionTuple(Major, Minor - 1);
6260 return VersionTuple(Major, Minor - 1, Subminor - 1);
6261}
6262
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006263CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006264 const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00006265 unsigned &Idx) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006266 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006267 return CXXTemporary::Create(Context, Decl);
Chris Lattnerd2598362010-05-10 00:25:06 +00006268}
6269
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006270DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00006271 return Diag(SourceLocation(), DiagID);
6272}
6273
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006274DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00006275 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00006276}
Douglas Gregor025452f2009-04-17 00:04:06 +00006277
Douglas Gregor668c1a42009-04-21 22:25:48 +00006278/// \brief Retrieve the identifier table associated with the
6279/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006280IdentifierTable &ASTReader::getIdentifierTable() {
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006281 return PP.getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00006282}
6283
Douglas Gregor025452f2009-04-17 00:04:06 +00006284/// \brief Record that the given ID maps to the given switch-case
6285/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006286void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006287 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
6288 "Already have a SwitchCase with this ID");
6289 (*CurrSwitchCaseStmts)[ID] = SC;
Douglas Gregor025452f2009-04-17 00:04:06 +00006290}
6291
6292/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006293SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006294 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
6295 return (*CurrSwitchCaseStmts)[ID];
Douglas Gregor025452f2009-04-17 00:04:06 +00006296}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00006297
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006298void ASTReader::ClearSwitchCaseIDs() {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006299 CurrSwitchCaseStmts->clear();
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006300}
6301
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006302void ASTReader::ReadComments() {
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006303 std::vector<RawComment *> Comments;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006304 for (SmallVectorImpl<std::pair<llvm::BitstreamCursor,
6305 serialization::ModuleFile *> >::iterator
6306 I = CommentsCursors.begin(),
6307 E = CommentsCursors.end();
6308 I != E; ++I) {
6309 llvm::BitstreamCursor &Cursor = I->first;
6310 serialization::ModuleFile &F = *I->second;
6311 SavedStreamPosition SavedPosition(Cursor);
6312
6313 RecordData Record;
6314 while (true) {
6315 unsigned Code = Cursor.ReadCode();
6316 if (Code == llvm::bitc::END_BLOCK)
6317 break;
6318
6319 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
6320 // No known subblocks, always skip them.
6321 Cursor.ReadSubBlockID();
6322 if (Cursor.SkipBlock()) {
6323 Error("malformed block record in AST file");
6324 return;
6325 }
6326 continue;
6327 }
6328
6329 if (Code == llvm::bitc::DEFINE_ABBREV) {
6330 Cursor.ReadAbbrevRecord();
6331 continue;
6332 }
6333
6334 // Read a record.
6335 Record.clear();
6336 switch ((CommentRecordTypes) Cursor.ReadRecord(Code, Record)) {
Chandler Carruth13691bb2012-06-20 06:47:54 +00006337 case COMMENTS_RAW_COMMENT: {
6338 unsigned Idx = 0;
6339 SourceRange SR = ReadSourceRange(F, Record, Idx);
6340 RawComment::CommentKind Kind =
6341 (RawComment::CommentKind) Record[Idx++];
6342 bool IsTrailingComment = Record[Idx++];
6343 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006344 Comments.push_back(new (Context) RawComment(SR, Kind,
6345 IsTrailingComment,
6346 IsAlmostTrailingComment));
Chandler Carruth13691bb2012-06-20 06:47:54 +00006347 break;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006348 }
6349 }
6350 }
6351 }
6352 Context.Comments.addCommentsToFront(Comments);
6353}
6354
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006355void ASTReader::finishPendingActions() {
Douglas Gregorcff9f262012-01-27 01:47:08 +00006356 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty()) {
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006357 // If any identifiers with corresponding top-level declarations have
6358 // been loaded, load those declarations now.
6359 while (!PendingIdentifierInfos.empty()) {
6360 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
6361 PendingIdentifierInfos.front().DeclIDs, true);
6362 PendingIdentifierInfos.pop_front();
6363 }
6364
Douglas Gregora1be2782011-12-17 23:38:30 +00006365 // Load pending declaration chains.
6366 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
6367 loadPendingDeclChain(PendingDeclChains[I]);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006368 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
Douglas Gregora1be2782011-12-17 23:38:30 +00006369 }
6370 PendingDeclChains.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006371 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006372
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006373 // If we deserialized any C++ or Objective-C class definitions, any
6374 // Objective-C protocol definitions, or any redeclarable templates, make sure
6375 // that all redeclarations point to the definitions. Note that this can only
6376 // happen now, after the redeclaration chains have been fully wired.
Douglas Gregorfc529f72011-12-19 19:00:47 +00006377 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
6378 DEnd = PendingDefinitions.end();
6379 D != DEnd; ++D) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006380 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
6381 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
6382 // Make sure that the TagType points at the definition.
6383 const_cast<TagType*>(TagT)->decl = TD;
6384 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006385
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006386 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
6387 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
6388 REnd = RD->redecls_end();
6389 R != REnd; ++R)
6390 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
6391
6392 }
6393
Douglas Gregorfc529f72011-12-19 19:00:47 +00006394 continue;
6395 }
6396
Douglas Gregor1d784b22012-01-01 19:51:50 +00006397 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006398 // Make sure that the ObjCInterfaceType points at the definition.
6399 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
6400 ->Decl = ID;
6401
Douglas Gregor1d784b22012-01-01 19:51:50 +00006402 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
6403 REnd = ID->redecls_end();
6404 R != REnd; ++R)
6405 R->Data = ID->Data;
6406
6407 continue;
6408 }
6409
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006410 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
6411 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
6412 REnd = PD->redecls_end();
6413 R != REnd; ++R)
6414 R->Data = PD->Data;
6415
6416 continue;
6417 }
6418
6419 RedeclarableTemplateDecl *RTD
6420 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
6421 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
6422 REnd = RTD->redecls_end();
Douglas Gregorfc529f72011-12-19 19:00:47 +00006423 R != REnd; ++R)
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006424 R->Common = RTD->Common;
Douglas Gregorfc529f72011-12-19 19:00:47 +00006425 }
6426 PendingDefinitions.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006427}
6428
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006429void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00006430 assert(NumCurrentElementsDeserializing &&
6431 "FinishedDeserializing not paired with StartedDeserializing");
6432 if (NumCurrentElementsDeserializing == 1) {
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006433 // We decrease NumCurrentElementsDeserializing only after pending actions
6434 // are finished, to avoid recursively re-calling finishPendingActions().
6435 finishPendingActions();
6436 }
6437 --NumCurrentElementsDeserializing;
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006438
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006439 if (NumCurrentElementsDeserializing == 0 &&
6440 Consumer && !PassingDeclsToConsumer) {
6441 // Guard variable to avoid recursively redoing the process of passing
6442 // decls to consumer.
6443 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6444 true);
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006445
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006446 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00006447 // We are not in recursive loading, so it's safe to pass the "interesting"
6448 // decls to the consumer.
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006449 Decl *D = InterestingDecls.front();
6450 InterestingDecls.pop_front();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006451 PassInterestingDeclToConsumer(D);
6452 }
Douglas Gregord89275b2009-07-06 18:54:52 +00006453 }
Douglas Gregord89275b2009-07-06 18:54:52 +00006454}
Douglas Gregor501c1032010-08-19 00:28:17 +00006455
Douglas Gregorf8a1e512011-09-02 00:26:20 +00006456ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
Douglas Gregor832d6202011-07-22 16:35:34 +00006457 StringRef isysroot, bool DisableValidation,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00006458 bool DisableStatCache, bool AllowASTWithCompilerErrors)
Sebastian Redle1dde812010-08-24 00:50:04 +00006459 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
6460 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006461 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
Jonathan D. Turner1afb6612011-07-28 17:20:23 +00006462 Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
6463 RelocatablePCH(false), isysroot(isysroot),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006464 DisableValidation(DisableValidation),
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00006465 DisableStatCache(DisableStatCache),
6466 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006467 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
6468 NumStatHits(0), NumStatMisses(0),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006469 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00006470 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
6471 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
6472 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
6473 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00006474 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
6475 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006476 PassingDeclsToConsumer(false),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00006477 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00006478{
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006479 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redle1dde812010-08-24 00:50:04 +00006480}
6481
Sebastian Redle1dde812010-08-24 00:50:04 +00006482ASTReader::~ASTReader() {
Sebastian Redle1dde812010-08-24 00:50:04 +00006483 for (DeclContextVisibleUpdatesPending::iterator
6484 I = PendingVisibleUpdates.begin(),
6485 E = PendingVisibleUpdates.end();
6486 I != E; ++I) {
6487 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
6488 F = I->second.end();
6489 J != F; ++J)
Benjamin Kramerb1758c62012-04-15 12:36:49 +00006490 delete J->first;
Sebastian Redle1dde812010-08-24 00:50:04 +00006491 }
6492}