blob: 8961974856b67d5a02f41e75b2829eee3df53b13 [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 Gregoreee242f2011-10-27 09:33:13 +0000517 II->setOutOfDate(false);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000518 return II;
519 }
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Douglas Gregor98339b92011-08-25 20:47:51 +0000521 unsigned Bits = ReadUnalignedLE16(d);
522 bool CPlusPlusOperatorKeyword = Bits & 0x01;
523 Bits >>= 1;
524 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
525 Bits >>= 1;
526 bool Poisoned = Bits & 0x01;
527 Bits >>= 1;
528 bool ExtensionToken = Bits & 0x01;
529 Bits >>= 1;
530 bool hasMacroDefinition = Bits & 0x01;
531 Bits >>= 1;
532 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
533 Bits >>= 10;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000534
Douglas Gregor98339b92011-08-25 20:47:51 +0000535 assert(Bits == 0 && "Extra bits in the identifier?");
536 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000537
Douglas Gregor98339b92011-08-25 20:47:51 +0000538 // Build the IdentifierInfo itself and link the identifier ID with
539 // the new IdentifierInfo.
540 IdentifierInfo *II = KnownII;
541 if (!II)
542 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregoreee242f2011-10-27 09:33:13 +0000543 II->setOutOfDate(false);
544 II->setIsFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000545
Douglas Gregor98339b92011-08-25 20:47:51 +0000546 // Set or check the various bits in the IdentifierInfo structure.
547 // Token IDs are read-only.
548 if (HasRevertedTokenIDToIdentifier)
549 II->RevertTokenIDToIdentifier();
550 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
551 assert(II->isExtensionToken() == ExtensionToken &&
552 "Incorrect extension token flag");
553 (void)ExtensionToken;
554 if (Poisoned)
555 II->setIsPoisoned(true);
556 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
557 "Incorrect C++ operator keyword flag");
558 (void)CPlusPlusOperatorKeyword;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000559
Douglas Gregor98339b92011-08-25 20:47:51 +0000560 // If this identifier is a macro, deserialize the macro
561 // definition.
562 if (hasMacroDefinition) {
563 // FIXME: Check for conflicts?
564 uint32_t Offset = ReadUnalignedLE32(d);
565 Reader.SetIdentifierIsMacro(II, F, Offset);
566 DataLen -= 4;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000567 }
568
Douglas Gregoreee242f2011-10-27 09:33:13 +0000569 Reader.SetIdentifierInfo(ID, II);
570
Douglas Gregor98339b92011-08-25 20:47:51 +0000571 // Read all of the declarations visible at global scope with this
572 // name.
Douglas Gregor98339b92011-08-25 20:47:51 +0000573 if (DataLen > 0) {
574 SmallVector<uint32_t, 4> DeclIDs;
575 for (; DataLen > 0; DataLen -= 4)
576 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
577 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000578 }
579
Douglas Gregor98339b92011-08-25 20:47:51 +0000580 return II;
581}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000582
Douglas Gregor98339b92011-08-25 20:47:51 +0000583unsigned
584ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
585 llvm::FoldingSetNodeID ID;
586 ID.AddInteger(Key.Kind);
587
588 switch (Key.Kind) {
589 case DeclarationName::Identifier:
590 case DeclarationName::CXXLiteralOperatorName:
591 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
592 break;
593 case DeclarationName::ObjCZeroArgSelector:
594 case DeclarationName::ObjCOneArgSelector:
595 case DeclarationName::ObjCMultiArgSelector:
596 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
597 break;
598 case DeclarationName::CXXOperatorName:
599 ID.AddInteger((OverloadedOperatorKind)Key.Data);
600 break;
601 case DeclarationName::CXXConstructorName:
602 case DeclarationName::CXXDestructorName:
603 case DeclarationName::CXXConversionFunctionName:
604 case DeclarationName::CXXUsingDirective:
605 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000606 }
607
Douglas Gregor98339b92011-08-25 20:47:51 +0000608 return ID.ComputeHash();
609}
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000610
Douglas Gregor98339b92011-08-25 20:47:51 +0000611ASTDeclContextNameLookupTrait::internal_key_type
612ASTDeclContextNameLookupTrait::GetInternalKey(
613 const external_key_type& Name) const {
614 DeclNameKey Key;
615 Key.Kind = Name.getNameKind();
616 switch (Name.getNameKind()) {
617 case DeclarationName::Identifier:
618 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
619 break;
620 case DeclarationName::ObjCZeroArgSelector:
621 case DeclarationName::ObjCOneArgSelector:
622 case DeclarationName::ObjCMultiArgSelector:
623 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
624 break;
625 case DeclarationName::CXXOperatorName:
626 Key.Data = Name.getCXXOverloadedOperator();
627 break;
628 case DeclarationName::CXXLiteralOperatorName:
629 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
630 break;
631 case DeclarationName::CXXConstructorName:
632 case DeclarationName::CXXDestructorName:
633 case DeclarationName::CXXConversionFunctionName:
634 case DeclarationName::CXXUsingDirective:
635 Key.Data = 0;
636 break;
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000637 }
638
Douglas Gregor98339b92011-08-25 20:47:51 +0000639 return Key;
640}
641
642ASTDeclContextNameLookupTrait::external_key_type
643ASTDeclContextNameLookupTrait::GetExternalKey(
644 const internal_key_type& Key) const {
Douglas Gregor35942772011-09-09 21:34:22 +0000645 ASTContext &Context = Reader.getContext();
Douglas Gregor98339b92011-08-25 20:47:51 +0000646 switch (Key.Kind) {
647 case DeclarationName::Identifier:
648 return DeclarationName((IdentifierInfo*)Key.Data);
649
650 case DeclarationName::ObjCZeroArgSelector:
651 case DeclarationName::ObjCOneArgSelector:
652 case DeclarationName::ObjCMultiArgSelector:
653 return DeclarationName(Selector(Key.Data));
654
655 case DeclarationName::CXXConstructorName:
Douglas Gregor35942772011-09-09 21:34:22 +0000656 return Context.DeclarationNames.getCXXConstructorName(
657 Context.getCanonicalType(Reader.getLocalType(F, Key.Data)));
Douglas Gregor98339b92011-08-25 20:47:51 +0000658
659 case DeclarationName::CXXDestructorName:
Douglas Gregor35942772011-09-09 21:34:22 +0000660 return Context.DeclarationNames.getCXXDestructorName(
661 Context.getCanonicalType(Reader.getLocalType(F, Key.Data)));
Douglas Gregor98339b92011-08-25 20:47:51 +0000662
663 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor35942772011-09-09 21:34:22 +0000664 return Context.DeclarationNames.getCXXConversionFunctionName(
665 Context.getCanonicalType(Reader.getLocalType(F, Key.Data)));
Douglas Gregor98339b92011-08-25 20:47:51 +0000666
667 case DeclarationName::CXXOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +0000668 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregor98339b92011-08-25 20:47:51 +0000669 (OverloadedOperatorKind)Key.Data);
670
671 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +0000672 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregor98339b92011-08-25 20:47:51 +0000673 (IdentifierInfo*)Key.Data);
674
675 case DeclarationName::CXXUsingDirective:
676 return DeclarationName::getUsingDirectiveName();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000677 }
678
Douglas Gregor98339b92011-08-25 20:47:51 +0000679 llvm_unreachable("Invalid Name Kind ?");
680}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000681
Douglas Gregor98339b92011-08-25 20:47:51 +0000682std::pair<unsigned, unsigned>
683ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
684 using namespace clang::io;
685 unsigned KeyLen = ReadUnalignedLE16(d);
686 unsigned DataLen = ReadUnalignedLE16(d);
687 return std::make_pair(KeyLen, DataLen);
688}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000689
Douglas Gregor98339b92011-08-25 20:47:51 +0000690ASTDeclContextNameLookupTrait::internal_key_type
691ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
692 using namespace clang::io;
693
694 DeclNameKey Key;
695 Key.Kind = (DeclarationName::NameKind)*d++;
696 switch (Key.Kind) {
697 case DeclarationName::Identifier:
698 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
699 break;
700 case DeclarationName::ObjCZeroArgSelector:
701 case DeclarationName::ObjCOneArgSelector:
702 case DeclarationName::ObjCMultiArgSelector:
703 Key.Data =
704 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
705 .getAsOpaquePtr();
706 break;
707 case DeclarationName::CXXOperatorName:
708 Key.Data = *d++; // OverloadedOperatorKind
709 break;
710 case DeclarationName::CXXLiteralOperatorName:
711 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
712 break;
713 case DeclarationName::CXXConstructorName:
714 case DeclarationName::CXXDestructorName:
715 case DeclarationName::CXXConversionFunctionName:
716 case DeclarationName::CXXUsingDirective:
717 Key.Data = 0;
718 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000719 }
720
Douglas Gregor98339b92011-08-25 20:47:51 +0000721 return Key;
722}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000723
Douglas Gregor98339b92011-08-25 20:47:51 +0000724ASTDeclContextNameLookupTrait::data_type
725ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
726 const unsigned char* d,
727 unsigned DataLen) {
728 using namespace clang::io;
729 unsigned NumDecls = ReadUnalignedLE16(d);
730 DeclID *Start = (DeclID *)d;
731 return std::make_pair(Start, Start + NumDecls);
732}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000733
Douglas Gregor0d95f772011-08-24 19:03:07 +0000734bool ASTReader::ReadDeclContextStorage(Module &M,
735 llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000736 const std::pair<uint64_t, uint64_t> &Offsets,
737 DeclContextInfo &Info) {
738 SavedStreamPosition SavedPosition(Cursor);
739 // First the lexical decls.
740 if (Offsets.first != 0) {
741 Cursor.JumpToBit(Offsets.first);
742
743 RecordData Record;
744 const char *Blob;
745 unsigned BlobLen;
746 unsigned Code = Cursor.ReadCode();
747 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
748 if (RecCode != DECL_CONTEXT_LEXICAL) {
749 Error("Expected lexical block");
750 return true;
751 }
752
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000753 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
754 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000755 }
756
757 // Now the lookup table.
758 if (Offsets.second != 0) {
759 Cursor.JumpToBit(Offsets.second);
760
761 RecordData Record;
762 const char *Blob;
763 unsigned BlobLen;
764 unsigned Code = Cursor.ReadCode();
765 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
766 if (RecCode != DECL_CONTEXT_VISIBLE) {
767 Error("Expected visible lookup table block");
768 return true;
769 }
770 Info.NameLookupTableData
771 = ASTDeclContextNameLookupTable::Create(
772 (const unsigned char *)Blob + Record[0],
773 (const unsigned char *)Blob,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000774 ASTDeclContextNameLookupTrait(*this, M));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000775 }
776
777 return false;
778}
779
Chris Lattner5f9e2722011-07-23 10:55:15 +0000780void ASTReader::Error(StringRef Msg) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000781 Error(diag::err_fe_pch_malformed, Msg);
782}
783
784void ASTReader::Error(unsigned DiagID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000785 StringRef Arg1, StringRef Arg2) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000786 if (Diags.isDiagnosticInFlight())
787 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
788 else
789 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000790}
791
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000792/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000793bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000794 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000795 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000796 ActualOriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000797 SuggestedPredefines,
798 FileMgr);
Douglas Gregore721f952009-04-28 18:58:38 +0000799 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000800}
801
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000802//===----------------------------------------------------------------------===//
803// Source Manager Deserialization
804//===----------------------------------------------------------------------===//
805
Douglas Gregorbd945002009-04-13 16:31:14 +0000806/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000807/// \returns true if there was an error.
Douglas Gregor72a9ae12011-07-22 16:00:58 +0000808bool ASTReader::ParseLineTable(Module &F,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000809 SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000810 unsigned Idx = 0;
811 LineTableInfo &LineTable = SourceMgr.getLineTable();
812
813 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000814 std::map<int, int> FileIDs;
815 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000816 // Extract the file name
817 unsigned FilenameLen = Record[Idx++];
818 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
819 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000820 MaybeAddSystemRootToFilename(Filename);
Jay Foad65aa6882011-06-21 15:13:30 +0000821 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
Douglas Gregorbd945002009-04-13 16:31:14 +0000822 }
823
824 // Parse the line entries
825 std::vector<LineEntry> Entries;
826 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000827 int FID = Record[Idx++];
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000828 assert(FID >= 0 && "Serialized line entries for non-local file.");
829 // Remap FileID from 1-based old view.
830 FID += F.SLocEntryBaseID - 1;
Douglas Gregorbd945002009-04-13 16:31:14 +0000831
832 // Extract the line entries
833 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000834 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000835 Entries.clear();
836 Entries.reserve(NumEntries);
837 for (unsigned I = 0; I != NumEntries; ++I) {
838 unsigned FileOffset = Record[Idx++];
839 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000840 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000841 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000842 = (SrcMgr::CharacteristicKind)Record[Idx++];
843 unsigned IncludeOffset = Record[Idx++];
844 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
845 FileKind, IncludeOffset));
846 }
847 LineTable.AddEntry(FID, Entries);
848 }
849
850 return false;
851}
852
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000853namespace {
854
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000855class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000856public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000857 const ino_t ino;
858 const dev_t dev;
859 const mode_t mode;
860 const time_t mtime;
861 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000863 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +0000864 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000865};
866
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000867class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000868 public:
869 typedef const char *external_key_type;
870 typedef const char *internal_key_type;
871
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000872 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000873
874 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000875 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000876 }
877
878 static internal_key_type GetInternalKey(const char *path) { return path; }
879
880 static bool EqualKey(internal_key_type a, internal_key_type b) {
881 return strcmp(a, b) == 0;
882 }
883
884 static std::pair<unsigned, unsigned>
885 ReadKeyDataLength(const unsigned char*& d) {
886 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
887 unsigned DataLen = (unsigned) *d++;
888 return std::make_pair(KeyLen + 1, DataLen);
889 }
890
891 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
892 return (const char *)d;
893 }
894
895 static data_type ReadData(const internal_key_type, const unsigned char *d,
896 unsigned /*DataLen*/) {
897 using namespace clang::io;
898
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000899 ino_t ino = (ino_t) ReadUnalignedLE32(d);
900 dev_t dev = (dev_t) ReadUnalignedLE32(d);
901 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000902 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000903 off_t size = (off_t) ReadUnalignedLE64(d);
904 return data_type(ino, dev, mode, mtime, size);
905 }
906};
907
908/// \brief stat() cache for precompiled headers.
909///
910/// This cache is very similar to the stat cache used by pretokenized
911/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +0000912class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000913 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000914 CacheTy *Cache;
915
916 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000917public:
Chris Lattner74e976b2010-11-23 19:28:12 +0000918 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
919 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000920 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
921 Cache = CacheTy::Create(Buckets, Base);
922 }
923
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000924 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Chris Lattner898a0612010-11-23 21:17:56 +0000926 LookupResult getStat(const char *Path, struct stat &StatBuf,
927 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000928 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +0000929 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000930
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000931 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000932 if (I == Cache->end()) {
933 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +0000934 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000935 }
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000937 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000938 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Chris Lattner10e286a2010-11-23 19:19:34 +0000940 StatBuf.st_ino = Data.ino;
941 StatBuf.st_dev = Data.dev;
942 StatBuf.st_mtime = Data.mtime;
943 StatBuf.st_mode = Data.mode;
944 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +0000945 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000946 }
947};
948} // end anonymous namespace
949
950
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000951/// \brief Read a source manager block
Douglas Gregor72a9ae12011-07-22 16:00:58 +0000952ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(Module &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000953 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000954
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000955 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +0000956
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000957 // Set the source-location entry cursor to the current position in
958 // the stream. This cursor will be used to read the contents of the
959 // source manager block initially, and then lazily read
960 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000961 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000962
963 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000964 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000965 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000966 return Failure;
967 }
968
969 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000970 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000971 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000972 return Failure;
973 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000974
Douglas Gregor14f79002009-04-10 03:52:48 +0000975 RecordData Record;
976 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000977 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +0000978 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000979 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000980 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000981 return Failure;
982 }
Douglas Gregore1d918e2009-04-10 23:10:45 +0000983 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000984 }
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Douglas Gregor14f79002009-04-10 03:52:48 +0000986 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
987 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000988 SLocEntryCursor.ReadSubBlockID();
989 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000990 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000991 return Failure;
992 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000993 continue;
994 }
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Douglas Gregor14f79002009-04-10 03:52:48 +0000996 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000997 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +0000998 continue;
999 }
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Douglas Gregor14f79002009-04-10 03:52:48 +00001001 // Read a record.
1002 const char *BlobStart;
1003 unsigned BlobLen;
1004 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001005 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001006 default: // Default behavior: ignore.
1007 break;
1008
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001009 case SM_SLOC_FILE_ENTRY:
1010 case SM_SLOC_BUFFER_ENTRY:
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001011 case SM_SLOC_EXPANSION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001012 // Once we hit one of the source location entries, we're done.
1013 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001014 }
1015 }
1016}
1017
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001018/// \brief If a header file is not found at the path that we expect it to be
1019/// and the PCH file was moved from its original location, try to resolve the
1020/// file by assuming that header+PCH were moved together and the header is in
1021/// the same place relative to the PCH.
1022static std::string
1023resolveFileRelativeToOriginalDir(const std::string &Filename,
1024 const std::string &OriginalDir,
1025 const std::string &CurrDir) {
1026 assert(OriginalDir != CurrDir &&
1027 "No point trying to resolve the file if the PCH dir didn't change");
1028 using namespace llvm::sys;
1029 llvm::SmallString<128> filePath(Filename);
1030 fs::make_absolute(filePath);
1031 assert(path::is_absolute(OriginalDir));
1032 llvm::SmallString<128> currPCHPath(CurrDir);
1033
1034 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1035 fileDirE = path::end(path::parent_path(filePath));
1036 path::const_iterator origDirI = path::begin(OriginalDir),
1037 origDirE = path::end(OriginalDir);
1038 // Skip the common path components from filePath and OriginalDir.
1039 while (fileDirI != fileDirE && origDirI != origDirE &&
1040 *fileDirI == *origDirI) {
1041 ++fileDirI;
1042 ++origDirI;
1043 }
1044 for (; origDirI != origDirE; ++origDirI)
1045 path::append(currPCHPath, "..");
1046 path::append(currPCHPath, fileDirI, fileDirE);
1047 path::append(currPCHPath, path::filename(Filename));
1048 return currPCHPath.str();
1049}
1050
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001051/// \brief Read in the source location entry with the given ID.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001052ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(int ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001053 if (ID == 0)
1054 return Success;
1055
Douglas Gregor0cdd7982011-07-21 18:46:38 +00001056 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001057 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001058 return Failure;
1059 }
1060
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001061 Module *F = GlobalSLocEntryMap.find(-ID)->second;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001062 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001063 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001064 unsigned BaseOffset = F->SLocEntryBaseOffset;
Sebastian Redl9137a522010-07-16 17:50:48 +00001065
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001066 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001067 unsigned Code = SLocEntryCursor.ReadCode();
1068 if (Code == llvm::bitc::END_BLOCK ||
1069 Code == llvm::bitc::ENTER_SUBBLOCK ||
1070 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001071 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001072 return Failure;
1073 }
1074
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001075 RecordData Record;
1076 const char *BlobStart;
1077 unsigned BlobLen;
1078 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1079 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001080 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001081 return Failure;
1082
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001083 case SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001084 std::string Filename(BlobStart, BlobStart + BlobLen);
1085 MaybeAddSystemRootToFilename(Filename);
Chris Lattner39b49bc2010-11-23 08:35:12 +00001086 const FileEntry *File = FileMgr.getFile(Filename);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001087 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1088 OriginalDir != CurrentDir) {
1089 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1090 OriginalDir,
1091 CurrentDir);
1092 if (!resolved.empty())
1093 File = FileMgr.getFile(resolved);
1094 }
Axel Naumann04331162011-01-27 10:55:51 +00001095 if (File == 0)
1096 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1097 (time_t)Record[5]);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001098 if (File == 0) {
1099 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001100 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001101 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001102 Error(ErrorStr.c_str());
1103 return Failure;
1104 }
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001106 if (Record.size() < 6) {
Ted Kremenek1857f622010-03-18 21:23:05 +00001107 Error("source location entry is incorrect");
1108 return Failure;
1109 }
1110
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001111 if (!DisableValidation &&
1112 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001113#if !defined(LLVM_ON_WIN32)
1114 // In our regression testing, the Windows file system seems to
1115 // have inconsistent modification times that sometimes
1116 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001117 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001118#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001119 )) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +00001120 Error(diag::err_fe_pch_file_modified, Filename);
Douglas Gregor2d52be52010-03-21 22:49:54 +00001121 return Failure;
1122 }
1123
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001124 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001125 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001126 // This is the module's main file.
1127 IncludeLoc = getImportLocation(F);
1128 }
1129 FileID FID = SourceMgr.createFileID(File, IncludeLoc,
Chris Lattner75dfb652010-11-23 09:19:42 +00001130 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001131 ID, BaseOffset + Record[0]);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001132 SrcMgr::FileInfo &FileInfo =
1133 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1134 FileInfo.NumCreatedFIDs = Record[6];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001135 if (Record[3])
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001136 FileInfo.setHasLineDirectives();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001137
1138 const DeclID *FirstDecl = F->FileSortedDecls + Record[7];
1139 unsigned NumFileDecls = Record[8];
1140 if (NumFileDecls) {
1141 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
Argyrios Kyrtzidis9d128d02011-10-31 07:20:08 +00001142 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1143 NumFileDecls));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001144 }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001145
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001146 break;
1147 }
1148
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001149 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001150 const char *Name = BlobStart;
1151 unsigned Offset = Record[0];
1152 unsigned Code = SLocEntryCursor.ReadCode();
1153 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001154 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001155 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001156
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001157 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001158 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001159 return Failure;
1160 }
1161
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001162 llvm::MemoryBuffer *Buffer
Chris Lattner5f9e2722011-07-23 10:55:15 +00001163 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
Chris Lattnera0a270c2010-04-05 22:42:27 +00001164 Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001165 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID,
1166 BaseOffset + Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Douglas Gregor92b059e2009-04-28 20:33:11 +00001168 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001169 PCHPredefinesBlock Block = {
1170 BufferID,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001171 StringRef(BlobStart, BlobLen - 1)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001172 };
1173 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001174 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001175
1176 break;
1177 }
1178
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001179 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001180 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruthbf340e42011-07-26 03:03:05 +00001181 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001182 ReadSourceLocation(*F, Record[2]),
1183 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001184 Record[4],
1185 ID,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001186 BaseOffset + Record[0]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001187 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001188 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001189 }
1190
1191 return Success;
1192}
1193
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001194/// \brief Find the location where the module F is imported.
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001195SourceLocation ASTReader::getImportLocation(Module *F) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001196 if (F->ImportLoc.isValid())
1197 return F->ImportLoc;
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001198
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001199 // Otherwise we have a PCH. It's considered to be "imported" at the first
1200 // location of its includer.
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001201 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001202 // Main file is the importer. We assume that it is the first entry in the
1203 // entry table. We can't ask the manager, because at the time of PCH loading
1204 // the main file entry doesn't exist yet.
1205 // The very first entry is the invalid instantiation loc, which takes up
1206 // offsets 0 and 1.
1207 return SourceLocation::getFromRawEncoding(2U);
1208 }
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001209 //return F->Loaders[0]->FirstLoc;
1210 return F->ImportedBy[0]->FirstLoc;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001211}
1212
Chris Lattner6367f6d2009-04-27 01:05:14 +00001213/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1214/// specified cursor. Read the abbreviations that are at the top of the block
1215/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001216bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001217 unsigned BlockID) {
1218 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001219 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001220 return Failure;
1221 }
Mike Stump1eb44332009-09-09 15:08:12 +00001222
Chris Lattner6367f6d2009-04-27 01:05:14 +00001223 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001224 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001225 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001226
Chris Lattner6367f6d2009-04-27 01:05:14 +00001227 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001228 if (Code != llvm::bitc::DEFINE_ABBREV) {
1229 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001230 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001231 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001232 Cursor.ReadAbbrevRecord();
1233 }
1234}
1235
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001236void ASTReader::ReadMacroRecord(Module &F, uint64_t Offset) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001237 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001238
Douglas Gregor37e26842009-04-21 23:56:24 +00001239 // Keep track of where we are in the stream, then jump back there
1240 // after reading this macro.
1241 SavedStreamPosition SavedPosition(Stream);
1242
1243 Stream.JumpToBit(Offset);
1244 RecordData Record;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001245 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001246 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Douglas Gregor37e26842009-04-21 23:56:24 +00001248 while (true) {
1249 unsigned Code = Stream.ReadCode();
1250 switch (Code) {
1251 case llvm::bitc::END_BLOCK:
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001252 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001253
1254 case llvm::bitc::ENTER_SUBBLOCK:
1255 // No known subblocks, always skip them.
1256 Stream.ReadSubBlockID();
1257 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001258 Error("malformed block record in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001259 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001260 }
1261 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001262
Douglas Gregor37e26842009-04-21 23:56:24 +00001263 case llvm::bitc::DEFINE_ABBREV:
1264 Stream.ReadAbbrevRecord();
1265 continue;
1266 default: break;
1267 }
1268
1269 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001270 const char *BlobStart = 0;
1271 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001272 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001273 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001274 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001275 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001276 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001277 case PP_MACRO_OBJECT_LIKE:
1278 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001279 // If we already have a macro, that means that we've hit the end
1280 // of the definition of the macro we were looking for. We're
1281 // done.
1282 if (Macro)
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001283 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001284
Douglas Gregor95eab172011-07-28 20:55:49 +00001285 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001286 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001287 Error("macro must have a name in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001288 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001289 }
Douglas Gregoreee242f2011-10-27 09:33:13 +00001290
Sebastian Redlc3632732010-10-05 15:59:54 +00001291 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001292 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001294 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001295 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001296 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Douglas Gregoraa93a872011-10-17 15:32:29 +00001298 bool IsPublic = Record[3];
1299 unsigned NextIndex = 4;
1300 MI->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex));
Douglas Gregor7143aab2011-09-01 17:04:32 +00001301
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001302 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001303 // Decode function-like macro info.
Douglas Gregor7143aab2011-09-01 17:04:32 +00001304 bool isC99VarArgs = Record[NextIndex++];
1305 bool isGNUVarArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001306 MacroArgs.clear();
Douglas Gregor7143aab2011-09-01 17:04:32 +00001307 unsigned NumArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001308 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregor7143aab2011-09-01 17:04:32 +00001309 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001310
1311 // Install function-like macro info.
1312 MI->setIsFunctionLike();
1313 if (isC99VarArgs) MI->setIsC99Varargs();
1314 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001315 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001316 PP.getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001317 }
1318
1319 // Finally, install the macro.
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001320 PP.setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001321
1322 // Remember that we saw this macro last so that we add the tokens that
1323 // form its body to it.
1324 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001325
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001326 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1327 Record[NextIndex]) {
1328 // We have a macro definition. Register the association
1329 PreprocessedEntityID
1330 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1331 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1332 PPRec.RegisterMacroDefinition(Macro,
1333 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001334 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001335
Douglas Gregor37e26842009-04-21 23:56:24 +00001336 ++NumMacrosRead;
1337 break;
1338 }
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001340 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001341 // If we see a TOKEN before a PP_MACRO_*, then the file is
1342 // erroneous, just pretend we didn't see this.
1343 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001344
Douglas Gregor37e26842009-04-21 23:56:24 +00001345 Token Tok;
1346 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001347 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001348 Tok.setLength(Record[1]);
Douglas Gregor95eab172011-07-28 20:55:49 +00001349 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregor37e26842009-04-21 23:56:24 +00001350 Tok.setIdentifierInfo(II);
1351 Tok.setKind((tok::TokenKind)Record[3]);
1352 Tok.setFlag((Token::TokenFlags)Record[4]);
1353 Macro->AddTokenToBody(Tok);
1354 break;
1355 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001356 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001357 }
Douglas Gregor89d99802010-11-30 06:16:57 +00001358
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001359 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001360}
1361
Douglas Gregor86c67d82011-07-28 22:39:26 +00001362PreprocessedEntityID
Argyrios Kyrtzidis1f6d2252011-09-19 20:40:02 +00001363ASTReader::getGlobalPreprocessedEntityID(Module &M, unsigned LocalID) const {
1364 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001365 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1366 assert(I != M.PreprocessedEntityRemap.end()
1367 && "Invalid index into preprocessed entity index remap");
1368
1369 return LocalID + I->second;
Douglas Gregor86c67d82011-07-28 22:39:26 +00001370}
1371
Douglas Gregor98339b92011-08-25 20:47:51 +00001372unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1373 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001374}
Douglas Gregor98339b92011-08-25 20:47:51 +00001375
1376HeaderFileInfoTrait::internal_key_type
1377HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1378
1379bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1380 if (strcmp(a, b) == 0)
1381 return true;
1382
1383 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1384 return false;
1385
1386 // The file names match, but the path names don't. stat() the files to
1387 // see if they are the same.
1388 struct stat StatBufA, StatBufB;
1389 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1390 return false;
1391
1392 return StatBufA.st_ino == StatBufB.st_ino;
1393}
1394
1395std::pair<unsigned, unsigned>
1396HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1397 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1398 unsigned DataLen = (unsigned) *d++;
1399 return std::make_pair(KeyLen + 1, DataLen);
1400}
1401
1402HeaderFileInfoTrait::data_type
1403HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1404 unsigned DataLen) {
1405 const unsigned char *End = d + DataLen;
1406 using namespace clang::io;
1407 HeaderFileInfo HFI;
1408 unsigned Flags = *d++;
1409 HFI.isImport = (Flags >> 5) & 0x01;
1410 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1411 HFI.DirInfo = (Flags >> 2) & 0x03;
1412 HFI.Resolved = (Flags >> 1) & 0x01;
1413 HFI.IndexHeaderMapHeader = Flags & 0x01;
1414 HFI.NumIncludes = ReadUnalignedLE16(d);
Douglas Gregor541ba162011-10-17 18:53:12 +00001415 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1416 ReadUnalignedLE32(d));
Douglas Gregor98339b92011-08-25 20:47:51 +00001417 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1418 // The framework offset is 1 greater than the actual offset,
1419 // since 0 is used as an indicator for "no framework name".
1420 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1421 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1422 }
1423
1424 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1425 (void)End;
1426
1427 // This HeaderFileInfo was externally loaded.
1428 HFI.External = true;
1429 return HFI;
1430}
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001431
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001432void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, Module &F,
Douglas Gregor2d2689a2011-07-28 21:16:51 +00001433 uint64_t LocalOffset) {
Douglas Gregor295a2a62010-10-30 00:23:06 +00001434 // Note that this identifier has a macro definition.
1435 II->setHasMacroDefinition(true);
1436
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001437 // Adjust the offset to a global offset.
Douglas Gregor2d2689a2011-07-28 21:16:51 +00001438 UnreadMacroRecordOffsets[II] = F.GlobalBitOffset + LocalOffset;
Douglas Gregor295a2a62010-10-30 00:23:06 +00001439}
1440
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001441void ASTReader::ReadDefinedMacros() {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001442 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1443 E = ModuleMgr.rend(); I != E; ++I) {
1444 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001445
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001446 // If there was no preprocessor block, skip this file.
1447 if (!MacroCursor.getBitStreamReader())
1448 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001449
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001450 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001451 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001452
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001453 RecordData Record;
1454 while (true) {
1455 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001456 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001457 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001458
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001459 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1460 // No known subblocks, always skip them.
1461 Cursor.ReadSubBlockID();
1462 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001463 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001464 return;
1465 }
1466 continue;
1467 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001468
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001469 if (Code == llvm::bitc::DEFINE_ABBREV) {
1470 Cursor.ReadAbbrevRecord();
1471 continue;
1472 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001473
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001474 // Read a record.
1475 const char *BlobStart;
1476 unsigned BlobLen;
1477 Record.clear();
1478 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1479 default: // Default behavior: ignore.
1480 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001481
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001482 case PP_MACRO_OBJECT_LIKE:
1483 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregor95eab172011-07-28 20:55:49 +00001484 getLocalIdentifier(**I, Record[0]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001485 break;
1486
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001487 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001488 // Ignore tokens.
1489 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001490 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001491 }
1492 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001493
1494 // Drain the unread macro-record offsets map.
1495 while (!UnreadMacroRecordOffsets.empty())
1496 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1497}
1498
1499void ASTReader::LoadMacroDefinition(
1500 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1501 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
Douglas Gregor295a2a62010-10-30 00:23:06 +00001502 uint64_t Offset = Pos->second;
1503 UnreadMacroRecordOffsets.erase(Pos);
1504
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001505 RecordLocation Loc = getLocalBitOffset(Offset);
1506 ReadMacroRecord(*Loc.F, Loc.Offset);
Douglas Gregor295a2a62010-10-30 00:23:06 +00001507}
1508
1509void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1510 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1511 = UnreadMacroRecordOffsets.find(II);
1512 LoadMacroDefinition(Pos);
Douglas Gregor88a35862010-01-04 19:18:44 +00001513}
1514
Douglas Gregoreee242f2011-10-27 09:33:13 +00001515namespace {
1516 /// \brief Visitor class used to look up identifirs in an AST file.
1517 class IdentifierLookupVisitor {
1518 StringRef Name;
1519 IdentifierInfo *Found;
1520 public:
1521 explicit IdentifierLookupVisitor(StringRef Name) : Name(Name), Found() { }
1522
1523 static bool visit(Module &M, void *UserData) {
1524 IdentifierLookupVisitor *This
1525 = static_cast<IdentifierLookupVisitor *>(UserData);
1526
1527 ASTIdentifierLookupTable *IdTable
1528 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1529 if (!IdTable)
1530 return false;
1531
1532 std::pair<const char*, unsigned> Key(This->Name.begin(),
1533 This->Name.size());
1534 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
1535 if (Pos == IdTable->end())
1536 return false;
1537
1538 // Dereferencing the iterator has the effect of building the
1539 // IdentifierInfo node and populating it with the various
1540 // declarations it needs.
1541 This->Found = *Pos;
1542 return true;
1543 }
1544
1545 // \brief Retrieve the identifier info found within the module
1546 // files.
1547 IdentifierInfo *getIdentifierInfo() const { return Found; }
1548 };
1549}
1550
1551void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1552 get(II.getName());
1553}
1554
Chris Lattner5f9e2722011-07-23 10:55:15 +00001555const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001556 std::string Filename = filenameStrRef;
1557 MaybeAddSystemRootToFilename(Filename);
1558 const FileEntry *File = FileMgr.getFile(Filename);
1559 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1560 OriginalDir != CurrentDir) {
1561 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1562 OriginalDir,
1563 CurrentDir);
1564 if (!resolved.empty())
1565 File = FileMgr.getFile(resolved);
1566 }
1567
1568 return File;
1569}
1570
Douglas Gregore650c8c2009-07-07 00:12:59 +00001571/// \brief If we are loading a relocatable PCH file, and the filename is
1572/// not an absolute path, add the system root to the beginning of the file
1573/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001574void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001575 // If this is not a relocatable PCH file, there's nothing to do.
1576 if (!RelocatablePCH)
1577 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Michael J. Spencer256053b2010-12-17 21:22:22 +00001579 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregore650c8c2009-07-07 00:12:59 +00001580 return;
1581
Douglas Gregor832d6202011-07-22 16:35:34 +00001582 if (isysroot.empty()) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001583 // If no system root was given, default to '/'
1584 Filename.insert(Filename.begin(), '/');
1585 return;
1586 }
Mike Stump1eb44332009-09-09 15:08:12 +00001587
Douglas Gregor832d6202011-07-22 16:35:34 +00001588 unsigned Length = isysroot.size();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001589 if (isysroot[Length - 1] != '/')
1590 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001591
Douglas Gregor832d6202011-07-22 16:35:34 +00001592 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregore650c8c2009-07-07 00:12:59 +00001593}
1594
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001595ASTReader::ASTReadResult
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001596ASTReader::ReadASTBlock(Module &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001597 llvm::BitstreamCursor &Stream = F.Stream;
1598
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001599 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001600 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001601 return Failure;
1602 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001603
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001604 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001605 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001606 while (!Stream.AtEndOfStream()) {
1607 unsigned Code = Stream.ReadCode();
1608 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001609 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001610 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001611 return Failure;
1612 }
Chris Lattner7356a312009-04-11 21:15:38 +00001613
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001614 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001615 }
1616
1617 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1618 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001619 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001620 // We lazily load the decls block, but we want to set up the
1621 // DeclsCursor cursor to point into it. Clone our current bitcode
1622 // cursor to it, enter the block and read the abbrevs in that block.
1623 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001624 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001625 if (Stream.SkipBlock() || // Skip with the main cursor.
1626 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001627 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001628 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001629 return Failure;
1630 }
1631 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001632
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001633 case DECL_UPDATES_BLOCK_ID:
1634 if (Stream.SkipBlock()) {
1635 Error("malformed block record in AST file");
1636 return Failure;
1637 }
1638 break;
1639
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001640 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001641 F.MacroCursor = Stream;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001642 if (!PP.getExternalSource())
1643 PP.setExternalSource(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00001644
Douglas Gregorecdcb882010-10-20 22:00:55 +00001645 if (Stream.SkipBlock() ||
1646 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001647 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001648 return Failure;
1649 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001650 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001651 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001652
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001653 case PREPROCESSOR_DETAIL_BLOCK_ID:
1654 F.PreprocessorDetailCursor = Stream;
1655 if (Stream.SkipBlock() ||
1656 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1657 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1658 Error("malformed preprocessor detail record in AST file");
1659 return Failure;
1660 }
1661 F.PreprocessorDetailStartOffset
1662 = F.PreprocessorDetailCursor.GetCurrentBitNo();
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001663
1664 if (!PP.getPreprocessingRecord())
1665 PP.createPreprocessingRecord(true);
1666 if (!PP.getPreprocessingRecord()->getExternalSource())
1667 PP.getPreprocessingRecord()->SetExternalSource(*this);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001668 break;
1669
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001670 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001671 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001672 case Success:
1673 break;
1674
1675 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001676 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001677 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001678
1679 case IgnorePCH:
1680 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001681 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001682 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001683 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001684 continue;
1685 }
1686
1687 if (Code == llvm::bitc::DEFINE_ABBREV) {
1688 Stream.ReadAbbrevRecord();
1689 continue;
1690 }
1691
1692 // Read and process a record.
1693 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001694 const char *BlobStart = 0;
1695 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001696 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001697 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001698 default: // Default behavior: ignore.
1699 break;
1700
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001701 case METADATA: {
1702 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1703 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001704 : diag::warn_pch_version_too_new);
1705 return IgnorePCH;
1706 }
1707
1708 RelocatablePCH = Record[4];
1709 if (Listener) {
1710 std::string TargetTriple(BlobStart, BlobLen);
1711 if (Listener->ReadTargetTriple(TargetTriple))
1712 return IgnorePCH;
1713 }
1714 break;
1715 }
1716
Douglas Gregore95b9192011-08-17 21:07:30 +00001717 case IMPORTS: {
1718 // Load each of the imported PCH files.
1719 unsigned Idx = 0, N = Record.size();
1720 while (Idx < N) {
1721 // Read information about the AST file.
1722 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1723 unsigned Length = Record[Idx++];
1724 llvm::SmallString<128> ImportedFile(Record.begin() + Idx,
1725 Record.begin() + Idx + Length);
1726 Idx += Length;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001727
Douglas Gregore95b9192011-08-17 21:07:30 +00001728 // Load the AST file.
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001729 switch(ReadASTCore(ImportedFile, ImportedKind, &F)) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001730 case Failure: return Failure;
1731 // If we have to ignore the dependency, we'll have to ignore this too.
1732 case IgnorePCH: return IgnorePCH;
1733 case Success: break;
1734 }
1735 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001736 break;
1737 }
1738
Douglas Gregora119da02011-08-02 16:26:37 +00001739 case TYPE_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001740 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001741 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001742 return Failure;
1743 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001744 F.TypeOffsets = (const uint32_t *)BlobStart;
1745 F.LocalNumTypes = Record[0];
Douglas Gregore3605012011-08-02 18:32:54 +00001746 unsigned LocalBaseTypeIndex = Record[1];
1747 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor1e849b62011-07-29 00:21:44 +00001748
Douglas Gregora119da02011-08-02 16:26:37 +00001749 if (F.LocalNumTypes > 0) {
1750 // Introduce the global -> local mapping for types within this module.
1751 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
1752
1753 // Introduce the local -> global mapping for types within this module.
Douglas Gregore3605012011-08-02 18:32:54 +00001754 F.TypeRemap.insert(std::make_pair(LocalBaseTypeIndex,
1755 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregora119da02011-08-02 16:26:37 +00001756
1757 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
1758 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001759 break;
Douglas Gregora119da02011-08-02 16:26:37 +00001760 }
1761
Douglas Gregor496c7092011-08-03 15:48:04 +00001762 case DECL_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001763 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001764 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001765 return Failure;
1766 }
Argyrios Kyrtzidis9d31fa72011-10-27 18:47:35 +00001767 F.DeclOffsets = (const DeclOffset *)BlobStart;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001768 F.LocalNumDecls = Record[0];
Douglas Gregor496c7092011-08-03 15:48:04 +00001769 unsigned LocalBaseDeclID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001770 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor96e973f2011-07-20 00:27:43 +00001771
Douglas Gregor496c7092011-08-03 15:48:04 +00001772 if (F.LocalNumDecls > 0) {
1773 // Introduce the global -> local mapping for declarations within this
1774 // module.
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001775 GlobalDeclMap.insert(
1776 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregor496c7092011-08-03 15:48:04 +00001777
1778 // Introduce the local -> global mapping for declarations within this
1779 // module.
1780 F.DeclRemap.insert(std::make_pair(LocalBaseDeclID,
1781 F.BaseDeclID - LocalBaseDeclID));
1782
1783 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
1784 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001785 break;
Douglas Gregor496c7092011-08-03 15:48:04 +00001786 }
1787
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001788 case TU_UPDATE_LEXICAL: {
Douglas Gregor35942772011-09-09 21:34:22 +00001789 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001790 DeclContextInfo &Info = F.DeclContextInfos[TU];
1791 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
1792 Info.NumLexicalDecls
1793 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregor35942772011-09-09 21:34:22 +00001794 TU->setHasExternalLexicalStorage(true);
Sebastian Redld692af72010-07-27 18:24:41 +00001795 break;
1796 }
1797
Sebastian Redle1dde812010-08-24 00:50:04 +00001798 case UPDATE_VISIBLE: {
Douglas Gregor496c7092011-08-03 15:48:04 +00001799 unsigned Idx = 0;
1800 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Sebastian Redle1dde812010-08-24 00:50:04 +00001801 void *Table = ASTDeclContextNameLookupTable::Create(
Douglas Gregor496c7092011-08-03 15:48:04 +00001802 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redle1dde812010-08-24 00:50:04 +00001803 (const unsigned char *)BlobStart,
Douglas Gregor393f2492011-07-22 00:38:23 +00001804 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregor35942772011-09-09 21:34:22 +00001805 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
1806 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001807 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001808 TU->setHasExternalVisibleStorage(true);
Sebastian Redle1dde812010-08-24 00:50:04 +00001809 } else
Douglas Gregor496c7092011-08-03 15:48:04 +00001810 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redle1dde812010-08-24 00:50:04 +00001811 break;
1812 }
1813
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001814 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001815 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
Douglas Gregor496c7092011-08-03 15:48:04 +00001816 for (unsigned i = 0, e = Record.size(); i < e; /* in loop */) {
1817 DeclID First = ReadDeclID(F, Record, i);
1818 DeclID Latest = ReadDeclID(F, Record, i);
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001819 FirstLatestDeclIDs[First] = Latest;
1820 }
1821 break;
1822 }
1823
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001824 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001825 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001826 return IgnorePCH;
1827 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001828
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001829 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001830 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001831 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001832 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001833 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001834 (const unsigned char *)F.IdentifierTableData + Record[0],
1835 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00001836 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001837
1838 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001839 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001840 break;
1841
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001842 case IDENTIFIER_OFFSET: {
Sebastian Redl2da08f92010-07-19 22:28:42 +00001843 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001844 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001845 return Failure;
1846 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001847 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1848 F.LocalNumIdentifiers = Record[0];
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001849 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001850 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor67268d02011-07-20 00:59:32 +00001851
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001852 if (F.LocalNumIdentifiers > 0) {
1853 // Introduce the global -> local mapping for identifiers within this
1854 // module.
1855 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
1856 &F));
1857
1858 // Introduce the local -> global mapping for identifiers within this
1859 // module.
1860 F.IdentifierRemap.insert(
1861 std::make_pair(LocalBaseIdentifierID,
1862 F.BaseIdentifierID - LocalBaseIdentifierID));
1863
1864 IdentifiersLoaded.resize(IdentifiersLoaded.size()
1865 + F.LocalNumIdentifiers);
1866 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001867 break;
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001868 }
1869
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001870 case EXTERNAL_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001871 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1872 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorfdd01722009-04-14 00:24:19 +00001873 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001874
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001875 case SPECIAL_TYPES:
Douglas Gregor393f2492011-07-22 00:38:23 +00001876 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1877 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregorad1de002009-04-18 05:55:16 +00001878 break;
1879
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001880 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001881 TotalNumStatements += Record[0];
1882 TotalNumMacros += Record[1];
1883 TotalLexicalDeclContexts += Record[2];
1884 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001885 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001886
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001887 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001888 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1889 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001890 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001891
Sean Huntebcbe1d2011-05-04 23:29:54 +00001892 case DELEGATING_CTORS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001893 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1894 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00001895 break;
1896
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001897 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor31e37b22011-07-28 18:09:57 +00001898 if (Record.size() % 4 != 0) {
1899 Error("invalid weak identifiers record");
1900 return Failure;
1901 }
1902
1903 // FIXME: Ignore weak undeclared identifiers from non-original PCH
1904 // files. This isn't the way to do it :)
1905 WeakUndeclaredIdentifiers.clear();
1906
1907 // Translate the weak, undeclared identifiers into global IDs.
1908 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
1909 WeakUndeclaredIdentifiers.push_back(
1910 getGlobalIdentifierID(F, Record[I++]));
1911 WeakUndeclaredIdentifiers.push_back(
1912 getGlobalIdentifierID(F, Record[I++]));
1913 WeakUndeclaredIdentifiers.push_back(
1914 ReadSourceLocation(F, Record, I).getRawEncoding());
1915 WeakUndeclaredIdentifiers.push_back(Record[I++]);
1916 }
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00001917 break;
1918
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001919 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001920 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1921 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor14c22f22009-04-22 22:18:58 +00001922 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001923
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00001924 case SELECTOR_OFFSETS: {
Sebastian Redl059612d2010-08-03 21:58:15 +00001925 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00001926 F.LocalNumSelectors = Record[0];
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00001927 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001928 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor96958cb2011-07-20 01:10:58 +00001929
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00001930 if (F.LocalNumSelectors > 0) {
1931 // Introduce the global -> local mapping for selectors within this
1932 // module.
1933 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
1934
1935 // Introduce the local -> global mapping for selectors within this
1936 // module.
1937 F.SelectorRemap.insert(std::make_pair(LocalBaseSelectorID,
1938 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor83941df2009-04-25 17:48:32 +00001939
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00001940 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
1941 }
1942 break;
1943 }
1944
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001945 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00001946 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00001947 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00001948 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001949 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00001950 F.SelectorLookupTableData + Record[0],
1951 F.SelectorLookupTableData,
Douglas Gregor409448c2011-07-21 22:35:25 +00001952 ASTSelectorLookupTrait(*this, F));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00001953 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001954 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001955
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00001956 case REFERENCED_SELECTOR_POOL:
Douglas Gregor8451ec72011-07-28 14:41:43 +00001957 if (!Record.empty()) {
1958 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
1959 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
1960 Record[Idx++]));
1961 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
1962 getRawEncoding());
1963 }
1964 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00001965 break;
1966
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001967 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001968 if (!Record.empty() && Listener)
1969 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001970 break;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001971
1972 case FILE_SORTED_DECLS:
1973 F.FileSortedDecls = (const DeclID *)BlobStart;
1974 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001975
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001976 case SOURCE_LOCATION_OFFSETS: {
1977 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001978 F.LocalNumSLocEntries = Record[0];
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001979 unsigned SLocSpaceSize = Record[1];
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001980 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001981 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
1982 SLocSpaceSize);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001983 // Make our entry in the range map. BaseID is negative and growing, so
1984 // we invert it. Because we invert it, though, we need the other end of
1985 // the range.
1986 unsigned RangeStart =
1987 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
1988 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
1989 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
1990
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001991 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
1992 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
1993 GlobalSLocOffsetMap.insert(
1994 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
1995 - SLocSpaceSize,&F));
1996
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001997 // Initialize the remapping table.
1998 // Invalid stays invalid.
1999 F.SLocRemap.insert(std::make_pair(0U, 0));
2000 // This module. Base was 2 when being compiled.
2001 F.SLocRemap.insert(std::make_pair(2U,
2002 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002003
2004 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002005 break;
2006 }
2007
Douglas Gregor5d51a1d2011-08-01 16:01:55 +00002008 case MODULE_OFFSET_MAP: {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002009 // Additional remapping information.
2010 const unsigned char *Data = (const unsigned char*)BlobStart;
2011 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregorf33740e2011-08-02 10:56:51 +00002012
2013 // Continuous range maps we may be updating in our module.
2014 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002015 ContinuousRangeMap<uint32_t, int, 2>::Builder
2016 IdentifierRemap(F.IdentifierRemap);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002017 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002018 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2019 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002020 SelectorRemap(F.SelectorRemap);
Douglas Gregor496c7092011-08-03 15:48:04 +00002021 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregora119da02011-08-02 16:26:37 +00002022 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2023
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002024 while(Data < DataEnd) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002025 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002026 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002027 Data += Len;
Jonathan D. Turner48d2c3f2011-07-26 18:21:30 +00002028 Module *OM = ModuleMgr.lookup(Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002029 if (!OM) {
2030 Error("SourceLocation remap refers to unknown module");
2031 return Failure;
2032 }
Douglas Gregorf33740e2011-08-02 10:56:51 +00002033
2034 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2035 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2036 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002037 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2038 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora119da02011-08-02 16:26:37 +00002039 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002040
2041 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2042 SLocRemap.insert(std::make_pair(SLocOffset,
2043 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002044 IdentifierRemap.insert(
2045 std::make_pair(IdentifierIDOffset,
2046 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002047 PreprocessedEntityRemap.insert(
2048 std::make_pair(PreprocessedEntityIDOffset,
2049 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002050 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2051 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregor496c7092011-08-03 15:48:04 +00002052 DeclRemap.insert(std::make_pair(DeclIDOffset,
2053 OM->BaseDeclID - DeclIDOffset));
2054
Douglas Gregora119da02011-08-02 16:26:37 +00002055 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregore3605012011-08-02 18:32:54 +00002056 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002057 }
2058 break;
2059 }
2060
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002061 case SOURCE_MANAGER_LINE_TABLE:
2062 if (ParseLineTable(F, Record))
2063 return Failure;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002064 break;
2065
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00002066 case FILE_SOURCE_LOCATION_OFFSETS:
2067 F.SLocFileOffsets = (const uint32_t *)BlobStart;
2068 F.LocalNumSLocFileEntries = Record[0];
2069 break;
2070
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002071 case SOURCE_LOCATION_PRELOADS: {
2072 // Need to transform from the local view (1-based IDs) to the global view,
2073 // which is based off F.SLocEntryBaseID.
Douglas Gregorf249bf32011-08-25 21:09:44 +00002074 if (!F.PreloadSLocEntries.empty()) {
2075 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2076 return Failure;
2077 }
2078
2079 F.PreloadSLocEntries.swap(Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002080 break;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002081 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002082
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002083 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002084 if (!DisableStatCache) {
2085 ASTStatCache *MyStatCache =
2086 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2087 (const unsigned char *)BlobStart,
2088 NumStatHits, NumStatMisses);
2089 FileMgr.addStatCache(MyStatCache);
2090 F.StatCache = MyStatCache;
2091 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002092 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002093 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002094
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002095 case EXT_VECTOR_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002096 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2097 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorb81c1702009-04-27 20:06:05 +00002098 break;
2099
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002100 case VTABLE_USES:
Douglas Gregordfe65432011-07-28 19:11:31 +00002101 if (Record.size() % 3 != 0) {
2102 Error("Invalid VTABLE_USES record");
2103 return Failure;
2104 }
2105
Sebastian Redl40566802010-08-05 18:21:25 +00002106 // Later tables overwrite earlier ones.
Douglas Gregordfe65432011-07-28 19:11:31 +00002107 // FIXME: Modules will have some trouble with this. This is clearly not
2108 // the right way to do this.
Douglas Gregor409448c2011-07-21 22:35:25 +00002109 VTableUses.clear();
Douglas Gregordfe65432011-07-28 19:11:31 +00002110
2111 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2112 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2113 VTableUses.push_back(
2114 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2115 VTableUses.push_back(Record[Idx++]);
2116 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002117 break;
2118
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002119 case DYNAMIC_CLASSES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002120 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2121 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002122 break;
2123
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002124 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorf2abb522011-07-28 19:26:52 +00002125 if (PendingInstantiations.size() % 2 != 0) {
2126 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2127 return Failure;
2128 }
2129
2130 // Later lists of pending instantiations overwrite earlier ones.
2131 // FIXME: This is most certainly wrong for modules.
2132 PendingInstantiations.clear();
2133 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2134 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2135 PendingInstantiations.push_back(
2136 ReadSourceLocation(F, Record, I).getRawEncoding());
2137 }
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002138 break;
2139
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002140 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002141 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002142 // FIXME: Modules will have some trouble with this.
2143 SemaDeclRefs.clear();
2144 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2145 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002146 break;
2147
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002148 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002149 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002150 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002151 ActualOriginalFileName.assign(BlobStart, BlobLen);
2152 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002153 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002154 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002155
Douglas Gregor31d375f2011-05-06 21:43:30 +00002156 case ORIGINAL_FILE_ID:
2157 OriginalFileID = FileID::get(Record[0]);
2158 break;
2159
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002160 case ORIGINAL_PCH_DIR:
2161 // The primary AST will be the last to get here, so it will be the one
2162 // that's used.
2163 OriginalDir.assign(BlobStart, BlobLen);
2164 break;
2165
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002166 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002167 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002168 StringRef ASTBranch(BlobStart, BlobLen);
2169 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002170 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002171 return IgnorePCH;
2172 }
2173 break;
2174 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002175
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002176 case PPD_ENTITIES_OFFSETS: {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002177 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart;
2178 assert(BlobLen % sizeof(PPEntityOffset) == 0);
2179 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002180
2181 unsigned LocalBasePreprocessedEntityID = Record[0];
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002182
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002183 unsigned StartingID;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002184 if (!PP.getPreprocessingRecord())
2185 PP.createPreprocessingRecord(true);
2186 if (!PP.getPreprocessingRecord()->getExternalSource())
2187 PP.getPreprocessingRecord()->SetExternalSource(*this);
2188 StartingID
2189 = PP.getPreprocessingRecord()
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002190 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Douglas Gregor9827a802011-07-29 00:56:45 +00002191 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002192
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002193 if (F.NumPreprocessedEntities > 0) {
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002194 // Introduce the global -> local mapping for preprocessed entities in
2195 // this module.
2196 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2197
2198 // Introduce the local -> global mapping for preprocessed entities in
2199 // this module.
2200 F.PreprocessedEntityRemap.insert(
2201 std::make_pair(LocalBasePreprocessedEntityID,
2202 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2203 }
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002204
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002205 break;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002206 }
2207
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002208 case DECL_UPDATE_OFFSETS: {
2209 if (Record.size() % 2 != 0) {
2210 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2211 return Failure;
2212 }
2213 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregor496c7092011-08-03 15:48:04 +00002214 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2215 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002216 break;
2217 }
2218
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002219 case DECL_REPLACEMENTS: {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002220 if (Record.size() % 3 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002221 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002222 return Failure;
2223 }
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002224 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
Douglas Gregor496c7092011-08-03 15:48:04 +00002225 ReplacedDecls[getGlobalDeclID(F, Record[I])]
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002226 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002227 break;
2228 }
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002229
2230 case OBJC_CHAINED_CATEGORIES: {
2231 if (Record.size() % 3 != 0) {
2232 Error("invalid OBJC_CHAINED_CATEGORIES block in AST file");
2233 return Failure;
2234 }
2235 for (unsigned I = 0, N = Record.size(); I != N; I += 3) {
2236 serialization::GlobalDeclID GlobID = getGlobalDeclID(F, Record[I]);
2237 F.ChainedObjCCategories[GlobID] = std::make_pair(Record[I+1],
2238 Record[I+2]);
2239 ObjCChainedCategoriesInterfaces.insert(GlobID);
2240 }
2241 break;
2242 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002243
2244 case CXX_BASE_SPECIFIER_OFFSETS: {
2245 if (F.LocalNumCXXBaseSpecifiers != 0) {
2246 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2247 return Failure;
2248 }
2249
2250 F.LocalNumCXXBaseSpecifiers = Record[0];
2251 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner1da90142011-07-21 21:15:19 +00002252 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002253 break;
2254 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002255
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002256 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002257 if (Record.size() % 2 != 0) {
2258 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2259 return Failure;
2260 }
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002261
2262 if (F.PragmaDiagMappings.empty())
2263 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002264 else
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002265 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2266 Record.begin(), Record.end());
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002267 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002268
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002269 case CUDA_SPECIAL_DECL_REFS:
2270 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002271 // FIXME: Modules will have trouble with this.
2272 CUDASpecialDeclRefs.clear();
2273 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2274 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002275 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002276
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002277 case HEADER_SEARCH_TABLE: {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002278 F.HeaderFileInfoTableData = BlobStart;
2279 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002280 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002281 if (Record[0]) {
2282 F.HeaderFileInfoTable
2283 = HeaderFileInfoLookupTable::Create(
2284 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002285 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregor95eab172011-07-28 20:55:49 +00002286 HeaderFileInfoTrait(*this, F,
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002287 &PP.getHeaderSearchInfo(),
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002288 BlobStart + Record[2]));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002289
2290 PP.getHeaderSearchInfo().SetExternalSource(this);
2291 if (!PP.getHeaderSearchInfo().getExternalLookup())
2292 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002293 }
2294 break;
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002295 }
2296
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002297 case FP_PRAGMA_OPTIONS:
2298 // Later tables overwrite earlier ones.
2299 FPPragmaOptions.swap(Record);
2300 break;
2301
2302 case OPENCL_EXTENSIONS:
2303 // Later tables overwrite earlier ones.
2304 OpenCLExtensions.swap(Record);
2305 break;
Sean Huntebcbe1d2011-05-04 23:29:54 +00002306
2307 case TENTATIVE_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002308 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2309 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002310 break;
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002311
2312 case KNOWN_NAMESPACES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002313 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2314 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002315 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002316 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002317 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002318 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002319 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002320}
2321
Douglas Gregorc69a2922011-08-25 20:58:51 +00002322ASTReader::ASTReadResult ASTReader::validateFileEntries(Module &M) {
2323 llvm::BitstreamCursor &SLocEntryCursor = M.SLocEntryCursor;
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002324
Douglas Gregorc69a2922011-08-25 20:58:51 +00002325 for (unsigned i = 0, e = M.LocalNumSLocFileEntries; i != e; ++i) {
2326 SLocEntryCursor.JumpToBit(M.SLocFileOffsets[i]);
2327 unsigned Code = SLocEntryCursor.ReadCode();
2328 if (Code == llvm::bitc::END_BLOCK ||
2329 Code == llvm::bitc::ENTER_SUBBLOCK ||
2330 Code == llvm::bitc::DEFINE_ABBREV) {
2331 Error("incorrectly-formatted source location entry in AST file");
2332 return Failure;
2333 }
2334
2335 RecordData Record;
2336 const char *BlobStart;
2337 unsigned BlobLen;
2338 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2339 default:
2340 Error("incorrectly-formatted source location entry in AST file");
2341 return Failure;
2342
2343 case SM_SLOC_FILE_ENTRY: {
2344 StringRef Filename(BlobStart, BlobLen);
2345 const FileEntry *File = getFileEntry(Filename);
2346
2347 if (File == 0) {
2348 std::string ErrorStr = "could not find file '";
2349 ErrorStr += Filename;
2350 ErrorStr += "' referenced by AST file";
2351 Error(ErrorStr.c_str());
2352 return IgnorePCH;
2353 }
2354
2355 if (Record.size() < 6) {
2356 Error("source location entry is incorrect");
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002357 return Failure;
2358 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002359
Douglas Gregorc69a2922011-08-25 20:58:51 +00002360 // The stat info from the FileEntry came from the cached stat
2361 // info of the PCH, so we cannot trust it.
2362 struct stat StatBuf;
2363 if (::stat(File->getName(), &StatBuf) != 0) {
2364 StatBuf.st_size = File->getSize();
2365 StatBuf.st_mtime = File->getModificationTime();
2366 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002367
Douglas Gregorc69a2922011-08-25 20:58:51 +00002368 if (((off_t)Record[4] != StatBuf.st_size
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002369#if !defined(LLVM_ON_WIN32)
Douglas Gregorc69a2922011-08-25 20:58:51 +00002370 // In our regression testing, the Windows file system seems to
2371 // have inconsistent modification times that sometimes
2372 // erroneously trigger this error-handling path.
2373 || (time_t)Record[5] != StatBuf.st_mtime
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002374#endif
Douglas Gregorc69a2922011-08-25 20:58:51 +00002375 )) {
2376 Error(diag::err_fe_pch_file_modified, Filename);
2377 return IgnorePCH;
2378 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002379
Douglas Gregorc69a2922011-08-25 20:58:51 +00002380 break;
2381 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002382 }
2383 }
2384
2385 return Success;
2386}
2387
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002388ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregor72a9ae12011-07-22 16:00:58 +00002389 ModuleKind Type) {
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002390 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002391 case Failure: return Failure;
2392 case IgnorePCH: return IgnorePCH;
2393 case Success: break;
2394 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002395
2396 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002397
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002398 // Check the predefines buffers.
Argyrios Kyrtzidis26d43cd2011-09-12 18:09:38 +00002399 if (!DisableValidation && Type != MK_Module && Type != MK_Preamble &&
2400 // FIXME: CheckPredefinesBuffers also sets the SuggestedPredefines;
2401 // if DisableValidation is true, defines that were set on command-line
2402 // but not in the PCH file will not be added to SuggestedPredefines.
2403 CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002404 return IgnorePCH;
2405
Douglas Gregoreee242f2011-10-27 09:33:13 +00002406 // Mark all of the identifiers in the identifier table as being out of date,
2407 // so that various accessors know to check the loaded modules when the
2408 // identifier is used.
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002409 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2410 IdEnd = PP.getIdentifierTable().end();
2411 Id != IdEnd; ++Id)
Douglas Gregoreee242f2011-10-27 09:33:13 +00002412 Id->second->setOutOfDate(true);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002413
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 Gregor72cd7a02011-11-11 19:13:12 +00002607 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002608 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
Douglas Gregor72cd7a02011-11-11 19:13:12 +00002628 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002629 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 }
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00002663
2664 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
2665 QualType Ucontext_tType = GetType(Ucontext_t);
2666 if (Ucontext_tType.isNull()) {
2667 Error("ucontext_t type is NULL");
2668 return;
2669 }
2670
2671 if (!Context.ucontext_tDecl) {
2672 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
2673 Context.setucontext_tDecl(Typedef->getDecl());
2674 else {
2675 const TagType *Tag = Ucontext_tType->getAs<TagType>();
2676 assert(Tag && "Invalid ucontext_t type in AST file");
2677 Context.setucontext_tDecl(Tag->getDecl());
2678 }
2679 }
2680 }
Douglas Gregor5f957282011-08-11 22:18:49 +00002681 }
2682
Douglas Gregor35942772011-09-09 21:34:22 +00002683 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002684
2685 // If there were any CUDA special declarations, deserialize them.
2686 if (!CUDASpecialDeclRefs.empty()) {
2687 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
Douglas Gregor35942772011-09-09 21:34:22 +00002688 Context.setcudaConfigureCallDecl(
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002689 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2690 }
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002691}
2692
Douglas Gregorb64c1932009-05-12 01:31:05 +00002693/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002694/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002695/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002696std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002697 FileManager &FileMgr,
David Blaikied6471f72011-09-25 23:23:43 +00002698 DiagnosticsEngine &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002699 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002700 std::string ErrStr;
2701 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00002702 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002703 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002704 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002705 return std::string();
2706 }
2707
2708 // Initialize the stream
2709 llvm::BitstreamReader StreamFile;
2710 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002711 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002712 (const unsigned char *)Buffer->getBufferEnd());
2713 Stream.init(StreamFile);
2714
2715 // Sniff for the signature.
2716 if (Stream.Read(8) != 'C' ||
2717 Stream.Read(8) != 'P' ||
2718 Stream.Read(8) != 'C' ||
2719 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002720 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002721 return std::string();
2722 }
2723
2724 RecordData Record;
2725 while (!Stream.AtEndOfStream()) {
2726 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002727
Douglas Gregorb64c1932009-05-12 01:31:05 +00002728 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2729 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002730
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002731 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002732 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002733 case AST_BLOCK_ID:
2734 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002735 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002736 return std::string();
2737 }
2738 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002739
Douglas Gregorb64c1932009-05-12 01:31:05 +00002740 default:
2741 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002742 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002743 return std::string();
2744 }
2745 break;
2746 }
2747 continue;
2748 }
2749
2750 if (Code == llvm::bitc::END_BLOCK) {
2751 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002752 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002753 return std::string();
2754 }
2755 continue;
2756 }
2757
2758 if (Code == llvm::bitc::DEFINE_ABBREV) {
2759 Stream.ReadAbbrevRecord();
2760 continue;
2761 }
2762
2763 Record.clear();
2764 const char *BlobStart = 0;
2765 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002766 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002767 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00002768 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00002769 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00002770
2771 return std::string();
2772}
2773
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002774/// \brief Parse the record that corresponds to a LangOptions data
2775/// structure.
2776///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002777/// This routine parses the language options from the AST file and then gives
2778/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002779///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002780/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002781bool ASTReader::ParseLanguageOptions(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002782 const SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002783 if (Listener) {
2784 LangOptions LangOpts;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002785 unsigned Idx = 0;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00002786#define LANGOPT(Name, Bits, Default, Description) \
2787 LangOpts.Name = Record[Idx++];
2788#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
2789 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
2790#include "clang/Basic/LangOptions.def"
2791
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00002792 unsigned Length = Record[Idx++];
2793 LangOpts.CurrentModule.assign(Record.begin() + Idx,
2794 Record.begin() + Idx + Length);
2795 Idx += Length;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002796 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002797 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002798
2799 return false;
2800}
2801
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00002802std::pair<Module *, unsigned>
2803ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002804 GlobalPreprocessedEntityMapType::iterator
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00002805 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002806 assert(I != GlobalPreprocessedEntityMap.end() &&
2807 "Corrupted global preprocessed entity map");
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00002808 Module *M = I->second;
2809 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
2810 return std::make_pair(M, LocalIndex);
2811}
2812
2813PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
2814 PreprocessedEntityID PPID = Index+1;
2815 std::pair<Module *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
2816 Module &M = *PPInfo.first;
2817 unsigned LocalIndex = PPInfo.second;
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002818 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002819
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002820 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002821 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002822
2823 unsigned Code = M.PreprocessorDetailCursor.ReadCode();
2824 switch (Code) {
2825 case llvm::bitc::END_BLOCK:
2826 return 0;
2827
2828 case llvm::bitc::ENTER_SUBBLOCK:
2829 Error("unexpected subblock record in preprocessor detail block");
2830 return 0;
2831
2832 case llvm::bitc::DEFINE_ABBREV:
2833 Error("unexpected abbrevation record in preprocessor detail block");
2834 return 0;
2835
2836 default:
2837 break;
2838 }
2839
2840 if (!PP.getPreprocessingRecord()) {
2841 Error("no preprocessing record");
2842 return 0;
2843 }
2844
2845 // Read the record.
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002846 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
2847 ReadSourceLocation(M, PPOffs.End));
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002848 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
2849 const char *BlobStart = 0;
2850 unsigned BlobLen = 0;
2851 RecordData Record;
2852 PreprocessorDetailRecordTypes RecType =
2853 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord(
2854 Code, Record, BlobStart, BlobLen);
2855 switch (RecType) {
2856 case PPD_MACRO_EXPANSION: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002857 bool isBuiltin = Record[0];
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002858 IdentifierInfo *Name = 0;
2859 MacroDefinition *Def = 0;
2860 if (isBuiltin)
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002861 Name = getLocalIdentifier(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002862 else {
2863 PreprocessedEntityID
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002864 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002865 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
2866 }
2867
2868 MacroExpansion *ME;
2869 if (isBuiltin)
2870 ME = new (PPRec) MacroExpansion(Name, Range);
2871 else
2872 ME = new (PPRec) MacroExpansion(Def, Range);
2873
2874 return ME;
2875 }
2876
2877 case PPD_MACRO_DEFINITION: {
2878 // Decode the identifier info and then check again; if the macro is
2879 // still defined and associated with the identifier,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002880 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002881 MacroDefinition *MD
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002882 = new (PPRec) MacroDefinition(II, Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002883
2884 if (DeserializationListener)
2885 DeserializationListener->MacroDefinitionRead(PPID, MD);
2886
2887 return MD;
2888 }
2889
2890 case PPD_INCLUSION_DIRECTIVE: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002891 const char *FullFileNameStart = BlobStart + Record[0];
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002892 const FileEntry *File
2893 = PP.getFileManager().getFile(StringRef(FullFileNameStart,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002894 BlobLen - Record[0]));
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002895
2896 // FIXME: Stable encoding
2897 InclusionDirective::InclusionKind Kind
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002898 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002899 InclusionDirective *ID
2900 = new (PPRec) InclusionDirective(PPRec, Kind,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002901 StringRef(BlobStart, Record[0]),
2902 Record[1],
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002903 File,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002904 Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002905 return ID;
2906 }
2907 }
2908
2909 Error("invalid offset in preprocessor detail block");
2910 return 0;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002911}
2912
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002913/// \brief \arg SLocMapI points at a chunk of a module that contains no
2914/// preprocessed entities or the entities it contains are not the ones we are
2915/// looking for. Find the next module that contains entities and return the ID
2916/// of the first entry.
2917PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
2918 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
2919 ++SLocMapI;
2920 for (GlobalSLocOffsetMapType::const_iterator
2921 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
2922 Module &M = *SLocMapI->second;
2923 if (M.NumPreprocessedEntities)
2924 return getGlobalPreprocessedEntityID(M, M.BasePreprocessedEntityID);
2925 }
2926
2927 return getTotalNumPreprocessedEntities();
2928}
2929
2930namespace {
2931
2932template <unsigned PPEntityOffset::*PPLoc>
2933struct PPEntityComp {
2934 const ASTReader &Reader;
2935 Module &M;
2936
2937 PPEntityComp(const ASTReader &Reader, Module &M) : Reader(Reader), M(M) { }
2938
Benjamin Kramer88df1252011-09-21 06:42:26 +00002939 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
2940 SourceLocation LHS = getLoc(L);
2941 SourceLocation RHS = getLoc(R);
2942 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
2943 }
2944
2945 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002946 SourceLocation LHS = getLoc(L);
2947 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
2948 }
2949
Benjamin Kramer88df1252011-09-21 06:42:26 +00002950 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002951 SourceLocation RHS = getLoc(R);
2952 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
2953 }
2954
2955 SourceLocation getLoc(const PPEntityOffset &PPE) const {
2956 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
2957 }
2958};
2959
2960}
2961
2962/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
2963PreprocessedEntityID
2964ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
2965 if (SourceMgr.isLocalSourceLocation(BLoc))
2966 return getTotalNumPreprocessedEntities();
2967
2968 GlobalSLocOffsetMapType::const_iterator
2969 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
2970 BLoc.getOffset());
2971 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
2972 "Corrupted global sloc offset map");
2973
2974 if (SLocMapI->second->NumPreprocessedEntities == 0)
2975 return findNextPreprocessedEntity(SLocMapI);
2976
2977 Module &M = *SLocMapI->second;
2978 typedef const PPEntityOffset *pp_iterator;
2979 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
2980 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
Argyrios Kyrtzidis4cd06342011-09-22 21:17:02 +00002981
2982 size_t Count = M.NumPreprocessedEntities;
2983 size_t Half;
2984 pp_iterator First = pp_begin;
2985 pp_iterator PPI;
2986
2987 // Do a binary search manually instead of using std::lower_bound because
2988 // The end locations of entities may be unordered (when a macro expansion
2989 // is inside another macro argument), but for this case it is not important
2990 // whether we get the first macro expansion or its containing macro.
2991 while (Count > 0) {
2992 Half = Count/2;
2993 PPI = First;
2994 std::advance(PPI, Half);
2995 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
2996 BLoc)){
2997 First = PPI;
2998 ++First;
2999 Count = Count - Half - 1;
3000 } else
3001 Count = Half;
3002 }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003003
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 the first preprocessed entity ID that begins after \arg ELoc.
3012PreprocessedEntityID
3013ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
3014 if (SourceMgr.isLocalSourceLocation(ELoc))
3015 return getTotalNumPreprocessedEntities();
3016
3017 GlobalSLocOffsetMapType::const_iterator
3018 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3019 ELoc.getOffset());
3020 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3021 "Corrupted global sloc offset map");
3022
3023 if (SLocMapI->second->NumPreprocessedEntities == 0)
3024 return findNextPreprocessedEntity(SLocMapI);
3025
3026 Module &M = *SLocMapI->second;
3027 typedef const PPEntityOffset *pp_iterator;
3028 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3029 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
3030 pp_iterator PPI =
3031 std::upper_bound(pp_begin, pp_end, ELoc,
3032 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
3033
3034 if (PPI == pp_end)
3035 return findNextPreprocessedEntity(SLocMapI);
3036
3037 return getGlobalPreprocessedEntityID(M,
3038 M.BasePreprocessedEntityID + (PPI - pp_begin));
3039}
3040
3041/// \brief Returns a pair of [Begin, End) indices of preallocated
3042/// preprocessed entities that \arg Range encompasses.
3043std::pair<unsigned, unsigned>
3044 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
3045 if (Range.isInvalid())
3046 return std::make_pair(0,0);
3047 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
3048
3049 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
3050 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
3051 return std::make_pair(BeginID, EndID);
3052}
3053
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003054/// \brief Optionally returns true or false if the preallocated preprocessed
3055/// entity with index \arg Index came from file \arg FID.
3056llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
3057 FileID FID) {
3058 if (FID.isInvalid())
3059 return false;
3060
3061 std::pair<Module *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3062 Module &M = *PPInfo.first;
3063 unsigned LocalIndex = PPInfo.second;
3064 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
3065
3066 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
3067 if (Loc.isInvalid())
3068 return false;
3069
3070 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
3071 return true;
3072 else
3073 return false;
3074}
3075
Douglas Gregord10a3812011-08-25 18:14:34 +00003076namespace {
3077 /// \brief Visitor used to search for information about a header file.
3078 class HeaderFileInfoVisitor {
3079 ASTReader &Reader;
3080 const FileEntry *FE;
3081
3082 llvm::Optional<HeaderFileInfo> HFI;
3083
3084 public:
3085 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
3086 : Reader(Reader), FE(FE) { }
3087
3088 static bool visit(Module &M, void *UserData) {
3089 HeaderFileInfoVisitor *This
3090 = static_cast<HeaderFileInfoVisitor *>(UserData);
3091
3092 HeaderFileInfoTrait Trait(This->Reader, M,
3093 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
3094 M.HeaderFileFrameworkStrings,
3095 This->FE->getName());
3096
3097 HeaderFileInfoLookupTable *Table
3098 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
3099 if (!Table)
3100 return false;
3101
3102 // Look in the on-disk hash table for an entry for this file name.
3103 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
3104 &Trait);
3105 if (Pos == Table->end())
3106 return false;
3107
3108 This->HFI = *Pos;
3109 return true;
3110 }
3111
3112 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
3113 };
3114}
3115
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003116HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003117 HeaderFileInfoVisitor Visitor(*this, FE);
3118 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
3119 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003120 if (Listener)
Douglas Gregord10a3812011-08-25 18:14:34 +00003121 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
3122 return *HFI;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003123 }
3124
3125 return HeaderFileInfo();
3126}
3127
David Blaikied6471f72011-09-25 23:23:43 +00003128void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00003129 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
3130 Module &F = *(*I);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003131 unsigned Idx = 0;
3132 while (Idx < F.PragmaDiagMappings.size()) {
3133 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00003134 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
3135 Diag.DiagStatePoints.push_back(
3136 DiagnosticsEngine::DiagStatePoint(&Diag.DiagStates.back(),
3137 FullSourceLoc(Loc, SourceMgr)));
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003138 while (1) {
3139 assert(Idx < F.PragmaDiagMappings.size() &&
3140 "Invalid data, didn't find '-1' marking end of diag/map pairs");
3141 if (Idx >= F.PragmaDiagMappings.size()) {
3142 break; // Something is messed up but at least avoid infinite loop in
3143 // release build.
3144 }
3145 unsigned DiagID = F.PragmaDiagMappings[Idx++];
3146 if (DiagID == (unsigned)-1) {
3147 break; // no more diag/map pairs for this location.
3148 }
3149 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00003150 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
3151 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003152 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003153 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00003154 }
3155}
3156
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003157/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003158ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregora119da02011-08-02 16:26:37 +00003159 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turnere9b76c12011-07-20 21:31:32 +00003160 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor1e849b62011-07-29 00:21:44 +00003161 Module *M = I->second;
Douglas Gregore3605012011-08-02 18:32:54 +00003162 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003163}
3164
3165/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00003166///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003167/// The index is the type ID, shifted and minus the number of predefs. This
3168/// routine actually reads the record corresponding to the type at the given
3169/// location. It is a helper routine for GetType, which deals with reading type
3170/// IDs.
Douglas Gregor393f2492011-07-22 00:38:23 +00003171QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003172 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00003173 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00003174
Douglas Gregor0b748912009-04-14 21:18:50 +00003175 // Keep track of where we are in the stream, then jump back there
3176 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003177 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00003178
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003179 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00003180
Douglas Gregord89275b2009-07-06 18:54:52 +00003181 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003182 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00003183
Douglas Gregor393f2492011-07-22 00:38:23 +00003184 unsigned Idx = 0;
Sebastian Redlc3632732010-10-05 15:59:54 +00003185 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003186 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003187 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003188 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3189 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003190 if (Record.size() != 2) {
3191 Error("Incorrect encoding of extended qualifier type");
3192 return QualType();
3193 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003194 QualType Base = readType(*Loc.F, Record, Idx);
3195 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
Douglas Gregor35942772011-09-09 21:34:22 +00003196 return Context.getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00003197 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003198
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003199 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003200 if (Record.size() != 1) {
3201 Error("Incorrect encoding of complex type");
3202 return QualType();
3203 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003204 QualType ElemType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003205 return Context.getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003206 }
3207
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003208 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003209 if (Record.size() != 1) {
3210 Error("Incorrect encoding of pointer type");
3211 return QualType();
3212 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003213 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003214 return Context.getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003215 }
3216
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003217 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003218 if (Record.size() != 1) {
3219 Error("Incorrect encoding of block pointer type");
3220 return QualType();
3221 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003222 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003223 return Context.getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003224 }
3225
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003226 case TYPE_LVALUE_REFERENCE: {
Richard Smithdf1550f2011-04-12 10:38:03 +00003227 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003228 Error("Incorrect encoding of lvalue reference type");
3229 return QualType();
3230 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003231 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003232 return Context.getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003233 }
3234
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003235 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003236 if (Record.size() != 1) {
3237 Error("Incorrect encoding of rvalue reference type");
3238 return QualType();
3239 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003240 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003241 return Context.getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003242 }
3243
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003244 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00003245 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003246 Error("Incorrect encoding of member pointer type");
3247 return QualType();
3248 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003249 QualType PointeeType = readType(*Loc.F, Record, Idx);
3250 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00003251 if (PointeeType.isNull() || ClassType.isNull())
3252 return QualType();
3253
Douglas Gregor35942772011-09-09 21:34:22 +00003254 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003255 }
3256
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003257 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003258 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003259 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3260 unsigned IndexTypeQuals = Record[2];
3261 unsigned Idx = 3;
3262 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003263 return Context.getConstantArrayType(ElementType, Size,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003264 ASM, IndexTypeQuals);
3265 }
3266
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003267 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003268 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003269 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3270 unsigned IndexTypeQuals = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003271 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003272 }
3273
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003274 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003275 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00003276 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3277 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00003278 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3279 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
Douglas Gregor35942772011-09-09 21:34:22 +00003280 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003281 ASM, IndexTypeQuals,
3282 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003283 }
3284
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003285 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003286 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003287 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003288 return QualType();
3289 }
3290
Douglas Gregor393f2492011-07-22 00:38:23 +00003291 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003292 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00003293 unsigned VecKind = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003294 return Context.getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00003295 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003296 }
3297
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003298 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003299 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003300 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003301 return QualType();
3302 }
3303
Douglas Gregor393f2492011-07-22 00:38:23 +00003304 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003305 unsigned NumElements = Record[1];
Douglas Gregor35942772011-09-09 21:34:22 +00003306 return Context.getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003307 }
3308
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003309 case TYPE_FUNCTION_NO_PROTO: {
John McCallf85e1932011-06-15 23:02:42 +00003310 if (Record.size() != 6) {
Douglas Gregora02b1472009-04-28 21:53:25 +00003311 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003312 return QualType();
3313 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003314 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCallf85e1932011-06-15 23:02:42 +00003315 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
3316 (CallingConv)Record[4], Record[5]);
Douglas Gregor35942772011-09-09 21:34:22 +00003317 return Context.getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003318 }
3319
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003320 case TYPE_FUNCTION_PROTO: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003321 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalle23cf432010-12-14 08:05:40 +00003322
3323 FunctionProtoType::ExtProtoInfo EPI;
3324 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmana49218e2011-04-09 08:18:08 +00003325 /*hasregparm*/ Record[2],
3326 /*regparm*/ Record[3],
John McCallf85e1932011-06-15 23:02:42 +00003327 static_cast<CallingConv>(Record[4]),
3328 /*produces*/ Record[5]);
John McCalle23cf432010-12-14 08:05:40 +00003329
John McCallf85e1932011-06-15 23:02:42 +00003330 unsigned Idx = 6;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003331 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003332 SmallVector<QualType, 16> ParamTypes;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003333 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00003334 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalle23cf432010-12-14 08:05:40 +00003335
3336 EPI.Variadic = Record[Idx++];
3337 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00003338 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redl60618fa2011-03-12 11:50:43 +00003339 ExceptionSpecificationType EST =
3340 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3341 EPI.ExceptionSpecType = EST;
3342 if (EST == EST_Dynamic) {
3343 EPI.NumExceptions = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003344 SmallVector<QualType, 2> Exceptions;
Sebastian Redl60618fa2011-03-12 11:50:43 +00003345 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00003346 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redl60618fa2011-03-12 11:50:43 +00003347 EPI.Exceptions = Exceptions.data();
3348 } else if (EST == EST_ComputedNoexcept) {
3349 EPI.NoexceptExpr = ReadExpr(*Loc.F);
3350 }
Douglas Gregor35942772011-09-09 21:34:22 +00003351 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00003352 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003353 }
3354
Douglas Gregor409448c2011-07-21 22:35:25 +00003355 case TYPE_UNRESOLVED_USING: {
3356 unsigned Idx = 0;
Douglas Gregor35942772011-09-09 21:34:22 +00003357 return Context.getTypeDeclType(
Douglas Gregor409448c2011-07-21 22:35:25 +00003358 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
3359 }
3360
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003361 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003362 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003363 Error("incorrect encoding of typedef type");
3364 return QualType();
3365 }
Douglas Gregor409448c2011-07-21 22:35:25 +00003366 unsigned Idx = 0;
3367 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003368 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003369 if (!Canonical.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00003370 Canonical = Context.getCanonicalType(Canonical);
3371 return Context.getTypedefType(Decl, Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003372 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003373
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003374 case TYPE_TYPEOF_EXPR:
Douglas Gregor35942772011-09-09 21:34:22 +00003375 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003376
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003377 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003378 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003379 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003380 return QualType();
3381 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003382 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003383 return Context.getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003384 }
Mike Stump1eb44332009-09-09 15:08:12 +00003385
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003386 case TYPE_DECLTYPE:
Douglas Gregor35942772011-09-09 21:34:22 +00003387 return Context.getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson395b4752009-06-24 19:06:50 +00003388
Sean Huntca63c202011-05-24 22:41:36 +00003389 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003390 QualType BaseType = readType(*Loc.F, Record, Idx);
3391 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Sean Huntca63c202011-05-24 22:41:36 +00003392 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003393 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
Sean Huntca63c202011-05-24 22:41:36 +00003394 }
3395
Richard Smith34b41d92011-02-20 03:19:35 +00003396 case TYPE_AUTO:
Douglas Gregor35942772011-09-09 21:34:22 +00003397 return Context.getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith34b41d92011-02-20 03:19:35 +00003398
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003399 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003400 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003401 Error("incorrect encoding of record type");
3402 return QualType();
3403 }
Douglas Gregor409448c2011-07-21 22:35:25 +00003404 unsigned Idx = 0;
3405 bool IsDependent = Record[Idx++];
3406 QualType T
Douglas Gregor35942772011-09-09 21:34:22 +00003407 = Context.getRecordType(ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00003408 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003409 return T;
3410 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003411
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003412 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003413 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003414 Error("incorrect encoding of enum type");
3415 return QualType();
3416 }
Douglas Gregor409448c2011-07-21 22:35:25 +00003417 unsigned Idx = 0;
3418 bool IsDependent = Record[Idx++];
3419 QualType T
Douglas Gregor35942772011-09-09 21:34:22 +00003420 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00003421 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003422 return T;
3423 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003424
John McCall9d156a72011-01-06 01:58:22 +00003425 case TYPE_ATTRIBUTED: {
3426 if (Record.size() != 3) {
3427 Error("incorrect encoding of attributed type");
3428 return QualType();
3429 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003430 QualType modifiedType = readType(*Loc.F, Record, Idx);
3431 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall9d156a72011-01-06 01:58:22 +00003432 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
Douglas Gregor35942772011-09-09 21:34:22 +00003433 return Context.getAttributedType(kind, modifiedType, equivalentType);
John McCall9d156a72011-01-06 01:58:22 +00003434 }
3435
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003436 case TYPE_PAREN: {
3437 if (Record.size() != 1) {
3438 Error("incorrect encoding of paren type");
3439 return QualType();
3440 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003441 QualType InnerType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003442 return Context.getParenType(InnerType);
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003443 }
3444
Douglas Gregor7536dd52010-12-20 02:24:11 +00003445 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00003446 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00003447 Error("incorrect encoding of pack expansion type");
3448 return QualType();
3449 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003450 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregor7536dd52010-12-20 02:24:11 +00003451 if (Pattern.isNull())
3452 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00003453 llvm::Optional<unsigned> NumExpansions;
3454 if (Record[1])
3455 NumExpansions = Record[1] - 1;
Douglas Gregor35942772011-09-09 21:34:22 +00003456 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00003457 }
3458
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003459 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003460 unsigned Idx = 0;
3461 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00003462 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003463 QualType NamedType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003464 return Context.getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00003465 }
3466
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003467 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003468 unsigned Idx = 0;
Douglas Gregor409448c2011-07-21 22:35:25 +00003469 ObjCInterfaceDecl *ItfD
3470 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003471 return Context.getObjCInterfaceType(ItfD);
John McCallc12c5bb2010-05-15 11:32:37 +00003472 }
3473
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003474 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00003475 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00003476 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003477 unsigned NumProtos = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003478 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003479 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor409448c2011-07-21 22:35:25 +00003480 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00003481 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003482 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003483
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003484 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003485 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00003486 QualType Pointee = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003487 return Context.getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003488 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00003489
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003490 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00003491 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00003492 QualType Parm = readType(*Loc.F, Record, Idx);
3493 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCall49a832b2009-10-18 09:09:24 +00003494 return
Douglas Gregor35942772011-09-09 21:34:22 +00003495 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
John McCall49a832b2009-10-18 09:09:24 +00003496 Replacement);
3497 }
John McCall3cb0ebd2010-03-10 03:28:59 +00003498
Douglas Gregorc3069d62011-01-14 02:55:32 +00003499 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3500 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00003501 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorc3069d62011-01-14 02:55:32 +00003502 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003503 return Context.getSubstTemplateTypeParmPackType(
Douglas Gregorc3069d62011-01-14 02:55:32 +00003504 cast<TemplateTypeParmType>(Parm),
3505 ArgPack);
3506 }
3507
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003508 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor409448c2011-07-21 22:35:25 +00003509 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003510 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003511 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003512 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003513 return
Douglas Gregor35942772011-09-09 21:34:22 +00003514 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00003515 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003516
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003517 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003518 unsigned Idx = 0;
3519 unsigned Depth = Record[Idx++];
3520 unsigned Index = Record[Idx++];
3521 bool Pack = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00003522 TemplateTypeParmDecl *D
3523 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003524 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003525 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003526
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003527 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003528 unsigned Idx = 0;
3529 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00003530 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00003531 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003532 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003533 if (!Canon.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00003534 Canon = Context.getCanonicalType(Canon);
3535 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003536 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003537
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003538 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003539 unsigned Idx = 0;
3540 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00003541 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00003542 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003543 unsigned NumArgs = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003544 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003545 Args.reserve(NumArgs);
3546 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00003547 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00003548 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003549 Args.size(), Args.data());
3550 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003551
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003552 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003553 unsigned Idx = 0;
3554
3555 // ArrayType
Douglas Gregor393f2492011-07-22 00:38:23 +00003556 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003557 ArrayType::ArraySizeModifier ASM
3558 = (ArrayType::ArraySizeModifier)Record[Idx++];
3559 unsigned IndexTypeQuals = Record[Idx++];
3560
3561 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00003562 Expr *NumElts = ReadExpr(*Loc.F);
3563 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003564
Douglas Gregor35942772011-09-09 21:34:22 +00003565 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003566 IndexTypeQuals, Brackets);
3567 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003568
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003569 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003570 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003571 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003572 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner5f9e2722011-07-23 10:55:15 +00003573 SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00003574 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003575 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003576 QualType T;
Richard Smith3e4c6c42011-05-05 21:57:07 +00003577 if (Underlying.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00003578 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003579 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003580 else
Douglas Gregor35942772011-09-09 21:34:22 +00003581 T = Context.getTemplateSpecializationType(Name, Args.data(),
Richard Smith3e4c6c42011-05-05 21:57:07 +00003582 Args.size(), Underlying);
John McCallf4c73712011-01-19 06:33:43 +00003583 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003584 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003585 }
Eli Friedmanb001de72011-10-06 23:00:33 +00003586
3587 case TYPE_ATOMIC: {
3588 if (Record.size() != 1) {
3589 Error("Incorrect encoding of atomic type");
3590 return QualType();
3591 }
3592 QualType ValueType = readType(*Loc.F, Record, Idx);
3593 return Context.getAtomicType(ValueType);
3594 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003595 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003596 // Suppress a GCC warning
3597 return QualType();
3598}
3599
Sebastian Redlc3632732010-10-05 15:59:54 +00003600class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003601 ASTReader &Reader;
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003602 Module &F;
Sebastian Redl577d4792010-07-22 22:43:28 +00003603 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003604 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00003605 unsigned &Idx;
3606
Sebastian Redlc3632732010-10-05 15:59:54 +00003607 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3608 unsigned &I) {
3609 return Reader.ReadSourceLocation(F, R, I);
3610 }
3611
Douglas Gregor409448c2011-07-21 22:35:25 +00003612 template<typename T>
3613 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
3614 return Reader.ReadDeclAs<T>(F, Record, Idx);
3615 }
3616
John McCalla1ee0c52009-10-16 21:56:05 +00003617public:
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003618 TypeLocReader(ASTReader &Reader, Module &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003619 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00003620 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3621 { }
John McCalla1ee0c52009-10-16 21:56:05 +00003622
John McCall51bd8032009-10-18 01:05:36 +00003623 // We want compile-time assurance that we've enumerated all of
3624 // these, so unfortunately we have to declare them first, then
3625 // define them out-of-line.
3626#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00003627#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00003628 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003629#include "clang/AST/TypeLocNodes.def"
3630
John McCall51bd8032009-10-18 01:05:36 +00003631 void VisitFunctionTypeLoc(FunctionTypeLoc);
3632 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003633};
3634
John McCall51bd8032009-10-18 01:05:36 +00003635void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00003636 // nothing to do
3637}
John McCall51bd8032009-10-18 01:05:36 +00003638void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003639 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00003640 if (TL.needsExtraLocalData()) {
3641 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3642 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3643 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3644 TL.setModeAttr(Record[Idx++]);
3645 }
John McCalla1ee0c52009-10-16 21:56:05 +00003646}
John McCall51bd8032009-10-18 01:05:36 +00003647void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003648 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003649}
John McCall51bd8032009-10-18 01:05:36 +00003650void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003651 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003652}
John McCall51bd8032009-10-18 01:05:36 +00003653void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003654 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003655}
John McCall51bd8032009-10-18 01:05:36 +00003656void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003657 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003658}
John McCall51bd8032009-10-18 01:05:36 +00003659void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003660 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003661}
John McCall51bd8032009-10-18 01:05:36 +00003662void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003663 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00003664 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003665}
John McCall51bd8032009-10-18 01:05:36 +00003666void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003667 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3668 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003669 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00003670 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003671 else
John McCall51bd8032009-10-18 01:05:36 +00003672 TL.setSizeExpr(0);
3673}
3674void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3675 VisitArrayTypeLoc(TL);
3676}
3677void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3678 VisitArrayTypeLoc(TL);
3679}
3680void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3681 VisitArrayTypeLoc(TL);
3682}
3683void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3684 DependentSizedArrayTypeLoc TL) {
3685 VisitArrayTypeLoc(TL);
3686}
3687void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3688 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003689 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003690}
3691void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003692 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003693}
3694void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003695 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003696}
3697void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +00003698 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
3699 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Douglas Gregordab60ad2010-10-01 18:44:50 +00003700 TL.setTrailingReturn(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00003701 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor409448c2011-07-21 22:35:25 +00003702 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003703 }
3704}
3705void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3706 VisitFunctionTypeLoc(TL);
3707}
3708void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3709 VisitFunctionTypeLoc(TL);
3710}
John McCalled976492009-12-04 22:46:56 +00003711void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003712 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00003713}
John McCall51bd8032009-10-18 01:05:36 +00003714void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003715 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003716}
3717void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003718 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3719 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3720 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003721}
3722void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003723 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3724 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3725 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3726 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003727}
3728void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003729 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003730}
Sean Huntca63c202011-05-24 22:41:36 +00003731void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3732 TL.setKWLoc(ReadSourceLocation(Record, Idx));
3733 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3734 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3735 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3736}
Richard Smith34b41d92011-02-20 03:19:35 +00003737void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3738 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3739}
John McCall51bd8032009-10-18 01:05:36 +00003740void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003741 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003742}
3743void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003744 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003745}
John McCall9d156a72011-01-06 01:58:22 +00003746void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3747 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3748 if (TL.hasAttrOperand()) {
3749 SourceRange range;
3750 range.setBegin(ReadSourceLocation(Record, Idx));
3751 range.setEnd(ReadSourceLocation(Record, Idx));
3752 TL.setAttrOperandParensRange(range);
3753 }
3754 if (TL.hasAttrExprOperand()) {
3755 if (Record[Idx++])
3756 TL.setAttrExprOperand(Reader.ReadExpr(F));
3757 else
3758 TL.setAttrExprOperand(0);
3759 } else if (TL.hasAttrEnumOperand())
3760 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3761}
John McCall51bd8032009-10-18 01:05:36 +00003762void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003763 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003764}
John McCall49a832b2009-10-18 09:09:24 +00003765void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3766 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003767 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00003768}
Douglas Gregorc3069d62011-01-14 02:55:32 +00003769void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3770 SubstTemplateTypeParmPackTypeLoc TL) {
3771 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3772}
John McCall51bd8032009-10-18 01:05:36 +00003773void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3774 TemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003775 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3776 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3777 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00003778 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3779 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00003780 Reader.GetTemplateArgumentLocInfo(F,
3781 TL.getTypePtr()->getArg(i).getKind(),
3782 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003783}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003784void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3785 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3786 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3787}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003788void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003789 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor9e876872011-03-01 18:12:44 +00003790 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003791}
John McCall3cb0ebd2010-03-10 03:28:59 +00003792void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003793 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00003794}
Douglas Gregor4714c122010-03-31 17:34:00 +00003795void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003796 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor2494dd02011-03-01 01:34:45 +00003797 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00003798 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003799}
John McCall33500952010-06-11 00:33:02 +00003800void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3801 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003802 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor94fdffa2011-03-01 20:11:18 +00003803 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00003804 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3805 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3806 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003807 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3808 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00003809 Reader.GetTemplateArgumentLocInfo(F,
3810 TL.getTypePtr()->getArg(I).getKind(),
3811 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003812}
Douglas Gregor7536dd52010-12-20 02:24:11 +00003813void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3814 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3815}
John McCall51bd8032009-10-18 01:05:36 +00003816void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003817 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00003818}
3819void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3820 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00003821 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3822 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003823 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00003824 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003825}
John McCall54e14c42009-10-22 22:37:11 +00003826void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003827 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00003828}
Eli Friedmanb001de72011-10-06 23:00:33 +00003829void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
3830 TL.setKWLoc(ReadSourceLocation(Record, Idx));
3831 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3832 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3833}
John McCalla1ee0c52009-10-16 21:56:05 +00003834
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003835TypeSourceInfo *ASTReader::GetTypeSourceInfo(Module &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003836 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00003837 unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00003838 QualType InfoTy = readType(F, Record, Idx);
John McCalla1ee0c52009-10-16 21:56:05 +00003839 if (InfoTy.isNull())
3840 return 0;
3841
Douglas Gregor35942772011-09-09 21:34:22 +00003842 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00003843 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00003844 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00003845 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00003846 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00003847}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003848
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003849QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00003850 unsigned FastQuals = ID & Qualifiers::FastMask;
3851 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003852
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003853 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003854 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003855 switch ((PredefinedTypeIDs)Index) {
3856 case PREDEF_TYPE_NULL_ID: return QualType();
Douglas Gregor35942772011-09-09 21:34:22 +00003857 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
3858 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003859
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003860 case PREDEF_TYPE_CHAR_U_ID:
3861 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00003862 // FIXME: Check that the signedness of CharTy is correct!
Douglas Gregor35942772011-09-09 21:34:22 +00003863 T = Context.CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003864 break;
3865
Douglas Gregor35942772011-09-09 21:34:22 +00003866 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
3867 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
3868 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
3869 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
3870 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
3871 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
3872 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
3873 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
3874 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
3875 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
3876 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
3877 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
3878 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003879 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00003880 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
3881 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
3882 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
3883 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
3884 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
John McCall3c3b7f92011-10-25 17:37:35 +00003885 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00003886 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
3887 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
3888 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
3889 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
3890 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
3891 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
3892 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
3893 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
3894 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003895
3896 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
Douglas Gregor35942772011-09-09 21:34:22 +00003897 T = Context.getAutoRRefDeductType();
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003898 break;
John McCall0ddaeb92011-10-17 18:09:15 +00003899
3900 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
3901 T = Context.ARCUnbridgedCastTy;
3902 break;
3903
Douglas Gregor2cf26342009-04-09 22:27:44 +00003904 }
3905
3906 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00003907 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003908 }
3909
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003910 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003911 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00003912 if (TypesLoaded[Index].isNull()) {
Douglas Gregor393f2492011-07-22 00:38:23 +00003913 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00003914 if (TypesLoaded[Index].isNull())
3915 return QualType();
3916
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003917 TypesLoaded[Index]->setFromAST();
Sebastian Redl30c514c2010-07-14 23:45:08 +00003918 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003919 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00003920 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00003921 }
Mike Stump1eb44332009-09-09 15:08:12 +00003922
John McCall0953e762009-09-24 19:53:00 +00003923 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003924}
3925
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003926QualType ASTReader::getLocalType(Module &F, unsigned LocalID) {
Douglas Gregor393f2492011-07-22 00:38:23 +00003927 return GetType(getGlobalTypeID(F, LocalID));
3928}
3929
3930serialization::TypeID
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003931ASTReader::getGlobalTypeID(Module &F, unsigned LocalID) const {
Douglas Gregora119da02011-08-02 16:26:37 +00003932 unsigned FastQuals = LocalID & Qualifiers::FastMask;
3933 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
3934
3935 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
3936 return LocalID;
3937
3938 ContinuousRangeMap<uint32_t, int, 2>::iterator I
3939 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
3940 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
3941
3942 unsigned GlobalIndex = LocalIndex + I->second;
3943 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
3944}
3945
John McCall833ca992009-10-29 08:12:44 +00003946TemplateArgumentLocInfo
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003947ASTReader::GetTemplateArgumentLocInfo(Module &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00003948 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00003949 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003950 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00003951 switch (Kind) {
3952 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00003953 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00003954 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00003955 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00003956 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003957 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3958 Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00003959 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003960 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregora7fc9012011-01-05 18:58:31 +00003961 SourceLocation());
3962 }
3963 case TemplateArgument::TemplateExpansion: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003964 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3965 Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003966 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003967 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003968 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregorba68eca2011-01-05 17:40:24 +00003969 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00003970 }
John McCall833ca992009-10-29 08:12:44 +00003971 case TemplateArgument::Null:
3972 case TemplateArgument::Integral:
3973 case TemplateArgument::Declaration:
3974 case TemplateArgument::Pack:
3975 return TemplateArgumentLocInfo();
3976 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00003977 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00003978 return TemplateArgumentLocInfo();
3979}
3980
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003981TemplateArgumentLoc
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003982ASTReader::ReadTemplateArgumentLoc(Module &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003983 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003984 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003985
3986 if (Arg.getKind() == TemplateArgument::Expression) {
3987 if (Record[Index++]) // bool InfoHasSameExpr.
3988 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3989 }
Sebastian Redlc3632732010-10-05 15:59:54 +00003990 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003991 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003992}
3993
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003994Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00003995 return GetDecl(ID);
3996}
3997
Douglas Gregore92b8a12011-08-04 00:01:48 +00003998uint64_t ASTReader::readCXXBaseSpecifiers(Module &M, const RecordData &Record,
3999 unsigned &Idx){
4000 if (Idx >= Record.size())
Douglas Gregor7c789c12010-10-29 22:39:52 +00004001 return 0;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004002
Douglas Gregore92b8a12011-08-04 00:01:48 +00004003 unsigned LocalID = Record[Idx++];
4004 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004005}
4006
4007CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004008 RecordLocation Loc = getLocalBitOffset(Offset);
4009 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004010 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004011 Cursor.JumpToBit(Loc.Offset);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004012 ReadingKindTracker ReadingKind(Read_Decl, *this);
4013 RecordData Record;
4014 unsigned Code = Cursor.ReadCode();
4015 unsigned RecCode = Cursor.ReadRecord(Code, Record);
4016 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4017 Error("Malformed AST file: missing C++ base specifiers");
4018 return 0;
4019 }
4020
4021 unsigned Idx = 0;
4022 unsigned NumBases = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00004023 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004024 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4025 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004026 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004027 return Bases;
4028}
4029
Douglas Gregor409448c2011-07-21 22:35:25 +00004030serialization::DeclID
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004031ASTReader::getGlobalDeclID(Module &F, unsigned LocalID) const {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004032 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregor496c7092011-08-03 15:48:04 +00004033 return LocalID;
4034
4035 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004036 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregor496c7092011-08-03 15:48:04 +00004037 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
4038
4039 return LocalID + I->second;
Douglas Gregor409448c2011-07-21 22:35:25 +00004040}
4041
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004042bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
4043 Module &M) const {
4044 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
4045 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4046 return &M == I->second;
4047}
4048
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004049SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
4050 if (ID < NUM_PREDEF_DECL_IDS)
4051 return SourceLocation();
4052
4053 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4054
4055 if (Index > DeclsLoaded.size()) {
4056 Error("declaration ID out-of-range for AST file");
4057 return SourceLocation();
4058 }
4059
4060 if (Decl *D = DeclsLoaded[Index])
4061 return D->getLocation();
4062
4063 unsigned RawLocation = 0;
4064 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
4065 return ReadSourceLocation(*Rec.F, RawLocation);
4066}
4067
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004068Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004069 if (ID < NUM_PREDEF_DECL_IDS) {
4070 switch ((PredefinedDeclIDs)ID) {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004071 case PREDEF_DECL_NULL_ID:
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004072 return 0;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004073
4074 case PREDEF_DECL_TRANSLATION_UNIT_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004075 return Context.getTranslationUnitDecl();
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00004076
4077 case PREDEF_DECL_OBJC_ID_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004078 return Context.getObjCIdDecl();
Douglas Gregor79d67262011-08-12 05:59:41 +00004079
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004080 case PREDEF_DECL_OBJC_SEL_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004081 return Context.getObjCSelDecl();
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004082
Douglas Gregor79d67262011-08-12 05:59:41 +00004083 case PREDEF_DECL_OBJC_CLASS_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004084 return Context.getObjCClassDecl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004085
4086 case PREDEF_DECL_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004087 return Context.getInt128Decl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004088
4089 case PREDEF_DECL_UNSIGNED_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004090 return Context.getUInt128Decl();
Douglas Gregore97179c2011-09-08 01:46:34 +00004091
4092 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004093 return Context.getObjCInstanceTypeDecl();
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004094 }
4095
Douglas Gregor2cf26342009-04-09 22:27:44 +00004096 return 0;
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004097 }
4098
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004099 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4100
4101 if (Index > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004102 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004103 return 0;
4104 }
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004105
4106if (!DeclsLoaded[Index]) {
Douglas Gregor496c7092011-08-03 15:48:04 +00004107 ReadDeclRecord(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00004108 if (DeserializationListener)
4109 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
4110 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004111
4112 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004113}
4114
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004115serialization::DeclID ASTReader::ReadDeclID(Module &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00004116 const RecordData &Record,
4117 unsigned &Idx) {
4118 if (Idx >= Record.size()) {
4119 Error("Corrupted AST file");
4120 return 0;
4121 }
4122
4123 return getGlobalDeclID(F, Record[Idx++]);
4124}
4125
Chris Lattner887e2b32009-04-27 05:46:25 +00004126/// \brief Resolve the offset of a statement into a statement.
4127///
4128/// This operation will read a new statement from the external
4129/// source each time it is called, and is meant to be used via a
4130/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004131Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004132 // Switch case IDs are per Decl.
4133 ClearSwitchCaseIDs();
4134
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004135 // Offset here is a global offset across the entire chain.
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004136 RecordLocation Loc = getLocalBitOffset(Offset);
4137 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
4138 return ReadStmtFromStream(*Loc.F);
Douglas Gregor250fc9c2009-04-18 00:07:54 +00004139}
4140
Douglas Gregor851c75a2011-08-24 21:27:34 +00004141namespace {
4142 class FindExternalLexicalDeclsVisitor {
4143 ASTReader &Reader;
4144 const DeclContext *DC;
4145 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004146
Douglas Gregor851c75a2011-08-24 21:27:34 +00004147 SmallVectorImpl<Decl*> &Decls;
4148 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
4149
4150 public:
4151 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
4152 bool (*isKindWeWant)(Decl::Kind),
4153 SmallVectorImpl<Decl*> &Decls)
4154 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
4155 {
4156 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
4157 PredefsVisited[I] = false;
4158 }
4159
4160 static bool visit(Module &M, bool Preorder, void *UserData) {
4161 if (Preorder)
4162 return false;
4163
4164 FindExternalLexicalDeclsVisitor *This
4165 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
4166
4167 Module::DeclContextInfosMap::iterator Info
4168 = M.DeclContextInfos.find(This->DC);
4169 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
4170 return false;
4171
4172 // Load all of the declaration IDs
4173 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
4174 *IDE = ID + Info->second.NumLexicalDecls;
4175 ID != IDE; ++ID) {
4176 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
4177 continue;
4178
4179 // Don't add predefined declarations to the lexical context more
4180 // than once.
4181 if (ID->second < NUM_PREDEF_DECL_IDS) {
4182 if (This->PredefsVisited[ID->second])
4183 continue;
4184
4185 This->PredefsVisited[ID->second] = true;
4186 }
4187
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004188 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
4189 if (!This->DC->isDeclInLexicalTraversal(D))
4190 This->Decls.push_back(D);
4191 }
Douglas Gregor851c75a2011-08-24 21:27:34 +00004192 }
4193
4194 return false;
4195 }
4196 };
4197}
4198
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004199ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00004200 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner5f9e2722011-07-23 10:55:15 +00004201 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00004202 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor851c75a2011-08-24 21:27:34 +00004203 // least. Walk all of the modules in the order they were loaded.
4204 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
4205 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00004206 ++NumLexicalDeclContextsRead;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004207 return ELR_Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004208}
4209
Douglas Gregor0d95f772011-08-24 19:03:07 +00004210namespace {
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004211
4212class DeclIDComp {
4213 ASTReader &Reader;
4214 Module &Mod;
4215
4216public:
4217 DeclIDComp(ASTReader &Reader, Module &M) : Reader(Reader), Mod(M) {}
4218
4219 bool operator()(LocalDeclID L, LocalDeclID R) const {
4220 SourceLocation LHS = getLocation(L);
4221 SourceLocation RHS = getLocation(R);
4222 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4223 }
4224
4225 bool operator()(SourceLocation LHS, LocalDeclID R) const {
4226 SourceLocation RHS = getLocation(R);
4227 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4228 }
4229
4230 bool operator()(LocalDeclID L, SourceLocation RHS) const {
4231 SourceLocation LHS = getLocation(L);
4232 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4233 }
4234
4235 SourceLocation getLocation(LocalDeclID ID) const {
4236 return Reader.getSourceManager().getFileLoc(
4237 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
4238 }
4239};
4240
4241}
4242
4243void ASTReader::FindFileRegionDecls(FileID File,
4244 unsigned Offset, unsigned Length,
4245 SmallVectorImpl<Decl *> &Decls) {
4246 SourceManager &SM = getSourceManager();
4247
4248 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
4249 if (I == FileDeclIDs.end())
4250 return;
4251
4252 FileDeclsInfo &DInfo = I->second;
4253 if (DInfo.Decls.empty())
4254 return;
4255
4256 SourceLocation
4257 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
4258 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
4259
4260 DeclIDComp DIDComp(*this, *DInfo.Mod);
4261 ArrayRef<serialization::LocalDeclID>::iterator
4262 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
4263 BeginLoc, DIDComp);
4264 if (BeginIt != DInfo.Decls.begin())
4265 --BeginIt;
4266
4267 ArrayRef<serialization::LocalDeclID>::iterator
4268 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
4269 EndLoc, DIDComp);
4270 if (EndIt != DInfo.Decls.end())
4271 ++EndIt;
4272
4273 for (ArrayRef<serialization::LocalDeclID>::iterator
4274 DIt = BeginIt; DIt != EndIt; ++DIt)
4275 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
4276}
4277
4278namespace {
Douglas Gregor0d95f772011-08-24 19:03:07 +00004279 /// \brief Module visitor used to perform name lookup into a
4280 /// declaration context.
4281 class DeclContextNameLookupVisitor {
4282 ASTReader &Reader;
4283 const DeclContext *DC;
4284 DeclarationName Name;
4285 SmallVectorImpl<NamedDecl *> &Decls;
4286
4287 public:
4288 DeclContextNameLookupVisitor(ASTReader &Reader,
4289 const DeclContext *DC, DeclarationName Name,
4290 SmallVectorImpl<NamedDecl *> &Decls)
4291 : Reader(Reader), DC(DC), Name(Name), Decls(Decls) { }
4292
4293 static bool visit(Module &M, void *UserData) {
4294 DeclContextNameLookupVisitor *This
4295 = static_cast<DeclContextNameLookupVisitor *>(UserData);
4296
4297 // Check whether we have any visible declaration information for
4298 // this context in this module.
4299 Module::DeclContextInfosMap::iterator Info
4300 = M.DeclContextInfos.find(This->DC);
4301 if (Info == M.DeclContextInfos.end() || !Info->second.NameLookupTableData)
4302 return false;
4303
4304 // Look for this name within this module.
4305 ASTDeclContextNameLookupTable *LookupTable =
4306 (ASTDeclContextNameLookupTable*)Info->second.NameLookupTableData;
4307 ASTDeclContextNameLookupTable::iterator Pos
4308 = LookupTable->find(This->Name);
4309 if (Pos == LookupTable->end())
4310 return false;
4311
4312 bool FoundAnything = false;
4313 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
4314 for (; Data.first != Data.second; ++Data.first) {
4315 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
4316 if (!ND)
4317 continue;
4318
4319 if (ND->getDeclName() != This->Name) {
4320 assert(!This->Name.getCXXNameType().isNull() &&
4321 "Name mismatch without a type");
4322 continue;
4323 }
4324
4325 // Record this declaration.
4326 FoundAnything = true;
4327 This->Decls.push_back(ND);
4328 }
4329
4330 return FoundAnything;
4331 }
4332 };
4333}
4334
John McCall76bd1f32010-06-01 09:23:16 +00004335DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004336ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00004337 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00004338 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00004339 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00004340 if (!Name)
4341 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
4342 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004343
Chris Lattner5f9e2722011-07-23 10:55:15 +00004344 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregor0d95f772011-08-24 19:03:07 +00004345 DeclContextNameLookupVisitor Visitor(*this, DC, Name, Decls);
4346 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00004347 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00004348 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00004349 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004350}
4351
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00004352/// \brief Under non-PCH compilation the consumer receives the objc methods
4353/// before receiving the implementation, and codegen depends on this.
4354/// We simulate this by deserializing and passing to consumer the methods of the
4355/// implementation before passing the deserialized implementation decl.
4356static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
4357 ASTConsumer *Consumer) {
4358 assert(ImplD && Consumer);
4359
4360 for (ObjCImplDecl::method_iterator
4361 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
4362 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
4363
4364 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
4365}
4366
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004367void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004368 assert(Consumer);
4369 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00004370 Decl *D = InterestingDecls.front();
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004371 InterestingDecls.pop_front();
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00004372
4373 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
4374 PassObjCImplDeclToConsumer(ImplD, Consumer);
4375 else
4376 Consumer->HandleInterestingDecl(DeclGroupRef(D));
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004377 }
4378}
4379
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004380void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00004381 this->Consumer = Consumer;
4382
Douglas Gregorfdd01722009-04-14 00:24:19 +00004383 if (!Consumer)
4384 return;
4385
4386 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004387 // Force deserialization of this decl, which will cause it to be queued for
4388 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00004389 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00004390 }
Douglas Gregor1a995dd2011-09-15 18:47:32 +00004391 ExternalDefinitions.clear();
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00004392
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004393 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00004394}
4395
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004396void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004397 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00004398
Mike Stump1eb44332009-09-09 15:08:12 +00004399 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004400 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00004401 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004402 unsigned NumDeclsLoaded
4403 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
4404 (Decl *)0);
4405 unsigned NumIdentifiersLoaded
4406 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
4407 IdentifiersLoaded.end(),
4408 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00004409 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004410 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
4411 SelectorsLoaded.end(),
4412 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00004413
Douglas Gregor4fed3f42009-04-27 18:38:38 +00004414 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
4415 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor0cdd7982011-07-21 18:46:38 +00004416 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00004417 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
4418 NumSLocEntriesRead, TotalNumSLocEntries,
4419 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004420 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004421 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004422 NumTypesLoaded, (unsigned)TypesLoaded.size(),
4423 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
4424 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004425 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004426 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
4427 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004428 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004429 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004430 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
4431 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00004432 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004433 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00004434 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
4435 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00004436 if (TotalNumStatements)
4437 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
4438 NumStatementsRead, TotalNumStatements,
4439 ((float)NumStatementsRead/TotalNumStatements * 100));
4440 if (TotalNumMacros)
4441 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
4442 NumMacrosRead, TotalNumMacros,
4443 ((float)NumMacrosRead/TotalNumMacros * 100));
4444 if (TotalLexicalDeclContexts)
4445 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
4446 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4447 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4448 * 100));
4449 if (TotalVisibleDeclContexts)
4450 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
4451 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4452 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4453 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004454 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004455 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004456 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4457 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00004458 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004459 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00004460 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004461 std::fprintf(stderr, "\n");
Douglas Gregor23d7df52011-07-21 19:50:14 +00004462 dump();
4463 std::fprintf(stderr, "\n");
4464}
4465
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004466template<typename Key, typename Module, unsigned InitialCapacity>
Douglas Gregor23d7df52011-07-21 19:50:14 +00004467static void
Chris Lattner5f9e2722011-07-23 10:55:15 +00004468dumpModuleIDMap(StringRef Name,
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004469 const ContinuousRangeMap<Key, Module *,
Douglas Gregor23d7df52011-07-21 19:50:14 +00004470 InitialCapacity> &Map) {
4471 if (Map.begin() == Map.end())
4472 return;
4473
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004474 typedef ContinuousRangeMap<Key, Module *, InitialCapacity> MapType;
Douglas Gregor23d7df52011-07-21 19:50:14 +00004475 llvm::errs() << Name << ":\n";
4476 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
4477 I != IEnd; ++I) {
4478 llvm::errs() << " " << I->first << " -> " << I->second->FileName
4479 << "\n";
4480 }
4481}
4482
Douglas Gregor23d7df52011-07-21 19:50:14 +00004483void ASTReader::dump() {
Douglas Gregor8df5c9b2011-08-02 11:12:41 +00004484 llvm::errs() << "*** PCH/Module Remappings:\n";
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004485 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor23d7df52011-07-21 19:50:14 +00004486 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor1e849b62011-07-29 00:21:44 +00004487 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00004488 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00004489 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
4490 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00004491 dumpModuleIDMap("Global preprocessed entity map",
4492 GlobalPreprocessedEntityMap);
Douglas Gregor8df5c9b2011-08-02 11:12:41 +00004493
4494 llvm::errs() << "\n*** PCH/Modules Loaded:";
4495 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
4496 MEnd = ModuleMgr.end();
4497 M != MEnd; ++M)
4498 (*M)->dump();
Douglas Gregor2cf26342009-04-09 22:27:44 +00004499}
4500
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00004501/// Return the amount of memory used by memory buffers, breaking down
4502/// by heap-backed versus mmap'ed memory.
4503void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004504 for (ModuleConstIterator I = ModuleMgr.begin(),
4505 E = ModuleMgr.end(); I != E; ++I) {
4506 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00004507 size_t bytes = buf->getBufferSize();
4508 switch (buf->getBufferKind()) {
4509 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
4510 sizes.malloc_bytes += bytes;
4511 break;
4512 case llvm::MemoryBuffer::MemoryBuffer_MMap:
4513 sizes.mmap_bytes += bytes;
4514 break;
4515 }
4516 }
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004517 }
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00004518}
4519
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004520void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00004521 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004522 S.ExternalSource = this;
4523
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004524 // Makes sure any declarations that were deserialized "too early"
4525 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00004526 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00004527 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
4528 PreloadedDecls[I]->getDeclName());
Douglas Gregor668c1a42009-04-21 22:25:48 +00004529 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004530 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004531
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004532 // Load the offsets of the declarations that Sema references.
4533 // They will be lazily deserialized when needed.
4534 if (!SemaDeclRefs.empty()) {
4535 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregor1e5b6f62011-07-28 00:57:24 +00004536 if (!SemaObj->StdNamespace)
4537 SemaObj->StdNamespace = SemaDeclRefs[0];
4538 if (!SemaObj->StdBadAlloc)
4539 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004540 }
4541
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004542 if (!FPPragmaOptions.empty()) {
4543 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4544 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4545 }
4546
4547 if (!OpenCLExtensions.empty()) {
4548 unsigned I = 0;
4549#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4550#include "clang/Basic/OpenCLExtensions.def"
4551
4552 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4553 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00004554}
4555
Douglas Gregor211f6e82011-08-20 04:39:52 +00004556IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregor211f6e82011-08-20 04:39:52 +00004557 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart));
4558 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Douglas Gregoreee242f2011-10-27 09:33:13 +00004559 IdentifierInfo *II = Visitor.getIdentifierInfo();
4560 if (II)
4561 II->setOutOfDate(false);
4562 return II;
Douglas Gregor668c1a42009-04-21 22:25:48 +00004563}
4564
Douglas Gregor95f42922010-10-14 22:11:03 +00004565namespace clang {
4566 /// \brief An identifier-lookup iterator that enumerates all of the
4567 /// identifiers stored within a set of AST files.
4568 class ASTIdentifierIterator : public IdentifierIterator {
4569 /// \brief The AST reader whose identifiers are being enumerated.
4570 const ASTReader &Reader;
4571
4572 /// \brief The current index into the chain of AST files stored in
4573 /// the AST reader.
4574 unsigned Index;
4575
4576 /// \brief The current position within the identifier lookup table
4577 /// of the current AST file.
4578 ASTIdentifierLookupTable::key_iterator Current;
4579
4580 /// \brief The end position within the identifier lookup table of
4581 /// the current AST file.
4582 ASTIdentifierLookupTable::key_iterator End;
4583
4584 public:
4585 explicit ASTIdentifierIterator(const ASTReader &Reader);
4586
Chris Lattner5f9e2722011-07-23 10:55:15 +00004587 virtual StringRef Next();
Douglas Gregor95f42922010-10-14 22:11:03 +00004588 };
4589}
4590
4591ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004592 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor95f42922010-10-14 22:11:03 +00004593 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004594 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00004595 Current = IdTable->key_begin();
4596 End = IdTable->key_end();
4597}
4598
Chris Lattner5f9e2722011-07-23 10:55:15 +00004599StringRef ASTIdentifierIterator::Next() {
Douglas Gregor95f42922010-10-14 22:11:03 +00004600 while (Current == End) {
4601 // If we have exhausted all of our AST files, we're done.
4602 if (Index == 0)
Chris Lattner5f9e2722011-07-23 10:55:15 +00004603 return StringRef();
Douglas Gregor95f42922010-10-14 22:11:03 +00004604
4605 --Index;
4606 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004607 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
4608 IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00004609 Current = IdTable->key_begin();
4610 End = IdTable->key_end();
4611 }
4612
4613 // We have any identifiers remaining in the current AST file; return
4614 // the next one.
4615 std::pair<const char*, unsigned> Key = *Current;
4616 ++Current;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004617 return StringRef(Key.first, Key.second);
Douglas Gregor95f42922010-10-14 22:11:03 +00004618}
4619
4620IdentifierIterator *ASTReader::getIdentifiers() const {
4621 return new ASTIdentifierIterator(*this);
4622}
4623
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004624namespace clang { namespace serialization {
4625 class ReadMethodPoolVisitor {
4626 ASTReader &Reader;
4627 Selector Sel;
4628 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
4629 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004630
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004631 /// \brief Build an ObjCMethodList from a vector of Objective-C method
4632 /// declarations.
4633 ObjCMethodList
4634 buildObjCMethodList(const SmallVectorImpl<ObjCMethodDecl *> &Vec) const
4635 {
4636 ObjCMethodList List;
4637 ObjCMethodList *Prev = 0;
4638 for (unsigned I = 0, N = Vec.size(); I != N; ++I) {
4639 if (!List.Method) {
4640 // This is the first method, which is the easy case.
4641 List.Method = Vec[I];
4642 Prev = &List;
4643 continue;
4644 }
4645
4646 ObjCMethodList *Mem =
4647 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
4648 Prev->Next = new (Mem) ObjCMethodList(Vec[I], 0);
4649 Prev = Prev->Next;
4650 }
4651
4652 return List;
4653 }
4654
4655 public:
4656 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel)
4657 : Reader(Reader), Sel(Sel) { }
4658
4659 static bool visit(Module &M, void *UserData) {
4660 ReadMethodPoolVisitor *This
4661 = static_cast<ReadMethodPoolVisitor *>(UserData);
4662
4663 if (!M.SelectorLookupTable)
4664 return false;
4665
4666 ASTSelectorLookupTable *PoolTable
4667 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
4668 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
4669 if (Pos == PoolTable->end())
4670 return false;
4671
4672 ++This->Reader.NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004673 // FIXME: Not quite happy with the statistics here. We probably should
4674 // disable this tracking when called via LoadSelector.
4675 // Also, should entries without methods count as misses?
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004676 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004677 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004678 if (This->Reader.DeserializationListener)
4679 This->Reader.DeserializationListener->SelectorRead(Data.ID,
4680 This->Sel);
4681
4682 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
4683 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
4684 return true;
Sebastian Redl725cd962010-08-04 20:40:17 +00004685 }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004686
4687 /// \brief Retrieve the instance methods found by this visitor.
4688 ObjCMethodList getInstanceMethods() const {
4689 return buildObjCMethodList(InstanceMethods);
4690 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004691
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004692 /// \brief Retrieve the instance methods found by this visitor.
4693 ObjCMethodList getFactoryMethods() const {
4694 return buildObjCMethodList(FactoryMethods);
4695 }
4696 };
4697} } // end namespace clang::serialization
4698
4699std::pair<ObjCMethodList, ObjCMethodList>
4700ASTReader::ReadMethodPool(Selector Sel) {
4701 ReadMethodPoolVisitor Visitor(*this, Sel);
4702 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
4703 std::pair<ObjCMethodList, ObjCMethodList> Result;
4704 Result.first = Visitor.getInstanceMethods();
4705 Result.second = Visitor.getFactoryMethods();
4706
4707 if (!Result.first.Method && !Result.second.Method)
4708 ++NumMethodPoolMisses;
4709 return Result;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004710}
4711
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004712void ASTReader::ReadKnownNamespaces(
Chris Lattner5f9e2722011-07-23 10:55:15 +00004713 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004714 Namespaces.clear();
4715
4716 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
4717 if (NamespaceDecl *Namespace
4718 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
4719 Namespaces.push_back(Namespace);
4720 }
4721}
4722
Douglas Gregora8623202011-07-27 20:58:46 +00004723void ASTReader::ReadTentativeDefinitions(
4724 SmallVectorImpl<VarDecl *> &TentativeDefs) {
4725 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4726 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
4727 if (Var)
4728 TentativeDefs.push_back(Var);
4729 }
4730 TentativeDefinitions.clear();
4731}
4732
Douglas Gregora2ee20a2011-07-27 21:45:57 +00004733void ASTReader::ReadUnusedFileScopedDecls(
4734 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
4735 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4736 DeclaratorDecl *D
4737 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4738 if (D)
4739 Decls.push_back(D);
4740 }
4741 UnusedFileScopedDecls.clear();
4742}
4743
Douglas Gregor0129b562011-07-27 21:57:17 +00004744void ASTReader::ReadDelegatingConstructors(
4745 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
4746 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
4747 CXXConstructorDecl *D
4748 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
4749 if (D)
4750 Decls.push_back(D);
4751 }
4752 DelegatingCtorDecls.clear();
4753}
4754
Douglas Gregord58a0a52011-07-28 00:39:29 +00004755void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
4756 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
4757 TypedefNameDecl *D
4758 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
4759 if (D)
4760 Decls.push_back(D);
4761 }
4762 ExtVectorDecls.clear();
4763}
4764
Douglas Gregora126f172011-07-28 00:53:40 +00004765void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
4766 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
4767 CXXRecordDecl *D
4768 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
4769 if (D)
4770 Decls.push_back(D);
4771 }
4772 DynamicClasses.clear();
4773}
4774
Douglas Gregorec12ce22011-07-28 14:20:37 +00004775void
4776ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
4777 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4778 NamedDecl *D
4779 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4780 if (D)
4781 Decls.push_back(D);
4782 }
4783 LocallyScopedExternalDecls.clear();
4784}
4785
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00004786void ASTReader::ReadReferencedSelectors(
4787 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
4788 if (ReferencedSelectorsData.empty())
4789 return;
4790
4791 // If there are @selector references added them to its pool. This is for
4792 // implementation of -Wselector.
4793 unsigned int DataSize = ReferencedSelectorsData.size()-1;
4794 unsigned I = 0;
4795 while (I < DataSize) {
4796 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
4797 SourceLocation SelLoc
4798 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
4799 Sels.push_back(std::make_pair(Sel, SelLoc));
4800 }
4801 ReferencedSelectorsData.clear();
4802}
4803
Douglas Gregor31e37b22011-07-28 18:09:57 +00004804void ASTReader::ReadWeakUndeclaredIdentifiers(
4805 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
4806 if (WeakUndeclaredIdentifiers.empty())
4807 return;
4808
4809 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
4810 IdentifierInfo *WeakId
4811 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4812 IdentifierInfo *AliasId
4813 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4814 SourceLocation Loc
4815 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
4816 bool Used = WeakUndeclaredIdentifiers[I++];
4817 WeakInfo WI(AliasId, Loc);
4818 WI.setUsed(Used);
4819 WeakIDs.push_back(std::make_pair(WeakId, WI));
4820 }
4821 WeakUndeclaredIdentifiers.clear();
4822}
4823
Douglas Gregordfe65432011-07-28 19:11:31 +00004824void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
4825 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
4826 ExternalVTableUse VT;
4827 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4828 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
4829 VT.DefinitionRequired = VTableUses[Idx++];
4830 VTables.push_back(VT);
4831 }
4832
4833 VTableUses.clear();
4834}
4835
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00004836void ASTReader::ReadPendingInstantiations(
4837 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
4838 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
4839 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
4840 SourceLocation Loc
4841 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
4842 Pending.push_back(std::make_pair(D, Loc));
4843 }
4844 PendingInstantiations.clear();
4845}
4846
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004847void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004848 // It would be complicated to avoid reading the methods anyway. So don't.
4849 ReadMethodPool(Sel);
4850}
4851
Douglas Gregor95eab172011-07-28 20:55:49 +00004852void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00004853 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00004854 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004855 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004856 if (DeserializationListener)
4857 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00004858}
4859
Douglas Gregord89275b2009-07-06 18:54:52 +00004860/// \brief Set the globally-visible declarations associated with the given
4861/// identifier.
4862///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004863/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00004864/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00004865/// them.
4866///
4867/// \param II an IdentifierInfo that refers to one or more globally-visible
4868/// declarations.
4869///
4870/// \param DeclIDs the set of declaration IDs with the name @p II that are
4871/// visible at global scope.
4872///
4873/// \param Nonrecursive should be true to indicate that the caller knows that
4874/// this call is non-recursive, and therefore the globally-visible declarations
4875/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00004876void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004877ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004878 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregord89275b2009-07-06 18:54:52 +00004879 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004880 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004881 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4882 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4883 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004884 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00004885 return;
4886 }
Mike Stump1eb44332009-09-09 15:08:12 +00004887
Douglas Gregord89275b2009-07-06 18:54:52 +00004888 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4889 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4890 if (SemaObj) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00004891 // Introduce this declaration into the translation-unit scope
4892 // and add it to the declaration chain for this identifier, so
4893 // that (unqualified) name lookup will find it.
4894 SemaObj->pushExternalDeclIntoScope(D, II);
Douglas Gregord89275b2009-07-06 18:54:52 +00004895 } else {
4896 // Queue this declaration so that it will be added to the
4897 // translation unit scope and identifier's declaration chain
4898 // once a Sema object is known.
4899 PreloadedDecls.push_back(D);
4900 }
4901 }
4902}
4903
Douglas Gregor95eab172011-07-28 20:55:49 +00004904IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00004905 if (ID == 0)
4906 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00004907
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004908 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004909 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00004910 return 0;
4911 }
Mike Stump1eb44332009-09-09 15:08:12 +00004912
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004913 ID -= 1;
4914 if (!IdentifiersLoaded[ID]) {
Douglas Gregor67268d02011-07-20 00:59:32 +00004915 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
4916 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregor9827a802011-07-29 00:56:45 +00004917 Module *M = I->second;
4918 unsigned Index = ID - M->BaseIdentifierID;
4919 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregord6595a42009-04-25 21:04:17 +00004920
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004921 // All of the strings in the AST file are preceded by a 16-bit length.
4922 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00004923 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4924 // unsigned integers. This is important to avoid integer overflow when
4925 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00004926 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00004927 unsigned StrLen = (((unsigned) StrLenPtr[0])
4928 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004929 IdentifiersLoaded[ID]
Douglas Gregor712f2fc2011-09-09 22:02:16 +00004930 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004931 if (DeserializationListener)
4932 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00004933 }
Mike Stump1eb44332009-09-09 15:08:12 +00004934
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004935 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004936}
4937
Douglas Gregor95eab172011-07-28 20:55:49 +00004938IdentifierInfo *ASTReader::getLocalIdentifier(Module &M, unsigned LocalID) {
4939 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
4940}
4941
4942IdentifierID ASTReader::getGlobalIdentifierID(Module &M, unsigned LocalID) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +00004943 if (LocalID < NUM_PREDEF_IDENT_IDS)
4944 return LocalID;
4945
4946 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4947 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
4948 assert(I != M.IdentifierRemap.end()
4949 && "Invalid index into identifier index remap");
4950
4951 return LocalID + I->second;
Douglas Gregor95eab172011-07-28 20:55:49 +00004952}
4953
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004954bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregore23ac652011-04-20 00:21:03 +00004955 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00004956}
4957
Douglas Gregor2d2689a2011-07-28 21:16:51 +00004958Selector ASTReader::getLocalSelector(Module &M, unsigned LocalID) {
4959 return DecodeSelector(getGlobalSelectorID(M, LocalID));
4960}
4961
4962Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004963 if (ID == 0)
4964 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00004965
Sebastian Redl725cd962010-08-04 20:40:17 +00004966 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004967 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004968 return Selector();
4969 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004970
Sebastian Redl725cd962010-08-04 20:40:17 +00004971 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004972 // Load this selector from the selector table.
Douglas Gregor96958cb2011-07-20 01:10:58 +00004973 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
4974 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregor9827a802011-07-29 00:56:45 +00004975 Module &M = *I->second;
4976 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00004977 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor96958cb2011-07-20 01:10:58 +00004978 SelectorsLoaded[ID - 1] =
Douglas Gregor9827a802011-07-29 00:56:45 +00004979 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor96958cb2011-07-20 01:10:58 +00004980 if (DeserializationListener)
4981 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor83941df2009-04-25 17:48:32 +00004982 }
4983
Sebastian Redl725cd962010-08-04 20:40:17 +00004984 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004985}
4986
Douglas Gregor8451ec72011-07-28 14:41:43 +00004987Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00004988 return DecodeSelector(ID);
4989}
4990
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004991uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00004992 // ID 0 (the null selector) is considered an external selector.
4993 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00004994}
4995
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00004996serialization::SelectorID
4997ASTReader::getGlobalSelectorID(Module &M, unsigned LocalID) const {
4998 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
4999 return LocalID;
5000
5001 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5002 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
5003 assert(I != M.SelectorRemap.end()
5004 && "Invalid index into identifier index remap");
5005
5006 return LocalID + I->second;
Douglas Gregor8451ec72011-07-28 14:41:43 +00005007}
5008
Mike Stump1eb44332009-09-09 15:08:12 +00005009DeclarationName
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005010ASTReader::ReadDeclarationName(Module &F,
Douglas Gregor393f2492011-07-22 00:38:23 +00005011 const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00005012 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
5013 switch (Kind) {
5014 case DeclarationName::Identifier:
Douglas Gregor95eab172011-07-28 20:55:49 +00005015 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005016
5017 case DeclarationName::ObjCZeroArgSelector:
5018 case DeclarationName::ObjCOneArgSelector:
5019 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor2d2689a2011-07-28 21:16:51 +00005020 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005021
5022 case DeclarationName::CXXConstructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005023 return Context.DeclarationNames.getCXXConstructorName(
5024 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005025
5026 case DeclarationName::CXXDestructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005027 return Context.DeclarationNames.getCXXDestructorName(
5028 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005029
5030 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor35942772011-09-09 21:34:22 +00005031 return Context.DeclarationNames.getCXXConversionFunctionName(
5032 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005033
5034 case DeclarationName::CXXOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005035 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00005036 (OverloadedOperatorKind)Record[Idx++]);
5037
Sean Hunt3e518bd2009-11-29 07:34:05 +00005038 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005039 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregor95eab172011-07-28 20:55:49 +00005040 GetIdentifierInfo(F, Record, Idx));
Sean Hunt3e518bd2009-11-29 07:34:05 +00005041
Douglas Gregor2cf26342009-04-09 22:27:44 +00005042 case DeclarationName::CXXUsingDirective:
5043 return DeclarationName::getUsingDirectiveName();
5044 }
5045
5046 // Required to silence GCC warning
5047 return DeclarationName();
5048}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00005049
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005050void ASTReader::ReadDeclarationNameLoc(Module &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005051 DeclarationNameLoc &DNLoc,
5052 DeclarationName Name,
5053 const RecordData &Record, unsigned &Idx) {
5054 switch (Name.getNameKind()) {
5055 case DeclarationName::CXXConstructorName:
5056 case DeclarationName::CXXDestructorName:
5057 case DeclarationName::CXXConversionFunctionName:
5058 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
5059 break;
5060
5061 case DeclarationName::CXXOperatorName:
5062 DNLoc.CXXOperatorName.BeginOpNameLoc
5063 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5064 DNLoc.CXXOperatorName.EndOpNameLoc
5065 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5066 break;
5067
5068 case DeclarationName::CXXLiteralOperatorName:
5069 DNLoc.CXXLiteralOperatorName.OpNameLoc
5070 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5071 break;
5072
5073 case DeclarationName::Identifier:
5074 case DeclarationName::ObjCZeroArgSelector:
5075 case DeclarationName::ObjCOneArgSelector:
5076 case DeclarationName::ObjCMultiArgSelector:
5077 case DeclarationName::CXXUsingDirective:
5078 break;
5079 }
5080}
5081
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005082void ASTReader::ReadDeclarationNameInfo(Module &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005083 DeclarationNameInfo &NameInfo,
5084 const RecordData &Record, unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00005085 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005086 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
5087 DeclarationNameLoc DNLoc;
5088 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
5089 NameInfo.setInfo(DNLoc);
5090}
5091
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005092void ASTReader::ReadQualifierInfo(Module &F, QualifierInfo &Info,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005093 const RecordData &Record, unsigned &Idx) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005094 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005095 unsigned NumTPLists = Record[Idx++];
5096 Info.NumTemplParamLists = NumTPLists;
5097 if (NumTPLists) {
Douglas Gregor35942772011-09-09 21:34:22 +00005098 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005099 for (unsigned i=0; i != NumTPLists; ++i)
5100 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
5101 }
5102}
5103
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005104TemplateName
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005105ASTReader::ReadTemplateName(Module &F, const RecordData &Record,
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005106 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00005107 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005108 switch (Kind) {
5109 case TemplateName::Template:
Douglas Gregor409448c2011-07-21 22:35:25 +00005110 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005111
5112 case TemplateName::OverloadedTemplate: {
5113 unsigned size = Record[Idx++];
5114 UnresolvedSet<8> Decls;
5115 while (size--)
Douglas Gregor409448c2011-07-21 22:35:25 +00005116 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005117
Douglas Gregor35942772011-09-09 21:34:22 +00005118 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005119 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005120
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005121 case TemplateName::QualifiedTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005122 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005123 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00005124 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005125 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005126 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005127
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005128 case TemplateName::DependentTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005129 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005130 if (Record[Idx++]) // isIdentifier
Douglas Gregor35942772011-09-09 21:34:22 +00005131 return Context.getDependentTemplateName(NNS,
Douglas Gregor95eab172011-07-28 20:55:49 +00005132 GetIdentifierInfo(F, Record,
5133 Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00005134 return Context.getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00005135 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005136 }
John McCall14606042011-06-30 08:33:18 +00005137
5138 case TemplateName::SubstTemplateTemplateParm: {
5139 TemplateTemplateParmDecl *param
Douglas Gregor409448c2011-07-21 22:35:25 +00005140 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCall14606042011-06-30 08:33:18 +00005141 if (!param) return TemplateName();
5142 TemplateName replacement = ReadTemplateName(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005143 return Context.getSubstTemplateTemplateParm(param, replacement);
John McCall14606042011-06-30 08:33:18 +00005144 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005145
5146 case TemplateName::SubstTemplateTemplateParmPack: {
5147 TemplateTemplateParmDecl *Param
Douglas Gregor409448c2011-07-21 22:35:25 +00005148 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005149 if (!Param)
5150 return TemplateName();
5151
5152 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
5153 if (ArgPack.getKind() != TemplateArgument::Pack)
5154 return TemplateName();
5155
Douglas Gregor35942772011-09-09 21:34:22 +00005156 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005157 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005158 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005159
David Blaikieb219cfc2011-09-23 05:06:16 +00005160 llvm_unreachable("Unhandled template name kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005161}
5162
5163TemplateArgument
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005164ASTReader::ReadTemplateArgument(Module &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00005165 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00005166 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
5167 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005168 case TemplateArgument::Null:
5169 return TemplateArgument();
5170 case TemplateArgument::Type:
Douglas Gregor393f2492011-07-22 00:38:23 +00005171 return TemplateArgument(readType(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005172 case TemplateArgument::Declaration:
Douglas Gregor409448c2011-07-21 22:35:25 +00005173 return TemplateArgument(ReadDecl(F, Record, Idx));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00005174 case TemplateArgument::Integral: {
5175 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00005176 QualType T = readType(F, Record, Idx);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00005177 return TemplateArgument(Value, T);
5178 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00005179 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005180 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00005181 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005182 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00005183 llvm::Optional<unsigned> NumTemplateExpansions;
5184 if (unsigned NumExpansions = Record[Idx++])
5185 NumTemplateExpansions = NumExpansions - 1;
5186 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00005187 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005188 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00005189 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005190 case TemplateArgument::Pack: {
5191 unsigned NumArgs = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00005192 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
Douglas Gregor910f8002010-11-07 23:05:16 +00005193 for (unsigned I = 0; I != NumArgs; ++I)
5194 Args[I] = ReadTemplateArgument(F, Record, Idx);
5195 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005196 }
5197 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005198
David Blaikieb219cfc2011-09-23 05:06:16 +00005199 llvm_unreachable("Unhandled template argument kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005200}
5201
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005202TemplateParameterList *
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005203ASTReader::ReadTemplateParameterList(Module &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00005204 const RecordData &Record, unsigned &Idx) {
5205 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
5206 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
5207 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005208
5209 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00005210 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005211 Params.reserve(NumParams);
5212 while (NumParams--)
Douglas Gregor409448c2011-07-21 22:35:25 +00005213 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer20249a12010-10-21 03:16:25 +00005214
5215 TemplateParameterList* TemplateParams =
Douglas Gregor35942772011-09-09 21:34:22 +00005216 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005217 Params.data(), Params.size(), RAngleLoc);
5218 return TemplateParams;
5219}
5220
5221void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005222ASTReader::
Chris Lattner5f9e2722011-07-23 10:55:15 +00005223ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005224 Module &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00005225 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005226 unsigned NumTemplateArgs = Record[Idx++];
5227 TemplArgs.reserve(NumTemplateArgs);
5228 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00005229 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005230}
5231
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005232/// \brief Read a UnresolvedSet structure.
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005233void ASTReader::ReadUnresolvedSet(Module &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005234 const RecordData &Record, unsigned &Idx) {
5235 unsigned NumDecls = Record[Idx++];
5236 while (NumDecls--) {
Douglas Gregor409448c2011-07-21 22:35:25 +00005237 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005238 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
5239 Set.addDecl(D, AS);
5240 }
5241}
5242
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005243CXXBaseSpecifier
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005244ASTReader::ReadCXXBaseSpecifier(Module &F,
Nick Lewycky56062202010-07-26 16:56:01 +00005245 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005246 bool isVirtual = static_cast<bool>(Record[Idx++]);
5247 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
5248 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00005249 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00005250 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
5251 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00005252 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00005253 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00005254 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00005255 Result.setInheritConstructors(inheritConstructors);
5256 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005257}
5258
Sean Huntcbb67482011-01-08 20:30:50 +00005259std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005260ASTReader::ReadCXXCtorInitializers(Module &F, const RecordData &Record,
Sean Huntcbb67482011-01-08 20:30:50 +00005261 unsigned &Idx) {
5262 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005263 unsigned NumInitializers = Record[Idx++];
5264 if (NumInitializers) {
Sean Huntcbb67482011-01-08 20:30:50 +00005265 CtorInitializers
Douglas Gregor35942772011-09-09 21:34:22 +00005266 = new (Context) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005267 for (unsigned i=0; i != NumInitializers; ++i) {
Douglas Gregor76852c22011-11-01 01:16:03 +00005268 TypeSourceInfo *TInfo = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005269 bool IsBaseVirtual = false;
5270 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00005271 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00005272
Sean Hunt156b6402011-05-04 01:19:08 +00005273 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
5274 switch (Type) {
Douglas Gregor76852c22011-11-01 01:16:03 +00005275 case CTOR_INITIALIZER_BASE:
5276 TInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005277 IsBaseVirtual = Record[Idx++];
Sean Hunt156b6402011-05-04 01:19:08 +00005278 break;
Douglas Gregor76852c22011-11-01 01:16:03 +00005279
5280 case CTOR_INITIALIZER_DELEGATING:
5281 TInfo = GetTypeSourceInfo(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00005282 break;
5283
5284 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00005285 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00005286 break;
5287
5288 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00005289 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00005290 break;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005291 }
Sean Hunt156b6402011-05-04 01:19:08 +00005292
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00005293 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00005294 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00005295 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
5296 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005297 bool IsWritten = Record[Idx++];
5298 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner5f9e2722011-07-23 10:55:15 +00005299 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005300 if (IsWritten) {
5301 SourceOrderOrNumArrayIndices = Record[Idx++];
5302 } else {
5303 SourceOrderOrNumArrayIndices = Record[Idx++];
5304 Indices.reserve(SourceOrderOrNumArrayIndices);
5305 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor409448c2011-07-21 22:35:25 +00005306 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005307 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005308
Sean Huntcbb67482011-01-08 20:30:50 +00005309 CXXCtorInitializer *BOMInit;
Sean Hunt156b6402011-05-04 01:19:08 +00005310 if (Type == CTOR_INITIALIZER_BASE) {
Douglas Gregor76852c22011-11-01 01:16:03 +00005311 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
Sean Huntcbb67482011-01-08 20:30:50 +00005312 LParenLoc, Init, RParenLoc,
5313 MemberOrEllipsisLoc);
Sean Hunt156b6402011-05-04 01:19:08 +00005314 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
Douglas Gregor76852c22011-11-01 01:16:03 +00005315 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
5316 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005317 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00005318 if (Member)
Douglas Gregor35942772011-09-09 21:34:22 +00005319 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00005320 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00005321 else
Douglas Gregor35942772011-09-09 21:34:22 +00005322 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
Sean Huntcbb67482011-01-08 20:30:50 +00005323 MemberOrEllipsisLoc, LParenLoc,
5324 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005325 } else {
Douglas Gregor35942772011-09-09 21:34:22 +00005326 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00005327 LParenLoc, Init, RParenLoc,
5328 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005329 }
5330
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00005331 if (IsWritten)
5332 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00005333 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005334 }
5335 }
5336
Sean Huntcbb67482011-01-08 20:30:50 +00005337 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005338}
5339
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005340NestedNameSpecifier *
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005341ASTReader::ReadNestedNameSpecifier(Module &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00005342 const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005343 unsigned N = Record[Idx++];
5344 NestedNameSpecifier *NNS = 0, *Prev = 0;
5345 for (unsigned I = 0; I != N; ++I) {
5346 NestedNameSpecifier::SpecifierKind Kind
5347 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5348 switch (Kind) {
5349 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00005350 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005351 NNS = NestedNameSpecifier::Create(Context, Prev, II);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005352 break;
5353 }
5354
5355 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005356 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005357 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005358 break;
5359 }
5360
Douglas Gregor14aba762011-02-24 02:36:08 +00005361 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005362 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005363 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
Douglas Gregor14aba762011-02-24 02:36:08 +00005364 break;
5365 }
5366
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005367 case NestedNameSpecifier::TypeSpec:
5368 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor393f2492011-07-22 00:38:23 +00005369 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00005370 if (!T)
5371 return 0;
5372
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005373 bool Template = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00005374 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005375 break;
5376 }
5377
5378 case NestedNameSpecifier::Global: {
Douglas Gregor35942772011-09-09 21:34:22 +00005379 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005380 // No associated value, and there can't be a prefix.
5381 break;
5382 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005383 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00005384 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005385 }
5386 return NNS;
5387}
5388
Douglas Gregordc355712011-02-25 00:36:19 +00005389NestedNameSpecifierLoc
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005390ASTReader::ReadNestedNameSpecifierLoc(Module &F, const RecordData &Record,
Douglas Gregordc355712011-02-25 00:36:19 +00005391 unsigned &Idx) {
5392 unsigned N = Record[Idx++];
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005393 NestedNameSpecifierLocBuilder Builder;
Douglas Gregordc355712011-02-25 00:36:19 +00005394 for (unsigned I = 0; I != N; ++I) {
5395 NestedNameSpecifier::SpecifierKind Kind
5396 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5397 switch (Kind) {
5398 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00005399 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00005400 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005401 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00005402 break;
5403 }
5404
5405 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005406 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00005407 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005408 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00005409 break;
5410 }
5411
5412 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005413 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00005414 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005415 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00005416 break;
5417 }
5418
5419 case NestedNameSpecifier::TypeSpec:
5420 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregordc355712011-02-25 00:36:19 +00005421 bool Template = Record[Idx++];
5422 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
5423 if (!T)
5424 return NestedNameSpecifierLoc();
Douglas Gregordc355712011-02-25 00:36:19 +00005425 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005426
5427 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
Douglas Gregor35942772011-09-09 21:34:22 +00005428 Builder.Extend(Context,
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005429 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
5430 T->getTypeLoc(), ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00005431 break;
5432 }
5433
5434 case NestedNameSpecifier::Global: {
Douglas Gregordc355712011-02-25 00:36:19 +00005435 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005436 Builder.MakeGlobal(Context, ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00005437 break;
5438 }
5439 }
Douglas Gregordc355712011-02-25 00:36:19 +00005440 }
5441
Douglas Gregor35942772011-09-09 21:34:22 +00005442 return Builder.getWithLocInContext(Context);
Douglas Gregordc355712011-02-25 00:36:19 +00005443}
5444
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005445SourceRange
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005446ASTReader::ReadSourceRange(Module &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00005447 unsigned &Idx) {
5448 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
5449 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00005450 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005451}
5452
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00005453/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005454llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00005455 unsigned BitWidth = Record[Idx++];
5456 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
5457 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
5458 Idx += NumWords;
5459 return Result;
5460}
5461
5462/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005463llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00005464 bool isUnsigned = Record[Idx++];
5465 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
5466}
5467
Douglas Gregor17fc2232009-04-14 21:55:33 +00005468/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005469llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00005470 return llvm::APFloat(ReadAPInt(Record, Idx));
5471}
5472
Douglas Gregor68a2eb02009-04-15 21:30:51 +00005473// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005474std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00005475 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00005476 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00005477 Idx += Len;
5478 return Result;
5479}
5480
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005481VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
5482 unsigned &Idx) {
5483 unsigned Major = Record[Idx++];
5484 unsigned Minor = Record[Idx++];
5485 unsigned Subminor = Record[Idx++];
5486 if (Minor == 0)
5487 return VersionTuple(Major);
5488 if (Subminor == 0)
5489 return VersionTuple(Major, Minor - 1);
5490 return VersionTuple(Major, Minor - 1, Subminor - 1);
5491}
5492
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005493CXXTemporary *ASTReader::ReadCXXTemporary(Module &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00005494 const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00005495 unsigned &Idx) {
Douglas Gregor409448c2011-07-21 22:35:25 +00005496 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005497 return CXXTemporary::Create(Context, Decl);
Chris Lattnerd2598362010-05-10 00:25:06 +00005498}
5499
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005500DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00005501 return Diag(SourceLocation(), DiagID);
5502}
5503
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005504DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00005505 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00005506}
Douglas Gregor025452f2009-04-17 00:04:06 +00005507
Douglas Gregor668c1a42009-04-21 22:25:48 +00005508/// \brief Retrieve the identifier table associated with the
5509/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005510IdentifierTable &ASTReader::getIdentifierTable() {
Douglas Gregor712f2fc2011-09-09 22:02:16 +00005511 return PP.getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00005512}
5513
Douglas Gregor025452f2009-04-17 00:04:06 +00005514/// \brief Record that the given ID maps to the given switch-case
5515/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005516void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00005517 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
5518 SwitchCaseStmts[ID] = SC;
5519}
5520
5521/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005522SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00005523 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
5524 return SwitchCaseStmts[ID];
5525}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00005526
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00005527void ASTReader::ClearSwitchCaseIDs() {
5528 SwitchCaseStmts.clear();
5529}
5530
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005531void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005532 assert(NumCurrentElementsDeserializing &&
5533 "FinishedDeserializing not paired with StartedDeserializing");
5534 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregord89275b2009-07-06 18:54:52 +00005535 // If any identifiers with corresponding top-level declarations have
5536 // been loaded, load those declarations now.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005537 while (!PendingIdentifierInfos.empty()) {
5538 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
5539 PendingIdentifierInfos.front().DeclIDs, true);
5540 PendingIdentifierInfos.pop_front();
Douglas Gregord89275b2009-07-06 18:54:52 +00005541 }
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005542
Argyrios Kyrtzidis0895d152011-02-12 07:50:47 +00005543 // Ready to load previous declarations of Decls that were delayed.
5544 while (!PendingPreviousDecls.empty()) {
5545 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
5546 PendingPreviousDecls.front().second);
5547 PendingPreviousDecls.pop_front();
5548 }
5549
Argyrios Kyrtzidis5a174992011-11-14 07:07:59 +00005550 for (std::vector<std::pair<ObjCInterfaceDecl *,
5551 serialization::DeclID> >::iterator
5552 I = PendingChainedObjCCategories.begin(),
5553 E = PendingChainedObjCCategories.end(); I != E; ++I) {
5554 loadObjCChainedCategories(I->second, I->first);
5555 }
5556 PendingChainedObjCCategories.clear();
5557
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005558 // We are not in recursive loading, so it's safe to pass the "interesting"
5559 // decls to the consumer.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005560 if (Consumer)
5561 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidis134db1f2010-10-24 17:26:31 +00005562
5563 assert(PendingForwardRefs.size() == 0 &&
5564 "Some forward refs did not get linked to the definition!");
Douglas Gregord89275b2009-07-06 18:54:52 +00005565 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005566 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00005567}
Douglas Gregor501c1032010-08-19 00:28:17 +00005568
Douglas Gregorf8a1e512011-09-02 00:26:20 +00005569ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
Douglas Gregor832d6202011-07-22 16:35:34 +00005570 StringRef isysroot, bool DisableValidation,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005571 bool DisableStatCache)
Sebastian Redle1dde812010-08-24 00:50:04 +00005572 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
5573 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00005574 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
Jonathan D. Turner1afb6612011-07-28 17:20:23 +00005575 Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
5576 RelocatablePCH(false), isysroot(isysroot),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005577 DisableValidation(DisableValidation),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005578 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005579 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005580 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
5581 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
5582 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
5583 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00005584 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
5585 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
5586 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005587{
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005588 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redle1dde812010-08-24 00:50:04 +00005589}
5590
Sebastian Redle1dde812010-08-24 00:50:04 +00005591ASTReader::~ASTReader() {
Sebastian Redle1dde812010-08-24 00:50:04 +00005592 for (DeclContextVisibleUpdatesPending::iterator
5593 I = PendingVisibleUpdates.begin(),
5594 E = PendingVisibleUpdates.end();
5595 I != E; ++I) {
5596 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5597 F = I->second.end();
5598 J != F; ++J)
Douglas Gregor496c7092011-08-03 15:48:04 +00005599 delete static_cast<ASTDeclContextNameLookupTable*>(J->first);
Sebastian Redle1dde812010-08-24 00:50:04 +00005600 }
5601}