blob: 3a8e5036a8dfee3a7742ea2c1fc9405998ab9db3 [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"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000017#include "ASTCommon.h"
Douglas Gregor98339b92011-08-25 20:47:51 +000018#include "ASTReaderInternals.h"
Douglas Gregor0a0428e2009-04-10 20:39:37 +000019#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbarc7162932009-11-11 23:58:53 +000020#include "clang/Frontend/Utils.h"
Douglas Gregore737f502010-08-12 20:07:10 +000021#include "clang/Sema/Sema.h"
John McCall5f1e0942010-08-24 08:50:51 +000022#include "clang/Sema/Scope.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000023#include "clang/AST/ASTConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000024#include "clang/AST/ASTContext.h"
John McCall2a7fb272010-08-25 05:32:35 +000025#include "clang/AST/DeclTemplate.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000026#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000027#include "clang/AST/ExprCXX.h"
Douglas Gregor5f791bb2011-02-28 23:58:31 +000028#include "clang/AST/NestedNameSpecifier.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000029#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000030#include "clang/AST/TypeLocVisitor.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000031#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000032#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000033#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000034#include "clang/Lex/HeaderSearch.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000035#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000036#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000037#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000038#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000039#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000040#include "clang/Basic/TargetInfo.h"
Douglas Gregor445e23e2009-10-05 21:07:28 +000041#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000042#include "clang/Basic/VersionTuple.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000043#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000044#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000045#include "llvm/Support/MemoryBuffer.h"
John McCall833ca992009-10-29 08:12:44 +000046#include "llvm/Support/ErrorHandling.h"
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000047#include "llvm/Support/FileSystem.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000048#include "llvm/Support/Path.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) {
67 const LangOptions &PPLangOpts = PP.getLangOptions();
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"
93
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000094 return false;
95}
96
Chris Lattner5f9e2722011-07-23 10:55:15 +000097bool PCHValidator::ReadTargetTriple(StringRef Triple) {
Daniel Dunbardc3c0d22009-11-11 00:52:11 +000098 if (Triple == PP.getTargetInfo().getTriple().str())
99 return false;
100
101 Reader.Diag(diag::warn_pch_target_triple)
102 << Triple << PP.getTargetInfo().getTriple().str();
103 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000104}
105
Benjamin Kramer54353f42010-11-25 18:29:30 +0000106namespace {
107 struct EmptyStringRef {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000108 bool operator ()(StringRef r) const { return r.empty(); }
Benjamin Kramer54353f42010-11-25 18:29:30 +0000109 };
110 struct EmptyBlock {
111 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
112 };
113}
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000114
Chris Lattner5f9e2722011-07-23 10:55:15 +0000115static bool EqualConcatenations(SmallVector<StringRef, 2> L,
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000116 PCHPredefinesBlocks R) {
117 // First, sum up the lengths.
118 unsigned LL = 0, RL = 0;
119 for (unsigned I = 0, N = L.size(); I != N; ++I) {
120 LL += L[I].size();
121 }
122 for (unsigned I = 0, N = R.size(); I != N; ++I) {
123 RL += R[I].Data.size();
124 }
125 if (LL != RL)
126 return false;
127 if (LL == 0 && RL == 0)
128 return true;
129
130 // Kick out empty parts, they confuse the algorithm below.
131 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
132 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
133
134 // Do it the hard way. At this point, both vectors must be non-empty.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000135 StringRef LR = L[0], RR = R[0].Data;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000136 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000137 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000138 for (;;) {
139 // Compare the current pieces.
140 if (LR.size() == RR.size()) {
141 // If they're the same length, it's pretty easy.
142 if (LR != RR)
143 return false;
144 // Both pieces are done, advance.
145 ++LI;
146 ++RI;
147 // If either string is done, they're both done, since they're the same
148 // length.
149 if (LI == LN) {
150 assert(RI == RN && "Strings not the same length after all?");
151 return true;
152 }
153 LR = L[LI];
154 RR = R[RI].Data;
155 } else if (LR.size() < RR.size()) {
156 // Right piece is longer.
157 if (!RR.startswith(LR))
158 return false;
159 ++LI;
160 assert(LI != LN && "Strings not the same length after all?");
161 RR = RR.substr(LR.size());
162 LR = L[LI];
163 } else {
164 // Left piece is longer.
165 if (!LR.startswith(RR))
166 return false;
167 ++RI;
168 assert(RI != RN && "Strings not the same length after all?");
169 LR = LR.substr(RR.size());
170 RR = R[RI].Data;
171 }
172 }
173}
174
Chris Lattner5f9e2722011-07-23 10:55:15 +0000175static std::pair<FileID, StringRef::size_type>
176FindMacro(const PCHPredefinesBlocks &Buffers, StringRef MacroDef) {
177 std::pair<FileID, StringRef::size_type> Res;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000178 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
179 Res.second = Buffers[I].Data.find(MacroDef);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000180 if (Res.second != StringRef::npos) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000181 Res.first = Buffers[I].BufferID;
182 break;
183 }
184 }
185 return Res;
186}
187
188bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000189 StringRef OriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000190 std::string &SuggestedPredefines,
191 FileManager &FileMgr) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000192 // We are in the context of an implicit include, so the predefines buffer will
193 // have a #include entry for the PCH file itself (as normalized by the
194 // preprocessor initialization). Find it and skip over it in the checking
195 // below.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000196 llvm::SmallString<256> PCHInclude;
197 PCHInclude += "#include \"";
Nick Lewycky277a6e72011-02-23 21:16:44 +0000198 PCHInclude += NormalizeDashIncludePath(OriginalFileName, FileMgr);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000199 PCHInclude += "\"\n";
Chris Lattner5f9e2722011-07-23 10:55:15 +0000200 std::pair<StringRef,StringRef> Split =
201 StringRef(PP.getPredefines()).split(PCHInclude.str());
202 StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000203 if (Left == PP.getPredefines()) {
204 Error("Missing PCH include entry!");
205 return true;
206 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000207
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000208 // If the concatenation of all the PCH buffers is equal to the adjusted
209 // command line, we're done.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000210 SmallVector<StringRef, 2> CommandLine;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000211 CommandLine.push_back(Left);
212 CommandLine.push_back(Right);
213 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000214 return false;
215
216 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000217
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000218 // The predefines buffers are different. Determine what the differences are,
219 // and whether they require us to reject the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000220 SmallVector<StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000221 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
222 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000223
Chris Lattner5f9e2722011-07-23 10:55:15 +0000224 SmallVector<StringRef, 8> CmdLineLines;
Daniel Dunbare6750492009-11-13 16:46:11 +0000225 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000226
227 // Pick out implicit #includes after the PCH and don't consider them for
228 // validation; we will insert them into SuggestedPredefines so that the
229 // preprocessor includes them.
230 std::string IncludesAfterPCH;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000231 SmallVector<StringRef, 8> AfterPCHLines;
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000232 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
233 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
234 if (AfterPCHLines[i].startswith("#include ")) {
235 IncludesAfterPCH += AfterPCHLines[i];
236 IncludesAfterPCH += '\n';
237 } else {
238 CmdLineLines.push_back(AfterPCHLines[i]);
239 }
240 }
241
242 // Make sure we add the includes last into SuggestedPredefines before we
243 // exit this function.
244 struct AddIncludesRAII {
245 std::string &SuggestedPredefines;
246 std::string &IncludesAfterPCH;
247
248 AddIncludesRAII(std::string &SuggestedPredefines,
249 std::string &IncludesAfterPCH)
250 : SuggestedPredefines(SuggestedPredefines),
251 IncludesAfterPCH(IncludesAfterPCH) { }
252 ~AddIncludesRAII() {
253 SuggestedPredefines += IncludesAfterPCH;
254 }
255 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000256
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000257 // Sort both sets of predefined buffer lines, since we allow some extra
258 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000259 std::sort(CmdLineLines.begin(), CmdLineLines.end());
260 std::sort(PCHLines.begin(), PCHLines.end());
261
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000262 // Determine which predefines that were used to build the PCH file are missing
263 // from the command line.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000264 std::vector<StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000265 std::set_difference(PCHLines.begin(), PCHLines.end(),
266 CmdLineLines.begin(), CmdLineLines.end(),
267 std::back_inserter(MissingPredefines));
268
269 bool MissingDefines = false;
270 bool ConflictingDefines = false;
271 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000272 StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000273 if (Missing.startswith("#include ")) {
274 // An -include was specified when generating the PCH; it is included in
275 // the PCH, just ignore it.
276 continue;
277 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000278 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000279 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
280 return true;
281 }
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000283 // This is a macro definition. Determine the name of the macro we're
284 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000285 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000286 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000287 = Missing.find_first_of("( \n\r", StartOfMacroName);
288 assert(EndOfMacroName != std::string::npos &&
289 "Couldn't find the end of the macro name");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000290 StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000291
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000292 // Determine whether this macro was given a different definition on the
293 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000294 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000295 std::string::size_type MacroDefLen = MacroDefStart.size();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000296 SmallVector<StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000297 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
298 MacroDefStart);
299 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000300 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000301 // Different macro; we're done.
302 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000303 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000304 }
Mike Stump1eb44332009-09-09 15:08:12 +0000305
306 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000307 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000308 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000309 (*ConflictPos)[MacroDefLen] != '(')
310 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000312 // We found a conflicting macro definition.
313 break;
314 }
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000316 if (ConflictPos != CmdLineLines.end()) {
317 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
318 << MacroName;
319
320 // Show the definition of this macro within the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000321 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000322 FindMacro(Buffers, Missing);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000323 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000324 SourceLocation PCHMissingLoc =
325 SourceMgr.getLocForStartOfFile(MacroLoc.first)
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000326 .getLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000327 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000328
329 ConflictingDefines = true;
330 continue;
331 }
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000333 // If the macro doesn't conflict, then we'll just pick up the macro
334 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000335 if (ConflictingDefines)
336 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000338 if (!MissingDefines) {
339 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
340 MissingDefines = true;
341 }
342
343 // Show the definition of this macro within the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000344 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000345 FindMacro(Buffers, Missing);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000346 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000347 SourceLocation PCHMissingLoc =
348 SourceMgr.getLocForStartOfFile(MacroLoc.first)
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000349 .getLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000350 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
351 }
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000353 if (ConflictingDefines)
354 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000356 // Determine what predefines were introduced based on command-line
357 // parameters that were not present when building the PCH
358 // file. Extra #defines are okay, so long as the identifiers being
359 // defined were not used within the precompiled header.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000360 std::vector<StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000361 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
362 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000363 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000364 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000365 StringRef &Extra = ExtraPredefines[I];
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000366 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000367 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
368 return true;
369 }
370
371 // This is an extra macro definition. Determine the name of the
372 // macro we're defining.
373 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000374 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000375 = Extra.find_first_of("( \n\r", StartOfMacroName);
376 assert(EndOfMacroName != std::string::npos &&
377 "Couldn't find the end of the macro name");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000378 StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000379
380 // Check whether this name was used somewhere in the PCH file. If
381 // so, defining it as a macro could change behavior, so we reject
382 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000383 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000384 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000385 return true;
386 }
387
388 // Add this definition to the suggested predefines buffer.
389 SuggestedPredefines += Extra;
390 SuggestedPredefines += '\n';
391 }
392
393 // If we get here, it's because the predefines buffer had compatible
394 // contents. Accept the PCH file.
395 return false;
396}
397
Douglas Gregor12fab312010-03-16 16:35:32 +0000398void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
399 unsigned ID) {
400 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
401 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000402}
403
404void PCHValidator::ReadCounter(unsigned Value) {
405 PP.setCounterValue(Value);
406}
407
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000408//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000409// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000410//===----------------------------------------------------------------------===//
411
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000412void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000413ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000414 DeserializationListener = Listener;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000415}
416
Chris Lattner4c6f9522009-04-27 05:14:47 +0000417
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000418
Douglas Gregor98339b92011-08-25 20:47:51 +0000419unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
420 return serialization::ComputeHash(Sel);
421}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000422
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Douglas Gregor98339b92011-08-25 20:47:51 +0000424std::pair<unsigned, unsigned>
425ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
426 using namespace clang::io;
427 unsigned KeyLen = ReadUnalignedLE16(d);
428 unsigned DataLen = ReadUnalignedLE16(d);
429 return std::make_pair(KeyLen, DataLen);
430}
431
432ASTSelectorLookupTrait::internal_key_type
433ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
434 using namespace clang::io;
Douglas Gregor35942772011-09-09 21:34:22 +0000435 SelectorTable &SelTable = Reader.getContext().Selectors;
Douglas Gregor98339b92011-08-25 20:47:51 +0000436 unsigned N = ReadUnalignedLE16(d);
437 IdentifierInfo *FirstII
438 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
439 if (N == 0)
440 return SelTable.getNullarySelector(FirstII);
441 else if (N == 1)
442 return SelTable.getUnarySelector(FirstII);
443
444 SmallVector<IdentifierInfo *, 16> Args;
445 Args.push_back(FirstII);
446 for (unsigned I = 1; I != N; ++I)
447 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
448
449 return SelTable.getSelector(N, Args.data());
450}
451
452ASTSelectorLookupTrait::data_type
453ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
454 unsigned DataLen) {
455 using namespace clang::io;
456
457 data_type Result;
458
459 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
460 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
461 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
462
463 // Load instance methods
Douglas Gregor98339b92011-08-25 20:47:51 +0000464 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
465 if (ObjCMethodDecl *Method
466 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
467 Result.Instance.push_back(Method);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000468 }
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Douglas Gregor98339b92011-08-25 20:47:51 +0000470 // Load factory methods
Douglas Gregor98339b92011-08-25 20:47:51 +0000471 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
472 if (ObjCMethodDecl *Method
473 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
474 Result.Factory.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 return Result;
478}
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Douglas Gregor98339b92011-08-25 20:47:51 +0000480unsigned ASTIdentifierLookupTrait::ComputeHash(const internal_key_type& a) {
481 return llvm::HashString(StringRef(a.first, a.second));
482}
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Douglas Gregor98339b92011-08-25 20:47:51 +0000484std::pair<unsigned, unsigned>
485ASTIdentifierLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
486 using namespace clang::io;
487 unsigned DataLen = ReadUnalignedLE16(d);
488 unsigned KeyLen = ReadUnalignedLE16(d);
489 return std::make_pair(KeyLen, DataLen);
490}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000491
Douglas Gregor98339b92011-08-25 20:47:51 +0000492std::pair<const char*, unsigned>
493ASTIdentifierLookupTrait::ReadKey(const unsigned char* d, unsigned n) {
494 assert(n >= 2 && d[n-1] == '\0');
495 return std::make_pair((const char*) d, n-1);
496}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000497
Douglas Gregor98339b92011-08-25 20:47:51 +0000498IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
499 const unsigned char* d,
500 unsigned DataLen) {
501 using namespace clang::io;
502 unsigned RawID = ReadUnalignedLE32(d);
503 bool IsInteresting = RawID & 0x01;
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Douglas Gregor98339b92011-08-25 20:47:51 +0000505 // Wipe out the "is interesting" bit.
506 RawID = RawID >> 1;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000507
Douglas Gregor98339b92011-08-25 20:47:51 +0000508 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
509 if (!IsInteresting) {
510 // For uninteresting identifiers, just build the IdentifierInfo
511 // and associate it with the persistent ID.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000512 IdentifierInfo *II = KnownII;
513 if (!II)
Douglas Gregor6ec60e02011-08-03 21:49:18 +0000514 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor668c1a42009-04-21 22:25:48 +0000515 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000516 II->setIsFromAST();
Douglas Gregor668c1a42009-04-21 22:25:48 +0000517 return II;
518 }
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Douglas Gregor98339b92011-08-25 20:47:51 +0000520 unsigned Bits = ReadUnalignedLE16(d);
521 bool CPlusPlusOperatorKeyword = Bits & 0x01;
522 Bits >>= 1;
523 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
524 Bits >>= 1;
525 bool Poisoned = Bits & 0x01;
526 Bits >>= 1;
527 bool ExtensionToken = Bits & 0x01;
528 Bits >>= 1;
529 bool hasMacroDefinition = Bits & 0x01;
530 Bits >>= 1;
531 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
532 Bits >>= 10;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000533
Douglas Gregor98339b92011-08-25 20:47:51 +0000534 assert(Bits == 0 && "Extra bits in the identifier?");
535 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000536
Douglas Gregor98339b92011-08-25 20:47:51 +0000537 // Build the IdentifierInfo itself and link the identifier ID with
538 // the new IdentifierInfo.
539 IdentifierInfo *II = KnownII;
540 if (!II)
541 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
542 Reader.SetIdentifierInfo(ID, II);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000543
Douglas Gregor98339b92011-08-25 20:47:51 +0000544 // Set or check the various bits in the IdentifierInfo structure.
545 // Token IDs are read-only.
546 if (HasRevertedTokenIDToIdentifier)
547 II->RevertTokenIDToIdentifier();
548 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
549 assert(II->isExtensionToken() == ExtensionToken &&
550 "Incorrect extension token flag");
551 (void)ExtensionToken;
552 if (Poisoned)
553 II->setIsPoisoned(true);
554 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
555 "Incorrect C++ operator keyword flag");
556 (void)CPlusPlusOperatorKeyword;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000557
Douglas Gregor98339b92011-08-25 20:47:51 +0000558 // If this identifier is a macro, deserialize the macro
559 // definition.
560 if (hasMacroDefinition) {
561 // FIXME: Check for conflicts?
562 uint32_t Offset = ReadUnalignedLE32(d);
563 Reader.SetIdentifierIsMacro(II, F, Offset);
564 DataLen -= 4;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000565 }
566
Douglas Gregor98339b92011-08-25 20:47:51 +0000567 // Read all of the declarations visible at global scope with this
568 // name.
Douglas Gregor98339b92011-08-25 20:47:51 +0000569 if (DataLen > 0) {
570 SmallVector<uint32_t, 4> DeclIDs;
571 for (; DataLen > 0; DataLen -= 4)
572 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
573 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000574 }
575
Douglas Gregor98339b92011-08-25 20:47:51 +0000576 II->setIsFromAST();
577 return II;
578}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000579
Douglas Gregor98339b92011-08-25 20:47:51 +0000580unsigned
581ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
582 llvm::FoldingSetNodeID ID;
583 ID.AddInteger(Key.Kind);
584
585 switch (Key.Kind) {
586 case DeclarationName::Identifier:
587 case DeclarationName::CXXLiteralOperatorName:
588 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
589 break;
590 case DeclarationName::ObjCZeroArgSelector:
591 case DeclarationName::ObjCOneArgSelector:
592 case DeclarationName::ObjCMultiArgSelector:
593 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
594 break;
595 case DeclarationName::CXXOperatorName:
596 ID.AddInteger((OverloadedOperatorKind)Key.Data);
597 break;
598 case DeclarationName::CXXConstructorName:
599 case DeclarationName::CXXDestructorName:
600 case DeclarationName::CXXConversionFunctionName:
601 case DeclarationName::CXXUsingDirective:
602 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000603 }
604
Douglas Gregor98339b92011-08-25 20:47:51 +0000605 return ID.ComputeHash();
606}
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000607
Douglas Gregor98339b92011-08-25 20:47:51 +0000608ASTDeclContextNameLookupTrait::internal_key_type
609ASTDeclContextNameLookupTrait::GetInternalKey(
610 const external_key_type& Name) const {
611 DeclNameKey Key;
612 Key.Kind = Name.getNameKind();
613 switch (Name.getNameKind()) {
614 case DeclarationName::Identifier:
615 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
616 break;
617 case DeclarationName::ObjCZeroArgSelector:
618 case DeclarationName::ObjCOneArgSelector:
619 case DeclarationName::ObjCMultiArgSelector:
620 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
621 break;
622 case DeclarationName::CXXOperatorName:
623 Key.Data = Name.getCXXOverloadedOperator();
624 break;
625 case DeclarationName::CXXLiteralOperatorName:
626 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
627 break;
628 case DeclarationName::CXXConstructorName:
629 case DeclarationName::CXXDestructorName:
630 case DeclarationName::CXXConversionFunctionName:
631 case DeclarationName::CXXUsingDirective:
632 Key.Data = 0;
633 break;
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000634 }
635
Douglas Gregor98339b92011-08-25 20:47:51 +0000636 return Key;
637}
638
639ASTDeclContextNameLookupTrait::external_key_type
640ASTDeclContextNameLookupTrait::GetExternalKey(
641 const internal_key_type& Key) const {
Douglas Gregor35942772011-09-09 21:34:22 +0000642 ASTContext &Context = Reader.getContext();
Douglas Gregor98339b92011-08-25 20:47:51 +0000643 switch (Key.Kind) {
644 case DeclarationName::Identifier:
645 return DeclarationName((IdentifierInfo*)Key.Data);
646
647 case DeclarationName::ObjCZeroArgSelector:
648 case DeclarationName::ObjCOneArgSelector:
649 case DeclarationName::ObjCMultiArgSelector:
650 return DeclarationName(Selector(Key.Data));
651
652 case DeclarationName::CXXConstructorName:
Douglas Gregor35942772011-09-09 21:34:22 +0000653 return Context.DeclarationNames.getCXXConstructorName(
654 Context.getCanonicalType(Reader.getLocalType(F, Key.Data)));
Douglas Gregor98339b92011-08-25 20:47:51 +0000655
656 case DeclarationName::CXXDestructorName:
Douglas Gregor35942772011-09-09 21:34:22 +0000657 return Context.DeclarationNames.getCXXDestructorName(
658 Context.getCanonicalType(Reader.getLocalType(F, Key.Data)));
Douglas Gregor98339b92011-08-25 20:47:51 +0000659
660 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor35942772011-09-09 21:34:22 +0000661 return Context.DeclarationNames.getCXXConversionFunctionName(
662 Context.getCanonicalType(Reader.getLocalType(F, Key.Data)));
Douglas Gregor98339b92011-08-25 20:47:51 +0000663
664 case DeclarationName::CXXOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +0000665 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregor98339b92011-08-25 20:47:51 +0000666 (OverloadedOperatorKind)Key.Data);
667
668 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +0000669 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregor98339b92011-08-25 20:47:51 +0000670 (IdentifierInfo*)Key.Data);
671
672 case DeclarationName::CXXUsingDirective:
673 return DeclarationName::getUsingDirectiveName();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000674 }
675
Douglas Gregor98339b92011-08-25 20:47:51 +0000676 llvm_unreachable("Invalid Name Kind ?");
677}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000678
Douglas Gregor98339b92011-08-25 20:47:51 +0000679std::pair<unsigned, unsigned>
680ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
681 using namespace clang::io;
682 unsigned KeyLen = ReadUnalignedLE16(d);
683 unsigned DataLen = ReadUnalignedLE16(d);
684 return std::make_pair(KeyLen, DataLen);
685}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000686
Douglas Gregor98339b92011-08-25 20:47:51 +0000687ASTDeclContextNameLookupTrait::internal_key_type
688ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
689 using namespace clang::io;
690
691 DeclNameKey Key;
692 Key.Kind = (DeclarationName::NameKind)*d++;
693 switch (Key.Kind) {
694 case DeclarationName::Identifier:
695 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
696 break;
697 case DeclarationName::ObjCZeroArgSelector:
698 case DeclarationName::ObjCOneArgSelector:
699 case DeclarationName::ObjCMultiArgSelector:
700 Key.Data =
701 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
702 .getAsOpaquePtr();
703 break;
704 case DeclarationName::CXXOperatorName:
705 Key.Data = *d++; // OverloadedOperatorKind
706 break;
707 case DeclarationName::CXXLiteralOperatorName:
708 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
709 break;
710 case DeclarationName::CXXConstructorName:
711 case DeclarationName::CXXDestructorName:
712 case DeclarationName::CXXConversionFunctionName:
713 case DeclarationName::CXXUsingDirective:
714 Key.Data = 0;
715 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000716 }
717
Douglas Gregor98339b92011-08-25 20:47:51 +0000718 return Key;
719}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000720
Douglas Gregor98339b92011-08-25 20:47:51 +0000721ASTDeclContextNameLookupTrait::data_type
722ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
723 const unsigned char* d,
724 unsigned DataLen) {
725 using namespace clang::io;
726 unsigned NumDecls = ReadUnalignedLE16(d);
727 DeclID *Start = (DeclID *)d;
728 return std::make_pair(Start, Start + NumDecls);
729}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000730
Douglas Gregor0d95f772011-08-24 19:03:07 +0000731bool ASTReader::ReadDeclContextStorage(Module &M,
732 llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000733 const std::pair<uint64_t, uint64_t> &Offsets,
734 DeclContextInfo &Info) {
735 SavedStreamPosition SavedPosition(Cursor);
736 // First the lexical decls.
737 if (Offsets.first != 0) {
738 Cursor.JumpToBit(Offsets.first);
739
740 RecordData Record;
741 const char *Blob;
742 unsigned BlobLen;
743 unsigned Code = Cursor.ReadCode();
744 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
745 if (RecCode != DECL_CONTEXT_LEXICAL) {
746 Error("Expected lexical block");
747 return true;
748 }
749
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000750 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
751 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000752 }
753
754 // Now the lookup table.
755 if (Offsets.second != 0) {
756 Cursor.JumpToBit(Offsets.second);
757
758 RecordData Record;
759 const char *Blob;
760 unsigned BlobLen;
761 unsigned Code = Cursor.ReadCode();
762 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
763 if (RecCode != DECL_CONTEXT_VISIBLE) {
764 Error("Expected visible lookup table block");
765 return true;
766 }
767 Info.NameLookupTableData
768 = ASTDeclContextNameLookupTable::Create(
769 (const unsigned char *)Blob + Record[0],
770 (const unsigned char *)Blob,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000771 ASTDeclContextNameLookupTrait(*this, M));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000772 }
773
774 return false;
775}
776
Chris Lattner5f9e2722011-07-23 10:55:15 +0000777void ASTReader::Error(StringRef Msg) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000778 Error(diag::err_fe_pch_malformed, Msg);
779}
780
781void ASTReader::Error(unsigned DiagID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000782 StringRef Arg1, StringRef Arg2) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000783 if (Diags.isDiagnosticInFlight())
784 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
785 else
786 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000787}
788
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000789/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000790bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000791 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000792 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000793 ActualOriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000794 SuggestedPredefines,
795 FileMgr);
Douglas Gregore721f952009-04-28 18:58:38 +0000796 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000797}
798
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000799//===----------------------------------------------------------------------===//
800// Source Manager Deserialization
801//===----------------------------------------------------------------------===//
802
Douglas Gregorbd945002009-04-13 16:31:14 +0000803/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000804/// \returns true if there was an error.
Douglas Gregor72a9ae12011-07-22 16:00:58 +0000805bool ASTReader::ParseLineTable(Module &F,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000806 SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000807 unsigned Idx = 0;
808 LineTableInfo &LineTable = SourceMgr.getLineTable();
809
810 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000811 std::map<int, int> FileIDs;
812 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000813 // Extract the file name
814 unsigned FilenameLen = Record[Idx++];
815 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
816 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000817 MaybeAddSystemRootToFilename(Filename);
Jay Foad65aa6882011-06-21 15:13:30 +0000818 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
Douglas Gregorbd945002009-04-13 16:31:14 +0000819 }
820
821 // Parse the line entries
822 std::vector<LineEntry> Entries;
823 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000824 int FID = Record[Idx++];
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000825 assert(FID >= 0 && "Serialized line entries for non-local file.");
826 // Remap FileID from 1-based old view.
827 FID += F.SLocEntryBaseID - 1;
Douglas Gregorbd945002009-04-13 16:31:14 +0000828
829 // Extract the line entries
830 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000831 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000832 Entries.clear();
833 Entries.reserve(NumEntries);
834 for (unsigned I = 0; I != NumEntries; ++I) {
835 unsigned FileOffset = Record[Idx++];
836 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000837 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000838 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000839 = (SrcMgr::CharacteristicKind)Record[Idx++];
840 unsigned IncludeOffset = Record[Idx++];
841 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
842 FileKind, IncludeOffset));
843 }
844 LineTable.AddEntry(FID, Entries);
845 }
846
847 return false;
848}
849
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000850namespace {
851
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000852class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000853public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000854 const ino_t ino;
855 const dev_t dev;
856 const mode_t mode;
857 const time_t mtime;
858 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000860 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +0000861 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000862};
863
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000864class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000865 public:
866 typedef const char *external_key_type;
867 typedef const char *internal_key_type;
868
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000869 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000870
871 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000872 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000873 }
874
875 static internal_key_type GetInternalKey(const char *path) { return path; }
876
877 static bool EqualKey(internal_key_type a, internal_key_type b) {
878 return strcmp(a, b) == 0;
879 }
880
881 static std::pair<unsigned, unsigned>
882 ReadKeyDataLength(const unsigned char*& d) {
883 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
884 unsigned DataLen = (unsigned) *d++;
885 return std::make_pair(KeyLen + 1, DataLen);
886 }
887
888 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
889 return (const char *)d;
890 }
891
892 static data_type ReadData(const internal_key_type, const unsigned char *d,
893 unsigned /*DataLen*/) {
894 using namespace clang::io;
895
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000896 ino_t ino = (ino_t) ReadUnalignedLE32(d);
897 dev_t dev = (dev_t) ReadUnalignedLE32(d);
898 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000899 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000900 off_t size = (off_t) ReadUnalignedLE64(d);
901 return data_type(ino, dev, mode, mtime, size);
902 }
903};
904
905/// \brief stat() cache for precompiled headers.
906///
907/// This cache is very similar to the stat cache used by pretokenized
908/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +0000909class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000910 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000911 CacheTy *Cache;
912
913 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000914public:
Chris Lattner74e976b2010-11-23 19:28:12 +0000915 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
916 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000917 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
918 Cache = CacheTy::Create(Buckets, Base);
919 }
920
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000921 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Chris Lattner898a0612010-11-23 21:17:56 +0000923 LookupResult getStat(const char *Path, struct stat &StatBuf,
924 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000925 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +0000926 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000927
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000928 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000929 if (I == Cache->end()) {
930 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +0000931 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000932 }
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000934 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000935 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Chris Lattner10e286a2010-11-23 19:19:34 +0000937 StatBuf.st_ino = Data.ino;
938 StatBuf.st_dev = Data.dev;
939 StatBuf.st_mtime = Data.mtime;
940 StatBuf.st_mode = Data.mode;
941 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +0000942 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000943 }
944};
945} // end anonymous namespace
946
947
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000948/// \brief Read a source manager block
Douglas Gregor72a9ae12011-07-22 16:00:58 +0000949ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(Module &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000950 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000951
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000952 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +0000953
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000954 // Set the source-location entry cursor to the current position in
955 // the stream. This cursor will be used to read the contents of the
956 // source manager block initially, and then lazily read
957 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000958 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000959
960 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000961 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000962 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000963 return Failure;
964 }
965
966 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000967 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000968 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000969 return Failure;
970 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000971
Douglas Gregor14f79002009-04-10 03:52:48 +0000972 RecordData Record;
973 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000974 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +0000975 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000976 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000977 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000978 return Failure;
979 }
Douglas Gregore1d918e2009-04-10 23:10:45 +0000980 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000981 }
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Douglas Gregor14f79002009-04-10 03:52:48 +0000983 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
984 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000985 SLocEntryCursor.ReadSubBlockID();
986 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000987 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000988 return Failure;
989 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000990 continue;
991 }
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Douglas Gregor14f79002009-04-10 03:52:48 +0000993 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000994 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +0000995 continue;
996 }
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Douglas Gregor14f79002009-04-10 03:52:48 +0000998 // Read a record.
999 const char *BlobStart;
1000 unsigned BlobLen;
1001 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001002 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001003 default: // Default behavior: ignore.
1004 break;
1005
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001006 case SM_SLOC_FILE_ENTRY:
1007 case SM_SLOC_BUFFER_ENTRY:
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001008 case SM_SLOC_EXPANSION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001009 // Once we hit one of the source location entries, we're done.
1010 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001011 }
1012 }
1013}
1014
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001015/// \brief If a header file is not found at the path that we expect it to be
1016/// and the PCH file was moved from its original location, try to resolve the
1017/// file by assuming that header+PCH were moved together and the header is in
1018/// the same place relative to the PCH.
1019static std::string
1020resolveFileRelativeToOriginalDir(const std::string &Filename,
1021 const std::string &OriginalDir,
1022 const std::string &CurrDir) {
1023 assert(OriginalDir != CurrDir &&
1024 "No point trying to resolve the file if the PCH dir didn't change");
1025 using namespace llvm::sys;
1026 llvm::SmallString<128> filePath(Filename);
1027 fs::make_absolute(filePath);
1028 assert(path::is_absolute(OriginalDir));
1029 llvm::SmallString<128> currPCHPath(CurrDir);
1030
1031 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1032 fileDirE = path::end(path::parent_path(filePath));
1033 path::const_iterator origDirI = path::begin(OriginalDir),
1034 origDirE = path::end(OriginalDir);
1035 // Skip the common path components from filePath and OriginalDir.
1036 while (fileDirI != fileDirE && origDirI != origDirE &&
1037 *fileDirI == *origDirI) {
1038 ++fileDirI;
1039 ++origDirI;
1040 }
1041 for (; origDirI != origDirE; ++origDirI)
1042 path::append(currPCHPath, "..");
1043 path::append(currPCHPath, fileDirI, fileDirE);
1044 path::append(currPCHPath, path::filename(Filename));
1045 return currPCHPath.str();
1046}
1047
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001048/// \brief Read in the source location entry with the given ID.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001049ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(int ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001050 if (ID == 0)
1051 return Success;
1052
Douglas Gregor0cdd7982011-07-21 18:46:38 +00001053 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001054 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001055 return Failure;
1056 }
1057
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001058 Module *F = GlobalSLocEntryMap.find(-ID)->second;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001059 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001060 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001061 unsigned BaseOffset = F->SLocEntryBaseOffset;
Sebastian Redl9137a522010-07-16 17:50:48 +00001062
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001063 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001064 unsigned Code = SLocEntryCursor.ReadCode();
1065 if (Code == llvm::bitc::END_BLOCK ||
1066 Code == llvm::bitc::ENTER_SUBBLOCK ||
1067 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001068 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001069 return Failure;
1070 }
1071
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001072 RecordData Record;
1073 const char *BlobStart;
1074 unsigned BlobLen;
1075 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1076 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001077 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001078 return Failure;
1079
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001080 case SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001081 std::string Filename(BlobStart, BlobStart + BlobLen);
1082 MaybeAddSystemRootToFilename(Filename);
Chris Lattner39b49bc2010-11-23 08:35:12 +00001083 const FileEntry *File = FileMgr.getFile(Filename);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001084 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1085 OriginalDir != CurrentDir) {
1086 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1087 OriginalDir,
1088 CurrentDir);
1089 if (!resolved.empty())
1090 File = FileMgr.getFile(resolved);
1091 }
Axel Naumann04331162011-01-27 10:55:51 +00001092 if (File == 0)
1093 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1094 (time_t)Record[5]);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001095 if (File == 0) {
1096 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001097 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001098 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001099 Error(ErrorStr.c_str());
1100 return Failure;
1101 }
Mike Stump1eb44332009-09-09 15:08:12 +00001102
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001103 if (Record.size() < 6) {
Ted Kremenek1857f622010-03-18 21:23:05 +00001104 Error("source location entry is incorrect");
1105 return Failure;
1106 }
1107
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001108 if (!DisableValidation &&
1109 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001110#if !defined(LLVM_ON_WIN32)
1111 // In our regression testing, the Windows file system seems to
1112 // have inconsistent modification times that sometimes
1113 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001114 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001115#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001116 )) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +00001117 Error(diag::err_fe_pch_file_modified, Filename);
Douglas Gregor2d52be52010-03-21 22:49:54 +00001118 return Failure;
1119 }
1120
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001121 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001122 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001123 // This is the module's main file.
1124 IncludeLoc = getImportLocation(F);
1125 }
1126 FileID FID = SourceMgr.createFileID(File, IncludeLoc,
Chris Lattner75dfb652010-11-23 09:19:42 +00001127 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001128 ID, BaseOffset + Record[0]);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001129 SrcMgr::FileInfo &FileInfo =
1130 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1131 FileInfo.NumCreatedFIDs = Record[6];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001132 if (Record[3])
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001133 FileInfo.setHasLineDirectives();
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001134
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001135 break;
1136 }
1137
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001138 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001139 const char *Name = BlobStart;
1140 unsigned Offset = Record[0];
1141 unsigned Code = SLocEntryCursor.ReadCode();
1142 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001143 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001144 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001145
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001146 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001147 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001148 return Failure;
1149 }
1150
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001151 llvm::MemoryBuffer *Buffer
Chris Lattner5f9e2722011-07-23 10:55:15 +00001152 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
Chris Lattnera0a270c2010-04-05 22:42:27 +00001153 Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001154 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID,
1155 BaseOffset + Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Douglas Gregor92b059e2009-04-28 20:33:11 +00001157 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001158 PCHPredefinesBlock Block = {
1159 BufferID,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001160 StringRef(BlobStart, BlobLen - 1)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001161 };
1162 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001163 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001164
1165 break;
1166 }
1167
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001168 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001169 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruthbf340e42011-07-26 03:03:05 +00001170 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001171 ReadSourceLocation(*F, Record[2]),
1172 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001173 Record[4],
1174 ID,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001175 BaseOffset + Record[0]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001176 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001177 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001178 }
1179
1180 return Success;
1181}
1182
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001183/// \brief Find the location where the module F is imported.
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001184SourceLocation ASTReader::getImportLocation(Module *F) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001185 if (F->ImportLoc.isValid())
1186 return F->ImportLoc;
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001187
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001188 // Otherwise we have a PCH. It's considered to be "imported" at the first
1189 // location of its includer.
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001190 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001191 // Main file is the importer. We assume that it is the first entry in the
1192 // entry table. We can't ask the manager, because at the time of PCH loading
1193 // the main file entry doesn't exist yet.
1194 // The very first entry is the invalid instantiation loc, which takes up
1195 // offsets 0 and 1.
1196 return SourceLocation::getFromRawEncoding(2U);
1197 }
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001198 //return F->Loaders[0]->FirstLoc;
1199 return F->ImportedBy[0]->FirstLoc;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001200}
1201
Chris Lattner6367f6d2009-04-27 01:05:14 +00001202/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1203/// specified cursor. Read the abbreviations that are at the top of the block
1204/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001205bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001206 unsigned BlockID) {
1207 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001208 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001209 return Failure;
1210 }
Mike Stump1eb44332009-09-09 15:08:12 +00001211
Chris Lattner6367f6d2009-04-27 01:05:14 +00001212 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001213 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001214 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001215
Chris Lattner6367f6d2009-04-27 01:05:14 +00001216 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001217 if (Code != llvm::bitc::DEFINE_ABBREV) {
1218 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001219 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001220 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001221 Cursor.ReadAbbrevRecord();
1222 }
1223}
1224
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001225void ASTReader::ReadMacroRecord(Module &F, uint64_t Offset) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001226 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Douglas Gregor37e26842009-04-21 23:56:24 +00001228 // Keep track of where we are in the stream, then jump back there
1229 // after reading this macro.
1230 SavedStreamPosition SavedPosition(Stream);
1231
1232 Stream.JumpToBit(Offset);
1233 RecordData Record;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001234 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001235 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001236
Douglas Gregor37e26842009-04-21 23:56:24 +00001237 while (true) {
1238 unsigned Code = Stream.ReadCode();
1239 switch (Code) {
1240 case llvm::bitc::END_BLOCK:
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001241 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001242
1243 case llvm::bitc::ENTER_SUBBLOCK:
1244 // No known subblocks, always skip them.
1245 Stream.ReadSubBlockID();
1246 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001247 Error("malformed block record in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001248 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001249 }
1250 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001251
Douglas Gregor37e26842009-04-21 23:56:24 +00001252 case llvm::bitc::DEFINE_ABBREV:
1253 Stream.ReadAbbrevRecord();
1254 continue;
1255 default: break;
1256 }
1257
1258 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001259 const char *BlobStart = 0;
1260 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001261 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001262 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001263 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001264 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001265 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001266 case PP_MACRO_OBJECT_LIKE:
1267 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001268 // If we already have a macro, that means that we've hit the end
1269 // of the definition of the macro we were looking for. We're
1270 // done.
1271 if (Macro)
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001272 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001273
Douglas Gregor95eab172011-07-28 20:55:49 +00001274 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001275 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001276 Error("macro must have a name in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001277 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001278 }
Sebastian Redlc3632732010-10-05 15:59:54 +00001279 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001280 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001282 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001283 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001284 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001285
Douglas Gregoraa93a872011-10-17 15:32:29 +00001286 bool IsPublic = Record[3];
1287 unsigned NextIndex = 4;
1288 MI->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex));
Douglas Gregor7143aab2011-09-01 17:04:32 +00001289
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001290 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001291 // Decode function-like macro info.
Douglas Gregor7143aab2011-09-01 17:04:32 +00001292 bool isC99VarArgs = Record[NextIndex++];
1293 bool isGNUVarArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001294 MacroArgs.clear();
Douglas Gregor7143aab2011-09-01 17:04:32 +00001295 unsigned NumArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001296 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregor7143aab2011-09-01 17:04:32 +00001297 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001298
1299 // Install function-like macro info.
1300 MI->setIsFunctionLike();
1301 if (isC99VarArgs) MI->setIsC99Varargs();
1302 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001303 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001304 PP.getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001305 }
1306
1307 // Finally, install the macro.
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001308 PP.setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001309
1310 // Remember that we saw this macro last so that we add the tokens that
1311 // form its body to it.
1312 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001313
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001314 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1315 Record[NextIndex]) {
1316 // We have a macro definition. Register the association
1317 PreprocessedEntityID
1318 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1319 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1320 PPRec.RegisterMacroDefinition(Macro,
1321 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001322 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001323
Douglas Gregor37e26842009-04-21 23:56:24 +00001324 ++NumMacrosRead;
1325 break;
1326 }
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001328 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001329 // If we see a TOKEN before a PP_MACRO_*, then the file is
1330 // erroneous, just pretend we didn't see this.
1331 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001332
Douglas Gregor37e26842009-04-21 23:56:24 +00001333 Token Tok;
1334 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001335 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001336 Tok.setLength(Record[1]);
Douglas Gregor95eab172011-07-28 20:55:49 +00001337 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregor37e26842009-04-21 23:56:24 +00001338 Tok.setIdentifierInfo(II);
1339 Tok.setKind((tok::TokenKind)Record[3]);
1340 Tok.setFlag((Token::TokenFlags)Record[4]);
1341 Macro->AddTokenToBody(Tok);
1342 break;
1343 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001344 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001345 }
Douglas Gregor89d99802010-11-30 06:16:57 +00001346
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001347 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001348}
1349
Douglas Gregor86c67d82011-07-28 22:39:26 +00001350PreprocessedEntityID
Argyrios Kyrtzidis1f6d2252011-09-19 20:40:02 +00001351ASTReader::getGlobalPreprocessedEntityID(Module &M, unsigned LocalID) const {
1352 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001353 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1354 assert(I != M.PreprocessedEntityRemap.end()
1355 && "Invalid index into preprocessed entity index remap");
1356
1357 return LocalID + I->second;
Douglas Gregor86c67d82011-07-28 22:39:26 +00001358}
1359
Douglas Gregor98339b92011-08-25 20:47:51 +00001360unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1361 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001362}
Douglas Gregor98339b92011-08-25 20:47:51 +00001363
1364HeaderFileInfoTrait::internal_key_type
1365HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1366
1367bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1368 if (strcmp(a, b) == 0)
1369 return true;
1370
1371 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1372 return false;
1373
1374 // The file names match, but the path names don't. stat() the files to
1375 // see if they are the same.
1376 struct stat StatBufA, StatBufB;
1377 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1378 return false;
1379
1380 return StatBufA.st_ino == StatBufB.st_ino;
1381}
1382
1383std::pair<unsigned, unsigned>
1384HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1385 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1386 unsigned DataLen = (unsigned) *d++;
1387 return std::make_pair(KeyLen + 1, DataLen);
1388}
1389
1390HeaderFileInfoTrait::data_type
1391HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1392 unsigned DataLen) {
1393 const unsigned char *End = d + DataLen;
1394 using namespace clang::io;
1395 HeaderFileInfo HFI;
1396 unsigned Flags = *d++;
1397 HFI.isImport = (Flags >> 5) & 0x01;
1398 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1399 HFI.DirInfo = (Flags >> 2) & 0x03;
1400 HFI.Resolved = (Flags >> 1) & 0x01;
1401 HFI.IndexHeaderMapHeader = Flags & 0x01;
1402 HFI.NumIncludes = ReadUnalignedLE16(d);
1403 HFI.ControllingMacroID = Reader.getGlobalDeclID(M, ReadUnalignedLE32(d));
1404 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1405 // The framework offset is 1 greater than the actual offset,
1406 // since 0 is used as an indicator for "no framework name".
1407 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1408 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1409 }
1410
1411 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1412 (void)End;
1413
1414 // This HeaderFileInfo was externally loaded.
1415 HFI.External = true;
1416 return HFI;
1417}
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001418
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001419void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, Module &F,
Douglas Gregor2d2689a2011-07-28 21:16:51 +00001420 uint64_t LocalOffset) {
Douglas Gregor295a2a62010-10-30 00:23:06 +00001421 // Note that this identifier has a macro definition.
1422 II->setHasMacroDefinition(true);
1423
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001424 // Adjust the offset to a global offset.
Douglas Gregor2d2689a2011-07-28 21:16:51 +00001425 UnreadMacroRecordOffsets[II] = F.GlobalBitOffset + LocalOffset;
Douglas Gregor295a2a62010-10-30 00:23:06 +00001426}
1427
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001428void ASTReader::ReadDefinedMacros() {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001429 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1430 E = ModuleMgr.rend(); I != E; ++I) {
1431 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001432
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001433 // If there was no preprocessor block, skip this file.
1434 if (!MacroCursor.getBitStreamReader())
1435 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001436
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001437 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001438 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001439
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001440 RecordData Record;
1441 while (true) {
1442 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001443 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001444 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001445
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001446 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1447 // No known subblocks, always skip them.
1448 Cursor.ReadSubBlockID();
1449 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001450 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001451 return;
1452 }
1453 continue;
1454 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001455
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001456 if (Code == llvm::bitc::DEFINE_ABBREV) {
1457 Cursor.ReadAbbrevRecord();
1458 continue;
1459 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001460
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001461 // Read a record.
1462 const char *BlobStart;
1463 unsigned BlobLen;
1464 Record.clear();
1465 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1466 default: // Default behavior: ignore.
1467 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001468
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001469 case PP_MACRO_OBJECT_LIKE:
1470 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregor95eab172011-07-28 20:55:49 +00001471 getLocalIdentifier(**I, Record[0]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001472 break;
1473
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001474 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001475 // Ignore tokens.
1476 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001477 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001478 }
1479 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001480
1481 // Drain the unread macro-record offsets map.
1482 while (!UnreadMacroRecordOffsets.empty())
1483 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1484}
1485
1486void ASTReader::LoadMacroDefinition(
1487 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1488 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
Douglas Gregor295a2a62010-10-30 00:23:06 +00001489 uint64_t Offset = Pos->second;
1490 UnreadMacroRecordOffsets.erase(Pos);
1491
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001492 RecordLocation Loc = getLocalBitOffset(Offset);
1493 ReadMacroRecord(*Loc.F, Loc.Offset);
Douglas Gregor295a2a62010-10-30 00:23:06 +00001494}
1495
1496void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1497 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1498 = UnreadMacroRecordOffsets.find(II);
1499 LoadMacroDefinition(Pos);
Douglas Gregor88a35862010-01-04 19:18:44 +00001500}
1501
Chris Lattner5f9e2722011-07-23 10:55:15 +00001502const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001503 std::string Filename = filenameStrRef;
1504 MaybeAddSystemRootToFilename(Filename);
1505 const FileEntry *File = FileMgr.getFile(Filename);
1506 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1507 OriginalDir != CurrentDir) {
1508 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1509 OriginalDir,
1510 CurrentDir);
1511 if (!resolved.empty())
1512 File = FileMgr.getFile(resolved);
1513 }
1514
1515 return File;
1516}
1517
Douglas Gregore650c8c2009-07-07 00:12:59 +00001518/// \brief If we are loading a relocatable PCH file, and the filename is
1519/// not an absolute path, add the system root to the beginning of the file
1520/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001521void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001522 // If this is not a relocatable PCH file, there's nothing to do.
1523 if (!RelocatablePCH)
1524 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001525
Michael J. Spencer256053b2010-12-17 21:22:22 +00001526 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregore650c8c2009-07-07 00:12:59 +00001527 return;
1528
Douglas Gregor832d6202011-07-22 16:35:34 +00001529 if (isysroot.empty()) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001530 // If no system root was given, default to '/'
1531 Filename.insert(Filename.begin(), '/');
1532 return;
1533 }
Mike Stump1eb44332009-09-09 15:08:12 +00001534
Douglas Gregor832d6202011-07-22 16:35:34 +00001535 unsigned Length = isysroot.size();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001536 if (isysroot[Length - 1] != '/')
1537 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001538
Douglas Gregor832d6202011-07-22 16:35:34 +00001539 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregore650c8c2009-07-07 00:12:59 +00001540}
1541
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001542ASTReader::ASTReadResult
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001543ASTReader::ReadASTBlock(Module &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001544 llvm::BitstreamCursor &Stream = F.Stream;
1545
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001546 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001547 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001548 return Failure;
1549 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001550
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001551 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001552 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001553 while (!Stream.AtEndOfStream()) {
1554 unsigned Code = Stream.ReadCode();
1555 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001556 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001557 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001558 return Failure;
1559 }
Chris Lattner7356a312009-04-11 21:15:38 +00001560
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001561 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001562 }
1563
1564 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1565 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001566 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001567 // We lazily load the decls block, but we want to set up the
1568 // DeclsCursor cursor to point into it. Clone our current bitcode
1569 // cursor to it, enter the block and read the abbrevs in that block.
1570 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001571 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001572 if (Stream.SkipBlock() || // Skip with the main cursor.
1573 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001574 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001575 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001576 return Failure;
1577 }
1578 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001579
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001580 case DECL_UPDATES_BLOCK_ID:
1581 if (Stream.SkipBlock()) {
1582 Error("malformed block record in AST file");
1583 return Failure;
1584 }
1585 break;
1586
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001587 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001588 F.MacroCursor = Stream;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001589 if (!PP.getExternalSource())
1590 PP.setExternalSource(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00001591
Douglas Gregorecdcb882010-10-20 22:00:55 +00001592 if (Stream.SkipBlock() ||
1593 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001594 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001595 return Failure;
1596 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001597 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001598 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001599
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001600 case PREPROCESSOR_DETAIL_BLOCK_ID:
1601 F.PreprocessorDetailCursor = Stream;
1602 if (Stream.SkipBlock() ||
1603 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1604 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1605 Error("malformed preprocessor detail record in AST file");
1606 return Failure;
1607 }
1608 F.PreprocessorDetailStartOffset
1609 = F.PreprocessorDetailCursor.GetCurrentBitNo();
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001610
1611 if (!PP.getPreprocessingRecord())
1612 PP.createPreprocessingRecord(true);
1613 if (!PP.getPreprocessingRecord()->getExternalSource())
1614 PP.getPreprocessingRecord()->SetExternalSource(*this);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001615 break;
1616
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001617 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001618 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001619 case Success:
1620 break;
1621
1622 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001623 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001624 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001625
1626 case IgnorePCH:
1627 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001628 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001629 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001630 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001631 continue;
1632 }
1633
1634 if (Code == llvm::bitc::DEFINE_ABBREV) {
1635 Stream.ReadAbbrevRecord();
1636 continue;
1637 }
1638
1639 // Read and process a record.
1640 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001641 const char *BlobStart = 0;
1642 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001643 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001644 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001645 default: // Default behavior: ignore.
1646 break;
1647
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001648 case METADATA: {
1649 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1650 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001651 : diag::warn_pch_version_too_new);
1652 return IgnorePCH;
1653 }
1654
1655 RelocatablePCH = Record[4];
1656 if (Listener) {
1657 std::string TargetTriple(BlobStart, BlobLen);
1658 if (Listener->ReadTargetTriple(TargetTriple))
1659 return IgnorePCH;
1660 }
1661 break;
1662 }
1663
Douglas Gregore95b9192011-08-17 21:07:30 +00001664 case IMPORTS: {
1665 // Load each of the imported PCH files.
1666 unsigned Idx = 0, N = Record.size();
1667 while (Idx < N) {
1668 // Read information about the AST file.
1669 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1670 unsigned Length = Record[Idx++];
1671 llvm::SmallString<128> ImportedFile(Record.begin() + Idx,
1672 Record.begin() + Idx + Length);
1673 Idx += Length;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001674
Douglas Gregore95b9192011-08-17 21:07:30 +00001675 // Load the AST file.
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001676 switch(ReadASTCore(ImportedFile, ImportedKind, &F)) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001677 case Failure: return Failure;
1678 // If we have to ignore the dependency, we'll have to ignore this too.
1679 case IgnorePCH: return IgnorePCH;
1680 case Success: break;
1681 }
1682 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001683 break;
1684 }
1685
Douglas Gregora119da02011-08-02 16:26:37 +00001686 case TYPE_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001687 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001688 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001689 return Failure;
1690 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001691 F.TypeOffsets = (const uint32_t *)BlobStart;
1692 F.LocalNumTypes = Record[0];
Douglas Gregore3605012011-08-02 18:32:54 +00001693 unsigned LocalBaseTypeIndex = Record[1];
1694 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor1e849b62011-07-29 00:21:44 +00001695
Douglas Gregora119da02011-08-02 16:26:37 +00001696 if (F.LocalNumTypes > 0) {
1697 // Introduce the global -> local mapping for types within this module.
1698 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
1699
1700 // Introduce the local -> global mapping for types within this module.
Douglas Gregore3605012011-08-02 18:32:54 +00001701 F.TypeRemap.insert(std::make_pair(LocalBaseTypeIndex,
1702 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregora119da02011-08-02 16:26:37 +00001703
1704 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
1705 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001706 break;
Douglas Gregora119da02011-08-02 16:26:37 +00001707 }
1708
Douglas Gregor496c7092011-08-03 15:48:04 +00001709 case DECL_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001710 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001711 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001712 return Failure;
1713 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001714 F.DeclOffsets = (const uint32_t *)BlobStart;
1715 F.LocalNumDecls = Record[0];
Douglas Gregor496c7092011-08-03 15:48:04 +00001716 unsigned LocalBaseDeclID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001717 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor96e973f2011-07-20 00:27:43 +00001718
Douglas Gregor496c7092011-08-03 15:48:04 +00001719 if (F.LocalNumDecls > 0) {
1720 // Introduce the global -> local mapping for declarations within this
1721 // module.
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001722 GlobalDeclMap.insert(
1723 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregor496c7092011-08-03 15:48:04 +00001724
1725 // Introduce the local -> global mapping for declarations within this
1726 // module.
1727 F.DeclRemap.insert(std::make_pair(LocalBaseDeclID,
1728 F.BaseDeclID - LocalBaseDeclID));
1729
1730 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
1731 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001732 break;
Douglas Gregor496c7092011-08-03 15:48:04 +00001733 }
1734
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001735 case TU_UPDATE_LEXICAL: {
Douglas Gregor35942772011-09-09 21:34:22 +00001736 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001737 DeclContextInfo &Info = F.DeclContextInfos[TU];
1738 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
1739 Info.NumLexicalDecls
1740 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregor35942772011-09-09 21:34:22 +00001741 TU->setHasExternalLexicalStorage(true);
Sebastian Redld692af72010-07-27 18:24:41 +00001742 break;
1743 }
1744
Sebastian Redle1dde812010-08-24 00:50:04 +00001745 case UPDATE_VISIBLE: {
Douglas Gregor496c7092011-08-03 15:48:04 +00001746 unsigned Idx = 0;
1747 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Sebastian Redle1dde812010-08-24 00:50:04 +00001748 void *Table = ASTDeclContextNameLookupTable::Create(
Douglas Gregor496c7092011-08-03 15:48:04 +00001749 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redle1dde812010-08-24 00:50:04 +00001750 (const unsigned char *)BlobStart,
Douglas Gregor393f2492011-07-22 00:38:23 +00001751 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregor35942772011-09-09 21:34:22 +00001752 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
1753 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001754 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001755 TU->setHasExternalVisibleStorage(true);
Sebastian Redle1dde812010-08-24 00:50:04 +00001756 } else
Douglas Gregor496c7092011-08-03 15:48:04 +00001757 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redle1dde812010-08-24 00:50:04 +00001758 break;
1759 }
1760
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001761 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001762 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
Douglas Gregor496c7092011-08-03 15:48:04 +00001763 for (unsigned i = 0, e = Record.size(); i < e; /* in loop */) {
1764 DeclID First = ReadDeclID(F, Record, i);
1765 DeclID Latest = ReadDeclID(F, Record, i);
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001766 FirstLatestDeclIDs[First] = Latest;
1767 }
1768 break;
1769 }
1770
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001771 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001772 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001773 return IgnorePCH;
1774 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001775
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001776 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001777 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001778 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001779 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001780 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001781 (const unsigned char *)F.IdentifierTableData + Record[0],
1782 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00001783 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001784
1785 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001786 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001787 break;
1788
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001789 case IDENTIFIER_OFFSET: {
Sebastian Redl2da08f92010-07-19 22:28:42 +00001790 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001791 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001792 return Failure;
1793 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001794 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1795 F.LocalNumIdentifiers = Record[0];
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001796 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001797 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor67268d02011-07-20 00:59:32 +00001798
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001799 if (F.LocalNumIdentifiers > 0) {
1800 // Introduce the global -> local mapping for identifiers within this
1801 // module.
1802 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
1803 &F));
1804
1805 // Introduce the local -> global mapping for identifiers within this
1806 // module.
1807 F.IdentifierRemap.insert(
1808 std::make_pair(LocalBaseIdentifierID,
1809 F.BaseIdentifierID - LocalBaseIdentifierID));
1810
1811 IdentifiersLoaded.resize(IdentifiersLoaded.size()
1812 + F.LocalNumIdentifiers);
1813 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001814 break;
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001815 }
1816
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001817 case EXTERNAL_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001818 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1819 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorfdd01722009-04-14 00:24:19 +00001820 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001821
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001822 case SPECIAL_TYPES:
Douglas Gregor393f2492011-07-22 00:38:23 +00001823 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1824 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregorad1de002009-04-18 05:55:16 +00001825 break;
1826
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001827 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001828 TotalNumStatements += Record[0];
1829 TotalNumMacros += Record[1];
1830 TotalLexicalDeclContexts += Record[2];
1831 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001832 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001833
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001834 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001835 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1836 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001837 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001838
Sean Huntebcbe1d2011-05-04 23:29:54 +00001839 case DELEGATING_CTORS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001840 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1841 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00001842 break;
1843
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001844 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor31e37b22011-07-28 18:09:57 +00001845 if (Record.size() % 4 != 0) {
1846 Error("invalid weak identifiers record");
1847 return Failure;
1848 }
1849
1850 // FIXME: Ignore weak undeclared identifiers from non-original PCH
1851 // files. This isn't the way to do it :)
1852 WeakUndeclaredIdentifiers.clear();
1853
1854 // Translate the weak, undeclared identifiers into global IDs.
1855 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
1856 WeakUndeclaredIdentifiers.push_back(
1857 getGlobalIdentifierID(F, Record[I++]));
1858 WeakUndeclaredIdentifiers.push_back(
1859 getGlobalIdentifierID(F, Record[I++]));
1860 WeakUndeclaredIdentifiers.push_back(
1861 ReadSourceLocation(F, Record, I).getRawEncoding());
1862 WeakUndeclaredIdentifiers.push_back(Record[I++]);
1863 }
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00001864 break;
1865
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001866 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001867 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1868 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor14c22f22009-04-22 22:18:58 +00001869 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001870
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00001871 case SELECTOR_OFFSETS: {
Sebastian Redl059612d2010-08-03 21:58:15 +00001872 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00001873 F.LocalNumSelectors = Record[0];
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00001874 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001875 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor96958cb2011-07-20 01:10:58 +00001876
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00001877 if (F.LocalNumSelectors > 0) {
1878 // Introduce the global -> local mapping for selectors within this
1879 // module.
1880 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
1881
1882 // Introduce the local -> global mapping for selectors within this
1883 // module.
1884 F.SelectorRemap.insert(std::make_pair(LocalBaseSelectorID,
1885 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor83941df2009-04-25 17:48:32 +00001886
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00001887 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
1888 }
1889 break;
1890 }
1891
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001892 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00001893 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00001894 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00001895 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001896 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00001897 F.SelectorLookupTableData + Record[0],
1898 F.SelectorLookupTableData,
Douglas Gregor409448c2011-07-21 22:35:25 +00001899 ASTSelectorLookupTrait(*this, F));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00001900 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001901 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001902
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00001903 case REFERENCED_SELECTOR_POOL:
Douglas Gregor8451ec72011-07-28 14:41:43 +00001904 if (!Record.empty()) {
1905 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
1906 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
1907 Record[Idx++]));
1908 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
1909 getRawEncoding());
1910 }
1911 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00001912 break;
1913
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001914 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001915 if (!Record.empty() && Listener)
1916 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001917 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001918
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001919 case SOURCE_LOCATION_OFFSETS: {
1920 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001921 F.LocalNumSLocEntries = Record[0];
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001922 unsigned SLocSpaceSize = Record[1];
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001923 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001924 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
1925 SLocSpaceSize);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001926 // Make our entry in the range map. BaseID is negative and growing, so
1927 // we invert it. Because we invert it, though, we need the other end of
1928 // the range.
1929 unsigned RangeStart =
1930 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
1931 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
1932 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
1933
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001934 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
1935 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
1936 GlobalSLocOffsetMap.insert(
1937 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
1938 - SLocSpaceSize,&F));
1939
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001940 // Initialize the remapping table.
1941 // Invalid stays invalid.
1942 F.SLocRemap.insert(std::make_pair(0U, 0));
1943 // This module. Base was 2 when being compiled.
1944 F.SLocRemap.insert(std::make_pair(2U,
1945 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor0cdd7982011-07-21 18:46:38 +00001946
1947 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001948 break;
1949 }
1950
Douglas Gregor5d51a1d2011-08-01 16:01:55 +00001951 case MODULE_OFFSET_MAP: {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001952 // Additional remapping information.
1953 const unsigned char *Data = (const unsigned char*)BlobStart;
1954 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregorf33740e2011-08-02 10:56:51 +00001955
1956 // Continuous range maps we may be updating in our module.
1957 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001958 ContinuousRangeMap<uint32_t, int, 2>::Builder
1959 IdentifierRemap(F.IdentifierRemap);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00001960 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001961 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
1962 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00001963 SelectorRemap(F.SelectorRemap);
Douglas Gregor496c7092011-08-03 15:48:04 +00001964 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregora119da02011-08-02 16:26:37 +00001965 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
1966
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001967 while(Data < DataEnd) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001968 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001969 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregorf33740e2011-08-02 10:56:51 +00001970 Data += Len;
Jonathan D. Turner48d2c3f2011-07-26 18:21:30 +00001971 Module *OM = ModuleMgr.lookup(Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001972 if (!OM) {
1973 Error("SourceLocation remap refers to unknown module");
1974 return Failure;
1975 }
Douglas Gregorf33740e2011-08-02 10:56:51 +00001976
1977 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
1978 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
1979 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00001980 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
1981 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora119da02011-08-02 16:26:37 +00001982 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00001983
1984 // Source location offset is mapped to OM->SLocEntryBaseOffset.
1985 SLocRemap.insert(std::make_pair(SLocOffset,
1986 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001987 IdentifierRemap.insert(
1988 std::make_pair(IdentifierIDOffset,
1989 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001990 PreprocessedEntityRemap.insert(
1991 std::make_pair(PreprocessedEntityIDOffset,
1992 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00001993 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
1994 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregor496c7092011-08-03 15:48:04 +00001995 DeclRemap.insert(std::make_pair(DeclIDOffset,
1996 OM->BaseDeclID - DeclIDOffset));
1997
Douglas Gregora119da02011-08-02 16:26:37 +00001998 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregore3605012011-08-02 18:32:54 +00001999 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002000 }
2001 break;
2002 }
2003
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002004 case SOURCE_MANAGER_LINE_TABLE:
2005 if (ParseLineTable(F, Record))
2006 return Failure;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002007 break;
2008
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00002009 case FILE_SOURCE_LOCATION_OFFSETS:
2010 F.SLocFileOffsets = (const uint32_t *)BlobStart;
2011 F.LocalNumSLocFileEntries = Record[0];
2012 break;
2013
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002014 case SOURCE_LOCATION_PRELOADS: {
2015 // Need to transform from the local view (1-based IDs) to the global view,
2016 // which is based off F.SLocEntryBaseID.
Douglas Gregorf249bf32011-08-25 21:09:44 +00002017 if (!F.PreloadSLocEntries.empty()) {
2018 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2019 return Failure;
2020 }
2021
2022 F.PreloadSLocEntries.swap(Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002023 break;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002024 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002025
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002026 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002027 if (!DisableStatCache) {
2028 ASTStatCache *MyStatCache =
2029 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2030 (const unsigned char *)BlobStart,
2031 NumStatHits, NumStatMisses);
2032 FileMgr.addStatCache(MyStatCache);
2033 F.StatCache = MyStatCache;
2034 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002035 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002036 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002037
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002038 case EXT_VECTOR_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002039 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2040 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorb81c1702009-04-27 20:06:05 +00002041 break;
2042
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002043 case VTABLE_USES:
Douglas Gregordfe65432011-07-28 19:11:31 +00002044 if (Record.size() % 3 != 0) {
2045 Error("Invalid VTABLE_USES record");
2046 return Failure;
2047 }
2048
Sebastian Redl40566802010-08-05 18:21:25 +00002049 // Later tables overwrite earlier ones.
Douglas Gregordfe65432011-07-28 19:11:31 +00002050 // FIXME: Modules will have some trouble with this. This is clearly not
2051 // the right way to do this.
Douglas Gregor409448c2011-07-21 22:35:25 +00002052 VTableUses.clear();
Douglas Gregordfe65432011-07-28 19:11:31 +00002053
2054 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2055 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2056 VTableUses.push_back(
2057 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2058 VTableUses.push_back(Record[Idx++]);
2059 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002060 break;
2061
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002062 case DYNAMIC_CLASSES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002063 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2064 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002065 break;
2066
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002067 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorf2abb522011-07-28 19:26:52 +00002068 if (PendingInstantiations.size() % 2 != 0) {
2069 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2070 return Failure;
2071 }
2072
2073 // Later lists of pending instantiations overwrite earlier ones.
2074 // FIXME: This is most certainly wrong for modules.
2075 PendingInstantiations.clear();
2076 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2077 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2078 PendingInstantiations.push_back(
2079 ReadSourceLocation(F, Record, I).getRawEncoding());
2080 }
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002081 break;
2082
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002083 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002084 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002085 // FIXME: Modules will have some trouble with this.
2086 SemaDeclRefs.clear();
2087 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2088 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002089 break;
2090
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002091 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002092 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002093 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002094 ActualOriginalFileName.assign(BlobStart, BlobLen);
2095 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002096 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002097 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002098
Douglas Gregor31d375f2011-05-06 21:43:30 +00002099 case ORIGINAL_FILE_ID:
2100 OriginalFileID = FileID::get(Record[0]);
2101 break;
2102
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002103 case ORIGINAL_PCH_DIR:
2104 // The primary AST will be the last to get here, so it will be the one
2105 // that's used.
2106 OriginalDir.assign(BlobStart, BlobLen);
2107 break;
2108
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002109 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002110 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002111 StringRef ASTBranch(BlobStart, BlobLen);
2112 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002113 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002114 return IgnorePCH;
2115 }
2116 break;
2117 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002118
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002119 case PPD_ENTITIES_OFFSETS: {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002120 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart;
2121 assert(BlobLen % sizeof(PPEntityOffset) == 0);
2122 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002123
2124 unsigned LocalBasePreprocessedEntityID = Record[0];
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002125
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002126 unsigned StartingID;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002127 if (!PP.getPreprocessingRecord())
2128 PP.createPreprocessingRecord(true);
2129 if (!PP.getPreprocessingRecord()->getExternalSource())
2130 PP.getPreprocessingRecord()->SetExternalSource(*this);
2131 StartingID
2132 = PP.getPreprocessingRecord()
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002133 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Douglas Gregor9827a802011-07-29 00:56:45 +00002134 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002135
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002136 if (F.NumPreprocessedEntities > 0) {
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002137 // Introduce the global -> local mapping for preprocessed entities in
2138 // this module.
2139 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2140
2141 // Introduce the local -> global mapping for preprocessed entities in
2142 // this module.
2143 F.PreprocessedEntityRemap.insert(
2144 std::make_pair(LocalBasePreprocessedEntityID,
2145 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2146 }
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002147
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002148 break;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002149 }
2150
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002151 case DECL_UPDATE_OFFSETS: {
2152 if (Record.size() % 2 != 0) {
2153 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2154 return Failure;
2155 }
2156 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregor496c7092011-08-03 15:48:04 +00002157 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2158 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002159 break;
2160 }
2161
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002162 case DECL_REPLACEMENTS: {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002163 if (Record.size() % 2 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002164 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002165 return Failure;
2166 }
2167 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregor496c7092011-08-03 15:48:04 +00002168 ReplacedDecls[getGlobalDeclID(F, Record[I])]
2169 = std::make_pair(&F, Record[I+1]);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002170 break;
2171 }
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002172
2173 case OBJC_CHAINED_CATEGORIES: {
2174 if (Record.size() % 3 != 0) {
2175 Error("invalid OBJC_CHAINED_CATEGORIES block in AST file");
2176 return Failure;
2177 }
2178 for (unsigned I = 0, N = Record.size(); I != N; I += 3) {
2179 serialization::GlobalDeclID GlobID = getGlobalDeclID(F, Record[I]);
2180 F.ChainedObjCCategories[GlobID] = std::make_pair(Record[I+1],
2181 Record[I+2]);
2182 ObjCChainedCategoriesInterfaces.insert(GlobID);
2183 }
2184 break;
2185 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002186
2187 case CXX_BASE_SPECIFIER_OFFSETS: {
2188 if (F.LocalNumCXXBaseSpecifiers != 0) {
2189 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2190 return Failure;
2191 }
2192
2193 F.LocalNumCXXBaseSpecifiers = Record[0];
2194 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner1da90142011-07-21 21:15:19 +00002195 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002196 break;
2197 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002198
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002199 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002200 if (Record.size() % 2 != 0) {
2201 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2202 return Failure;
2203 }
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002204
2205 if (F.PragmaDiagMappings.empty())
2206 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002207 else
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002208 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2209 Record.begin(), Record.end());
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002210 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002211
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002212 case CUDA_SPECIAL_DECL_REFS:
2213 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002214 // FIXME: Modules will have trouble with this.
2215 CUDASpecialDeclRefs.clear();
2216 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2217 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002218 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002219
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002220 case HEADER_SEARCH_TABLE: {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002221 F.HeaderFileInfoTableData = BlobStart;
2222 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002223 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002224 if (Record[0]) {
2225 F.HeaderFileInfoTable
2226 = HeaderFileInfoLookupTable::Create(
2227 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002228 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregor95eab172011-07-28 20:55:49 +00002229 HeaderFileInfoTrait(*this, F,
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002230 &PP.getHeaderSearchInfo(),
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002231 BlobStart + Record[2]));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002232
2233 PP.getHeaderSearchInfo().SetExternalSource(this);
2234 if (!PP.getHeaderSearchInfo().getExternalLookup())
2235 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002236 }
2237 break;
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002238 }
2239
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002240 case FP_PRAGMA_OPTIONS:
2241 // Later tables overwrite earlier ones.
2242 FPPragmaOptions.swap(Record);
2243 break;
2244
2245 case OPENCL_EXTENSIONS:
2246 // Later tables overwrite earlier ones.
2247 OpenCLExtensions.swap(Record);
2248 break;
Sean Huntebcbe1d2011-05-04 23:29:54 +00002249
2250 case TENTATIVE_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002251 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2252 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002253 break;
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002254
2255 case KNOWN_NAMESPACES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002256 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2257 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002258 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002259 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002260 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002261 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002262 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002263}
2264
Douglas Gregorc69a2922011-08-25 20:58:51 +00002265ASTReader::ASTReadResult ASTReader::validateFileEntries(Module &M) {
2266 llvm::BitstreamCursor &SLocEntryCursor = M.SLocEntryCursor;
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002267
Douglas Gregorc69a2922011-08-25 20:58:51 +00002268 for (unsigned i = 0, e = M.LocalNumSLocFileEntries; i != e; ++i) {
2269 SLocEntryCursor.JumpToBit(M.SLocFileOffsets[i]);
2270 unsigned Code = SLocEntryCursor.ReadCode();
2271 if (Code == llvm::bitc::END_BLOCK ||
2272 Code == llvm::bitc::ENTER_SUBBLOCK ||
2273 Code == llvm::bitc::DEFINE_ABBREV) {
2274 Error("incorrectly-formatted source location entry in AST file");
2275 return Failure;
2276 }
2277
2278 RecordData Record;
2279 const char *BlobStart;
2280 unsigned BlobLen;
2281 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2282 default:
2283 Error("incorrectly-formatted source location entry in AST file");
2284 return Failure;
2285
2286 case SM_SLOC_FILE_ENTRY: {
2287 StringRef Filename(BlobStart, BlobLen);
2288 const FileEntry *File = getFileEntry(Filename);
2289
2290 if (File == 0) {
2291 std::string ErrorStr = "could not find file '";
2292 ErrorStr += Filename;
2293 ErrorStr += "' referenced by AST file";
2294 Error(ErrorStr.c_str());
2295 return IgnorePCH;
2296 }
2297
2298 if (Record.size() < 6) {
2299 Error("source location entry is incorrect");
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002300 return Failure;
2301 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002302
Douglas Gregorc69a2922011-08-25 20:58:51 +00002303 // The stat info from the FileEntry came from the cached stat
2304 // info of the PCH, so we cannot trust it.
2305 struct stat StatBuf;
2306 if (::stat(File->getName(), &StatBuf) != 0) {
2307 StatBuf.st_size = File->getSize();
2308 StatBuf.st_mtime = File->getModificationTime();
2309 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002310
Douglas Gregorc69a2922011-08-25 20:58:51 +00002311 if (((off_t)Record[4] != StatBuf.st_size
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002312#if !defined(LLVM_ON_WIN32)
Douglas Gregorc69a2922011-08-25 20:58:51 +00002313 // In our regression testing, the Windows file system seems to
2314 // have inconsistent modification times that sometimes
2315 // erroneously trigger this error-handling path.
2316 || (time_t)Record[5] != StatBuf.st_mtime
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002317#endif
Douglas Gregorc69a2922011-08-25 20:58:51 +00002318 )) {
2319 Error(diag::err_fe_pch_file_modified, Filename);
2320 return IgnorePCH;
2321 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002322
Douglas Gregorc69a2922011-08-25 20:58:51 +00002323 break;
2324 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002325 }
2326 }
2327
2328 return Success;
2329}
2330
Douglas Gregor46c03c32011-08-25 21:19:59 +00002331namespace {
2332 /// \brief Visitor class used to look up identifirs in an AST file.
2333 class IdentifierLookupVisitor {
2334 StringRef Name;
2335 IdentifierInfo *Found;
2336 public:
2337 explicit IdentifierLookupVisitor(StringRef Name) : Name(Name), Found() { }
2338
2339 static bool visit(Module &M, void *UserData) {
2340 IdentifierLookupVisitor *This
2341 = static_cast<IdentifierLookupVisitor *>(UserData);
2342
2343 ASTIdentifierLookupTable *IdTable
Douglas Gregor2ea054f2011-08-26 22:04:51 +00002344 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
Douglas Gregor46c03c32011-08-25 21:19:59 +00002345 if (!IdTable)
2346 return false;
2347
2348 std::pair<const char*, unsigned> Key(This->Name.begin(),
2349 This->Name.size());
2350 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2351 if (Pos == IdTable->end())
2352 return false;
2353
2354 // Dereferencing the iterator has the effect of building the
2355 // IdentifierInfo node and populating it with the various
2356 // declarations it needs.
2357 This->Found = *Pos;
2358 return true;
2359 }
2360
2361 // \brief Retrieve the identifier info found within the module
2362 // files.
2363 IdentifierInfo *getIdentifierInfo() const { return Found; }
2364 };
2365}
2366
2367
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002368ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregor72a9ae12011-07-22 16:00:58 +00002369 ModuleKind Type) {
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002370 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002371 case Failure: return Failure;
2372 case IgnorePCH: return IgnorePCH;
2373 case Success: break;
2374 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002375
2376 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002377
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002378 // Check the predefines buffers.
Argyrios Kyrtzidis26d43cd2011-09-12 18:09:38 +00002379 if (!DisableValidation && Type != MK_Module && Type != MK_Preamble &&
2380 // FIXME: CheckPredefinesBuffers also sets the SuggestedPredefines;
2381 // if DisableValidation is true, defines that were set on command-line
2382 // but not in the PCH file will not be added to SuggestedPredefines.
2383 CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002384 return IgnorePCH;
2385
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002386 // Initialization of keywords and pragmas occurs before the
2387 // AST file is read, so there may be some identifiers that were
2388 // loaded into the IdentifierTable before we intercepted the
2389 // creation of identifiers. Iterate through the list of known
2390 // identifiers and determine whether we have to establish
2391 // preprocessor definitions or top-level identifier declaration
2392 // chains for those identifiers.
2393 //
2394 // We copy the IdentifierInfo pointers to a small vector first,
2395 // since de-serializing declarations or macro definitions can add
2396 // new entries into the identifier table, invalidating the
2397 // iterators.
2398 //
2399 // FIXME: We need a lazier way to load this information, e.g., by marking
2400 // the identifier data as 'dirty', so that it will be looked up in the
2401 // AST file(s) if it is uttered in the source. This could save us some
2402 // module load time.
2403 SmallVector<IdentifierInfo *, 128> Identifiers;
2404 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2405 IdEnd = PP.getIdentifierTable().end();
2406 Id != IdEnd; ++Id)
2407 Identifiers.push_back(Id->second);
2408
2409 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2410 IdentifierLookupVisitor Visitor(Identifiers[I]->getName());
2411 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002412 }
2413
Douglas Gregor35942772011-09-09 21:34:22 +00002414 InitializeContext();
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002415
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002416 if (DeserializationListener)
2417 DeserializationListener->ReaderInitialized(this);
2418
Argyrios Kyrtzidis507097e2011-09-19 20:40:35 +00002419 // If this AST file is a precompiled preamble, then set the preamble file ID
2420 // of the source manager to the file source file from which the preamble was
2421 // built.
Douglas Gregor72a9ae12011-07-22 16:00:58 +00002422 if (Type == MK_Preamble) {
Argyrios Kyrtzidis507097e2011-09-19 20:40:35 +00002423 if (!OriginalFileID.isInvalid()) {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00002424 OriginalFileID = FileID::get(ModuleMgr.getPrimaryModule().SLocEntryBaseID
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002425 + OriginalFileID.getOpaqueValue() - 1);
Argyrios Kyrtzidis507097e2011-09-19 20:40:35 +00002426 SourceMgr.setPreambleFileID(OriginalFileID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002427 }
Douglas Gregor414cb642010-11-30 05:23:00 +00002428 }
2429
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002430 return Success;
2431}
2432
Chris Lattner5f9e2722011-07-23 10:55:15 +00002433ASTReader::ASTReadResult ASTReader::ReadASTCore(StringRef FileName,
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002434 ModuleKind Type,
2435 Module *ImportedBy) {
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002436 Module *M;
2437 bool NewModule;
2438 std::string ErrorStr;
2439 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
2440 ErrorStr);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002441
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002442 if (!M) {
2443 // We couldn't load the module.
2444 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2445 + ErrorStr;
2446 Error(Msg);
2447 return Failure;
2448 }
2449
2450 if (!NewModule) {
2451 // We've already loaded this module.
2452 return Success;
2453 }
2454
2455 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2456 // module?
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002457 if (FileName != "-") {
2458 CurrentDir = llvm::sys::path::parent_path(FileName);
2459 if (CurrentDir.empty()) CurrentDir = ".";
2460 }
2461
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002462 Module &F = *M;
Sebastian Redl9137a522010-07-16 17:50:48 +00002463 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002464 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002465 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002466
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002467 // Sniff for the signature.
2468 if (Stream.Read(8) != 'C' ||
2469 Stream.Read(8) != 'P' ||
2470 Stream.Read(8) != 'C' ||
2471 Stream.Read(8) != 'H') {
2472 Diag(diag::err_not_a_pch_file) << FileName;
2473 return Failure;
2474 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002475
Douglas Gregor2cf26342009-04-09 22:27:44 +00002476 while (!Stream.AtEndOfStream()) {
2477 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002478
Douglas Gregore1d918e2009-04-10 23:10:45 +00002479 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002480 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002481 return Failure;
2482 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002483
2484 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002485
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002486 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002487 switch (BlockID) {
2488 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002489 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002490 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002491 return Failure;
2492 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002493 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002494 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002495 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002496 case Success:
2497 break;
2498
2499 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002500 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002501
2502 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002503 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002504 // AST block, skipping subblocks, to see if there are other
2505 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002506
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002507 // FIXME: We can't clear loaded slocentries anymore.
2508 //SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002509
2510 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002511 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002512 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002513
Douglas Gregore1d918e2009-04-10 23:10:45 +00002514 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002515 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002516 break;
2517 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002518 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002519 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002520 return Failure;
2521 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002522 break;
2523 }
Mike Stump1eb44332009-09-09 15:08:12 +00002524 }
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002525
Douglas Gregor72a9ae12011-07-22 16:00:58 +00002526 // Once read, set the Module bit base offset and update the size in
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002527 // bits of all files we've seen.
2528 F.GlobalBitOffset = TotalModulesSizeInBits;
2529 TotalModulesSizeInBits += F.SizeInBits;
2530 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
Douglas Gregorc69a2922011-08-25 20:58:51 +00002531
2532 // Make sure that the files this module was built against are still available.
2533 if (!DisableValidation) {
2534 switch(validateFileEntries(*M)) {
2535 case Failure: return Failure;
2536 case IgnorePCH: return IgnorePCH;
2537 case Success: break;
2538 }
2539 }
Douglas Gregorf249bf32011-08-25 21:09:44 +00002540
2541 // Preload SLocEntries.
2542 for (unsigned I = 0, N = M->PreloadSLocEntries.size(); I != N; ++I) {
2543 int Index = int(M->PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Argyrios Kyrtzidisac1ffcc2011-09-19 20:39:54 +00002544 // Load it through the SourceManager and don't call ReadSLocEntryRecord()
2545 // directly because the entry may have already been loaded in which case
2546 // calling ReadSLocEntryRecord() directly would trigger an assertion in
2547 // SourceManager.
2548 SourceMgr.getLoadedSLocEntryByID(Index);
Douglas Gregorf249bf32011-08-25 21:09:44 +00002549 }
2550
Douglas Gregorc69a2922011-08-25 20:58:51 +00002551
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002552 return Success;
2553}
2554
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002555void ASTReader::InitializeContext() {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002556 // If there's a listener, notify them that we "read" the translation unit.
2557 if (DeserializationListener)
Douglas Gregor35942772011-09-09 21:34:22 +00002558 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
2559 Context.getTranslationUnitDecl());
Douglas Gregor3747ee72010-10-01 01:18:02 +00002560
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002561 // Make sure we load the declaration update records for the translation unit,
2562 // if there are any.
Douglas Gregor35942772011-09-09 21:34:22 +00002563 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
2564 Context.getTranslationUnitDecl());
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002565
Douglas Gregor5f957282011-08-11 22:18:49 +00002566 // FIXME: Find a better way to deal with collisions between these
2567 // built-in types. Right now, we just ignore the problem.
2568
2569 // Load the special types.
Douglas Gregor02a5e872011-09-10 00:30:18 +00002570 if (SpecialTypes.size() > NumSpecialTypeIDs) {
2571 if (Context.getBuiltinVaListType().isNull()) {
2572 Context.setBuiltinVaListType(
2573 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
Douglas Gregor5f957282011-08-11 22:18:49 +00002574 }
2575
Douglas Gregor02a5e872011-09-10 00:30:18 +00002576 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL]) {
2577 if (Context.ObjCProtoType.isNull())
2578 Context.ObjCProtoType = GetType(Proto);
2579 }
2580
2581 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2582 if (!Context.CFConstantStringTypeDecl)
2583 Context.setCFConstantStringType(GetType(String));
2584 }
2585
2586 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2587 QualType FileType = GetType(File);
2588 if (FileType.isNull()) {
2589 Error("FILE type is NULL");
2590 return;
2591 }
2592
2593 if (!Context.FILEDecl) {
2594 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2595 Context.setFILEDecl(Typedef->getDecl());
2596 else {
2597 const TagType *Tag = FileType->getAs<TagType>();
2598 if (!Tag) {
2599 Error("Invalid FILE type in AST file");
2600 return;
2601 }
2602 Context.setFILEDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002603 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002604 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002605 }
Douglas Gregor5f957282011-08-11 22:18:49 +00002606
Douglas Gregor02a5e872011-09-10 00:30:18 +00002607 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
2608 QualType Jmp_bufType = GetType(Jmp_buf);
2609 if (Jmp_bufType.isNull()) {
2610 Error("jmp_buf type is NULL");
2611 return;
2612 }
2613
2614 if (!Context.jmp_bufDecl) {
2615 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2616 Context.setjmp_bufDecl(Typedef->getDecl());
2617 else {
2618 const TagType *Tag = Jmp_bufType->getAs<TagType>();
2619 if (!Tag) {
2620 Error("Invalid jmp_buf type in AST file");
2621 return;
2622 }
2623 Context.setjmp_bufDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002624 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002625 }
Mike Stump782fa302009-07-28 02:25:19 +00002626 }
Douglas Gregor02a5e872011-09-10 00:30:18 +00002627
2628 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
2629 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2630 if (Sigjmp_bufType.isNull()) {
2631 Error("sigjmp_buf type is NULL");
2632 return;
2633 }
2634
2635 if (!Context.sigjmp_bufDecl) {
2636 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2637 Context.setsigjmp_bufDecl(Typedef->getDecl());
2638 else {
2639 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
2640 assert(Tag && "Invalid sigjmp_buf type in AST file");
2641 Context.setsigjmp_bufDecl(Tag->getDecl());
2642 }
2643 }
2644 }
2645
2646 if (unsigned ObjCIdRedef
2647 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
2648 if (Context.ObjCIdRedefinitionType.isNull())
2649 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
2650 }
2651
2652 if (unsigned ObjCClassRedef
2653 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
2654 if (Context.ObjCClassRedefinitionType.isNull())
2655 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
2656 }
2657
2658 if (unsigned ObjCSelRedef
2659 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
2660 if (Context.ObjCSelRedefinitionType.isNull())
2661 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
2662 }
Douglas Gregor5f957282011-08-11 22:18:49 +00002663 }
2664
Douglas Gregor35942772011-09-09 21:34:22 +00002665 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002666
2667 // If there were any CUDA special declarations, deserialize them.
2668 if (!CUDASpecialDeclRefs.empty()) {
2669 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
Douglas Gregor35942772011-09-09 21:34:22 +00002670 Context.setcudaConfigureCallDecl(
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002671 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2672 }
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002673}
2674
Douglas Gregorb64c1932009-05-12 01:31:05 +00002675/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002676/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002677/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002678std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002679 FileManager &FileMgr,
David Blaikied6471f72011-09-25 23:23:43 +00002680 DiagnosticsEngine &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002681 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002682 std::string ErrStr;
2683 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00002684 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002685 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002686 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002687 return std::string();
2688 }
2689
2690 // Initialize the stream
2691 llvm::BitstreamReader StreamFile;
2692 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002693 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002694 (const unsigned char *)Buffer->getBufferEnd());
2695 Stream.init(StreamFile);
2696
2697 // Sniff for the signature.
2698 if (Stream.Read(8) != 'C' ||
2699 Stream.Read(8) != 'P' ||
2700 Stream.Read(8) != 'C' ||
2701 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002702 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002703 return std::string();
2704 }
2705
2706 RecordData Record;
2707 while (!Stream.AtEndOfStream()) {
2708 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002709
Douglas Gregorb64c1932009-05-12 01:31:05 +00002710 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2711 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002712
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002713 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002714 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002715 case AST_BLOCK_ID:
2716 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002717 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002718 return std::string();
2719 }
2720 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002721
Douglas Gregorb64c1932009-05-12 01:31:05 +00002722 default:
2723 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002724 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002725 return std::string();
2726 }
2727 break;
2728 }
2729 continue;
2730 }
2731
2732 if (Code == llvm::bitc::END_BLOCK) {
2733 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002734 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002735 return std::string();
2736 }
2737 continue;
2738 }
2739
2740 if (Code == llvm::bitc::DEFINE_ABBREV) {
2741 Stream.ReadAbbrevRecord();
2742 continue;
2743 }
2744
2745 Record.clear();
2746 const char *BlobStart = 0;
2747 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002748 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002749 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00002750 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00002751 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00002752
2753 return std::string();
2754}
2755
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002756/// \brief Parse the record that corresponds to a LangOptions data
2757/// structure.
2758///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002759/// This routine parses the language options from the AST file and then gives
2760/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002761///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002762/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002763bool ASTReader::ParseLanguageOptions(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002764 const SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002765 if (Listener) {
2766 LangOptions LangOpts;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002767 unsigned Idx = 0;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00002768#define LANGOPT(Name, Bits, Default, Description) \
2769 LangOpts.Name = Record[Idx++];
2770#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
2771 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
2772#include "clang/Basic/LangOptions.def"
2773
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002774 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002775 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002776
2777 return false;
2778}
2779
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002780PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002781 PreprocessedEntityID PPID = Index+1;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002782 GlobalPreprocessedEntityMapType::iterator
2783 I = GlobalPreprocessedEntityMap.find(Index);
2784 assert(I != GlobalPreprocessedEntityMap.end() &&
2785 "Corrupted global preprocessed entity map");
2786 Module &M = *I->second;
2787 unsigned LocalIndex = Index - M.BasePreprocessedEntityID;
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002788 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002789
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002790 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002791 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002792
2793 unsigned Code = M.PreprocessorDetailCursor.ReadCode();
2794 switch (Code) {
2795 case llvm::bitc::END_BLOCK:
2796 return 0;
2797
2798 case llvm::bitc::ENTER_SUBBLOCK:
2799 Error("unexpected subblock record in preprocessor detail block");
2800 return 0;
2801
2802 case llvm::bitc::DEFINE_ABBREV:
2803 Error("unexpected abbrevation record in preprocessor detail block");
2804 return 0;
2805
2806 default:
2807 break;
2808 }
2809
2810 if (!PP.getPreprocessingRecord()) {
2811 Error("no preprocessing record");
2812 return 0;
2813 }
2814
2815 // Read the record.
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002816 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
2817 ReadSourceLocation(M, PPOffs.End));
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002818 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
2819 const char *BlobStart = 0;
2820 unsigned BlobLen = 0;
2821 RecordData Record;
2822 PreprocessorDetailRecordTypes RecType =
2823 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord(
2824 Code, Record, BlobStart, BlobLen);
2825 switch (RecType) {
2826 case PPD_MACRO_EXPANSION: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002827 bool isBuiltin = Record[0];
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002828 IdentifierInfo *Name = 0;
2829 MacroDefinition *Def = 0;
2830 if (isBuiltin)
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002831 Name = getLocalIdentifier(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002832 else {
2833 PreprocessedEntityID
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002834 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002835 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
2836 }
2837
2838 MacroExpansion *ME;
2839 if (isBuiltin)
2840 ME = new (PPRec) MacroExpansion(Name, Range);
2841 else
2842 ME = new (PPRec) MacroExpansion(Def, Range);
2843
2844 return ME;
2845 }
2846
2847 case PPD_MACRO_DEFINITION: {
2848 // Decode the identifier info and then check again; if the macro is
2849 // still defined and associated with the identifier,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002850 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002851 MacroDefinition *MD
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002852 = new (PPRec) MacroDefinition(II, Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002853
2854 if (DeserializationListener)
2855 DeserializationListener->MacroDefinitionRead(PPID, MD);
2856
2857 return MD;
2858 }
2859
2860 case PPD_INCLUSION_DIRECTIVE: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002861 const char *FullFileNameStart = BlobStart + Record[0];
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002862 const FileEntry *File
2863 = PP.getFileManager().getFile(StringRef(FullFileNameStart,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002864 BlobLen - Record[0]));
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002865
2866 // FIXME: Stable encoding
2867 InclusionDirective::InclusionKind Kind
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002868 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002869 InclusionDirective *ID
2870 = new (PPRec) InclusionDirective(PPRec, Kind,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002871 StringRef(BlobStart, Record[0]),
2872 Record[1],
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002873 File,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002874 Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002875 return ID;
2876 }
2877 }
2878
2879 Error("invalid offset in preprocessor detail block");
2880 return 0;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002881}
2882
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002883/// \brief \arg SLocMapI points at a chunk of a module that contains no
2884/// preprocessed entities or the entities it contains are not the ones we are
2885/// looking for. Find the next module that contains entities and return the ID
2886/// of the first entry.
2887PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
2888 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
2889 ++SLocMapI;
2890 for (GlobalSLocOffsetMapType::const_iterator
2891 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
2892 Module &M = *SLocMapI->second;
2893 if (M.NumPreprocessedEntities)
2894 return getGlobalPreprocessedEntityID(M, M.BasePreprocessedEntityID);
2895 }
2896
2897 return getTotalNumPreprocessedEntities();
2898}
2899
2900namespace {
2901
2902template <unsigned PPEntityOffset::*PPLoc>
2903struct PPEntityComp {
2904 const ASTReader &Reader;
2905 Module &M;
2906
2907 PPEntityComp(const ASTReader &Reader, Module &M) : Reader(Reader), M(M) { }
2908
Benjamin Kramer88df1252011-09-21 06:42:26 +00002909 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
2910 SourceLocation LHS = getLoc(L);
2911 SourceLocation RHS = getLoc(R);
2912 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
2913 }
2914
2915 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002916 SourceLocation LHS = getLoc(L);
2917 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
2918 }
2919
Benjamin Kramer88df1252011-09-21 06:42:26 +00002920 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002921 SourceLocation RHS = getLoc(R);
2922 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
2923 }
2924
2925 SourceLocation getLoc(const PPEntityOffset &PPE) const {
2926 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
2927 }
2928};
2929
2930}
2931
2932/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
2933PreprocessedEntityID
2934ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
2935 if (SourceMgr.isLocalSourceLocation(BLoc))
2936 return getTotalNumPreprocessedEntities();
2937
2938 GlobalSLocOffsetMapType::const_iterator
2939 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
2940 BLoc.getOffset());
2941 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
2942 "Corrupted global sloc offset map");
2943
2944 if (SLocMapI->second->NumPreprocessedEntities == 0)
2945 return findNextPreprocessedEntity(SLocMapI);
2946
2947 Module &M = *SLocMapI->second;
2948 typedef const PPEntityOffset *pp_iterator;
2949 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
2950 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
Argyrios Kyrtzidis4cd06342011-09-22 21:17:02 +00002951
2952 size_t Count = M.NumPreprocessedEntities;
2953 size_t Half;
2954 pp_iterator First = pp_begin;
2955 pp_iterator PPI;
2956
2957 // Do a binary search manually instead of using std::lower_bound because
2958 // The end locations of entities may be unordered (when a macro expansion
2959 // is inside another macro argument), but for this case it is not important
2960 // whether we get the first macro expansion or its containing macro.
2961 while (Count > 0) {
2962 Half = Count/2;
2963 PPI = First;
2964 std::advance(PPI, Half);
2965 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
2966 BLoc)){
2967 First = PPI;
2968 ++First;
2969 Count = Count - Half - 1;
2970 } else
2971 Count = Half;
2972 }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002973
2974 if (PPI == pp_end)
2975 return findNextPreprocessedEntity(SLocMapI);
2976
2977 return getGlobalPreprocessedEntityID(M,
2978 M.BasePreprocessedEntityID + (PPI - pp_begin));
2979}
2980
2981/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
2982PreprocessedEntityID
2983ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
2984 if (SourceMgr.isLocalSourceLocation(ELoc))
2985 return getTotalNumPreprocessedEntities();
2986
2987 GlobalSLocOffsetMapType::const_iterator
2988 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
2989 ELoc.getOffset());
2990 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
2991 "Corrupted global sloc offset map");
2992
2993 if (SLocMapI->second->NumPreprocessedEntities == 0)
2994 return findNextPreprocessedEntity(SLocMapI);
2995
2996 Module &M = *SLocMapI->second;
2997 typedef const PPEntityOffset *pp_iterator;
2998 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
2999 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
3000 pp_iterator PPI =
3001 std::upper_bound(pp_begin, pp_end, ELoc,
3002 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
3003
3004 if (PPI == pp_end)
3005 return findNextPreprocessedEntity(SLocMapI);
3006
3007 return getGlobalPreprocessedEntityID(M,
3008 M.BasePreprocessedEntityID + (PPI - pp_begin));
3009}
3010
3011/// \brief Returns a pair of [Begin, End) indices of preallocated
3012/// preprocessed entities that \arg Range encompasses.
3013std::pair<unsigned, unsigned>
3014 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
3015 if (Range.isInvalid())
3016 return std::make_pair(0,0);
3017 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
3018
3019 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
3020 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
3021 return std::make_pair(BeginID, EndID);
3022}
3023
Douglas Gregord10a3812011-08-25 18:14:34 +00003024namespace {
3025 /// \brief Visitor used to search for information about a header file.
3026 class HeaderFileInfoVisitor {
3027 ASTReader &Reader;
3028 const FileEntry *FE;
3029
3030 llvm::Optional<HeaderFileInfo> HFI;
3031
3032 public:
3033 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
3034 : Reader(Reader), FE(FE) { }
3035
3036 static bool visit(Module &M, void *UserData) {
3037 HeaderFileInfoVisitor *This
3038 = static_cast<HeaderFileInfoVisitor *>(UserData);
3039
3040 HeaderFileInfoTrait Trait(This->Reader, M,
3041 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
3042 M.HeaderFileFrameworkStrings,
3043 This->FE->getName());
3044
3045 HeaderFileInfoLookupTable *Table
3046 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
3047 if (!Table)
3048 return false;
3049
3050 // Look in the on-disk hash table for an entry for this file name.
3051 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
3052 &Trait);
3053 if (Pos == Table->end())
3054 return false;
3055
3056 This->HFI = *Pos;
3057 return true;
3058 }
3059
3060 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
3061 };
3062}
3063
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003064HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003065 HeaderFileInfoVisitor Visitor(*this, FE);
3066 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
3067 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003068 if (Listener)
Douglas Gregord10a3812011-08-25 18:14:34 +00003069 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
3070 return *HFI;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003071 }
3072
3073 return HeaderFileInfo();
3074}
3075
David Blaikied6471f72011-09-25 23:23:43 +00003076void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00003077 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
3078 Module &F = *(*I);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003079 unsigned Idx = 0;
3080 while (Idx < F.PragmaDiagMappings.size()) {
3081 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
3082 while (1) {
3083 assert(Idx < F.PragmaDiagMappings.size() &&
3084 "Invalid data, didn't find '-1' marking end of diag/map pairs");
3085 if (Idx >= F.PragmaDiagMappings.size()) {
3086 break; // Something is messed up but at least avoid infinite loop in
3087 // release build.
3088 }
3089 unsigned DiagID = F.PragmaDiagMappings[Idx++];
3090 if (DiagID == (unsigned)-1) {
3091 break; // no more diag/map pairs for this location.
3092 }
3093 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
Argyrios Kyrtzidiseabdd672011-09-26 23:06:41 +00003094 // The user bit gets set by WritePragmaDiagnosticMappings.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003095 Diag.setDiagnosticMapping(DiagID, Map, Loc);
3096 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003097 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00003098 }
3099}
3100
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003101/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003102ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregora119da02011-08-02 16:26:37 +00003103 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turnere9b76c12011-07-20 21:31:32 +00003104 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor1e849b62011-07-29 00:21:44 +00003105 Module *M = I->second;
Douglas Gregore3605012011-08-02 18:32:54 +00003106 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003107}
3108
3109/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00003110///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003111/// The index is the type ID, shifted and minus the number of predefs. This
3112/// routine actually reads the record corresponding to the type at the given
3113/// location. It is a helper routine for GetType, which deals with reading type
3114/// IDs.
Douglas Gregor393f2492011-07-22 00:38:23 +00003115QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003116 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00003117 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00003118
Douglas Gregor0b748912009-04-14 21:18:50 +00003119 // Keep track of where we are in the stream, then jump back there
3120 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003121 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00003122
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003123 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00003124
Douglas Gregord89275b2009-07-06 18:54:52 +00003125 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003126 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00003127
Douglas Gregor393f2492011-07-22 00:38:23 +00003128 unsigned Idx = 0;
Sebastian Redlc3632732010-10-05 15:59:54 +00003129 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003130 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003131 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003132 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3133 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003134 if (Record.size() != 2) {
3135 Error("Incorrect encoding of extended qualifier type");
3136 return QualType();
3137 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003138 QualType Base = readType(*Loc.F, Record, Idx);
3139 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
Douglas Gregor35942772011-09-09 21:34:22 +00003140 return Context.getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00003141 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003142
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003143 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003144 if (Record.size() != 1) {
3145 Error("Incorrect encoding of complex type");
3146 return QualType();
3147 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003148 QualType ElemType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003149 return Context.getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003150 }
3151
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003152 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003153 if (Record.size() != 1) {
3154 Error("Incorrect encoding of pointer type");
3155 return QualType();
3156 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003157 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003158 return Context.getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003159 }
3160
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003161 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003162 if (Record.size() != 1) {
3163 Error("Incorrect encoding of block pointer type");
3164 return QualType();
3165 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003166 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003167 return Context.getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003168 }
3169
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003170 case TYPE_LVALUE_REFERENCE: {
Richard Smithdf1550f2011-04-12 10:38:03 +00003171 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003172 Error("Incorrect encoding of lvalue reference type");
3173 return QualType();
3174 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003175 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003176 return Context.getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003177 }
3178
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003179 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003180 if (Record.size() != 1) {
3181 Error("Incorrect encoding of rvalue reference type");
3182 return QualType();
3183 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003184 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003185 return Context.getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003186 }
3187
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003188 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00003189 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003190 Error("Incorrect encoding of member pointer type");
3191 return QualType();
3192 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003193 QualType PointeeType = readType(*Loc.F, Record, Idx);
3194 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00003195 if (PointeeType.isNull() || ClassType.isNull())
3196 return QualType();
3197
Douglas Gregor35942772011-09-09 21:34:22 +00003198 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003199 }
3200
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003201 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003202 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003203 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3204 unsigned IndexTypeQuals = Record[2];
3205 unsigned Idx = 3;
3206 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003207 return Context.getConstantArrayType(ElementType, Size,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003208 ASM, IndexTypeQuals);
3209 }
3210
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003211 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003212 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003213 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3214 unsigned IndexTypeQuals = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003215 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003216 }
3217
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003218 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003219 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00003220 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3221 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00003222 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3223 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
Douglas Gregor35942772011-09-09 21:34:22 +00003224 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003225 ASM, IndexTypeQuals,
3226 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003227 }
3228
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003229 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003230 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003231 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003232 return QualType();
3233 }
3234
Douglas Gregor393f2492011-07-22 00:38:23 +00003235 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003236 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00003237 unsigned VecKind = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003238 return Context.getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00003239 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003240 }
3241
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003242 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003243 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003244 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003245 return QualType();
3246 }
3247
Douglas Gregor393f2492011-07-22 00:38:23 +00003248 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003249 unsigned NumElements = Record[1];
Douglas Gregor35942772011-09-09 21:34:22 +00003250 return Context.getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003251 }
3252
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003253 case TYPE_FUNCTION_NO_PROTO: {
John McCallf85e1932011-06-15 23:02:42 +00003254 if (Record.size() != 6) {
Douglas Gregora02b1472009-04-28 21:53:25 +00003255 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003256 return QualType();
3257 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003258 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCallf85e1932011-06-15 23:02:42 +00003259 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
3260 (CallingConv)Record[4], Record[5]);
Douglas Gregor35942772011-09-09 21:34:22 +00003261 return Context.getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003262 }
3263
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003264 case TYPE_FUNCTION_PROTO: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003265 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalle23cf432010-12-14 08:05:40 +00003266
3267 FunctionProtoType::ExtProtoInfo EPI;
3268 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmana49218e2011-04-09 08:18:08 +00003269 /*hasregparm*/ Record[2],
3270 /*regparm*/ Record[3],
John McCallf85e1932011-06-15 23:02:42 +00003271 static_cast<CallingConv>(Record[4]),
3272 /*produces*/ Record[5]);
John McCalle23cf432010-12-14 08:05:40 +00003273
John McCallf85e1932011-06-15 23:02:42 +00003274 unsigned Idx = 6;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003275 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003276 SmallVector<QualType, 16> ParamTypes;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003277 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00003278 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalle23cf432010-12-14 08:05:40 +00003279
3280 EPI.Variadic = Record[Idx++];
3281 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00003282 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redl60618fa2011-03-12 11:50:43 +00003283 ExceptionSpecificationType EST =
3284 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3285 EPI.ExceptionSpecType = EST;
3286 if (EST == EST_Dynamic) {
3287 EPI.NumExceptions = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003288 SmallVector<QualType, 2> Exceptions;
Sebastian Redl60618fa2011-03-12 11:50:43 +00003289 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00003290 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redl60618fa2011-03-12 11:50:43 +00003291 EPI.Exceptions = Exceptions.data();
3292 } else if (EST == EST_ComputedNoexcept) {
3293 EPI.NoexceptExpr = ReadExpr(*Loc.F);
3294 }
Douglas Gregor35942772011-09-09 21:34:22 +00003295 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00003296 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003297 }
3298
Douglas Gregor409448c2011-07-21 22:35:25 +00003299 case TYPE_UNRESOLVED_USING: {
3300 unsigned Idx = 0;
Douglas Gregor35942772011-09-09 21:34:22 +00003301 return Context.getTypeDeclType(
Douglas Gregor409448c2011-07-21 22:35:25 +00003302 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
3303 }
3304
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003305 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003306 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003307 Error("incorrect encoding of typedef type");
3308 return QualType();
3309 }
Douglas Gregor409448c2011-07-21 22:35:25 +00003310 unsigned Idx = 0;
3311 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003312 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003313 if (!Canonical.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00003314 Canonical = Context.getCanonicalType(Canonical);
3315 return Context.getTypedefType(Decl, Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003316 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003317
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003318 case TYPE_TYPEOF_EXPR:
Douglas Gregor35942772011-09-09 21:34:22 +00003319 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003320
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003321 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003322 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003323 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003324 return QualType();
3325 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003326 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003327 return Context.getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003328 }
Mike Stump1eb44332009-09-09 15:08:12 +00003329
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003330 case TYPE_DECLTYPE:
Douglas Gregor35942772011-09-09 21:34:22 +00003331 return Context.getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson395b4752009-06-24 19:06:50 +00003332
Sean Huntca63c202011-05-24 22:41:36 +00003333 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003334 QualType BaseType = readType(*Loc.F, Record, Idx);
3335 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Sean Huntca63c202011-05-24 22:41:36 +00003336 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003337 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
Sean Huntca63c202011-05-24 22:41:36 +00003338 }
3339
Richard Smith34b41d92011-02-20 03:19:35 +00003340 case TYPE_AUTO:
Douglas Gregor35942772011-09-09 21:34:22 +00003341 return Context.getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith34b41d92011-02-20 03:19:35 +00003342
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003343 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003344 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003345 Error("incorrect encoding of record type");
3346 return QualType();
3347 }
Douglas Gregor409448c2011-07-21 22:35:25 +00003348 unsigned Idx = 0;
3349 bool IsDependent = Record[Idx++];
3350 QualType T
Douglas Gregor35942772011-09-09 21:34:22 +00003351 = Context.getRecordType(ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00003352 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003353 return T;
3354 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003355
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003356 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003357 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003358 Error("incorrect encoding of enum type");
3359 return QualType();
3360 }
Douglas Gregor409448c2011-07-21 22:35:25 +00003361 unsigned Idx = 0;
3362 bool IsDependent = Record[Idx++];
3363 QualType T
Douglas Gregor35942772011-09-09 21:34:22 +00003364 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00003365 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003366 return T;
3367 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003368
John McCall9d156a72011-01-06 01:58:22 +00003369 case TYPE_ATTRIBUTED: {
3370 if (Record.size() != 3) {
3371 Error("incorrect encoding of attributed type");
3372 return QualType();
3373 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003374 QualType modifiedType = readType(*Loc.F, Record, Idx);
3375 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall9d156a72011-01-06 01:58:22 +00003376 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
Douglas Gregor35942772011-09-09 21:34:22 +00003377 return Context.getAttributedType(kind, modifiedType, equivalentType);
John McCall9d156a72011-01-06 01:58:22 +00003378 }
3379
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003380 case TYPE_PAREN: {
3381 if (Record.size() != 1) {
3382 Error("incorrect encoding of paren type");
3383 return QualType();
3384 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003385 QualType InnerType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003386 return Context.getParenType(InnerType);
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003387 }
3388
Douglas Gregor7536dd52010-12-20 02:24:11 +00003389 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00003390 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00003391 Error("incorrect encoding of pack expansion type");
3392 return QualType();
3393 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003394 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregor7536dd52010-12-20 02:24:11 +00003395 if (Pattern.isNull())
3396 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00003397 llvm::Optional<unsigned> NumExpansions;
3398 if (Record[1])
3399 NumExpansions = Record[1] - 1;
Douglas Gregor35942772011-09-09 21:34:22 +00003400 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00003401 }
3402
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003403 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003404 unsigned Idx = 0;
3405 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00003406 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003407 QualType NamedType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003408 return Context.getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00003409 }
3410
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003411 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003412 unsigned Idx = 0;
Douglas Gregor409448c2011-07-21 22:35:25 +00003413 ObjCInterfaceDecl *ItfD
3414 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003415 return Context.getObjCInterfaceType(ItfD);
John McCallc12c5bb2010-05-15 11:32:37 +00003416 }
3417
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003418 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00003419 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00003420 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003421 unsigned NumProtos = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003422 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003423 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor409448c2011-07-21 22:35:25 +00003424 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00003425 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003426 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003427
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003428 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003429 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00003430 QualType Pointee = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003431 return Context.getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003432 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00003433
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003434 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00003435 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00003436 QualType Parm = readType(*Loc.F, Record, Idx);
3437 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCall49a832b2009-10-18 09:09:24 +00003438 return
Douglas Gregor35942772011-09-09 21:34:22 +00003439 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
John McCall49a832b2009-10-18 09:09:24 +00003440 Replacement);
3441 }
John McCall3cb0ebd2010-03-10 03:28:59 +00003442
Douglas Gregorc3069d62011-01-14 02:55:32 +00003443 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3444 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00003445 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorc3069d62011-01-14 02:55:32 +00003446 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003447 return Context.getSubstTemplateTypeParmPackType(
Douglas Gregorc3069d62011-01-14 02:55:32 +00003448 cast<TemplateTypeParmType>(Parm),
3449 ArgPack);
3450 }
3451
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003452 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor409448c2011-07-21 22:35:25 +00003453 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003454 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003455 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003456 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003457 return
Douglas Gregor35942772011-09-09 21:34:22 +00003458 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00003459 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003460
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003461 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003462 unsigned Idx = 0;
3463 unsigned Depth = Record[Idx++];
3464 unsigned Index = Record[Idx++];
3465 bool Pack = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00003466 TemplateTypeParmDecl *D
3467 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003468 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003469 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003470
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003471 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003472 unsigned Idx = 0;
3473 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00003474 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00003475 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003476 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003477 if (!Canon.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00003478 Canon = Context.getCanonicalType(Canon);
3479 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003480 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003481
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003482 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003483 unsigned Idx = 0;
3484 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00003485 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00003486 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003487 unsigned NumArgs = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003488 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003489 Args.reserve(NumArgs);
3490 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00003491 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00003492 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003493 Args.size(), Args.data());
3494 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003495
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003496 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003497 unsigned Idx = 0;
3498
3499 // ArrayType
Douglas Gregor393f2492011-07-22 00:38:23 +00003500 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003501 ArrayType::ArraySizeModifier ASM
3502 = (ArrayType::ArraySizeModifier)Record[Idx++];
3503 unsigned IndexTypeQuals = Record[Idx++];
3504
3505 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00003506 Expr *NumElts = ReadExpr(*Loc.F);
3507 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003508
Douglas Gregor35942772011-09-09 21:34:22 +00003509 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003510 IndexTypeQuals, Brackets);
3511 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003512
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003513 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003514 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003515 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003516 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner5f9e2722011-07-23 10:55:15 +00003517 SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00003518 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003519 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003520 QualType T;
Richard Smith3e4c6c42011-05-05 21:57:07 +00003521 if (Underlying.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00003522 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003523 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003524 else
Douglas Gregor35942772011-09-09 21:34:22 +00003525 T = Context.getTemplateSpecializationType(Name, Args.data(),
Richard Smith3e4c6c42011-05-05 21:57:07 +00003526 Args.size(), Underlying);
John McCallf4c73712011-01-19 06:33:43 +00003527 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003528 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003529 }
Eli Friedmanb001de72011-10-06 23:00:33 +00003530
3531 case TYPE_ATOMIC: {
3532 if (Record.size() != 1) {
3533 Error("Incorrect encoding of atomic type");
3534 return QualType();
3535 }
3536 QualType ValueType = readType(*Loc.F, Record, Idx);
3537 return Context.getAtomicType(ValueType);
3538 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003539 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003540 // Suppress a GCC warning
3541 return QualType();
3542}
3543
Sebastian Redlc3632732010-10-05 15:59:54 +00003544class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003545 ASTReader &Reader;
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003546 Module &F;
Sebastian Redl577d4792010-07-22 22:43:28 +00003547 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003548 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00003549 unsigned &Idx;
3550
Sebastian Redlc3632732010-10-05 15:59:54 +00003551 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3552 unsigned &I) {
3553 return Reader.ReadSourceLocation(F, R, I);
3554 }
3555
Douglas Gregor409448c2011-07-21 22:35:25 +00003556 template<typename T>
3557 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
3558 return Reader.ReadDeclAs<T>(F, Record, Idx);
3559 }
3560
John McCalla1ee0c52009-10-16 21:56:05 +00003561public:
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003562 TypeLocReader(ASTReader &Reader, Module &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003563 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00003564 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3565 { }
John McCalla1ee0c52009-10-16 21:56:05 +00003566
John McCall51bd8032009-10-18 01:05:36 +00003567 // We want compile-time assurance that we've enumerated all of
3568 // these, so unfortunately we have to declare them first, then
3569 // define them out-of-line.
3570#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00003571#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00003572 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003573#include "clang/AST/TypeLocNodes.def"
3574
John McCall51bd8032009-10-18 01:05:36 +00003575 void VisitFunctionTypeLoc(FunctionTypeLoc);
3576 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003577};
3578
John McCall51bd8032009-10-18 01:05:36 +00003579void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00003580 // nothing to do
3581}
John McCall51bd8032009-10-18 01:05:36 +00003582void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003583 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00003584 if (TL.needsExtraLocalData()) {
3585 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3586 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3587 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3588 TL.setModeAttr(Record[Idx++]);
3589 }
John McCalla1ee0c52009-10-16 21:56:05 +00003590}
John McCall51bd8032009-10-18 01:05:36 +00003591void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003592 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003593}
John McCall51bd8032009-10-18 01:05:36 +00003594void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003595 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003596}
John McCall51bd8032009-10-18 01:05:36 +00003597void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003598 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003599}
John McCall51bd8032009-10-18 01:05:36 +00003600void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003601 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003602}
John McCall51bd8032009-10-18 01:05:36 +00003603void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003604 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003605}
John McCall51bd8032009-10-18 01:05:36 +00003606void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003607 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00003608 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003609}
John McCall51bd8032009-10-18 01:05:36 +00003610void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003611 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3612 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003613 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00003614 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003615 else
John McCall51bd8032009-10-18 01:05:36 +00003616 TL.setSizeExpr(0);
3617}
3618void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3619 VisitArrayTypeLoc(TL);
3620}
3621void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3622 VisitArrayTypeLoc(TL);
3623}
3624void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3625 VisitArrayTypeLoc(TL);
3626}
3627void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3628 DependentSizedArrayTypeLoc TL) {
3629 VisitArrayTypeLoc(TL);
3630}
3631void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3632 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003633 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003634}
3635void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003636 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003637}
3638void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003639 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003640}
3641void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +00003642 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
3643 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Douglas Gregordab60ad2010-10-01 18:44:50 +00003644 TL.setTrailingReturn(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00003645 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor409448c2011-07-21 22:35:25 +00003646 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003647 }
3648}
3649void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3650 VisitFunctionTypeLoc(TL);
3651}
3652void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3653 VisitFunctionTypeLoc(TL);
3654}
John McCalled976492009-12-04 22:46:56 +00003655void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003656 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00003657}
John McCall51bd8032009-10-18 01:05:36 +00003658void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003659 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003660}
3661void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003662 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3663 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3664 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003665}
3666void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003667 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3668 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3669 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3670 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003671}
3672void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003673 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003674}
Sean Huntca63c202011-05-24 22:41:36 +00003675void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3676 TL.setKWLoc(ReadSourceLocation(Record, Idx));
3677 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3678 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3679 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3680}
Richard Smith34b41d92011-02-20 03:19:35 +00003681void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3682 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3683}
John McCall51bd8032009-10-18 01:05:36 +00003684void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003685 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003686}
3687void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003688 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003689}
John McCall9d156a72011-01-06 01:58:22 +00003690void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3691 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3692 if (TL.hasAttrOperand()) {
3693 SourceRange range;
3694 range.setBegin(ReadSourceLocation(Record, Idx));
3695 range.setEnd(ReadSourceLocation(Record, Idx));
3696 TL.setAttrOperandParensRange(range);
3697 }
3698 if (TL.hasAttrExprOperand()) {
3699 if (Record[Idx++])
3700 TL.setAttrExprOperand(Reader.ReadExpr(F));
3701 else
3702 TL.setAttrExprOperand(0);
3703 } else if (TL.hasAttrEnumOperand())
3704 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3705}
John McCall51bd8032009-10-18 01:05:36 +00003706void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003707 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003708}
John McCall49a832b2009-10-18 09:09:24 +00003709void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3710 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003711 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00003712}
Douglas Gregorc3069d62011-01-14 02:55:32 +00003713void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3714 SubstTemplateTypeParmPackTypeLoc TL) {
3715 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3716}
John McCall51bd8032009-10-18 01:05:36 +00003717void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3718 TemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003719 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3720 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3721 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00003722 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3723 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00003724 Reader.GetTemplateArgumentLocInfo(F,
3725 TL.getTypePtr()->getArg(i).getKind(),
3726 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003727}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003728void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3729 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3730 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3731}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003732void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003733 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor9e876872011-03-01 18:12:44 +00003734 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003735}
John McCall3cb0ebd2010-03-10 03:28:59 +00003736void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003737 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00003738}
Douglas Gregor4714c122010-03-31 17:34:00 +00003739void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003740 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor2494dd02011-03-01 01:34:45 +00003741 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00003742 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003743}
John McCall33500952010-06-11 00:33:02 +00003744void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3745 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003746 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor94fdffa2011-03-01 20:11:18 +00003747 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00003748 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3749 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3750 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003751 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3752 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00003753 Reader.GetTemplateArgumentLocInfo(F,
3754 TL.getTypePtr()->getArg(I).getKind(),
3755 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003756}
Douglas Gregor7536dd52010-12-20 02:24:11 +00003757void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3758 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3759}
John McCall51bd8032009-10-18 01:05:36 +00003760void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003761 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00003762}
3763void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3764 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00003765 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3766 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003767 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00003768 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003769}
John McCall54e14c42009-10-22 22:37:11 +00003770void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003771 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00003772}
Eli Friedmanb001de72011-10-06 23:00:33 +00003773void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
3774 TL.setKWLoc(ReadSourceLocation(Record, Idx));
3775 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3776 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3777}
John McCalla1ee0c52009-10-16 21:56:05 +00003778
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003779TypeSourceInfo *ASTReader::GetTypeSourceInfo(Module &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003780 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00003781 unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00003782 QualType InfoTy = readType(F, Record, Idx);
John McCalla1ee0c52009-10-16 21:56:05 +00003783 if (InfoTy.isNull())
3784 return 0;
3785
Douglas Gregor35942772011-09-09 21:34:22 +00003786 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00003787 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00003788 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00003789 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00003790 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00003791}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003792
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003793QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00003794 unsigned FastQuals = ID & Qualifiers::FastMask;
3795 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003796
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003797 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003798 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003799 switch ((PredefinedTypeIDs)Index) {
3800 case PREDEF_TYPE_NULL_ID: return QualType();
Douglas Gregor35942772011-09-09 21:34:22 +00003801 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
3802 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003803
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003804 case PREDEF_TYPE_CHAR_U_ID:
3805 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00003806 // FIXME: Check that the signedness of CharTy is correct!
Douglas Gregor35942772011-09-09 21:34:22 +00003807 T = Context.CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003808 break;
3809
Douglas Gregor35942772011-09-09 21:34:22 +00003810 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
3811 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
3812 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
3813 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
3814 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
3815 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
3816 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
3817 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
3818 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
3819 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
3820 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
3821 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
3822 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003823 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00003824 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
3825 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
3826 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
3827 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
3828 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
3829 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
3830 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
3831 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
3832 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
3833 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
3834 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
3835 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
3836 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
3837 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003838
3839 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
Douglas Gregor35942772011-09-09 21:34:22 +00003840 T = Context.getAutoRRefDeductType();
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003841 break;
John McCall0ddaeb92011-10-17 18:09:15 +00003842
3843 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
3844 T = Context.ARCUnbridgedCastTy;
3845 break;
3846
Douglas Gregor2cf26342009-04-09 22:27:44 +00003847 }
3848
3849 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00003850 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003851 }
3852
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003853 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003854 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00003855 if (TypesLoaded[Index].isNull()) {
Douglas Gregor393f2492011-07-22 00:38:23 +00003856 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00003857 if (TypesLoaded[Index].isNull())
3858 return QualType();
3859
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003860 TypesLoaded[Index]->setFromAST();
Sebastian Redl30c514c2010-07-14 23:45:08 +00003861 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003862 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00003863 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00003864 }
Mike Stump1eb44332009-09-09 15:08:12 +00003865
John McCall0953e762009-09-24 19:53:00 +00003866 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003867}
3868
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003869QualType ASTReader::getLocalType(Module &F, unsigned LocalID) {
Douglas Gregor393f2492011-07-22 00:38:23 +00003870 return GetType(getGlobalTypeID(F, LocalID));
3871}
3872
3873serialization::TypeID
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003874ASTReader::getGlobalTypeID(Module &F, unsigned LocalID) const {
Douglas Gregora119da02011-08-02 16:26:37 +00003875 unsigned FastQuals = LocalID & Qualifiers::FastMask;
3876 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
3877
3878 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
3879 return LocalID;
3880
3881 ContinuousRangeMap<uint32_t, int, 2>::iterator I
3882 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
3883 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
3884
3885 unsigned GlobalIndex = LocalIndex + I->second;
3886 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
3887}
3888
John McCall833ca992009-10-29 08:12:44 +00003889TemplateArgumentLocInfo
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003890ASTReader::GetTemplateArgumentLocInfo(Module &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00003891 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00003892 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003893 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00003894 switch (Kind) {
3895 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00003896 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00003897 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00003898 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00003899 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003900 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3901 Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00003902 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003903 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregora7fc9012011-01-05 18:58:31 +00003904 SourceLocation());
3905 }
3906 case TemplateArgument::TemplateExpansion: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003907 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3908 Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003909 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003910 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003911 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregorba68eca2011-01-05 17:40:24 +00003912 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00003913 }
John McCall833ca992009-10-29 08:12:44 +00003914 case TemplateArgument::Null:
3915 case TemplateArgument::Integral:
3916 case TemplateArgument::Declaration:
3917 case TemplateArgument::Pack:
3918 return TemplateArgumentLocInfo();
3919 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00003920 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00003921 return TemplateArgumentLocInfo();
3922}
3923
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003924TemplateArgumentLoc
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003925ASTReader::ReadTemplateArgumentLoc(Module &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003926 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003927 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003928
3929 if (Arg.getKind() == TemplateArgument::Expression) {
3930 if (Record[Index++]) // bool InfoHasSameExpr.
3931 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3932 }
Sebastian Redlc3632732010-10-05 15:59:54 +00003933 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003934 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003935}
3936
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003937Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00003938 return GetDecl(ID);
3939}
3940
Douglas Gregore92b8a12011-08-04 00:01:48 +00003941uint64_t ASTReader::readCXXBaseSpecifiers(Module &M, const RecordData &Record,
3942 unsigned &Idx){
3943 if (Idx >= Record.size())
Douglas Gregor7c789c12010-10-29 22:39:52 +00003944 return 0;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003945
Douglas Gregore92b8a12011-08-04 00:01:48 +00003946 unsigned LocalID = Record[Idx++];
3947 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003948}
3949
3950CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregor8f1231b2011-07-22 06:10:01 +00003951 RecordLocation Loc = getLocalBitOffset(Offset);
3952 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003953 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregor8f1231b2011-07-22 06:10:01 +00003954 Cursor.JumpToBit(Loc.Offset);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003955 ReadingKindTracker ReadingKind(Read_Decl, *this);
3956 RecordData Record;
3957 unsigned Code = Cursor.ReadCode();
3958 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3959 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3960 Error("Malformed AST file: missing C++ base specifiers");
3961 return 0;
3962 }
3963
3964 unsigned Idx = 0;
3965 unsigned NumBases = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00003966 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003967 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3968 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregor8f1231b2011-07-22 06:10:01 +00003969 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003970 return Bases;
3971}
3972
Douglas Gregor409448c2011-07-21 22:35:25 +00003973serialization::DeclID
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003974ASTReader::getGlobalDeclID(Module &F, unsigned LocalID) const {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003975 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregor496c7092011-08-03 15:48:04 +00003976 return LocalID;
3977
3978 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003979 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregor496c7092011-08-03 15:48:04 +00003980 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
3981
3982 return LocalID + I->second;
Douglas Gregor409448c2011-07-21 22:35:25 +00003983}
3984
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003985bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
3986 Module &M) const {
3987 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
3988 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
3989 return &M == I->second;
3990}
3991
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003992Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003993 if (ID < NUM_PREDEF_DECL_IDS) {
3994 switch ((PredefinedDeclIDs)ID) {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003995 case PREDEF_DECL_NULL_ID:
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003996 return 0;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003997
3998 case PREDEF_DECL_TRANSLATION_UNIT_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00003999 return Context.getTranslationUnitDecl();
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00004000
4001 case PREDEF_DECL_OBJC_ID_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004002 return Context.getObjCIdDecl();
Douglas Gregor79d67262011-08-12 05:59:41 +00004003
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004004 case PREDEF_DECL_OBJC_SEL_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004005 return Context.getObjCSelDecl();
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004006
Douglas Gregor79d67262011-08-12 05:59:41 +00004007 case PREDEF_DECL_OBJC_CLASS_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004008 return Context.getObjCClassDecl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004009
4010 case PREDEF_DECL_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004011 return Context.getInt128Decl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004012
4013 case PREDEF_DECL_UNSIGNED_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004014 return Context.getUInt128Decl();
Douglas Gregore97179c2011-09-08 01:46:34 +00004015
4016 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004017 return Context.getObjCInstanceTypeDecl();
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004018 }
4019
Douglas Gregor2cf26342009-04-09 22:27:44 +00004020 return 0;
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004021 }
4022
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004023 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4024
4025 if (Index > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004026 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004027 return 0;
4028 }
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004029
4030if (!DeclsLoaded[Index]) {
Douglas Gregor496c7092011-08-03 15:48:04 +00004031 ReadDeclRecord(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00004032 if (DeserializationListener)
4033 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
4034 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004035
4036 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004037}
4038
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004039serialization::DeclID ASTReader::ReadDeclID(Module &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00004040 const RecordData &Record,
4041 unsigned &Idx) {
4042 if (Idx >= Record.size()) {
4043 Error("Corrupted AST file");
4044 return 0;
4045 }
4046
4047 return getGlobalDeclID(F, Record[Idx++]);
4048}
4049
Chris Lattner887e2b32009-04-27 05:46:25 +00004050/// \brief Resolve the offset of a statement into a statement.
4051///
4052/// This operation will read a new statement from the external
4053/// source each time it is called, and is meant to be used via a
4054/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004055Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004056 // Switch case IDs are per Decl.
4057 ClearSwitchCaseIDs();
4058
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004059 // Offset here is a global offset across the entire chain.
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004060 RecordLocation Loc = getLocalBitOffset(Offset);
4061 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
4062 return ReadStmtFromStream(*Loc.F);
Douglas Gregor250fc9c2009-04-18 00:07:54 +00004063}
4064
Douglas Gregor851c75a2011-08-24 21:27:34 +00004065namespace {
4066 class FindExternalLexicalDeclsVisitor {
4067 ASTReader &Reader;
4068 const DeclContext *DC;
4069 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004070
Douglas Gregor851c75a2011-08-24 21:27:34 +00004071 SmallVectorImpl<Decl*> &Decls;
4072 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
4073
4074 public:
4075 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
4076 bool (*isKindWeWant)(Decl::Kind),
4077 SmallVectorImpl<Decl*> &Decls)
4078 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
4079 {
4080 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
4081 PredefsVisited[I] = false;
4082 }
4083
4084 static bool visit(Module &M, bool Preorder, void *UserData) {
4085 if (Preorder)
4086 return false;
4087
4088 FindExternalLexicalDeclsVisitor *This
4089 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
4090
4091 Module::DeclContextInfosMap::iterator Info
4092 = M.DeclContextInfos.find(This->DC);
4093 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
4094 return false;
4095
4096 // Load all of the declaration IDs
4097 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
4098 *IDE = ID + Info->second.NumLexicalDecls;
4099 ID != IDE; ++ID) {
4100 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
4101 continue;
4102
4103 // Don't add predefined declarations to the lexical context more
4104 // than once.
4105 if (ID->second < NUM_PREDEF_DECL_IDS) {
4106 if (This->PredefsVisited[ID->second])
4107 continue;
4108
4109 This->PredefsVisited[ID->second] = true;
4110 }
4111
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004112 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
4113 if (!This->DC->isDeclInLexicalTraversal(D))
4114 This->Decls.push_back(D);
4115 }
Douglas Gregor851c75a2011-08-24 21:27:34 +00004116 }
4117
4118 return false;
4119 }
4120 };
4121}
4122
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004123ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00004124 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner5f9e2722011-07-23 10:55:15 +00004125 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00004126 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor851c75a2011-08-24 21:27:34 +00004127 // least. Walk all of the modules in the order they were loaded.
4128 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
4129 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00004130 ++NumLexicalDeclContextsRead;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004131 return ELR_Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004132}
4133
Douglas Gregor0d95f772011-08-24 19:03:07 +00004134namespace {
4135 /// \brief Module visitor used to perform name lookup into a
4136 /// declaration context.
4137 class DeclContextNameLookupVisitor {
4138 ASTReader &Reader;
4139 const DeclContext *DC;
4140 DeclarationName Name;
4141 SmallVectorImpl<NamedDecl *> &Decls;
4142
4143 public:
4144 DeclContextNameLookupVisitor(ASTReader &Reader,
4145 const DeclContext *DC, DeclarationName Name,
4146 SmallVectorImpl<NamedDecl *> &Decls)
4147 : Reader(Reader), DC(DC), Name(Name), Decls(Decls) { }
4148
4149 static bool visit(Module &M, void *UserData) {
4150 DeclContextNameLookupVisitor *This
4151 = static_cast<DeclContextNameLookupVisitor *>(UserData);
4152
4153 // Check whether we have any visible declaration information for
4154 // this context in this module.
4155 Module::DeclContextInfosMap::iterator Info
4156 = M.DeclContextInfos.find(This->DC);
4157 if (Info == M.DeclContextInfos.end() || !Info->second.NameLookupTableData)
4158 return false;
4159
4160 // Look for this name within this module.
4161 ASTDeclContextNameLookupTable *LookupTable =
4162 (ASTDeclContextNameLookupTable*)Info->second.NameLookupTableData;
4163 ASTDeclContextNameLookupTable::iterator Pos
4164 = LookupTable->find(This->Name);
4165 if (Pos == LookupTable->end())
4166 return false;
4167
4168 bool FoundAnything = false;
4169 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
4170 for (; Data.first != Data.second; ++Data.first) {
4171 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
4172 if (!ND)
4173 continue;
4174
4175 if (ND->getDeclName() != This->Name) {
4176 assert(!This->Name.getCXXNameType().isNull() &&
4177 "Name mismatch without a type");
4178 continue;
4179 }
4180
4181 // Record this declaration.
4182 FoundAnything = true;
4183 This->Decls.push_back(ND);
4184 }
4185
4186 return FoundAnything;
4187 }
4188 };
4189}
4190
John McCall76bd1f32010-06-01 09:23:16 +00004191DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004192ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00004193 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00004194 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00004195 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00004196 if (!Name)
4197 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
4198 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004199
Chris Lattner5f9e2722011-07-23 10:55:15 +00004200 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregor0d95f772011-08-24 19:03:07 +00004201 DeclContextNameLookupVisitor Visitor(*this, DC, Name, Decls);
4202 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00004203 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00004204 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00004205 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004206}
4207
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00004208/// \brief Under non-PCH compilation the consumer receives the objc methods
4209/// before receiving the implementation, and codegen depends on this.
4210/// We simulate this by deserializing and passing to consumer the methods of the
4211/// implementation before passing the deserialized implementation decl.
4212static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
4213 ASTConsumer *Consumer) {
4214 assert(ImplD && Consumer);
4215
4216 for (ObjCImplDecl::method_iterator
4217 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
4218 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
4219
4220 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
4221}
4222
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004223void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004224 assert(Consumer);
4225 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00004226 Decl *D = InterestingDecls.front();
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004227 InterestingDecls.pop_front();
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00004228
4229 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
4230 PassObjCImplDeclToConsumer(ImplD, Consumer);
4231 else
4232 Consumer->HandleInterestingDecl(DeclGroupRef(D));
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004233 }
4234}
4235
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004236void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00004237 this->Consumer = Consumer;
4238
Douglas Gregorfdd01722009-04-14 00:24:19 +00004239 if (!Consumer)
4240 return;
4241
4242 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004243 // Force deserialization of this decl, which will cause it to be queued for
4244 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00004245 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00004246 }
Douglas Gregor1a995dd2011-09-15 18:47:32 +00004247 ExternalDefinitions.clear();
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00004248
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004249 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00004250}
4251
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004252void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004253 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00004254
Mike Stump1eb44332009-09-09 15:08:12 +00004255 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004256 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00004257 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004258 unsigned NumDeclsLoaded
4259 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
4260 (Decl *)0);
4261 unsigned NumIdentifiersLoaded
4262 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
4263 IdentifiersLoaded.end(),
4264 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00004265 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004266 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
4267 SelectorsLoaded.end(),
4268 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00004269
Douglas Gregor4fed3f42009-04-27 18:38:38 +00004270 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
4271 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor0cdd7982011-07-21 18:46:38 +00004272 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00004273 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
4274 NumSLocEntriesRead, TotalNumSLocEntries,
4275 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004276 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004277 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004278 NumTypesLoaded, (unsigned)TypesLoaded.size(),
4279 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
4280 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004281 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004282 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
4283 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004284 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004285 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004286 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
4287 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00004288 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004289 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00004290 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
4291 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00004292 if (TotalNumStatements)
4293 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
4294 NumStatementsRead, TotalNumStatements,
4295 ((float)NumStatementsRead/TotalNumStatements * 100));
4296 if (TotalNumMacros)
4297 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
4298 NumMacrosRead, TotalNumMacros,
4299 ((float)NumMacrosRead/TotalNumMacros * 100));
4300 if (TotalLexicalDeclContexts)
4301 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
4302 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4303 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4304 * 100));
4305 if (TotalVisibleDeclContexts)
4306 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
4307 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4308 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4309 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004310 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004311 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004312 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4313 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00004314 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004315 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00004316 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004317 std::fprintf(stderr, "\n");
Douglas Gregor23d7df52011-07-21 19:50:14 +00004318 dump();
4319 std::fprintf(stderr, "\n");
4320}
4321
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004322template<typename Key, typename Module, unsigned InitialCapacity>
Douglas Gregor23d7df52011-07-21 19:50:14 +00004323static void
Chris Lattner5f9e2722011-07-23 10:55:15 +00004324dumpModuleIDMap(StringRef Name,
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004325 const ContinuousRangeMap<Key, Module *,
Douglas Gregor23d7df52011-07-21 19:50:14 +00004326 InitialCapacity> &Map) {
4327 if (Map.begin() == Map.end())
4328 return;
4329
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004330 typedef ContinuousRangeMap<Key, Module *, InitialCapacity> MapType;
Douglas Gregor23d7df52011-07-21 19:50:14 +00004331 llvm::errs() << Name << ":\n";
4332 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
4333 I != IEnd; ++I) {
4334 llvm::errs() << " " << I->first << " -> " << I->second->FileName
4335 << "\n";
4336 }
4337}
4338
Douglas Gregor23d7df52011-07-21 19:50:14 +00004339void ASTReader::dump() {
Douglas Gregor8df5c9b2011-08-02 11:12:41 +00004340 llvm::errs() << "*** PCH/Module Remappings:\n";
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004341 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor23d7df52011-07-21 19:50:14 +00004342 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor1e849b62011-07-29 00:21:44 +00004343 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00004344 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00004345 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
4346 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00004347 dumpModuleIDMap("Global preprocessed entity map",
4348 GlobalPreprocessedEntityMap);
Douglas Gregor8df5c9b2011-08-02 11:12:41 +00004349
4350 llvm::errs() << "\n*** PCH/Modules Loaded:";
4351 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
4352 MEnd = ModuleMgr.end();
4353 M != MEnd; ++M)
4354 (*M)->dump();
Douglas Gregor2cf26342009-04-09 22:27:44 +00004355}
4356
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00004357/// Return the amount of memory used by memory buffers, breaking down
4358/// by heap-backed versus mmap'ed memory.
4359void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004360 for (ModuleConstIterator I = ModuleMgr.begin(),
4361 E = ModuleMgr.end(); I != E; ++I) {
4362 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00004363 size_t bytes = buf->getBufferSize();
4364 switch (buf->getBufferKind()) {
4365 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
4366 sizes.malloc_bytes += bytes;
4367 break;
4368 case llvm::MemoryBuffer::MemoryBuffer_MMap:
4369 sizes.mmap_bytes += bytes;
4370 break;
4371 }
4372 }
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004373 }
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00004374}
4375
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004376void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00004377 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004378 S.ExternalSource = this;
4379
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004380 // Makes sure any declarations that were deserialized "too early"
4381 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00004382 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4383 if (SemaObj->TUScope)
John McCalld226f652010-08-21 09:40:31 +00004384 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor76dc8892010-09-24 23:29:12 +00004385
4386 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregor668c1a42009-04-21 22:25:48 +00004387 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004388 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004389
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004390 // Load the offsets of the declarations that Sema references.
4391 // They will be lazily deserialized when needed.
4392 if (!SemaDeclRefs.empty()) {
4393 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregor1e5b6f62011-07-28 00:57:24 +00004394 if (!SemaObj->StdNamespace)
4395 SemaObj->StdNamespace = SemaDeclRefs[0];
4396 if (!SemaObj->StdBadAlloc)
4397 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004398 }
4399
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004400 if (!FPPragmaOptions.empty()) {
4401 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4402 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4403 }
4404
4405 if (!OpenCLExtensions.empty()) {
4406 unsigned I = 0;
4407#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4408#include "clang/Basic/OpenCLExtensions.def"
4409
4410 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4411 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00004412}
4413
Douglas Gregor211f6e82011-08-20 04:39:52 +00004414IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregor211f6e82011-08-20 04:39:52 +00004415 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart));
4416 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
4417 return Visitor.getIdentifierInfo();
Douglas Gregor668c1a42009-04-21 22:25:48 +00004418}
4419
Douglas Gregor95f42922010-10-14 22:11:03 +00004420namespace clang {
4421 /// \brief An identifier-lookup iterator that enumerates all of the
4422 /// identifiers stored within a set of AST files.
4423 class ASTIdentifierIterator : public IdentifierIterator {
4424 /// \brief The AST reader whose identifiers are being enumerated.
4425 const ASTReader &Reader;
4426
4427 /// \brief The current index into the chain of AST files stored in
4428 /// the AST reader.
4429 unsigned Index;
4430
4431 /// \brief The current position within the identifier lookup table
4432 /// of the current AST file.
4433 ASTIdentifierLookupTable::key_iterator Current;
4434
4435 /// \brief The end position within the identifier lookup table of
4436 /// the current AST file.
4437 ASTIdentifierLookupTable::key_iterator End;
4438
4439 public:
4440 explicit ASTIdentifierIterator(const ASTReader &Reader);
4441
Chris Lattner5f9e2722011-07-23 10:55:15 +00004442 virtual StringRef Next();
Douglas Gregor95f42922010-10-14 22:11:03 +00004443 };
4444}
4445
4446ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004447 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor95f42922010-10-14 22:11:03 +00004448 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004449 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00004450 Current = IdTable->key_begin();
4451 End = IdTable->key_end();
4452}
4453
Chris Lattner5f9e2722011-07-23 10:55:15 +00004454StringRef ASTIdentifierIterator::Next() {
Douglas Gregor95f42922010-10-14 22:11:03 +00004455 while (Current == End) {
4456 // If we have exhausted all of our AST files, we're done.
4457 if (Index == 0)
Chris Lattner5f9e2722011-07-23 10:55:15 +00004458 return StringRef();
Douglas Gregor95f42922010-10-14 22:11:03 +00004459
4460 --Index;
4461 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004462 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
4463 IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00004464 Current = IdTable->key_begin();
4465 End = IdTable->key_end();
4466 }
4467
4468 // We have any identifiers remaining in the current AST file; return
4469 // the next one.
4470 std::pair<const char*, unsigned> Key = *Current;
4471 ++Current;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004472 return StringRef(Key.first, Key.second);
Douglas Gregor95f42922010-10-14 22:11:03 +00004473}
4474
4475IdentifierIterator *ASTReader::getIdentifiers() const {
4476 return new ASTIdentifierIterator(*this);
4477}
4478
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004479namespace clang { namespace serialization {
4480 class ReadMethodPoolVisitor {
4481 ASTReader &Reader;
4482 Selector Sel;
4483 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
4484 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004485
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004486 /// \brief Build an ObjCMethodList from a vector of Objective-C method
4487 /// declarations.
4488 ObjCMethodList
4489 buildObjCMethodList(const SmallVectorImpl<ObjCMethodDecl *> &Vec) const
4490 {
4491 ObjCMethodList List;
4492 ObjCMethodList *Prev = 0;
4493 for (unsigned I = 0, N = Vec.size(); I != N; ++I) {
4494 if (!List.Method) {
4495 // This is the first method, which is the easy case.
4496 List.Method = Vec[I];
4497 Prev = &List;
4498 continue;
4499 }
4500
4501 ObjCMethodList *Mem =
4502 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
4503 Prev->Next = new (Mem) ObjCMethodList(Vec[I], 0);
4504 Prev = Prev->Next;
4505 }
4506
4507 return List;
4508 }
4509
4510 public:
4511 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel)
4512 : Reader(Reader), Sel(Sel) { }
4513
4514 static bool visit(Module &M, void *UserData) {
4515 ReadMethodPoolVisitor *This
4516 = static_cast<ReadMethodPoolVisitor *>(UserData);
4517
4518 if (!M.SelectorLookupTable)
4519 return false;
4520
4521 ASTSelectorLookupTable *PoolTable
4522 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
4523 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
4524 if (Pos == PoolTable->end())
4525 return false;
4526
4527 ++This->Reader.NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004528 // FIXME: Not quite happy with the statistics here. We probably should
4529 // disable this tracking when called via LoadSelector.
4530 // Also, should entries without methods count as misses?
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004531 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004532 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004533 if (This->Reader.DeserializationListener)
4534 This->Reader.DeserializationListener->SelectorRead(Data.ID,
4535 This->Sel);
4536
4537 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
4538 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
4539 return true;
Sebastian Redl725cd962010-08-04 20:40:17 +00004540 }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004541
4542 /// \brief Retrieve the instance methods found by this visitor.
4543 ObjCMethodList getInstanceMethods() const {
4544 return buildObjCMethodList(InstanceMethods);
4545 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004546
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004547 /// \brief Retrieve the instance methods found by this visitor.
4548 ObjCMethodList getFactoryMethods() const {
4549 return buildObjCMethodList(FactoryMethods);
4550 }
4551 };
4552} } // end namespace clang::serialization
4553
4554std::pair<ObjCMethodList, ObjCMethodList>
4555ASTReader::ReadMethodPool(Selector Sel) {
4556 ReadMethodPoolVisitor Visitor(*this, Sel);
4557 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
4558 std::pair<ObjCMethodList, ObjCMethodList> Result;
4559 Result.first = Visitor.getInstanceMethods();
4560 Result.second = Visitor.getFactoryMethods();
4561
4562 if (!Result.first.Method && !Result.second.Method)
4563 ++NumMethodPoolMisses;
4564 return Result;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004565}
4566
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004567void ASTReader::ReadKnownNamespaces(
Chris Lattner5f9e2722011-07-23 10:55:15 +00004568 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004569 Namespaces.clear();
4570
4571 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
4572 if (NamespaceDecl *Namespace
4573 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
4574 Namespaces.push_back(Namespace);
4575 }
4576}
4577
Douglas Gregora8623202011-07-27 20:58:46 +00004578void ASTReader::ReadTentativeDefinitions(
4579 SmallVectorImpl<VarDecl *> &TentativeDefs) {
4580 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4581 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
4582 if (Var)
4583 TentativeDefs.push_back(Var);
4584 }
4585 TentativeDefinitions.clear();
4586}
4587
Douglas Gregora2ee20a2011-07-27 21:45:57 +00004588void ASTReader::ReadUnusedFileScopedDecls(
4589 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
4590 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4591 DeclaratorDecl *D
4592 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4593 if (D)
4594 Decls.push_back(D);
4595 }
4596 UnusedFileScopedDecls.clear();
4597}
4598
Douglas Gregor0129b562011-07-27 21:57:17 +00004599void ASTReader::ReadDelegatingConstructors(
4600 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
4601 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
4602 CXXConstructorDecl *D
4603 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
4604 if (D)
4605 Decls.push_back(D);
4606 }
4607 DelegatingCtorDecls.clear();
4608}
4609
Douglas Gregord58a0a52011-07-28 00:39:29 +00004610void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
4611 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
4612 TypedefNameDecl *D
4613 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
4614 if (D)
4615 Decls.push_back(D);
4616 }
4617 ExtVectorDecls.clear();
4618}
4619
Douglas Gregora126f172011-07-28 00:53:40 +00004620void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
4621 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
4622 CXXRecordDecl *D
4623 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
4624 if (D)
4625 Decls.push_back(D);
4626 }
4627 DynamicClasses.clear();
4628}
4629
Douglas Gregorec12ce22011-07-28 14:20:37 +00004630void
4631ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
4632 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4633 NamedDecl *D
4634 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4635 if (D)
4636 Decls.push_back(D);
4637 }
4638 LocallyScopedExternalDecls.clear();
4639}
4640
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00004641void ASTReader::ReadReferencedSelectors(
4642 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
4643 if (ReferencedSelectorsData.empty())
4644 return;
4645
4646 // If there are @selector references added them to its pool. This is for
4647 // implementation of -Wselector.
4648 unsigned int DataSize = ReferencedSelectorsData.size()-1;
4649 unsigned I = 0;
4650 while (I < DataSize) {
4651 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
4652 SourceLocation SelLoc
4653 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
4654 Sels.push_back(std::make_pair(Sel, SelLoc));
4655 }
4656 ReferencedSelectorsData.clear();
4657}
4658
Douglas Gregor31e37b22011-07-28 18:09:57 +00004659void ASTReader::ReadWeakUndeclaredIdentifiers(
4660 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
4661 if (WeakUndeclaredIdentifiers.empty())
4662 return;
4663
4664 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
4665 IdentifierInfo *WeakId
4666 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4667 IdentifierInfo *AliasId
4668 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4669 SourceLocation Loc
4670 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
4671 bool Used = WeakUndeclaredIdentifiers[I++];
4672 WeakInfo WI(AliasId, Loc);
4673 WI.setUsed(Used);
4674 WeakIDs.push_back(std::make_pair(WeakId, WI));
4675 }
4676 WeakUndeclaredIdentifiers.clear();
4677}
4678
Douglas Gregordfe65432011-07-28 19:11:31 +00004679void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
4680 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
4681 ExternalVTableUse VT;
4682 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4683 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
4684 VT.DefinitionRequired = VTableUses[Idx++];
4685 VTables.push_back(VT);
4686 }
4687
4688 VTableUses.clear();
4689}
4690
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00004691void ASTReader::ReadPendingInstantiations(
4692 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
4693 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
4694 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
4695 SourceLocation Loc
4696 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
4697 Pending.push_back(std::make_pair(D, Loc));
4698 }
4699 PendingInstantiations.clear();
4700}
4701
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004702void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004703 // It would be complicated to avoid reading the methods anyway. So don't.
4704 ReadMethodPool(Sel);
4705}
4706
Douglas Gregor95eab172011-07-28 20:55:49 +00004707void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00004708 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00004709 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004710 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004711 if (DeserializationListener)
4712 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00004713}
4714
Douglas Gregord89275b2009-07-06 18:54:52 +00004715/// \brief Set the globally-visible declarations associated with the given
4716/// identifier.
4717///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004718/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00004719/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00004720/// them.
4721///
4722/// \param II an IdentifierInfo that refers to one or more globally-visible
4723/// declarations.
4724///
4725/// \param DeclIDs the set of declaration IDs with the name @p II that are
4726/// visible at global scope.
4727///
4728/// \param Nonrecursive should be true to indicate that the caller knows that
4729/// this call is non-recursive, and therefore the globally-visible declarations
4730/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00004731void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004732ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004733 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregord89275b2009-07-06 18:54:52 +00004734 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004735 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004736 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4737 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4738 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004739 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00004740 return;
4741 }
Mike Stump1eb44332009-09-09 15:08:12 +00004742
Douglas Gregord89275b2009-07-06 18:54:52 +00004743 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4744 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4745 if (SemaObj) {
Douglas Gregor914ed9d2010-08-13 03:15:25 +00004746 if (SemaObj->TUScope) {
4747 // Introduce this declaration into the translation-unit scope
4748 // and add it to the declaration chain for this identifier, so
4749 // that (unqualified) name lookup will find it.
John McCalld226f652010-08-21 09:40:31 +00004750 SemaObj->TUScope->AddDecl(D);
Douglas Gregor914ed9d2010-08-13 03:15:25 +00004751 }
Douglas Gregor76dc8892010-09-24 23:29:12 +00004752 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregord89275b2009-07-06 18:54:52 +00004753 } else {
4754 // Queue this declaration so that it will be added to the
4755 // translation unit scope and identifier's declaration chain
4756 // once a Sema object is known.
4757 PreloadedDecls.push_back(D);
4758 }
4759 }
4760}
4761
Douglas Gregor95eab172011-07-28 20:55:49 +00004762IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00004763 if (ID == 0)
4764 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00004765
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004766 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004767 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00004768 return 0;
4769 }
Mike Stump1eb44332009-09-09 15:08:12 +00004770
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004771 ID -= 1;
4772 if (!IdentifiersLoaded[ID]) {
Douglas Gregor67268d02011-07-20 00:59:32 +00004773 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
4774 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregor9827a802011-07-29 00:56:45 +00004775 Module *M = I->second;
4776 unsigned Index = ID - M->BaseIdentifierID;
4777 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregord6595a42009-04-25 21:04:17 +00004778
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004779 // All of the strings in the AST file are preceded by a 16-bit length.
4780 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00004781 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4782 // unsigned integers. This is important to avoid integer overflow when
4783 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00004784 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00004785 unsigned StrLen = (((unsigned) StrLenPtr[0])
4786 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004787 IdentifiersLoaded[ID]
Douglas Gregor712f2fc2011-09-09 22:02:16 +00004788 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004789 if (DeserializationListener)
4790 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00004791 }
Mike Stump1eb44332009-09-09 15:08:12 +00004792
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004793 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004794}
4795
Douglas Gregor95eab172011-07-28 20:55:49 +00004796IdentifierInfo *ASTReader::getLocalIdentifier(Module &M, unsigned LocalID) {
4797 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
4798}
4799
4800IdentifierID ASTReader::getGlobalIdentifierID(Module &M, unsigned LocalID) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +00004801 if (LocalID < NUM_PREDEF_IDENT_IDS)
4802 return LocalID;
4803
4804 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4805 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
4806 assert(I != M.IdentifierRemap.end()
4807 && "Invalid index into identifier index remap");
4808
4809 return LocalID + I->second;
Douglas Gregor95eab172011-07-28 20:55:49 +00004810}
4811
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004812bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregore23ac652011-04-20 00:21:03 +00004813 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00004814}
4815
Douglas Gregor2d2689a2011-07-28 21:16:51 +00004816Selector ASTReader::getLocalSelector(Module &M, unsigned LocalID) {
4817 return DecodeSelector(getGlobalSelectorID(M, LocalID));
4818}
4819
4820Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004821 if (ID == 0)
4822 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00004823
Sebastian Redl725cd962010-08-04 20:40:17 +00004824 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004825 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004826 return Selector();
4827 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004828
Sebastian Redl725cd962010-08-04 20:40:17 +00004829 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004830 // Load this selector from the selector table.
Douglas Gregor96958cb2011-07-20 01:10:58 +00004831 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
4832 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregor9827a802011-07-29 00:56:45 +00004833 Module &M = *I->second;
4834 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00004835 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor96958cb2011-07-20 01:10:58 +00004836 SelectorsLoaded[ID - 1] =
Douglas Gregor9827a802011-07-29 00:56:45 +00004837 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor96958cb2011-07-20 01:10:58 +00004838 if (DeserializationListener)
4839 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor83941df2009-04-25 17:48:32 +00004840 }
4841
Sebastian Redl725cd962010-08-04 20:40:17 +00004842 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004843}
4844
Douglas Gregor8451ec72011-07-28 14:41:43 +00004845Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00004846 return DecodeSelector(ID);
4847}
4848
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004849uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00004850 // ID 0 (the null selector) is considered an external selector.
4851 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00004852}
4853
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00004854serialization::SelectorID
4855ASTReader::getGlobalSelectorID(Module &M, unsigned LocalID) const {
4856 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
4857 return LocalID;
4858
4859 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4860 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
4861 assert(I != M.SelectorRemap.end()
4862 && "Invalid index into identifier index remap");
4863
4864 return LocalID + I->second;
Douglas Gregor8451ec72011-07-28 14:41:43 +00004865}
4866
Mike Stump1eb44332009-09-09 15:08:12 +00004867DeclarationName
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004868ASTReader::ReadDeclarationName(Module &F,
Douglas Gregor393f2492011-07-22 00:38:23 +00004869 const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004870 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4871 switch (Kind) {
4872 case DeclarationName::Identifier:
Douglas Gregor95eab172011-07-28 20:55:49 +00004873 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004874
4875 case DeclarationName::ObjCZeroArgSelector:
4876 case DeclarationName::ObjCOneArgSelector:
4877 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor2d2689a2011-07-28 21:16:51 +00004878 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004879
4880 case DeclarationName::CXXConstructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00004881 return Context.DeclarationNames.getCXXConstructorName(
4882 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004883
4884 case DeclarationName::CXXDestructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00004885 return Context.DeclarationNames.getCXXDestructorName(
4886 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004887
4888 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor35942772011-09-09 21:34:22 +00004889 return Context.DeclarationNames.getCXXConversionFunctionName(
4890 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004891
4892 case DeclarationName::CXXOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00004893 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00004894 (OverloadedOperatorKind)Record[Idx++]);
4895
Sean Hunt3e518bd2009-11-29 07:34:05 +00004896 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00004897 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregor95eab172011-07-28 20:55:49 +00004898 GetIdentifierInfo(F, Record, Idx));
Sean Hunt3e518bd2009-11-29 07:34:05 +00004899
Douglas Gregor2cf26342009-04-09 22:27:44 +00004900 case DeclarationName::CXXUsingDirective:
4901 return DeclarationName::getUsingDirectiveName();
4902 }
4903
4904 // Required to silence GCC warning
4905 return DeclarationName();
4906}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004907
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004908void ASTReader::ReadDeclarationNameLoc(Module &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004909 DeclarationNameLoc &DNLoc,
4910 DeclarationName Name,
4911 const RecordData &Record, unsigned &Idx) {
4912 switch (Name.getNameKind()) {
4913 case DeclarationName::CXXConstructorName:
4914 case DeclarationName::CXXDestructorName:
4915 case DeclarationName::CXXConversionFunctionName:
4916 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4917 break;
4918
4919 case DeclarationName::CXXOperatorName:
4920 DNLoc.CXXOperatorName.BeginOpNameLoc
4921 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4922 DNLoc.CXXOperatorName.EndOpNameLoc
4923 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4924 break;
4925
4926 case DeclarationName::CXXLiteralOperatorName:
4927 DNLoc.CXXLiteralOperatorName.OpNameLoc
4928 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4929 break;
4930
4931 case DeclarationName::Identifier:
4932 case DeclarationName::ObjCZeroArgSelector:
4933 case DeclarationName::ObjCOneArgSelector:
4934 case DeclarationName::ObjCMultiArgSelector:
4935 case DeclarationName::CXXUsingDirective:
4936 break;
4937 }
4938}
4939
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004940void ASTReader::ReadDeclarationNameInfo(Module &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004941 DeclarationNameInfo &NameInfo,
4942 const RecordData &Record, unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004943 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004944 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4945 DeclarationNameLoc DNLoc;
4946 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4947 NameInfo.setInfo(DNLoc);
4948}
4949
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004950void ASTReader::ReadQualifierInfo(Module &F, QualifierInfo &Info,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004951 const RecordData &Record, unsigned &Idx) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004952 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004953 unsigned NumTPLists = Record[Idx++];
4954 Info.NumTemplParamLists = NumTPLists;
4955 if (NumTPLists) {
Douglas Gregor35942772011-09-09 21:34:22 +00004956 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004957 for (unsigned i=0; i != NumTPLists; ++i)
4958 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4959 }
4960}
4961
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004962TemplateName
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004963ASTReader::ReadTemplateName(Module &F, const RecordData &Record,
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004964 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004965 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004966 switch (Kind) {
4967 case TemplateName::Template:
Douglas Gregor409448c2011-07-21 22:35:25 +00004968 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004969
4970 case TemplateName::OverloadedTemplate: {
4971 unsigned size = Record[Idx++];
4972 UnresolvedSet<8> Decls;
4973 while (size--)
Douglas Gregor409448c2011-07-21 22:35:25 +00004974 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004975
Douglas Gregor35942772011-09-09 21:34:22 +00004976 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004977 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004978
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004979 case TemplateName::QualifiedTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00004980 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004981 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004982 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004983 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004984 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004985
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004986 case TemplateName::DependentTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00004987 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004988 if (Record[Idx++]) // isIdentifier
Douglas Gregor35942772011-09-09 21:34:22 +00004989 return Context.getDependentTemplateName(NNS,
Douglas Gregor95eab172011-07-28 20:55:49 +00004990 GetIdentifierInfo(F, Record,
4991 Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004992 return Context.getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004993 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004994 }
John McCall14606042011-06-30 08:33:18 +00004995
4996 case TemplateName::SubstTemplateTemplateParm: {
4997 TemplateTemplateParmDecl *param
Douglas Gregor409448c2011-07-21 22:35:25 +00004998 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCall14606042011-06-30 08:33:18 +00004999 if (!param) return TemplateName();
5000 TemplateName replacement = ReadTemplateName(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005001 return Context.getSubstTemplateTemplateParm(param, replacement);
John McCall14606042011-06-30 08:33:18 +00005002 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005003
5004 case TemplateName::SubstTemplateTemplateParmPack: {
5005 TemplateTemplateParmDecl *Param
Douglas Gregor409448c2011-07-21 22:35:25 +00005006 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005007 if (!Param)
5008 return TemplateName();
5009
5010 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
5011 if (ArgPack.getKind() != TemplateArgument::Pack)
5012 return TemplateName();
5013
Douglas Gregor35942772011-09-09 21:34:22 +00005014 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005015 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005016 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005017
David Blaikieb219cfc2011-09-23 05:06:16 +00005018 llvm_unreachable("Unhandled template name kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005019}
5020
5021TemplateArgument
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005022ASTReader::ReadTemplateArgument(Module &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00005023 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00005024 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
5025 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005026 case TemplateArgument::Null:
5027 return TemplateArgument();
5028 case TemplateArgument::Type:
Douglas Gregor393f2492011-07-22 00:38:23 +00005029 return TemplateArgument(readType(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005030 case TemplateArgument::Declaration:
Douglas Gregor409448c2011-07-21 22:35:25 +00005031 return TemplateArgument(ReadDecl(F, Record, Idx));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00005032 case TemplateArgument::Integral: {
5033 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00005034 QualType T = readType(F, Record, Idx);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00005035 return TemplateArgument(Value, T);
5036 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00005037 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005038 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00005039 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005040 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00005041 llvm::Optional<unsigned> NumTemplateExpansions;
5042 if (unsigned NumExpansions = Record[Idx++])
5043 NumTemplateExpansions = NumExpansions - 1;
5044 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00005045 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005046 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00005047 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005048 case TemplateArgument::Pack: {
5049 unsigned NumArgs = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00005050 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
Douglas Gregor910f8002010-11-07 23:05:16 +00005051 for (unsigned I = 0; I != NumArgs; ++I)
5052 Args[I] = ReadTemplateArgument(F, Record, Idx);
5053 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005054 }
5055 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005056
David Blaikieb219cfc2011-09-23 05:06:16 +00005057 llvm_unreachable("Unhandled template argument kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005058}
5059
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005060TemplateParameterList *
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005061ASTReader::ReadTemplateParameterList(Module &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00005062 const RecordData &Record, unsigned &Idx) {
5063 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
5064 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
5065 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005066
5067 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00005068 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005069 Params.reserve(NumParams);
5070 while (NumParams--)
Douglas Gregor409448c2011-07-21 22:35:25 +00005071 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer20249a12010-10-21 03:16:25 +00005072
5073 TemplateParameterList* TemplateParams =
Douglas Gregor35942772011-09-09 21:34:22 +00005074 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005075 Params.data(), Params.size(), RAngleLoc);
5076 return TemplateParams;
5077}
5078
5079void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005080ASTReader::
Chris Lattner5f9e2722011-07-23 10:55:15 +00005081ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005082 Module &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00005083 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005084 unsigned NumTemplateArgs = Record[Idx++];
5085 TemplArgs.reserve(NumTemplateArgs);
5086 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00005087 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005088}
5089
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005090/// \brief Read a UnresolvedSet structure.
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005091void ASTReader::ReadUnresolvedSet(Module &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005092 const RecordData &Record, unsigned &Idx) {
5093 unsigned NumDecls = Record[Idx++];
5094 while (NumDecls--) {
Douglas Gregor409448c2011-07-21 22:35:25 +00005095 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005096 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
5097 Set.addDecl(D, AS);
5098 }
5099}
5100
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005101CXXBaseSpecifier
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005102ASTReader::ReadCXXBaseSpecifier(Module &F,
Nick Lewycky56062202010-07-26 16:56:01 +00005103 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005104 bool isVirtual = static_cast<bool>(Record[Idx++]);
5105 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
5106 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00005107 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00005108 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
5109 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00005110 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00005111 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00005112 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00005113 Result.setInheritConstructors(inheritConstructors);
5114 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005115}
5116
Sean Huntcbb67482011-01-08 20:30:50 +00005117std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005118ASTReader::ReadCXXCtorInitializers(Module &F, const RecordData &Record,
Sean Huntcbb67482011-01-08 20:30:50 +00005119 unsigned &Idx) {
5120 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005121 unsigned NumInitializers = Record[Idx++];
5122 if (NumInitializers) {
Sean Huntcbb67482011-01-08 20:30:50 +00005123 CtorInitializers
Douglas Gregor35942772011-09-09 21:34:22 +00005124 = new (Context) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005125 for (unsigned i=0; i != NumInitializers; ++i) {
5126 TypeSourceInfo *BaseClassInfo = 0;
5127 bool IsBaseVirtual = false;
5128 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00005129 IndirectFieldDecl *IndirectMember = 0;
Sean Hunt156b6402011-05-04 01:19:08 +00005130 CXXConstructorDecl *Target = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00005131
Sean Hunt156b6402011-05-04 01:19:08 +00005132 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
5133 switch (Type) {
5134 case CTOR_INITIALIZER_BASE:
Sebastian Redlc3632732010-10-05 15:59:54 +00005135 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005136 IsBaseVirtual = Record[Idx++];
Sean Hunt156b6402011-05-04 01:19:08 +00005137 break;
5138
5139 case CTOR_INITIALIZER_DELEGATING:
Douglas Gregor409448c2011-07-21 22:35:25 +00005140 Target = ReadDeclAs<CXXConstructorDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00005141 break;
5142
5143 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00005144 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00005145 break;
5146
5147 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00005148 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00005149 break;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005150 }
Sean Hunt156b6402011-05-04 01:19:08 +00005151
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00005152 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00005153 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00005154 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
5155 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005156 bool IsWritten = Record[Idx++];
5157 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner5f9e2722011-07-23 10:55:15 +00005158 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005159 if (IsWritten) {
5160 SourceOrderOrNumArrayIndices = Record[Idx++];
5161 } else {
5162 SourceOrderOrNumArrayIndices = Record[Idx++];
5163 Indices.reserve(SourceOrderOrNumArrayIndices);
5164 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor409448c2011-07-21 22:35:25 +00005165 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005166 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005167
Sean Huntcbb67482011-01-08 20:30:50 +00005168 CXXCtorInitializer *BOMInit;
Sean Hunt156b6402011-05-04 01:19:08 +00005169 if (Type == CTOR_INITIALIZER_BASE) {
Douglas Gregor35942772011-09-09 21:34:22 +00005170 BOMInit = new (Context) CXXCtorInitializer(Context, BaseClassInfo, IsBaseVirtual,
Sean Huntcbb67482011-01-08 20:30:50 +00005171 LParenLoc, Init, RParenLoc,
5172 MemberOrEllipsisLoc);
Sean Hunt156b6402011-05-04 01:19:08 +00005173 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
Douglas Gregor35942772011-09-09 21:34:22 +00005174 BOMInit = new (Context) CXXCtorInitializer(Context, MemberOrEllipsisLoc, LParenLoc,
Sean Hunt156b6402011-05-04 01:19:08 +00005175 Target, Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005176 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00005177 if (Member)
Douglas Gregor35942772011-09-09 21:34:22 +00005178 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00005179 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00005180 else
Douglas Gregor35942772011-09-09 21:34:22 +00005181 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
Sean Huntcbb67482011-01-08 20:30:50 +00005182 MemberOrEllipsisLoc, LParenLoc,
5183 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005184 } else {
Douglas Gregor35942772011-09-09 21:34:22 +00005185 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00005186 LParenLoc, Init, RParenLoc,
5187 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005188 }
5189
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00005190 if (IsWritten)
5191 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00005192 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005193 }
5194 }
5195
Sean Huntcbb67482011-01-08 20:30:50 +00005196 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005197}
5198
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005199NestedNameSpecifier *
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005200ASTReader::ReadNestedNameSpecifier(Module &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00005201 const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005202 unsigned N = Record[Idx++];
5203 NestedNameSpecifier *NNS = 0, *Prev = 0;
5204 for (unsigned I = 0; I != N; ++I) {
5205 NestedNameSpecifier::SpecifierKind Kind
5206 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5207 switch (Kind) {
5208 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00005209 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005210 NNS = NestedNameSpecifier::Create(Context, Prev, II);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005211 break;
5212 }
5213
5214 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005215 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005216 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005217 break;
5218 }
5219
Douglas Gregor14aba762011-02-24 02:36:08 +00005220 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005221 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005222 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
Douglas Gregor14aba762011-02-24 02:36:08 +00005223 break;
5224 }
5225
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005226 case NestedNameSpecifier::TypeSpec:
5227 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor393f2492011-07-22 00:38:23 +00005228 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00005229 if (!T)
5230 return 0;
5231
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005232 bool Template = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00005233 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005234 break;
5235 }
5236
5237 case NestedNameSpecifier::Global: {
Douglas Gregor35942772011-09-09 21:34:22 +00005238 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005239 // No associated value, and there can't be a prefix.
5240 break;
5241 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005242 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00005243 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005244 }
5245 return NNS;
5246}
5247
Douglas Gregordc355712011-02-25 00:36:19 +00005248NestedNameSpecifierLoc
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005249ASTReader::ReadNestedNameSpecifierLoc(Module &F, const RecordData &Record,
Douglas Gregordc355712011-02-25 00:36:19 +00005250 unsigned &Idx) {
5251 unsigned N = Record[Idx++];
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005252 NestedNameSpecifierLocBuilder Builder;
Douglas Gregordc355712011-02-25 00:36:19 +00005253 for (unsigned I = 0; I != N; ++I) {
5254 NestedNameSpecifier::SpecifierKind Kind
5255 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5256 switch (Kind) {
5257 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00005258 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00005259 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005260 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00005261 break;
5262 }
5263
5264 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005265 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00005266 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005267 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00005268 break;
5269 }
5270
5271 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005272 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00005273 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005274 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00005275 break;
5276 }
5277
5278 case NestedNameSpecifier::TypeSpec:
5279 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregordc355712011-02-25 00:36:19 +00005280 bool Template = Record[Idx++];
5281 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
5282 if (!T)
5283 return NestedNameSpecifierLoc();
Douglas Gregordc355712011-02-25 00:36:19 +00005284 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005285
5286 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
Douglas Gregor35942772011-09-09 21:34:22 +00005287 Builder.Extend(Context,
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005288 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
5289 T->getTypeLoc(), ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00005290 break;
5291 }
5292
5293 case NestedNameSpecifier::Global: {
Douglas Gregordc355712011-02-25 00:36:19 +00005294 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005295 Builder.MakeGlobal(Context, ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00005296 break;
5297 }
5298 }
Douglas Gregordc355712011-02-25 00:36:19 +00005299 }
5300
Douglas Gregor35942772011-09-09 21:34:22 +00005301 return Builder.getWithLocInContext(Context);
Douglas Gregordc355712011-02-25 00:36:19 +00005302}
5303
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005304SourceRange
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005305ASTReader::ReadSourceRange(Module &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00005306 unsigned &Idx) {
5307 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
5308 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00005309 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005310}
5311
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00005312/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005313llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00005314 unsigned BitWidth = Record[Idx++];
5315 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
5316 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
5317 Idx += NumWords;
5318 return Result;
5319}
5320
5321/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005322llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00005323 bool isUnsigned = Record[Idx++];
5324 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
5325}
5326
Douglas Gregor17fc2232009-04-14 21:55:33 +00005327/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005328llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00005329 return llvm::APFloat(ReadAPInt(Record, Idx));
5330}
5331
Douglas Gregor68a2eb02009-04-15 21:30:51 +00005332// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005333std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00005334 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00005335 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00005336 Idx += Len;
5337 return Result;
5338}
5339
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005340VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
5341 unsigned &Idx) {
5342 unsigned Major = Record[Idx++];
5343 unsigned Minor = Record[Idx++];
5344 unsigned Subminor = Record[Idx++];
5345 if (Minor == 0)
5346 return VersionTuple(Major);
5347 if (Subminor == 0)
5348 return VersionTuple(Major, Minor - 1);
5349 return VersionTuple(Major, Minor - 1, Subminor - 1);
5350}
5351
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005352CXXTemporary *ASTReader::ReadCXXTemporary(Module &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00005353 const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00005354 unsigned &Idx) {
Douglas Gregor409448c2011-07-21 22:35:25 +00005355 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005356 return CXXTemporary::Create(Context, Decl);
Chris Lattnerd2598362010-05-10 00:25:06 +00005357}
5358
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005359DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00005360 return Diag(SourceLocation(), DiagID);
5361}
5362
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005363DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00005364 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00005365}
Douglas Gregor025452f2009-04-17 00:04:06 +00005366
Douglas Gregor668c1a42009-04-21 22:25:48 +00005367/// \brief Retrieve the identifier table associated with the
5368/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005369IdentifierTable &ASTReader::getIdentifierTable() {
Douglas Gregor712f2fc2011-09-09 22:02:16 +00005370 return PP.getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00005371}
5372
Douglas Gregor025452f2009-04-17 00:04:06 +00005373/// \brief Record that the given ID maps to the given switch-case
5374/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005375void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00005376 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
5377 SwitchCaseStmts[ID] = SC;
5378}
5379
5380/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005381SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00005382 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
5383 return SwitchCaseStmts[ID];
5384}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00005385
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00005386void ASTReader::ClearSwitchCaseIDs() {
5387 SwitchCaseStmts.clear();
5388}
5389
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005390void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005391 assert(NumCurrentElementsDeserializing &&
5392 "FinishedDeserializing not paired with StartedDeserializing");
5393 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregord89275b2009-07-06 18:54:52 +00005394 // If any identifiers with corresponding top-level declarations have
5395 // been loaded, load those declarations now.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005396 while (!PendingIdentifierInfos.empty()) {
5397 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
5398 PendingIdentifierInfos.front().DeclIDs, true);
5399 PendingIdentifierInfos.pop_front();
Douglas Gregord89275b2009-07-06 18:54:52 +00005400 }
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005401
Argyrios Kyrtzidis0895d152011-02-12 07:50:47 +00005402 // Ready to load previous declarations of Decls that were delayed.
5403 while (!PendingPreviousDecls.empty()) {
5404 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
5405 PendingPreviousDecls.front().second);
5406 PendingPreviousDecls.pop_front();
5407 }
5408
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005409 // We are not in recursive loading, so it's safe to pass the "interesting"
5410 // decls to the consumer.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005411 if (Consumer)
5412 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidis134db1f2010-10-24 17:26:31 +00005413
5414 assert(PendingForwardRefs.size() == 0 &&
5415 "Some forward refs did not get linked to the definition!");
Douglas Gregord89275b2009-07-06 18:54:52 +00005416 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005417 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00005418}
Douglas Gregor501c1032010-08-19 00:28:17 +00005419
Douglas Gregorf8a1e512011-09-02 00:26:20 +00005420ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
Douglas Gregor832d6202011-07-22 16:35:34 +00005421 StringRef isysroot, bool DisableValidation,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005422 bool DisableStatCache)
Sebastian Redle1dde812010-08-24 00:50:04 +00005423 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
5424 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00005425 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
Jonathan D. Turner1afb6612011-07-28 17:20:23 +00005426 Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
5427 RelocatablePCH(false), isysroot(isysroot),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005428 DisableValidation(DisableValidation),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005429 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005430 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005431 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
5432 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
5433 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
5434 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00005435 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
5436 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
5437 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005438{
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005439 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redle1dde812010-08-24 00:50:04 +00005440}
5441
Sebastian Redle1dde812010-08-24 00:50:04 +00005442ASTReader::~ASTReader() {
Sebastian Redle1dde812010-08-24 00:50:04 +00005443 for (DeclContextVisibleUpdatesPending::iterator
5444 I = PendingVisibleUpdates.begin(),
5445 E = PendingVisibleUpdates.end();
5446 I != E; ++I) {
5447 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5448 F = I->second.end();
5449 J != F; ++J)
Douglas Gregor496c7092011-08-03 15:48:04 +00005450 delete static_cast<ASTDeclContextNameLookupTable*>(J->first);
Sebastian Redle1dde812010-08-24 00:50:04 +00005451 }
5452}