blob: 0b2a884f82a40841340b17a0528fc961da1276fc [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 Gregora081da52011-11-16 20:05:18 +00001084 if (Record.size() < 7) {
1085 Error("source location entry is incorrect");
1086 return Failure;
1087 }
1088
1089 bool OverriddenBuffer = Record[6];
1090
1091 std::string OrigFilename(BlobStart, BlobStart + BlobLen);
1092 std::string Filename = OrigFilename;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001093 MaybeAddSystemRootToFilename(Filename);
Douglas Gregora081da52011-11-16 20:05:18 +00001094 const FileEntry *File = FileMgr.getFile(Filename, /*OpenFile=*/false,
1095 /*CacheFailure=*/!OverriddenBuffer);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001096 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1097 OriginalDir != CurrentDir) {
1098 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1099 OriginalDir,
1100 CurrentDir);
1101 if (!resolved.empty())
1102 File = FileMgr.getFile(resolved);
1103 }
Axel Naumann04331162011-01-27 10:55:51 +00001104 if (File == 0)
1105 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1106 (time_t)Record[5]);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001107 if (File == 0) {
1108 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001109 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001110 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001111 Error(ErrorStr.c_str());
1112 return Failure;
1113 }
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001115 if (!DisableValidation &&
1116 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001117#if !defined(LLVM_ON_WIN32)
1118 // In our regression testing, the Windows file system seems to
1119 // have inconsistent modification times that sometimes
1120 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001121 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001122#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001123 )) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +00001124 Error(diag::err_fe_pch_file_modified, Filename);
Douglas Gregor2d52be52010-03-21 22:49:54 +00001125 return Failure;
1126 }
1127
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001128 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001129 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001130 // This is the module's main file.
1131 IncludeLoc = getImportLocation(F);
1132 }
1133 FileID FID = SourceMgr.createFileID(File, IncludeLoc,
Chris Lattner75dfb652010-11-23 09:19:42 +00001134 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001135 ID, BaseOffset + Record[0]);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001136 SrcMgr::FileInfo &FileInfo =
1137 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
Douglas Gregora081da52011-11-16 20:05:18 +00001138 FileInfo.NumCreatedFIDs = Record[7];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001139 if (Record[3])
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001140 FileInfo.setHasLineDirectives();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001141
Douglas Gregora081da52011-11-16 20:05:18 +00001142 const DeclID *FirstDecl = F->FileSortedDecls + Record[8];
1143 unsigned NumFileDecls = Record[9];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001144 if (NumFileDecls) {
1145 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
Argyrios Kyrtzidis9d128d02011-10-31 07:20:08 +00001146 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1147 NumFileDecls));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001148 }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001149
Douglas Gregora081da52011-11-16 20:05:18 +00001150 if (OverriddenBuffer &&
1151 !SourceMgr.getOrCreateContentCache(File)->BufferOverridden) {
1152 unsigned Code = SLocEntryCursor.ReadCode();
1153 Record.clear();
1154 unsigned RecCode
1155 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1156
1157 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1158 Error("AST record has invalid code");
1159 return Failure;
1160 }
1161
1162 llvm::MemoryBuffer *Buffer
1163 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
1164 Filename);
1165 SourceMgr.overrideFileContents(File, Buffer);
1166 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001167 break;
1168 }
1169
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001170 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001171 const char *Name = BlobStart;
1172 unsigned Offset = Record[0];
1173 unsigned Code = SLocEntryCursor.ReadCode();
1174 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001175 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001176 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001177
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001178 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001179 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001180 return Failure;
1181 }
1182
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001183 llvm::MemoryBuffer *Buffer
Douglas Gregora081da52011-11-16 20:05:18 +00001184 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
1185 Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001186 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID,
1187 BaseOffset + Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Douglas Gregor92b059e2009-04-28 20:33:11 +00001189 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001190 PCHPredefinesBlock Block = {
1191 BufferID,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001192 StringRef(BlobStart, BlobLen - 1)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001193 };
1194 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001195 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001196
1197 break;
1198 }
1199
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001200 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001201 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruthbf340e42011-07-26 03:03:05 +00001202 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001203 ReadSourceLocation(*F, Record[2]),
1204 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001205 Record[4],
1206 ID,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001207 BaseOffset + Record[0]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001208 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001209 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001210 }
1211
1212 return Success;
1213}
1214
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001215/// \brief Find the location where the module F is imported.
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001216SourceLocation ASTReader::getImportLocation(Module *F) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001217 if (F->ImportLoc.isValid())
1218 return F->ImportLoc;
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001219
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001220 // Otherwise we have a PCH. It's considered to be "imported" at the first
1221 // location of its includer.
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001222 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001223 // Main file is the importer. We assume that it is the first entry in the
1224 // entry table. We can't ask the manager, because at the time of PCH loading
1225 // the main file entry doesn't exist yet.
1226 // The very first entry is the invalid instantiation loc, which takes up
1227 // offsets 0 and 1.
1228 return SourceLocation::getFromRawEncoding(2U);
1229 }
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001230 //return F->Loaders[0]->FirstLoc;
1231 return F->ImportedBy[0]->FirstLoc;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001232}
1233
Chris Lattner6367f6d2009-04-27 01:05:14 +00001234/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1235/// specified cursor. Read the abbreviations that are at the top of the block
1236/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001237bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001238 unsigned BlockID) {
1239 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001240 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001241 return Failure;
1242 }
Mike Stump1eb44332009-09-09 15:08:12 +00001243
Chris Lattner6367f6d2009-04-27 01:05:14 +00001244 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001245 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001246 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001247
Chris Lattner6367f6d2009-04-27 01:05:14 +00001248 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001249 if (Code != llvm::bitc::DEFINE_ABBREV) {
1250 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001251 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001252 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001253 Cursor.ReadAbbrevRecord();
1254 }
1255}
1256
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001257void ASTReader::ReadMacroRecord(Module &F, uint64_t Offset) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001258 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Douglas Gregor37e26842009-04-21 23:56:24 +00001260 // Keep track of where we are in the stream, then jump back there
1261 // after reading this macro.
1262 SavedStreamPosition SavedPosition(Stream);
1263
1264 Stream.JumpToBit(Offset);
1265 RecordData Record;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001266 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001267 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Douglas Gregor37e26842009-04-21 23:56:24 +00001269 while (true) {
1270 unsigned Code = Stream.ReadCode();
1271 switch (Code) {
1272 case llvm::bitc::END_BLOCK:
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001273 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001274
1275 case llvm::bitc::ENTER_SUBBLOCK:
1276 // No known subblocks, always skip them.
1277 Stream.ReadSubBlockID();
1278 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001279 Error("malformed block record in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001280 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001281 }
1282 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001283
Douglas Gregor37e26842009-04-21 23:56:24 +00001284 case llvm::bitc::DEFINE_ABBREV:
1285 Stream.ReadAbbrevRecord();
1286 continue;
1287 default: break;
1288 }
1289
1290 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001291 const char *BlobStart = 0;
1292 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001293 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001294 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001295 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001296 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001297 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001298 case PP_MACRO_OBJECT_LIKE:
1299 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001300 // If we already have a macro, that means that we've hit the end
1301 // of the definition of the macro we were looking for. We're
1302 // done.
1303 if (Macro)
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001304 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001305
Douglas Gregor95eab172011-07-28 20:55:49 +00001306 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001307 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001308 Error("macro must have a name in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001309 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001310 }
Douglas Gregoreee242f2011-10-27 09:33:13 +00001311
Sebastian Redlc3632732010-10-05 15:59:54 +00001312 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001313 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001314
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001315 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001316 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001317 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Douglas Gregoraa93a872011-10-17 15:32:29 +00001319 bool IsPublic = Record[3];
1320 unsigned NextIndex = 4;
1321 MI->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex));
Douglas Gregor7143aab2011-09-01 17:04:32 +00001322
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001323 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001324 // Decode function-like macro info.
Douglas Gregor7143aab2011-09-01 17:04:32 +00001325 bool isC99VarArgs = Record[NextIndex++];
1326 bool isGNUVarArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001327 MacroArgs.clear();
Douglas Gregor7143aab2011-09-01 17:04:32 +00001328 unsigned NumArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001329 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregor7143aab2011-09-01 17:04:32 +00001330 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001331
1332 // Install function-like macro info.
1333 MI->setIsFunctionLike();
1334 if (isC99VarArgs) MI->setIsC99Varargs();
1335 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001336 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001337 PP.getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001338 }
1339
1340 // Finally, install the macro.
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001341 PP.setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001342
1343 // Remember that we saw this macro last so that we add the tokens that
1344 // form its body to it.
1345 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001346
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001347 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1348 Record[NextIndex]) {
1349 // We have a macro definition. Register the association
1350 PreprocessedEntityID
1351 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1352 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1353 PPRec.RegisterMacroDefinition(Macro,
1354 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001355 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001356
Douglas Gregor37e26842009-04-21 23:56:24 +00001357 ++NumMacrosRead;
1358 break;
1359 }
Mike Stump1eb44332009-09-09 15:08:12 +00001360
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001361 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001362 // If we see a TOKEN before a PP_MACRO_*, then the file is
1363 // erroneous, just pretend we didn't see this.
1364 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001365
Douglas Gregor37e26842009-04-21 23:56:24 +00001366 Token Tok;
1367 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001368 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001369 Tok.setLength(Record[1]);
Douglas Gregor95eab172011-07-28 20:55:49 +00001370 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregor37e26842009-04-21 23:56:24 +00001371 Tok.setIdentifierInfo(II);
1372 Tok.setKind((tok::TokenKind)Record[3]);
1373 Tok.setFlag((Token::TokenFlags)Record[4]);
1374 Macro->AddTokenToBody(Tok);
1375 break;
1376 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001377 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001378 }
Douglas Gregor89d99802010-11-30 06:16:57 +00001379
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001380 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001381}
1382
Douglas Gregor86c67d82011-07-28 22:39:26 +00001383PreprocessedEntityID
Argyrios Kyrtzidis1f6d2252011-09-19 20:40:02 +00001384ASTReader::getGlobalPreprocessedEntityID(Module &M, unsigned LocalID) const {
1385 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001386 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1387 assert(I != M.PreprocessedEntityRemap.end()
1388 && "Invalid index into preprocessed entity index remap");
1389
1390 return LocalID + I->second;
Douglas Gregor86c67d82011-07-28 22:39:26 +00001391}
1392
Douglas Gregor98339b92011-08-25 20:47:51 +00001393unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1394 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001395}
Douglas Gregor98339b92011-08-25 20:47:51 +00001396
1397HeaderFileInfoTrait::internal_key_type
1398HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1399
1400bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1401 if (strcmp(a, b) == 0)
1402 return true;
1403
1404 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1405 return false;
1406
1407 // The file names match, but the path names don't. stat() the files to
1408 // see if they are the same.
1409 struct stat StatBufA, StatBufB;
1410 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1411 return false;
1412
1413 return StatBufA.st_ino == StatBufB.st_ino;
1414}
1415
1416std::pair<unsigned, unsigned>
1417HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1418 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1419 unsigned DataLen = (unsigned) *d++;
1420 return std::make_pair(KeyLen + 1, DataLen);
1421}
1422
1423HeaderFileInfoTrait::data_type
1424HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1425 unsigned DataLen) {
1426 const unsigned char *End = d + DataLen;
1427 using namespace clang::io;
1428 HeaderFileInfo HFI;
1429 unsigned Flags = *d++;
1430 HFI.isImport = (Flags >> 5) & 0x01;
1431 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1432 HFI.DirInfo = (Flags >> 2) & 0x03;
1433 HFI.Resolved = (Flags >> 1) & 0x01;
1434 HFI.IndexHeaderMapHeader = Flags & 0x01;
1435 HFI.NumIncludes = ReadUnalignedLE16(d);
Douglas Gregor541ba162011-10-17 18:53:12 +00001436 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1437 ReadUnalignedLE32(d));
Douglas Gregor98339b92011-08-25 20:47:51 +00001438 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1439 // The framework offset is 1 greater than the actual offset,
1440 // since 0 is used as an indicator for "no framework name".
1441 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1442 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1443 }
1444
1445 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1446 (void)End;
1447
1448 // This HeaderFileInfo was externally loaded.
1449 HFI.External = true;
1450 return HFI;
1451}
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001452
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001453void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, Module &F,
Douglas Gregor2d2689a2011-07-28 21:16:51 +00001454 uint64_t LocalOffset) {
Douglas Gregor295a2a62010-10-30 00:23:06 +00001455 // Note that this identifier has a macro definition.
1456 II->setHasMacroDefinition(true);
1457
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001458 // Adjust the offset to a global offset.
Douglas Gregor2d2689a2011-07-28 21:16:51 +00001459 UnreadMacroRecordOffsets[II] = F.GlobalBitOffset + LocalOffset;
Douglas Gregor295a2a62010-10-30 00:23:06 +00001460}
1461
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001462void ASTReader::ReadDefinedMacros() {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001463 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1464 E = ModuleMgr.rend(); I != E; ++I) {
1465 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001466
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001467 // If there was no preprocessor block, skip this file.
1468 if (!MacroCursor.getBitStreamReader())
1469 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001470
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001471 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001472 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001473
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001474 RecordData Record;
1475 while (true) {
1476 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001477 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001478 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001479
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001480 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1481 // No known subblocks, always skip them.
1482 Cursor.ReadSubBlockID();
1483 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001484 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001485 return;
1486 }
1487 continue;
1488 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001489
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001490 if (Code == llvm::bitc::DEFINE_ABBREV) {
1491 Cursor.ReadAbbrevRecord();
1492 continue;
1493 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001494
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001495 // Read a record.
1496 const char *BlobStart;
1497 unsigned BlobLen;
1498 Record.clear();
1499 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1500 default: // Default behavior: ignore.
1501 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001502
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001503 case PP_MACRO_OBJECT_LIKE:
1504 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregor95eab172011-07-28 20:55:49 +00001505 getLocalIdentifier(**I, Record[0]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001506 break;
1507
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001508 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001509 // Ignore tokens.
1510 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001511 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001512 }
1513 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001514
1515 // Drain the unread macro-record offsets map.
1516 while (!UnreadMacroRecordOffsets.empty())
1517 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1518}
1519
1520void ASTReader::LoadMacroDefinition(
1521 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1522 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
Douglas Gregor295a2a62010-10-30 00:23:06 +00001523 uint64_t Offset = Pos->second;
1524 UnreadMacroRecordOffsets.erase(Pos);
1525
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001526 RecordLocation Loc = getLocalBitOffset(Offset);
1527 ReadMacroRecord(*Loc.F, Loc.Offset);
Douglas Gregor295a2a62010-10-30 00:23:06 +00001528}
1529
1530void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1531 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1532 = UnreadMacroRecordOffsets.find(II);
1533 LoadMacroDefinition(Pos);
Douglas Gregor88a35862010-01-04 19:18:44 +00001534}
1535
Douglas Gregoreee242f2011-10-27 09:33:13 +00001536namespace {
1537 /// \brief Visitor class used to look up identifirs in an AST file.
1538 class IdentifierLookupVisitor {
1539 StringRef Name;
1540 IdentifierInfo *Found;
1541 public:
1542 explicit IdentifierLookupVisitor(StringRef Name) : Name(Name), Found() { }
1543
1544 static bool visit(Module &M, void *UserData) {
1545 IdentifierLookupVisitor *This
1546 = static_cast<IdentifierLookupVisitor *>(UserData);
1547
1548 ASTIdentifierLookupTable *IdTable
1549 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1550 if (!IdTable)
1551 return false;
1552
1553 std::pair<const char*, unsigned> Key(This->Name.begin(),
1554 This->Name.size());
1555 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
1556 if (Pos == IdTable->end())
1557 return false;
1558
1559 // Dereferencing the iterator has the effect of building the
1560 // IdentifierInfo node and populating it with the various
1561 // declarations it needs.
1562 This->Found = *Pos;
1563 return true;
1564 }
1565
1566 // \brief Retrieve the identifier info found within the module
1567 // files.
1568 IdentifierInfo *getIdentifierInfo() const { return Found; }
1569 };
1570}
1571
1572void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1573 get(II.getName());
1574}
1575
Chris Lattner5f9e2722011-07-23 10:55:15 +00001576const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001577 std::string Filename = filenameStrRef;
1578 MaybeAddSystemRootToFilename(Filename);
1579 const FileEntry *File = FileMgr.getFile(Filename);
1580 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1581 OriginalDir != CurrentDir) {
1582 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1583 OriginalDir,
1584 CurrentDir);
1585 if (!resolved.empty())
1586 File = FileMgr.getFile(resolved);
1587 }
1588
1589 return File;
1590}
1591
Douglas Gregore650c8c2009-07-07 00:12:59 +00001592/// \brief If we are loading a relocatable PCH file, and the filename is
1593/// not an absolute path, add the system root to the beginning of the file
1594/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001595void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001596 // If this is not a relocatable PCH file, there's nothing to do.
1597 if (!RelocatablePCH)
1598 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001599
Michael J. Spencer256053b2010-12-17 21:22:22 +00001600 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregore650c8c2009-07-07 00:12:59 +00001601 return;
1602
Douglas Gregor832d6202011-07-22 16:35:34 +00001603 if (isysroot.empty()) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001604 // If no system root was given, default to '/'
1605 Filename.insert(Filename.begin(), '/');
1606 return;
1607 }
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Douglas Gregor832d6202011-07-22 16:35:34 +00001609 unsigned Length = isysroot.size();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001610 if (isysroot[Length - 1] != '/')
1611 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Douglas Gregor832d6202011-07-22 16:35:34 +00001613 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregore650c8c2009-07-07 00:12:59 +00001614}
1615
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001616ASTReader::ASTReadResult
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001617ASTReader::ReadASTBlock(Module &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001618 llvm::BitstreamCursor &Stream = F.Stream;
1619
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001620 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001621 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001622 return Failure;
1623 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001624
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001625 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001626 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001627 while (!Stream.AtEndOfStream()) {
1628 unsigned Code = Stream.ReadCode();
1629 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001630 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001631 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001632 return Failure;
1633 }
Chris Lattner7356a312009-04-11 21:15:38 +00001634
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001635 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001636 }
1637
1638 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1639 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001640 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001641 // We lazily load the decls block, but we want to set up the
1642 // DeclsCursor cursor to point into it. Clone our current bitcode
1643 // cursor to it, enter the block and read the abbrevs in that block.
1644 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001645 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001646 if (Stream.SkipBlock() || // Skip with the main cursor.
1647 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001648 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001649 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001650 return Failure;
1651 }
1652 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001653
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001654 case DECL_UPDATES_BLOCK_ID:
1655 if (Stream.SkipBlock()) {
1656 Error("malformed block record in AST file");
1657 return Failure;
1658 }
1659 break;
1660
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001661 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001662 F.MacroCursor = Stream;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001663 if (!PP.getExternalSource())
1664 PP.setExternalSource(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00001665
Douglas Gregorecdcb882010-10-20 22:00:55 +00001666 if (Stream.SkipBlock() ||
1667 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001668 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001669 return Failure;
1670 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001671 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001672 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001673
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001674 case PREPROCESSOR_DETAIL_BLOCK_ID:
1675 F.PreprocessorDetailCursor = Stream;
1676 if (Stream.SkipBlock() ||
1677 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1678 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1679 Error("malformed preprocessor detail record in AST file");
1680 return Failure;
1681 }
1682 F.PreprocessorDetailStartOffset
1683 = F.PreprocessorDetailCursor.GetCurrentBitNo();
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001684
1685 if (!PP.getPreprocessingRecord())
1686 PP.createPreprocessingRecord(true);
1687 if (!PP.getPreprocessingRecord()->getExternalSource())
1688 PP.getPreprocessingRecord()->SetExternalSource(*this);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001689 break;
1690
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001691 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001692 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001693 case Success:
1694 break;
1695
1696 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001697 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001698 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001699
1700 case IgnorePCH:
1701 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001702 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001703 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001704 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001705 continue;
1706 }
1707
1708 if (Code == llvm::bitc::DEFINE_ABBREV) {
1709 Stream.ReadAbbrevRecord();
1710 continue;
1711 }
1712
1713 // Read and process a record.
1714 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001715 const char *BlobStart = 0;
1716 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001717 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001718 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001719 default: // Default behavior: ignore.
1720 break;
1721
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001722 case METADATA: {
1723 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1724 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001725 : diag::warn_pch_version_too_new);
1726 return IgnorePCH;
1727 }
1728
1729 RelocatablePCH = Record[4];
1730 if (Listener) {
1731 std::string TargetTriple(BlobStart, BlobLen);
1732 if (Listener->ReadTargetTriple(TargetTriple))
1733 return IgnorePCH;
1734 }
1735 break;
1736 }
1737
Douglas Gregore95b9192011-08-17 21:07:30 +00001738 case IMPORTS: {
1739 // Load each of the imported PCH files.
1740 unsigned Idx = 0, N = Record.size();
1741 while (Idx < N) {
1742 // Read information about the AST file.
1743 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1744 unsigned Length = Record[Idx++];
1745 llvm::SmallString<128> ImportedFile(Record.begin() + Idx,
1746 Record.begin() + Idx + Length);
1747 Idx += Length;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001748
Douglas Gregore95b9192011-08-17 21:07:30 +00001749 // Load the AST file.
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001750 switch(ReadASTCore(ImportedFile, ImportedKind, &F)) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001751 case Failure: return Failure;
1752 // If we have to ignore the dependency, we'll have to ignore this too.
1753 case IgnorePCH: return IgnorePCH;
1754 case Success: break;
1755 }
1756 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001757 break;
1758 }
1759
Douglas Gregora119da02011-08-02 16:26:37 +00001760 case TYPE_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001761 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001762 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001763 return Failure;
1764 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001765 F.TypeOffsets = (const uint32_t *)BlobStart;
1766 F.LocalNumTypes = Record[0];
Douglas Gregore3605012011-08-02 18:32:54 +00001767 unsigned LocalBaseTypeIndex = Record[1];
1768 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor1e849b62011-07-29 00:21:44 +00001769
Douglas Gregora119da02011-08-02 16:26:37 +00001770 if (F.LocalNumTypes > 0) {
1771 // Introduce the global -> local mapping for types within this module.
1772 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
1773
1774 // Introduce the local -> global mapping for types within this module.
Douglas Gregore3605012011-08-02 18:32:54 +00001775 F.TypeRemap.insert(std::make_pair(LocalBaseTypeIndex,
1776 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregora119da02011-08-02 16:26:37 +00001777
1778 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
1779 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001780 break;
Douglas Gregora119da02011-08-02 16:26:37 +00001781 }
1782
Douglas Gregor496c7092011-08-03 15:48:04 +00001783 case DECL_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001784 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001785 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001786 return Failure;
1787 }
Argyrios Kyrtzidis9d31fa72011-10-27 18:47:35 +00001788 F.DeclOffsets = (const DeclOffset *)BlobStart;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001789 F.LocalNumDecls = Record[0];
Douglas Gregor496c7092011-08-03 15:48:04 +00001790 unsigned LocalBaseDeclID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001791 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor96e973f2011-07-20 00:27:43 +00001792
Douglas Gregor496c7092011-08-03 15:48:04 +00001793 if (F.LocalNumDecls > 0) {
1794 // Introduce the global -> local mapping for declarations within this
1795 // module.
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001796 GlobalDeclMap.insert(
1797 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregor496c7092011-08-03 15:48:04 +00001798
1799 // Introduce the local -> global mapping for declarations within this
1800 // module.
1801 F.DeclRemap.insert(std::make_pair(LocalBaseDeclID,
1802 F.BaseDeclID - LocalBaseDeclID));
1803
1804 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
1805 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001806 break;
Douglas Gregor496c7092011-08-03 15:48:04 +00001807 }
1808
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001809 case TU_UPDATE_LEXICAL: {
Douglas Gregor35942772011-09-09 21:34:22 +00001810 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001811 DeclContextInfo &Info = F.DeclContextInfos[TU];
1812 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
1813 Info.NumLexicalDecls
1814 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregor35942772011-09-09 21:34:22 +00001815 TU->setHasExternalLexicalStorage(true);
Sebastian Redld692af72010-07-27 18:24:41 +00001816 break;
1817 }
1818
Sebastian Redle1dde812010-08-24 00:50:04 +00001819 case UPDATE_VISIBLE: {
Douglas Gregor496c7092011-08-03 15:48:04 +00001820 unsigned Idx = 0;
1821 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Sebastian Redle1dde812010-08-24 00:50:04 +00001822 void *Table = ASTDeclContextNameLookupTable::Create(
Douglas Gregor496c7092011-08-03 15:48:04 +00001823 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redle1dde812010-08-24 00:50:04 +00001824 (const unsigned char *)BlobStart,
Douglas Gregor393f2492011-07-22 00:38:23 +00001825 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregor35942772011-09-09 21:34:22 +00001826 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
1827 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001828 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001829 TU->setHasExternalVisibleStorage(true);
Sebastian Redle1dde812010-08-24 00:50:04 +00001830 } else
Douglas Gregor496c7092011-08-03 15:48:04 +00001831 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redle1dde812010-08-24 00:50:04 +00001832 break;
1833 }
1834
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001835 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001836 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
Douglas Gregor496c7092011-08-03 15:48:04 +00001837 for (unsigned i = 0, e = Record.size(); i < e; /* in loop */) {
1838 DeclID First = ReadDeclID(F, Record, i);
1839 DeclID Latest = ReadDeclID(F, Record, i);
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001840 FirstLatestDeclIDs[First] = Latest;
1841 }
1842 break;
1843 }
1844
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001845 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001846 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001847 return IgnorePCH;
1848 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001849
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001850 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001851 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001852 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001853 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001854 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001855 (const unsigned char *)F.IdentifierTableData + Record[0],
1856 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00001857 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001858
1859 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001860 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001861 break;
1862
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001863 case IDENTIFIER_OFFSET: {
Sebastian Redl2da08f92010-07-19 22:28:42 +00001864 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001865 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001866 return Failure;
1867 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001868 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1869 F.LocalNumIdentifiers = Record[0];
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001870 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001871 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor67268d02011-07-20 00:59:32 +00001872
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001873 if (F.LocalNumIdentifiers > 0) {
1874 // Introduce the global -> local mapping for identifiers within this
1875 // module.
1876 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
1877 &F));
1878
1879 // Introduce the local -> global mapping for identifiers within this
1880 // module.
1881 F.IdentifierRemap.insert(
1882 std::make_pair(LocalBaseIdentifierID,
1883 F.BaseIdentifierID - LocalBaseIdentifierID));
1884
1885 IdentifiersLoaded.resize(IdentifiersLoaded.size()
1886 + F.LocalNumIdentifiers);
1887 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001888 break;
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001889 }
1890
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001891 case EXTERNAL_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001892 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1893 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorfdd01722009-04-14 00:24:19 +00001894 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001895
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001896 case SPECIAL_TYPES:
Douglas Gregor393f2492011-07-22 00:38:23 +00001897 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1898 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregorad1de002009-04-18 05:55:16 +00001899 break;
1900
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001901 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001902 TotalNumStatements += Record[0];
1903 TotalNumMacros += Record[1];
1904 TotalLexicalDeclContexts += Record[2];
1905 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001906 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001907
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001908 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001909 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1910 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001911 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001912
Sean Huntebcbe1d2011-05-04 23:29:54 +00001913 case DELEGATING_CTORS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001914 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1915 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00001916 break;
1917
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001918 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor31e37b22011-07-28 18:09:57 +00001919 if (Record.size() % 4 != 0) {
1920 Error("invalid weak identifiers record");
1921 return Failure;
1922 }
1923
1924 // FIXME: Ignore weak undeclared identifiers from non-original PCH
1925 // files. This isn't the way to do it :)
1926 WeakUndeclaredIdentifiers.clear();
1927
1928 // Translate the weak, undeclared identifiers into global IDs.
1929 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
1930 WeakUndeclaredIdentifiers.push_back(
1931 getGlobalIdentifierID(F, Record[I++]));
1932 WeakUndeclaredIdentifiers.push_back(
1933 getGlobalIdentifierID(F, Record[I++]));
1934 WeakUndeclaredIdentifiers.push_back(
1935 ReadSourceLocation(F, Record, I).getRawEncoding());
1936 WeakUndeclaredIdentifiers.push_back(Record[I++]);
1937 }
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00001938 break;
1939
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001940 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001941 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1942 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor14c22f22009-04-22 22:18:58 +00001943 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001944
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00001945 case SELECTOR_OFFSETS: {
Sebastian Redl059612d2010-08-03 21:58:15 +00001946 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00001947 F.LocalNumSelectors = Record[0];
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00001948 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001949 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor96958cb2011-07-20 01:10:58 +00001950
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00001951 if (F.LocalNumSelectors > 0) {
1952 // Introduce the global -> local mapping for selectors within this
1953 // module.
1954 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
1955
1956 // Introduce the local -> global mapping for selectors within this
1957 // module.
1958 F.SelectorRemap.insert(std::make_pair(LocalBaseSelectorID,
1959 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor83941df2009-04-25 17:48:32 +00001960
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00001961 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
1962 }
1963 break;
1964 }
1965
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001966 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00001967 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00001968 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00001969 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001970 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00001971 F.SelectorLookupTableData + Record[0],
1972 F.SelectorLookupTableData,
Douglas Gregor409448c2011-07-21 22:35:25 +00001973 ASTSelectorLookupTrait(*this, F));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00001974 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001975 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001976
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00001977 case REFERENCED_SELECTOR_POOL:
Douglas Gregor8451ec72011-07-28 14:41:43 +00001978 if (!Record.empty()) {
1979 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
1980 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
1981 Record[Idx++]));
1982 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
1983 getRawEncoding());
1984 }
1985 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00001986 break;
1987
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001988 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001989 if (!Record.empty() && Listener)
1990 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001991 break;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001992
1993 case FILE_SORTED_DECLS:
1994 F.FileSortedDecls = (const DeclID *)BlobStart;
1995 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001996
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001997 case SOURCE_LOCATION_OFFSETS: {
1998 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001999 F.LocalNumSLocEntries = Record[0];
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002000 unsigned SLocSpaceSize = Record[1];
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002001 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002002 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2003 SLocSpaceSize);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002004 // Make our entry in the range map. BaseID is negative and growing, so
2005 // we invert it. Because we invert it, though, we need the other end of
2006 // the range.
2007 unsigned RangeStart =
2008 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2009 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2010 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2011
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002012 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2013 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2014 GlobalSLocOffsetMap.insert(
2015 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2016 - SLocSpaceSize,&F));
2017
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002018 // Initialize the remapping table.
2019 // Invalid stays invalid.
2020 F.SLocRemap.insert(std::make_pair(0U, 0));
2021 // This module. Base was 2 when being compiled.
2022 F.SLocRemap.insert(std::make_pair(2U,
2023 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002024
2025 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002026 break;
2027 }
2028
Douglas Gregor5d51a1d2011-08-01 16:01:55 +00002029 case MODULE_OFFSET_MAP: {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002030 // Additional remapping information.
2031 const unsigned char *Data = (const unsigned char*)BlobStart;
2032 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregorf33740e2011-08-02 10:56:51 +00002033
2034 // Continuous range maps we may be updating in our module.
2035 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002036 ContinuousRangeMap<uint32_t, int, 2>::Builder
2037 IdentifierRemap(F.IdentifierRemap);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002038 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002039 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2040 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002041 SelectorRemap(F.SelectorRemap);
Douglas Gregor496c7092011-08-03 15:48:04 +00002042 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregora119da02011-08-02 16:26:37 +00002043 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2044
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002045 while(Data < DataEnd) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002046 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002047 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002048 Data += Len;
Jonathan D. Turner48d2c3f2011-07-26 18:21:30 +00002049 Module *OM = ModuleMgr.lookup(Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002050 if (!OM) {
2051 Error("SourceLocation remap refers to unknown module");
2052 return Failure;
2053 }
Douglas Gregorf33740e2011-08-02 10:56:51 +00002054
2055 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2056 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2057 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002058 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2059 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora119da02011-08-02 16:26:37 +00002060 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002061
2062 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2063 SLocRemap.insert(std::make_pair(SLocOffset,
2064 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002065 IdentifierRemap.insert(
2066 std::make_pair(IdentifierIDOffset,
2067 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002068 PreprocessedEntityRemap.insert(
2069 std::make_pair(PreprocessedEntityIDOffset,
2070 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002071 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2072 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregor496c7092011-08-03 15:48:04 +00002073 DeclRemap.insert(std::make_pair(DeclIDOffset,
2074 OM->BaseDeclID - DeclIDOffset));
2075
Douglas Gregora119da02011-08-02 16:26:37 +00002076 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregore3605012011-08-02 18:32:54 +00002077 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002078 }
2079 break;
2080 }
2081
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002082 case SOURCE_MANAGER_LINE_TABLE:
2083 if (ParseLineTable(F, Record))
2084 return Failure;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002085 break;
2086
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00002087 case FILE_SOURCE_LOCATION_OFFSETS:
2088 F.SLocFileOffsets = (const uint32_t *)BlobStart;
2089 F.LocalNumSLocFileEntries = Record[0];
2090 break;
2091
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002092 case SOURCE_LOCATION_PRELOADS: {
2093 // Need to transform from the local view (1-based IDs) to the global view,
2094 // which is based off F.SLocEntryBaseID.
Douglas Gregorf249bf32011-08-25 21:09:44 +00002095 if (!F.PreloadSLocEntries.empty()) {
2096 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2097 return Failure;
2098 }
2099
2100 F.PreloadSLocEntries.swap(Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002101 break;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002102 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002103
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002104 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002105 if (!DisableStatCache) {
2106 ASTStatCache *MyStatCache =
2107 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2108 (const unsigned char *)BlobStart,
2109 NumStatHits, NumStatMisses);
2110 FileMgr.addStatCache(MyStatCache);
2111 F.StatCache = MyStatCache;
2112 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002113 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002114 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002115
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002116 case EXT_VECTOR_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002117 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2118 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorb81c1702009-04-27 20:06:05 +00002119 break;
2120
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002121 case VTABLE_USES:
Douglas Gregordfe65432011-07-28 19:11:31 +00002122 if (Record.size() % 3 != 0) {
2123 Error("Invalid VTABLE_USES record");
2124 return Failure;
2125 }
2126
Sebastian Redl40566802010-08-05 18:21:25 +00002127 // Later tables overwrite earlier ones.
Douglas Gregordfe65432011-07-28 19:11:31 +00002128 // FIXME: Modules will have some trouble with this. This is clearly not
2129 // the right way to do this.
Douglas Gregor409448c2011-07-21 22:35:25 +00002130 VTableUses.clear();
Douglas Gregordfe65432011-07-28 19:11:31 +00002131
2132 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2133 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2134 VTableUses.push_back(
2135 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2136 VTableUses.push_back(Record[Idx++]);
2137 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002138 break;
2139
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002140 case DYNAMIC_CLASSES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002141 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2142 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002143 break;
2144
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002145 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorf2abb522011-07-28 19:26:52 +00002146 if (PendingInstantiations.size() % 2 != 0) {
2147 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2148 return Failure;
2149 }
2150
2151 // Later lists of pending instantiations overwrite earlier ones.
2152 // FIXME: This is most certainly wrong for modules.
2153 PendingInstantiations.clear();
2154 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2155 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2156 PendingInstantiations.push_back(
2157 ReadSourceLocation(F, Record, I).getRawEncoding());
2158 }
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002159 break;
2160
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002161 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002162 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002163 // FIXME: Modules will have some trouble with this.
2164 SemaDeclRefs.clear();
2165 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2166 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002167 break;
2168
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002169 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002170 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002171 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002172 ActualOriginalFileName.assign(BlobStart, BlobLen);
2173 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002174 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002175 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002176
Douglas Gregor31d375f2011-05-06 21:43:30 +00002177 case ORIGINAL_FILE_ID:
2178 OriginalFileID = FileID::get(Record[0]);
2179 break;
2180
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002181 case ORIGINAL_PCH_DIR:
2182 // The primary AST will be the last to get here, so it will be the one
2183 // that's used.
2184 OriginalDir.assign(BlobStart, BlobLen);
2185 break;
2186
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002187 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002188 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002189 StringRef ASTBranch(BlobStart, BlobLen);
2190 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002191 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002192 return IgnorePCH;
2193 }
2194 break;
2195 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002196
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002197 case PPD_ENTITIES_OFFSETS: {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002198 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart;
2199 assert(BlobLen % sizeof(PPEntityOffset) == 0);
2200 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002201
2202 unsigned LocalBasePreprocessedEntityID = Record[0];
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002203
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002204 unsigned StartingID;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002205 if (!PP.getPreprocessingRecord())
2206 PP.createPreprocessingRecord(true);
2207 if (!PP.getPreprocessingRecord()->getExternalSource())
2208 PP.getPreprocessingRecord()->SetExternalSource(*this);
2209 StartingID
2210 = PP.getPreprocessingRecord()
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002211 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Douglas Gregor9827a802011-07-29 00:56:45 +00002212 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002213
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002214 if (F.NumPreprocessedEntities > 0) {
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002215 // Introduce the global -> local mapping for preprocessed entities in
2216 // this module.
2217 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2218
2219 // Introduce the local -> global mapping for preprocessed entities in
2220 // this module.
2221 F.PreprocessedEntityRemap.insert(
2222 std::make_pair(LocalBasePreprocessedEntityID,
2223 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2224 }
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002225
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002226 break;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002227 }
2228
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002229 case DECL_UPDATE_OFFSETS: {
2230 if (Record.size() % 2 != 0) {
2231 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2232 return Failure;
2233 }
2234 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregor496c7092011-08-03 15:48:04 +00002235 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2236 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002237 break;
2238 }
2239
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002240 case DECL_REPLACEMENTS: {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002241 if (Record.size() % 3 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002242 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002243 return Failure;
2244 }
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002245 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
Douglas Gregor496c7092011-08-03 15:48:04 +00002246 ReplacedDecls[getGlobalDeclID(F, Record[I])]
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002247 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002248 break;
2249 }
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002250
2251 case OBJC_CHAINED_CATEGORIES: {
2252 if (Record.size() % 3 != 0) {
2253 Error("invalid OBJC_CHAINED_CATEGORIES block in AST file");
2254 return Failure;
2255 }
2256 for (unsigned I = 0, N = Record.size(); I != N; I += 3) {
2257 serialization::GlobalDeclID GlobID = getGlobalDeclID(F, Record[I]);
2258 F.ChainedObjCCategories[GlobID] = std::make_pair(Record[I+1],
2259 Record[I+2]);
2260 ObjCChainedCategoriesInterfaces.insert(GlobID);
2261 }
2262 break;
2263 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002264
2265 case CXX_BASE_SPECIFIER_OFFSETS: {
2266 if (F.LocalNumCXXBaseSpecifiers != 0) {
2267 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2268 return Failure;
2269 }
2270
2271 F.LocalNumCXXBaseSpecifiers = Record[0];
2272 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner1da90142011-07-21 21:15:19 +00002273 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002274 break;
2275 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002276
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002277 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002278 if (Record.size() % 2 != 0) {
2279 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2280 return Failure;
2281 }
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002282
2283 if (F.PragmaDiagMappings.empty())
2284 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002285 else
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002286 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2287 Record.begin(), Record.end());
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002288 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002289
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002290 case CUDA_SPECIAL_DECL_REFS:
2291 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002292 // FIXME: Modules will have trouble with this.
2293 CUDASpecialDeclRefs.clear();
2294 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2295 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002296 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002297
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002298 case HEADER_SEARCH_TABLE: {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002299 F.HeaderFileInfoTableData = BlobStart;
2300 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002301 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002302 if (Record[0]) {
2303 F.HeaderFileInfoTable
2304 = HeaderFileInfoLookupTable::Create(
2305 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002306 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregor95eab172011-07-28 20:55:49 +00002307 HeaderFileInfoTrait(*this, F,
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002308 &PP.getHeaderSearchInfo(),
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002309 BlobStart + Record[2]));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002310
2311 PP.getHeaderSearchInfo().SetExternalSource(this);
2312 if (!PP.getHeaderSearchInfo().getExternalLookup())
2313 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002314 }
2315 break;
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002316 }
2317
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002318 case FP_PRAGMA_OPTIONS:
2319 // Later tables overwrite earlier ones.
2320 FPPragmaOptions.swap(Record);
2321 break;
2322
2323 case OPENCL_EXTENSIONS:
2324 // Later tables overwrite earlier ones.
2325 OpenCLExtensions.swap(Record);
2326 break;
Sean Huntebcbe1d2011-05-04 23:29:54 +00002327
2328 case TENTATIVE_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002329 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2330 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002331 break;
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002332
2333 case KNOWN_NAMESPACES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002334 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2335 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002336 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002337 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002338 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002339 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002340 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002341}
2342
Douglas Gregorc69a2922011-08-25 20:58:51 +00002343ASTReader::ASTReadResult ASTReader::validateFileEntries(Module &M) {
2344 llvm::BitstreamCursor &SLocEntryCursor = M.SLocEntryCursor;
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002345
Douglas Gregorc69a2922011-08-25 20:58:51 +00002346 for (unsigned i = 0, e = M.LocalNumSLocFileEntries; i != e; ++i) {
2347 SLocEntryCursor.JumpToBit(M.SLocFileOffsets[i]);
2348 unsigned Code = SLocEntryCursor.ReadCode();
2349 if (Code == llvm::bitc::END_BLOCK ||
2350 Code == llvm::bitc::ENTER_SUBBLOCK ||
2351 Code == llvm::bitc::DEFINE_ABBREV) {
2352 Error("incorrectly-formatted source location entry in AST file");
2353 return Failure;
2354 }
2355
2356 RecordData Record;
2357 const char *BlobStart;
2358 unsigned BlobLen;
2359 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2360 default:
2361 Error("incorrectly-formatted source location entry in AST file");
2362 return Failure;
2363
2364 case SM_SLOC_FILE_ENTRY: {
Douglas Gregora081da52011-11-16 20:05:18 +00002365 // If the buffer was overridden, the file need not exist.
2366 if (Record[6])
2367 break;
2368
Douglas Gregorc69a2922011-08-25 20:58:51 +00002369 StringRef Filename(BlobStart, BlobLen);
2370 const FileEntry *File = getFileEntry(Filename);
2371
2372 if (File == 0) {
2373 std::string ErrorStr = "could not find file '";
2374 ErrorStr += Filename;
2375 ErrorStr += "' referenced by AST file";
2376 Error(ErrorStr.c_str());
2377 return IgnorePCH;
2378 }
2379
Douglas Gregora081da52011-11-16 20:05:18 +00002380 if (Record.size() < 7) {
Douglas Gregorc69a2922011-08-25 20:58:51 +00002381 Error("source location entry is incorrect");
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002382 return Failure;
2383 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002384
Douglas Gregorc69a2922011-08-25 20:58:51 +00002385 // The stat info from the FileEntry came from the cached stat
2386 // info of the PCH, so we cannot trust it.
2387 struct stat StatBuf;
2388 if (::stat(File->getName(), &StatBuf) != 0) {
2389 StatBuf.st_size = File->getSize();
2390 StatBuf.st_mtime = File->getModificationTime();
2391 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002392
Douglas Gregorc69a2922011-08-25 20:58:51 +00002393 if (((off_t)Record[4] != StatBuf.st_size
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002394#if !defined(LLVM_ON_WIN32)
Douglas Gregorc69a2922011-08-25 20:58:51 +00002395 // In our regression testing, the Windows file system seems to
2396 // have inconsistent modification times that sometimes
2397 // erroneously trigger this error-handling path.
2398 || (time_t)Record[5] != StatBuf.st_mtime
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002399#endif
Douglas Gregorc69a2922011-08-25 20:58:51 +00002400 )) {
2401 Error(diag::err_fe_pch_file_modified, Filename);
2402 return IgnorePCH;
2403 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002404
Douglas Gregorc69a2922011-08-25 20:58:51 +00002405 break;
2406 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002407 }
2408 }
2409
2410 return Success;
2411}
2412
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002413ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregor72a9ae12011-07-22 16:00:58 +00002414 ModuleKind Type) {
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002415 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002416 case Failure: return Failure;
2417 case IgnorePCH: return IgnorePCH;
2418 case Success: break;
2419 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002420
2421 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002422
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002423 // Check the predefines buffers.
Argyrios Kyrtzidis26d43cd2011-09-12 18:09:38 +00002424 if (!DisableValidation && Type != MK_Module && Type != MK_Preamble &&
2425 // FIXME: CheckPredefinesBuffers also sets the SuggestedPredefines;
2426 // if DisableValidation is true, defines that were set on command-line
2427 // but not in the PCH file will not be added to SuggestedPredefines.
2428 CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002429 return IgnorePCH;
2430
Douglas Gregoreee242f2011-10-27 09:33:13 +00002431 // Mark all of the identifiers in the identifier table as being out of date,
2432 // so that various accessors know to check the loaded modules when the
2433 // identifier is used.
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002434 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2435 IdEnd = PP.getIdentifierTable().end();
2436 Id != IdEnd; ++Id)
Douglas Gregoreee242f2011-10-27 09:33:13 +00002437 Id->second->setOutOfDate(true);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002438
Douglas Gregor35942772011-09-09 21:34:22 +00002439 InitializeContext();
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002440
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002441 if (DeserializationListener)
2442 DeserializationListener->ReaderInitialized(this);
2443
Argyrios Kyrtzidis507097e2011-09-19 20:40:35 +00002444 // If this AST file is a precompiled preamble, then set the preamble file ID
2445 // of the source manager to the file source file from which the preamble was
2446 // built.
Douglas Gregor72a9ae12011-07-22 16:00:58 +00002447 if (Type == MK_Preamble) {
Argyrios Kyrtzidis507097e2011-09-19 20:40:35 +00002448 if (!OriginalFileID.isInvalid()) {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00002449 OriginalFileID = FileID::get(ModuleMgr.getPrimaryModule().SLocEntryBaseID
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002450 + OriginalFileID.getOpaqueValue() - 1);
Argyrios Kyrtzidis507097e2011-09-19 20:40:35 +00002451 SourceMgr.setPreambleFileID(OriginalFileID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002452 }
Douglas Gregor414cb642010-11-30 05:23:00 +00002453 }
2454
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002455 return Success;
2456}
2457
Chris Lattner5f9e2722011-07-23 10:55:15 +00002458ASTReader::ASTReadResult ASTReader::ReadASTCore(StringRef FileName,
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002459 ModuleKind Type,
2460 Module *ImportedBy) {
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002461 Module *M;
2462 bool NewModule;
2463 std::string ErrorStr;
2464 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
2465 ErrorStr);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002466
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002467 if (!M) {
2468 // We couldn't load the module.
2469 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2470 + ErrorStr;
2471 Error(Msg);
2472 return Failure;
2473 }
2474
2475 if (!NewModule) {
2476 // We've already loaded this module.
2477 return Success;
2478 }
2479
2480 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2481 // module?
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002482 if (FileName != "-") {
2483 CurrentDir = llvm::sys::path::parent_path(FileName);
2484 if (CurrentDir.empty()) CurrentDir = ".";
2485 }
2486
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002487 Module &F = *M;
Sebastian Redl9137a522010-07-16 17:50:48 +00002488 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002489 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002490 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002491
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002492 // Sniff for the signature.
2493 if (Stream.Read(8) != 'C' ||
2494 Stream.Read(8) != 'P' ||
2495 Stream.Read(8) != 'C' ||
2496 Stream.Read(8) != 'H') {
2497 Diag(diag::err_not_a_pch_file) << FileName;
2498 return Failure;
2499 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002500
Douglas Gregor2cf26342009-04-09 22:27:44 +00002501 while (!Stream.AtEndOfStream()) {
2502 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002503
Douglas Gregore1d918e2009-04-10 23:10:45 +00002504 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002505 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002506 return Failure;
2507 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002508
2509 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002510
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002511 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002512 switch (BlockID) {
2513 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002514 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002515 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002516 return Failure;
2517 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002518 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002519 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002520 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002521 case Success:
2522 break;
2523
2524 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002525 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002526
2527 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002528 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002529 // AST block, skipping subblocks, to see if there are other
2530 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002531
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002532 // FIXME: We can't clear loaded slocentries anymore.
2533 //SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002534
2535 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002536 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002537 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002538
Douglas Gregore1d918e2009-04-10 23:10:45 +00002539 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002540 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002541 break;
2542 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002543 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002544 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002545 return Failure;
2546 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002547 break;
2548 }
Mike Stump1eb44332009-09-09 15:08:12 +00002549 }
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002550
Douglas Gregor72a9ae12011-07-22 16:00:58 +00002551 // Once read, set the Module bit base offset and update the size in
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002552 // bits of all files we've seen.
2553 F.GlobalBitOffset = TotalModulesSizeInBits;
2554 TotalModulesSizeInBits += F.SizeInBits;
2555 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
Douglas Gregorc69a2922011-08-25 20:58:51 +00002556
2557 // Make sure that the files this module was built against are still available.
2558 if (!DisableValidation) {
2559 switch(validateFileEntries(*M)) {
2560 case Failure: return Failure;
2561 case IgnorePCH: return IgnorePCH;
2562 case Success: break;
2563 }
2564 }
Douglas Gregorf249bf32011-08-25 21:09:44 +00002565
2566 // Preload SLocEntries.
2567 for (unsigned I = 0, N = M->PreloadSLocEntries.size(); I != N; ++I) {
2568 int Index = int(M->PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Argyrios Kyrtzidisac1ffcc2011-09-19 20:39:54 +00002569 // Load it through the SourceManager and don't call ReadSLocEntryRecord()
2570 // directly because the entry may have already been loaded in which case
2571 // calling ReadSLocEntryRecord() directly would trigger an assertion in
2572 // SourceManager.
2573 SourceMgr.getLoadedSLocEntryByID(Index);
Douglas Gregorf249bf32011-08-25 21:09:44 +00002574 }
2575
Douglas Gregorc69a2922011-08-25 20:58:51 +00002576
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002577 return Success;
2578}
2579
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002580void ASTReader::InitializeContext() {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002581 // If there's a listener, notify them that we "read" the translation unit.
2582 if (DeserializationListener)
Douglas Gregor35942772011-09-09 21:34:22 +00002583 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
2584 Context.getTranslationUnitDecl());
Douglas Gregor3747ee72010-10-01 01:18:02 +00002585
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002586 // Make sure we load the declaration update records for the translation unit,
2587 // if there are any.
Douglas Gregor35942772011-09-09 21:34:22 +00002588 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
2589 Context.getTranslationUnitDecl());
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002590
Douglas Gregor5f957282011-08-11 22:18:49 +00002591 // FIXME: Find a better way to deal with collisions between these
2592 // built-in types. Right now, we just ignore the problem.
2593
2594 // Load the special types.
Douglas Gregor02a5e872011-09-10 00:30:18 +00002595 if (SpecialTypes.size() > NumSpecialTypeIDs) {
2596 if (Context.getBuiltinVaListType().isNull()) {
2597 Context.setBuiltinVaListType(
2598 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
Douglas Gregor5f957282011-08-11 22:18:49 +00002599 }
2600
Douglas Gregor02a5e872011-09-10 00:30:18 +00002601 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL]) {
2602 if (Context.ObjCProtoType.isNull())
2603 Context.ObjCProtoType = GetType(Proto);
2604 }
2605
2606 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2607 if (!Context.CFConstantStringTypeDecl)
2608 Context.setCFConstantStringType(GetType(String));
2609 }
2610
2611 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2612 QualType FileType = GetType(File);
2613 if (FileType.isNull()) {
2614 Error("FILE type is NULL");
2615 return;
2616 }
2617
2618 if (!Context.FILEDecl) {
2619 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2620 Context.setFILEDecl(Typedef->getDecl());
2621 else {
2622 const TagType *Tag = FileType->getAs<TagType>();
2623 if (!Tag) {
2624 Error("Invalid FILE type in AST file");
2625 return;
2626 }
2627 Context.setFILEDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002628 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002629 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002630 }
Douglas Gregor5f957282011-08-11 22:18:49 +00002631
Douglas Gregor72cd7a02011-11-11 19:13:12 +00002632 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002633 QualType Jmp_bufType = GetType(Jmp_buf);
2634 if (Jmp_bufType.isNull()) {
2635 Error("jmp_buf type is NULL");
2636 return;
2637 }
2638
2639 if (!Context.jmp_bufDecl) {
2640 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2641 Context.setjmp_bufDecl(Typedef->getDecl());
2642 else {
2643 const TagType *Tag = Jmp_bufType->getAs<TagType>();
2644 if (!Tag) {
2645 Error("Invalid jmp_buf type in AST file");
2646 return;
2647 }
2648 Context.setjmp_bufDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002649 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002650 }
Mike Stump782fa302009-07-28 02:25:19 +00002651 }
Douglas Gregor02a5e872011-09-10 00:30:18 +00002652
Douglas Gregor72cd7a02011-11-11 19:13:12 +00002653 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002654 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2655 if (Sigjmp_bufType.isNull()) {
2656 Error("sigjmp_buf type is NULL");
2657 return;
2658 }
2659
2660 if (!Context.sigjmp_bufDecl) {
2661 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2662 Context.setsigjmp_bufDecl(Typedef->getDecl());
2663 else {
2664 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
2665 assert(Tag && "Invalid sigjmp_buf type in AST file");
2666 Context.setsigjmp_bufDecl(Tag->getDecl());
2667 }
2668 }
2669 }
2670
2671 if (unsigned ObjCIdRedef
2672 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
2673 if (Context.ObjCIdRedefinitionType.isNull())
2674 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
2675 }
2676
2677 if (unsigned ObjCClassRedef
2678 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
2679 if (Context.ObjCClassRedefinitionType.isNull())
2680 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
2681 }
2682
2683 if (unsigned ObjCSelRedef
2684 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
2685 if (Context.ObjCSelRedefinitionType.isNull())
2686 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
2687 }
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00002688
2689 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
2690 QualType Ucontext_tType = GetType(Ucontext_t);
2691 if (Ucontext_tType.isNull()) {
2692 Error("ucontext_t type is NULL");
2693 return;
2694 }
2695
2696 if (!Context.ucontext_tDecl) {
2697 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
2698 Context.setucontext_tDecl(Typedef->getDecl());
2699 else {
2700 const TagType *Tag = Ucontext_tType->getAs<TagType>();
2701 assert(Tag && "Invalid ucontext_t type in AST file");
2702 Context.setucontext_tDecl(Tag->getDecl());
2703 }
2704 }
2705 }
Douglas Gregor5f957282011-08-11 22:18:49 +00002706 }
2707
Douglas Gregor35942772011-09-09 21:34:22 +00002708 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002709
2710 // If there were any CUDA special declarations, deserialize them.
2711 if (!CUDASpecialDeclRefs.empty()) {
2712 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
Douglas Gregor35942772011-09-09 21:34:22 +00002713 Context.setcudaConfigureCallDecl(
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002714 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2715 }
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002716}
2717
Douglas Gregorb64c1932009-05-12 01:31:05 +00002718/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002719/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002720/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002721std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002722 FileManager &FileMgr,
David Blaikied6471f72011-09-25 23:23:43 +00002723 DiagnosticsEngine &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002724 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002725 std::string ErrStr;
2726 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00002727 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002728 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002729 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002730 return std::string();
2731 }
2732
2733 // Initialize the stream
2734 llvm::BitstreamReader StreamFile;
2735 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002736 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002737 (const unsigned char *)Buffer->getBufferEnd());
2738 Stream.init(StreamFile);
2739
2740 // Sniff for the signature.
2741 if (Stream.Read(8) != 'C' ||
2742 Stream.Read(8) != 'P' ||
2743 Stream.Read(8) != 'C' ||
2744 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002745 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002746 return std::string();
2747 }
2748
2749 RecordData Record;
2750 while (!Stream.AtEndOfStream()) {
2751 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002752
Douglas Gregorb64c1932009-05-12 01:31:05 +00002753 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2754 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002755
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002756 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002757 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002758 case AST_BLOCK_ID:
2759 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002760 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002761 return std::string();
2762 }
2763 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002764
Douglas Gregorb64c1932009-05-12 01:31:05 +00002765 default:
2766 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002767 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002768 return std::string();
2769 }
2770 break;
2771 }
2772 continue;
2773 }
2774
2775 if (Code == llvm::bitc::END_BLOCK) {
2776 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002777 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002778 return std::string();
2779 }
2780 continue;
2781 }
2782
2783 if (Code == llvm::bitc::DEFINE_ABBREV) {
2784 Stream.ReadAbbrevRecord();
2785 continue;
2786 }
2787
2788 Record.clear();
2789 const char *BlobStart = 0;
2790 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002791 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002792 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00002793 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00002794 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00002795
2796 return std::string();
2797}
2798
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002799/// \brief Parse the record that corresponds to a LangOptions data
2800/// structure.
2801///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002802/// This routine parses the language options from the AST file and then gives
2803/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002804///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002805/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002806bool ASTReader::ParseLanguageOptions(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002807 const SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002808 if (Listener) {
2809 LangOptions LangOpts;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002810 unsigned Idx = 0;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00002811#define LANGOPT(Name, Bits, Default, Description) \
2812 LangOpts.Name = Record[Idx++];
2813#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
2814 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
2815#include "clang/Basic/LangOptions.def"
2816
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00002817 unsigned Length = Record[Idx++];
2818 LangOpts.CurrentModule.assign(Record.begin() + Idx,
2819 Record.begin() + Idx + Length);
2820 Idx += Length;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002821 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002822 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002823
2824 return false;
2825}
2826
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00002827std::pair<Module *, unsigned>
2828ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002829 GlobalPreprocessedEntityMapType::iterator
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00002830 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002831 assert(I != GlobalPreprocessedEntityMap.end() &&
2832 "Corrupted global preprocessed entity map");
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00002833 Module *M = I->second;
2834 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
2835 return std::make_pair(M, LocalIndex);
2836}
2837
2838PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
2839 PreprocessedEntityID PPID = Index+1;
2840 std::pair<Module *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
2841 Module &M = *PPInfo.first;
2842 unsigned LocalIndex = PPInfo.second;
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002843 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002844
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002845 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002846 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002847
2848 unsigned Code = M.PreprocessorDetailCursor.ReadCode();
2849 switch (Code) {
2850 case llvm::bitc::END_BLOCK:
2851 return 0;
2852
2853 case llvm::bitc::ENTER_SUBBLOCK:
2854 Error("unexpected subblock record in preprocessor detail block");
2855 return 0;
2856
2857 case llvm::bitc::DEFINE_ABBREV:
2858 Error("unexpected abbrevation record in preprocessor detail block");
2859 return 0;
2860
2861 default:
2862 break;
2863 }
2864
2865 if (!PP.getPreprocessingRecord()) {
2866 Error("no preprocessing record");
2867 return 0;
2868 }
2869
2870 // Read the record.
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002871 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
2872 ReadSourceLocation(M, PPOffs.End));
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002873 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
2874 const char *BlobStart = 0;
2875 unsigned BlobLen = 0;
2876 RecordData Record;
2877 PreprocessorDetailRecordTypes RecType =
2878 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord(
2879 Code, Record, BlobStart, BlobLen);
2880 switch (RecType) {
2881 case PPD_MACRO_EXPANSION: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002882 bool isBuiltin = Record[0];
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002883 IdentifierInfo *Name = 0;
2884 MacroDefinition *Def = 0;
2885 if (isBuiltin)
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002886 Name = getLocalIdentifier(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002887 else {
2888 PreprocessedEntityID
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002889 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002890 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
2891 }
2892
2893 MacroExpansion *ME;
2894 if (isBuiltin)
2895 ME = new (PPRec) MacroExpansion(Name, Range);
2896 else
2897 ME = new (PPRec) MacroExpansion(Def, Range);
2898
2899 return ME;
2900 }
2901
2902 case PPD_MACRO_DEFINITION: {
2903 // Decode the identifier info and then check again; if the macro is
2904 // still defined and associated with the identifier,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002905 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002906 MacroDefinition *MD
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002907 = new (PPRec) MacroDefinition(II, Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002908
2909 if (DeserializationListener)
2910 DeserializationListener->MacroDefinitionRead(PPID, MD);
2911
2912 return MD;
2913 }
2914
2915 case PPD_INCLUSION_DIRECTIVE: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002916 const char *FullFileNameStart = BlobStart + Record[0];
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002917 const FileEntry *File
2918 = PP.getFileManager().getFile(StringRef(FullFileNameStart,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002919 BlobLen - Record[0]));
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002920
2921 // FIXME: Stable encoding
2922 InclusionDirective::InclusionKind Kind
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002923 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002924 InclusionDirective *ID
2925 = new (PPRec) InclusionDirective(PPRec, Kind,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002926 StringRef(BlobStart, Record[0]),
2927 Record[1],
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002928 File,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00002929 Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00002930 return ID;
2931 }
2932 }
2933
2934 Error("invalid offset in preprocessor detail block");
2935 return 0;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002936}
2937
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002938/// \brief \arg SLocMapI points at a chunk of a module that contains no
2939/// preprocessed entities or the entities it contains are not the ones we are
2940/// looking for. Find the next module that contains entities and return the ID
2941/// of the first entry.
2942PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
2943 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
2944 ++SLocMapI;
2945 for (GlobalSLocOffsetMapType::const_iterator
2946 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
2947 Module &M = *SLocMapI->second;
2948 if (M.NumPreprocessedEntities)
2949 return getGlobalPreprocessedEntityID(M, M.BasePreprocessedEntityID);
2950 }
2951
2952 return getTotalNumPreprocessedEntities();
2953}
2954
2955namespace {
2956
2957template <unsigned PPEntityOffset::*PPLoc>
2958struct PPEntityComp {
2959 const ASTReader &Reader;
2960 Module &M;
2961
2962 PPEntityComp(const ASTReader &Reader, Module &M) : Reader(Reader), M(M) { }
2963
Benjamin Kramer88df1252011-09-21 06:42:26 +00002964 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
2965 SourceLocation LHS = getLoc(L);
2966 SourceLocation RHS = getLoc(R);
2967 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
2968 }
2969
2970 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002971 SourceLocation LHS = getLoc(L);
2972 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
2973 }
2974
Benjamin Kramer88df1252011-09-21 06:42:26 +00002975 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002976 SourceLocation RHS = getLoc(R);
2977 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
2978 }
2979
2980 SourceLocation getLoc(const PPEntityOffset &PPE) const {
2981 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
2982 }
2983};
2984
2985}
2986
2987/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
2988PreprocessedEntityID
2989ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
2990 if (SourceMgr.isLocalSourceLocation(BLoc))
2991 return getTotalNumPreprocessedEntities();
2992
2993 GlobalSLocOffsetMapType::const_iterator
2994 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
2995 BLoc.getOffset());
2996 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
2997 "Corrupted global sloc offset map");
2998
2999 if (SLocMapI->second->NumPreprocessedEntities == 0)
3000 return findNextPreprocessedEntity(SLocMapI);
3001
3002 Module &M = *SLocMapI->second;
3003 typedef const PPEntityOffset *pp_iterator;
3004 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3005 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
Argyrios Kyrtzidis4cd06342011-09-22 21:17:02 +00003006
3007 size_t Count = M.NumPreprocessedEntities;
3008 size_t Half;
3009 pp_iterator First = pp_begin;
3010 pp_iterator PPI;
3011
3012 // Do a binary search manually instead of using std::lower_bound because
3013 // The end locations of entities may be unordered (when a macro expansion
3014 // is inside another macro argument), but for this case it is not important
3015 // whether we get the first macro expansion or its containing macro.
3016 while (Count > 0) {
3017 Half = Count/2;
3018 PPI = First;
3019 std::advance(PPI, Half);
3020 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
3021 BLoc)){
3022 First = PPI;
3023 ++First;
3024 Count = Count - Half - 1;
3025 } else
3026 Count = Half;
3027 }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003028
3029 if (PPI == pp_end)
3030 return findNextPreprocessedEntity(SLocMapI);
3031
3032 return getGlobalPreprocessedEntityID(M,
3033 M.BasePreprocessedEntityID + (PPI - pp_begin));
3034}
3035
3036/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
3037PreprocessedEntityID
3038ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
3039 if (SourceMgr.isLocalSourceLocation(ELoc))
3040 return getTotalNumPreprocessedEntities();
3041
3042 GlobalSLocOffsetMapType::const_iterator
3043 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3044 ELoc.getOffset());
3045 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3046 "Corrupted global sloc offset map");
3047
3048 if (SLocMapI->second->NumPreprocessedEntities == 0)
3049 return findNextPreprocessedEntity(SLocMapI);
3050
3051 Module &M = *SLocMapI->second;
3052 typedef const PPEntityOffset *pp_iterator;
3053 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3054 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
3055 pp_iterator PPI =
3056 std::upper_bound(pp_begin, pp_end, ELoc,
3057 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
3058
3059 if (PPI == pp_end)
3060 return findNextPreprocessedEntity(SLocMapI);
3061
3062 return getGlobalPreprocessedEntityID(M,
3063 M.BasePreprocessedEntityID + (PPI - pp_begin));
3064}
3065
3066/// \brief Returns a pair of [Begin, End) indices of preallocated
3067/// preprocessed entities that \arg Range encompasses.
3068std::pair<unsigned, unsigned>
3069 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
3070 if (Range.isInvalid())
3071 return std::make_pair(0,0);
3072 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
3073
3074 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
3075 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
3076 return std::make_pair(BeginID, EndID);
3077}
3078
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003079/// \brief Optionally returns true or false if the preallocated preprocessed
3080/// entity with index \arg Index came from file \arg FID.
3081llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
3082 FileID FID) {
3083 if (FID.isInvalid())
3084 return false;
3085
3086 std::pair<Module *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3087 Module &M = *PPInfo.first;
3088 unsigned LocalIndex = PPInfo.second;
3089 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
3090
3091 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
3092 if (Loc.isInvalid())
3093 return false;
3094
3095 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
3096 return true;
3097 else
3098 return false;
3099}
3100
Douglas Gregord10a3812011-08-25 18:14:34 +00003101namespace {
3102 /// \brief Visitor used to search for information about a header file.
3103 class HeaderFileInfoVisitor {
3104 ASTReader &Reader;
3105 const FileEntry *FE;
3106
3107 llvm::Optional<HeaderFileInfo> HFI;
3108
3109 public:
3110 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
3111 : Reader(Reader), FE(FE) { }
3112
3113 static bool visit(Module &M, void *UserData) {
3114 HeaderFileInfoVisitor *This
3115 = static_cast<HeaderFileInfoVisitor *>(UserData);
3116
3117 HeaderFileInfoTrait Trait(This->Reader, M,
3118 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
3119 M.HeaderFileFrameworkStrings,
3120 This->FE->getName());
3121
3122 HeaderFileInfoLookupTable *Table
3123 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
3124 if (!Table)
3125 return false;
3126
3127 // Look in the on-disk hash table for an entry for this file name.
3128 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
3129 &Trait);
3130 if (Pos == Table->end())
3131 return false;
3132
3133 This->HFI = *Pos;
3134 return true;
3135 }
3136
3137 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
3138 };
3139}
3140
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003141HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003142 HeaderFileInfoVisitor Visitor(*this, FE);
3143 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
3144 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003145 if (Listener)
Douglas Gregord10a3812011-08-25 18:14:34 +00003146 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
3147 return *HFI;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003148 }
3149
3150 return HeaderFileInfo();
3151}
3152
David Blaikied6471f72011-09-25 23:23:43 +00003153void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00003154 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
3155 Module &F = *(*I);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003156 unsigned Idx = 0;
3157 while (Idx < F.PragmaDiagMappings.size()) {
3158 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00003159 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
3160 Diag.DiagStatePoints.push_back(
3161 DiagnosticsEngine::DiagStatePoint(&Diag.DiagStates.back(),
3162 FullSourceLoc(Loc, SourceMgr)));
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003163 while (1) {
3164 assert(Idx < F.PragmaDiagMappings.size() &&
3165 "Invalid data, didn't find '-1' marking end of diag/map pairs");
3166 if (Idx >= F.PragmaDiagMappings.size()) {
3167 break; // Something is messed up but at least avoid infinite loop in
3168 // release build.
3169 }
3170 unsigned DiagID = F.PragmaDiagMappings[Idx++];
3171 if (DiagID == (unsigned)-1) {
3172 break; // no more diag/map pairs for this location.
3173 }
3174 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00003175 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
3176 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003177 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003178 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00003179 }
3180}
3181
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003182/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003183ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregora119da02011-08-02 16:26:37 +00003184 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turnere9b76c12011-07-20 21:31:32 +00003185 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor1e849b62011-07-29 00:21:44 +00003186 Module *M = I->second;
Douglas Gregore3605012011-08-02 18:32:54 +00003187 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003188}
3189
3190/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00003191///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003192/// The index is the type ID, shifted and minus the number of predefs. This
3193/// routine actually reads the record corresponding to the type at the given
3194/// location. It is a helper routine for GetType, which deals with reading type
3195/// IDs.
Douglas Gregor393f2492011-07-22 00:38:23 +00003196QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003197 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00003198 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00003199
Douglas Gregor0b748912009-04-14 21:18:50 +00003200 // Keep track of where we are in the stream, then jump back there
3201 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003202 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00003203
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003204 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00003205
Douglas Gregord89275b2009-07-06 18:54:52 +00003206 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003207 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00003208
Douglas Gregor393f2492011-07-22 00:38:23 +00003209 unsigned Idx = 0;
Sebastian Redlc3632732010-10-05 15:59:54 +00003210 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003211 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003212 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003213 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3214 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003215 if (Record.size() != 2) {
3216 Error("Incorrect encoding of extended qualifier type");
3217 return QualType();
3218 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003219 QualType Base = readType(*Loc.F, Record, Idx);
3220 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
Douglas Gregor35942772011-09-09 21:34:22 +00003221 return Context.getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00003222 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003223
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003224 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003225 if (Record.size() != 1) {
3226 Error("Incorrect encoding of complex type");
3227 return QualType();
3228 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003229 QualType ElemType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003230 return Context.getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003231 }
3232
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003233 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003234 if (Record.size() != 1) {
3235 Error("Incorrect encoding of pointer type");
3236 return QualType();
3237 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003238 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003239 return Context.getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003240 }
3241
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003242 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003243 if (Record.size() != 1) {
3244 Error("Incorrect encoding of block pointer type");
3245 return QualType();
3246 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003247 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003248 return Context.getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003249 }
3250
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003251 case TYPE_LVALUE_REFERENCE: {
Richard Smithdf1550f2011-04-12 10:38:03 +00003252 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003253 Error("Incorrect encoding of lvalue reference type");
3254 return QualType();
3255 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003256 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003257 return Context.getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003258 }
3259
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003260 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003261 if (Record.size() != 1) {
3262 Error("Incorrect encoding of rvalue reference type");
3263 return QualType();
3264 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003265 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003266 return Context.getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003267 }
3268
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003269 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00003270 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003271 Error("Incorrect encoding of member pointer type");
3272 return QualType();
3273 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003274 QualType PointeeType = readType(*Loc.F, Record, Idx);
3275 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00003276 if (PointeeType.isNull() || ClassType.isNull())
3277 return QualType();
3278
Douglas Gregor35942772011-09-09 21:34:22 +00003279 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003280 }
3281
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003282 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003283 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003284 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3285 unsigned IndexTypeQuals = Record[2];
3286 unsigned Idx = 3;
3287 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003288 return Context.getConstantArrayType(ElementType, Size,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003289 ASM, IndexTypeQuals);
3290 }
3291
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003292 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003293 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003294 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3295 unsigned IndexTypeQuals = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003296 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003297 }
3298
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003299 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003300 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00003301 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3302 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00003303 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3304 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
Douglas Gregor35942772011-09-09 21:34:22 +00003305 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003306 ASM, IndexTypeQuals,
3307 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003308 }
3309
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003310 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003311 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003312 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003313 return QualType();
3314 }
3315
Douglas Gregor393f2492011-07-22 00:38:23 +00003316 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003317 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00003318 unsigned VecKind = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003319 return Context.getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00003320 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003321 }
3322
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003323 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003324 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003325 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003326 return QualType();
3327 }
3328
Douglas Gregor393f2492011-07-22 00:38:23 +00003329 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003330 unsigned NumElements = Record[1];
Douglas Gregor35942772011-09-09 21:34:22 +00003331 return Context.getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003332 }
3333
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003334 case TYPE_FUNCTION_NO_PROTO: {
John McCallf85e1932011-06-15 23:02:42 +00003335 if (Record.size() != 6) {
Douglas Gregora02b1472009-04-28 21:53:25 +00003336 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003337 return QualType();
3338 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003339 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCallf85e1932011-06-15 23:02:42 +00003340 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
3341 (CallingConv)Record[4], Record[5]);
Douglas Gregor35942772011-09-09 21:34:22 +00003342 return Context.getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003343 }
3344
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003345 case TYPE_FUNCTION_PROTO: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003346 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalle23cf432010-12-14 08:05:40 +00003347
3348 FunctionProtoType::ExtProtoInfo EPI;
3349 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmana49218e2011-04-09 08:18:08 +00003350 /*hasregparm*/ Record[2],
3351 /*regparm*/ Record[3],
John McCallf85e1932011-06-15 23:02:42 +00003352 static_cast<CallingConv>(Record[4]),
3353 /*produces*/ Record[5]);
John McCalle23cf432010-12-14 08:05:40 +00003354
John McCallf85e1932011-06-15 23:02:42 +00003355 unsigned Idx = 6;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003356 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003357 SmallVector<QualType, 16> ParamTypes;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003358 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00003359 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalle23cf432010-12-14 08:05:40 +00003360
3361 EPI.Variadic = Record[Idx++];
3362 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00003363 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redl60618fa2011-03-12 11:50:43 +00003364 ExceptionSpecificationType EST =
3365 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3366 EPI.ExceptionSpecType = EST;
3367 if (EST == EST_Dynamic) {
3368 EPI.NumExceptions = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003369 SmallVector<QualType, 2> Exceptions;
Sebastian Redl60618fa2011-03-12 11:50:43 +00003370 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00003371 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redl60618fa2011-03-12 11:50:43 +00003372 EPI.Exceptions = Exceptions.data();
3373 } else if (EST == EST_ComputedNoexcept) {
3374 EPI.NoexceptExpr = ReadExpr(*Loc.F);
3375 }
Douglas Gregor35942772011-09-09 21:34:22 +00003376 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00003377 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003378 }
3379
Douglas Gregor409448c2011-07-21 22:35:25 +00003380 case TYPE_UNRESOLVED_USING: {
3381 unsigned Idx = 0;
Douglas Gregor35942772011-09-09 21:34:22 +00003382 return Context.getTypeDeclType(
Douglas Gregor409448c2011-07-21 22:35:25 +00003383 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
3384 }
3385
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003386 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003387 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003388 Error("incorrect encoding of typedef type");
3389 return QualType();
3390 }
Douglas Gregor409448c2011-07-21 22:35:25 +00003391 unsigned Idx = 0;
3392 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003393 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003394 if (!Canonical.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00003395 Canonical = Context.getCanonicalType(Canonical);
3396 return Context.getTypedefType(Decl, Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003397 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003398
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003399 case TYPE_TYPEOF_EXPR:
Douglas Gregor35942772011-09-09 21:34:22 +00003400 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003401
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003402 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003403 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003404 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003405 return QualType();
3406 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003407 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003408 return Context.getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003409 }
Mike Stump1eb44332009-09-09 15:08:12 +00003410
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003411 case TYPE_DECLTYPE:
Douglas Gregor35942772011-09-09 21:34:22 +00003412 return Context.getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson395b4752009-06-24 19:06:50 +00003413
Sean Huntca63c202011-05-24 22:41:36 +00003414 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003415 QualType BaseType = readType(*Loc.F, Record, Idx);
3416 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Sean Huntca63c202011-05-24 22:41:36 +00003417 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003418 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
Sean Huntca63c202011-05-24 22:41:36 +00003419 }
3420
Richard Smith34b41d92011-02-20 03:19:35 +00003421 case TYPE_AUTO:
Douglas Gregor35942772011-09-09 21:34:22 +00003422 return Context.getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith34b41d92011-02-20 03:19:35 +00003423
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003424 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003425 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003426 Error("incorrect encoding of record type");
3427 return QualType();
3428 }
Douglas Gregor409448c2011-07-21 22:35:25 +00003429 unsigned Idx = 0;
3430 bool IsDependent = Record[Idx++];
3431 QualType T
Douglas Gregor35942772011-09-09 21:34:22 +00003432 = Context.getRecordType(ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00003433 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003434 return T;
3435 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003436
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003437 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003438 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003439 Error("incorrect encoding of enum type");
3440 return QualType();
3441 }
Douglas Gregor409448c2011-07-21 22:35:25 +00003442 unsigned Idx = 0;
3443 bool IsDependent = Record[Idx++];
3444 QualType T
Douglas Gregor35942772011-09-09 21:34:22 +00003445 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00003446 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003447 return T;
3448 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003449
John McCall9d156a72011-01-06 01:58:22 +00003450 case TYPE_ATTRIBUTED: {
3451 if (Record.size() != 3) {
3452 Error("incorrect encoding of attributed type");
3453 return QualType();
3454 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003455 QualType modifiedType = readType(*Loc.F, Record, Idx);
3456 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall9d156a72011-01-06 01:58:22 +00003457 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
Douglas Gregor35942772011-09-09 21:34:22 +00003458 return Context.getAttributedType(kind, modifiedType, equivalentType);
John McCall9d156a72011-01-06 01:58:22 +00003459 }
3460
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003461 case TYPE_PAREN: {
3462 if (Record.size() != 1) {
3463 Error("incorrect encoding of paren type");
3464 return QualType();
3465 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003466 QualType InnerType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003467 return Context.getParenType(InnerType);
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003468 }
3469
Douglas Gregor7536dd52010-12-20 02:24:11 +00003470 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00003471 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00003472 Error("incorrect encoding of pack expansion type");
3473 return QualType();
3474 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003475 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregor7536dd52010-12-20 02:24:11 +00003476 if (Pattern.isNull())
3477 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00003478 llvm::Optional<unsigned> NumExpansions;
3479 if (Record[1])
3480 NumExpansions = Record[1] - 1;
Douglas Gregor35942772011-09-09 21:34:22 +00003481 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00003482 }
3483
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003484 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003485 unsigned Idx = 0;
3486 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00003487 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003488 QualType NamedType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003489 return Context.getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00003490 }
3491
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003492 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003493 unsigned Idx = 0;
Douglas Gregor409448c2011-07-21 22:35:25 +00003494 ObjCInterfaceDecl *ItfD
3495 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003496 return Context.getObjCInterfaceType(ItfD);
John McCallc12c5bb2010-05-15 11:32:37 +00003497 }
3498
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003499 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00003500 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00003501 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003502 unsigned NumProtos = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003503 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003504 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor409448c2011-07-21 22:35:25 +00003505 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00003506 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003507 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003508
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003509 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003510 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00003511 QualType Pointee = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003512 return Context.getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003513 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00003514
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003515 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00003516 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00003517 QualType Parm = readType(*Loc.F, Record, Idx);
3518 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCall49a832b2009-10-18 09:09:24 +00003519 return
Douglas Gregor35942772011-09-09 21:34:22 +00003520 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
John McCall49a832b2009-10-18 09:09:24 +00003521 Replacement);
3522 }
John McCall3cb0ebd2010-03-10 03:28:59 +00003523
Douglas Gregorc3069d62011-01-14 02:55:32 +00003524 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3525 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00003526 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorc3069d62011-01-14 02:55:32 +00003527 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003528 return Context.getSubstTemplateTypeParmPackType(
Douglas Gregorc3069d62011-01-14 02:55:32 +00003529 cast<TemplateTypeParmType>(Parm),
3530 ArgPack);
3531 }
3532
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003533 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor409448c2011-07-21 22:35:25 +00003534 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003535 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003536 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003537 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003538 return
Douglas Gregor35942772011-09-09 21:34:22 +00003539 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00003540 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003541
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003542 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003543 unsigned Idx = 0;
3544 unsigned Depth = Record[Idx++];
3545 unsigned Index = Record[Idx++];
3546 bool Pack = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00003547 TemplateTypeParmDecl *D
3548 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003549 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003550 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003551
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003552 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003553 unsigned Idx = 0;
3554 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00003555 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00003556 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003557 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003558 if (!Canon.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00003559 Canon = Context.getCanonicalType(Canon);
3560 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003561 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003562
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003563 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003564 unsigned Idx = 0;
3565 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00003566 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00003567 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003568 unsigned NumArgs = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003569 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003570 Args.reserve(NumArgs);
3571 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00003572 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00003573 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003574 Args.size(), Args.data());
3575 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003576
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003577 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003578 unsigned Idx = 0;
3579
3580 // ArrayType
Douglas Gregor393f2492011-07-22 00:38:23 +00003581 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003582 ArrayType::ArraySizeModifier ASM
3583 = (ArrayType::ArraySizeModifier)Record[Idx++];
3584 unsigned IndexTypeQuals = Record[Idx++];
3585
3586 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00003587 Expr *NumElts = ReadExpr(*Loc.F);
3588 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003589
Douglas Gregor35942772011-09-09 21:34:22 +00003590 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003591 IndexTypeQuals, Brackets);
3592 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003593
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003594 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003595 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003596 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003597 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner5f9e2722011-07-23 10:55:15 +00003598 SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00003599 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003600 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003601 QualType T;
Richard Smith3e4c6c42011-05-05 21:57:07 +00003602 if (Underlying.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00003603 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003604 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003605 else
Douglas Gregor35942772011-09-09 21:34:22 +00003606 T = Context.getTemplateSpecializationType(Name, Args.data(),
Richard Smith3e4c6c42011-05-05 21:57:07 +00003607 Args.size(), Underlying);
John McCallf4c73712011-01-19 06:33:43 +00003608 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003609 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003610 }
Eli Friedmanb001de72011-10-06 23:00:33 +00003611
3612 case TYPE_ATOMIC: {
3613 if (Record.size() != 1) {
3614 Error("Incorrect encoding of atomic type");
3615 return QualType();
3616 }
3617 QualType ValueType = readType(*Loc.F, Record, Idx);
3618 return Context.getAtomicType(ValueType);
3619 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003620 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003621 // Suppress a GCC warning
3622 return QualType();
3623}
3624
Sebastian Redlc3632732010-10-05 15:59:54 +00003625class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003626 ASTReader &Reader;
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003627 Module &F;
Sebastian Redl577d4792010-07-22 22:43:28 +00003628 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003629 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00003630 unsigned &Idx;
3631
Sebastian Redlc3632732010-10-05 15:59:54 +00003632 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3633 unsigned &I) {
3634 return Reader.ReadSourceLocation(F, R, I);
3635 }
3636
Douglas Gregor409448c2011-07-21 22:35:25 +00003637 template<typename T>
3638 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
3639 return Reader.ReadDeclAs<T>(F, Record, Idx);
3640 }
3641
John McCalla1ee0c52009-10-16 21:56:05 +00003642public:
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003643 TypeLocReader(ASTReader &Reader, Module &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003644 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00003645 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3646 { }
John McCalla1ee0c52009-10-16 21:56:05 +00003647
John McCall51bd8032009-10-18 01:05:36 +00003648 // We want compile-time assurance that we've enumerated all of
3649 // these, so unfortunately we have to declare them first, then
3650 // define them out-of-line.
3651#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00003652#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00003653 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003654#include "clang/AST/TypeLocNodes.def"
3655
John McCall51bd8032009-10-18 01:05:36 +00003656 void VisitFunctionTypeLoc(FunctionTypeLoc);
3657 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003658};
3659
John McCall51bd8032009-10-18 01:05:36 +00003660void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00003661 // nothing to do
3662}
John McCall51bd8032009-10-18 01:05:36 +00003663void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003664 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00003665 if (TL.needsExtraLocalData()) {
3666 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3667 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3668 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3669 TL.setModeAttr(Record[Idx++]);
3670 }
John McCalla1ee0c52009-10-16 21:56:05 +00003671}
John McCall51bd8032009-10-18 01:05:36 +00003672void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003673 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003674}
John McCall51bd8032009-10-18 01:05:36 +00003675void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003676 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003677}
John McCall51bd8032009-10-18 01:05:36 +00003678void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003679 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003680}
John McCall51bd8032009-10-18 01:05:36 +00003681void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003682 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003683}
John McCall51bd8032009-10-18 01:05:36 +00003684void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003685 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003686}
John McCall51bd8032009-10-18 01:05:36 +00003687void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003688 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00003689 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003690}
John McCall51bd8032009-10-18 01:05:36 +00003691void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003692 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3693 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003694 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00003695 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003696 else
John McCall51bd8032009-10-18 01:05:36 +00003697 TL.setSizeExpr(0);
3698}
3699void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3700 VisitArrayTypeLoc(TL);
3701}
3702void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3703 VisitArrayTypeLoc(TL);
3704}
3705void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3706 VisitArrayTypeLoc(TL);
3707}
3708void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3709 DependentSizedArrayTypeLoc TL) {
3710 VisitArrayTypeLoc(TL);
3711}
3712void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3713 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003714 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003715}
3716void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003717 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003718}
3719void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003720 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003721}
3722void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +00003723 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
3724 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Douglas Gregordab60ad2010-10-01 18:44:50 +00003725 TL.setTrailingReturn(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00003726 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor409448c2011-07-21 22:35:25 +00003727 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003728 }
3729}
3730void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3731 VisitFunctionTypeLoc(TL);
3732}
3733void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3734 VisitFunctionTypeLoc(TL);
3735}
John McCalled976492009-12-04 22:46:56 +00003736void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003737 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00003738}
John McCall51bd8032009-10-18 01:05:36 +00003739void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003740 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003741}
3742void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003743 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3744 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3745 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003746}
3747void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003748 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3749 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3750 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3751 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003752}
3753void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003754 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003755}
Sean Huntca63c202011-05-24 22:41:36 +00003756void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3757 TL.setKWLoc(ReadSourceLocation(Record, Idx));
3758 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3759 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3760 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3761}
Richard Smith34b41d92011-02-20 03:19:35 +00003762void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3763 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3764}
John McCall51bd8032009-10-18 01:05:36 +00003765void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003766 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003767}
3768void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003769 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003770}
John McCall9d156a72011-01-06 01:58:22 +00003771void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3772 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3773 if (TL.hasAttrOperand()) {
3774 SourceRange range;
3775 range.setBegin(ReadSourceLocation(Record, Idx));
3776 range.setEnd(ReadSourceLocation(Record, Idx));
3777 TL.setAttrOperandParensRange(range);
3778 }
3779 if (TL.hasAttrExprOperand()) {
3780 if (Record[Idx++])
3781 TL.setAttrExprOperand(Reader.ReadExpr(F));
3782 else
3783 TL.setAttrExprOperand(0);
3784 } else if (TL.hasAttrEnumOperand())
3785 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3786}
John McCall51bd8032009-10-18 01:05:36 +00003787void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003788 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003789}
John McCall49a832b2009-10-18 09:09:24 +00003790void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3791 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003792 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00003793}
Douglas Gregorc3069d62011-01-14 02:55:32 +00003794void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3795 SubstTemplateTypeParmPackTypeLoc TL) {
3796 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3797}
John McCall51bd8032009-10-18 01:05:36 +00003798void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3799 TemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003800 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3801 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3802 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00003803 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3804 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00003805 Reader.GetTemplateArgumentLocInfo(F,
3806 TL.getTypePtr()->getArg(i).getKind(),
3807 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003808}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003809void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3810 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3811 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3812}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003813void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003814 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor9e876872011-03-01 18:12:44 +00003815 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003816}
John McCall3cb0ebd2010-03-10 03:28:59 +00003817void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003818 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00003819}
Douglas Gregor4714c122010-03-31 17:34:00 +00003820void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003821 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor2494dd02011-03-01 01:34:45 +00003822 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00003823 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003824}
John McCall33500952010-06-11 00:33:02 +00003825void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3826 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003827 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor94fdffa2011-03-01 20:11:18 +00003828 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00003829 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3830 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3831 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003832 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3833 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00003834 Reader.GetTemplateArgumentLocInfo(F,
3835 TL.getTypePtr()->getArg(I).getKind(),
3836 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003837}
Douglas Gregor7536dd52010-12-20 02:24:11 +00003838void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3839 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3840}
John McCall51bd8032009-10-18 01:05:36 +00003841void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003842 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00003843}
3844void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3845 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00003846 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3847 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003848 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00003849 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003850}
John McCall54e14c42009-10-22 22:37:11 +00003851void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003852 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00003853}
Eli Friedmanb001de72011-10-06 23:00:33 +00003854void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
3855 TL.setKWLoc(ReadSourceLocation(Record, Idx));
3856 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3857 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3858}
John McCalla1ee0c52009-10-16 21:56:05 +00003859
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003860TypeSourceInfo *ASTReader::GetTypeSourceInfo(Module &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003861 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00003862 unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00003863 QualType InfoTy = readType(F, Record, Idx);
John McCalla1ee0c52009-10-16 21:56:05 +00003864 if (InfoTy.isNull())
3865 return 0;
3866
Douglas Gregor35942772011-09-09 21:34:22 +00003867 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00003868 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00003869 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00003870 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00003871 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00003872}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003873
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003874QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00003875 unsigned FastQuals = ID & Qualifiers::FastMask;
3876 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003877
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003878 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003879 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003880 switch ((PredefinedTypeIDs)Index) {
3881 case PREDEF_TYPE_NULL_ID: return QualType();
Douglas Gregor35942772011-09-09 21:34:22 +00003882 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
3883 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003884
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003885 case PREDEF_TYPE_CHAR_U_ID:
3886 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00003887 // FIXME: Check that the signedness of CharTy is correct!
Douglas Gregor35942772011-09-09 21:34:22 +00003888 T = Context.CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003889 break;
3890
Douglas Gregor35942772011-09-09 21:34:22 +00003891 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
3892 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
3893 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
3894 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
3895 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
3896 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
3897 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
3898 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
3899 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
3900 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
3901 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
3902 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
3903 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003904 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00003905 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
3906 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
3907 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
3908 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
3909 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
John McCall3c3b7f92011-10-25 17:37:35 +00003910 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00003911 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
3912 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
3913 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
3914 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
3915 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
3916 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
3917 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
3918 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
3919 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003920
3921 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
Douglas Gregor35942772011-09-09 21:34:22 +00003922 T = Context.getAutoRRefDeductType();
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003923 break;
John McCall0ddaeb92011-10-17 18:09:15 +00003924
3925 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
3926 T = Context.ARCUnbridgedCastTy;
3927 break;
3928
Douglas Gregor2cf26342009-04-09 22:27:44 +00003929 }
3930
3931 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00003932 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003933 }
3934
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003935 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003936 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00003937 if (TypesLoaded[Index].isNull()) {
Douglas Gregor393f2492011-07-22 00:38:23 +00003938 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00003939 if (TypesLoaded[Index].isNull())
3940 return QualType();
3941
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003942 TypesLoaded[Index]->setFromAST();
Sebastian Redl30c514c2010-07-14 23:45:08 +00003943 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003944 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00003945 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00003946 }
Mike Stump1eb44332009-09-09 15:08:12 +00003947
John McCall0953e762009-09-24 19:53:00 +00003948 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003949}
3950
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003951QualType ASTReader::getLocalType(Module &F, unsigned LocalID) {
Douglas Gregor393f2492011-07-22 00:38:23 +00003952 return GetType(getGlobalTypeID(F, LocalID));
3953}
3954
3955serialization::TypeID
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003956ASTReader::getGlobalTypeID(Module &F, unsigned LocalID) const {
Douglas Gregora119da02011-08-02 16:26:37 +00003957 unsigned FastQuals = LocalID & Qualifiers::FastMask;
3958 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
3959
3960 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
3961 return LocalID;
3962
3963 ContinuousRangeMap<uint32_t, int, 2>::iterator I
3964 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
3965 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
3966
3967 unsigned GlobalIndex = LocalIndex + I->second;
3968 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
3969}
3970
John McCall833ca992009-10-29 08:12:44 +00003971TemplateArgumentLocInfo
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003972ASTReader::GetTemplateArgumentLocInfo(Module &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00003973 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00003974 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003975 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00003976 switch (Kind) {
3977 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00003978 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00003979 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00003980 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00003981 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003982 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3983 Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00003984 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003985 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregora7fc9012011-01-05 18:58:31 +00003986 SourceLocation());
3987 }
3988 case TemplateArgument::TemplateExpansion: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003989 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3990 Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003991 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003992 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003993 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregorba68eca2011-01-05 17:40:24 +00003994 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00003995 }
John McCall833ca992009-10-29 08:12:44 +00003996 case TemplateArgument::Null:
3997 case TemplateArgument::Integral:
3998 case TemplateArgument::Declaration:
3999 case TemplateArgument::Pack:
4000 return TemplateArgumentLocInfo();
4001 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00004002 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00004003 return TemplateArgumentLocInfo();
4004}
4005
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004006TemplateArgumentLoc
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004007ASTReader::ReadTemplateArgumentLoc(Module &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004008 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004009 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004010
4011 if (Arg.getKind() == TemplateArgument::Expression) {
4012 if (Record[Index++]) // bool InfoHasSameExpr.
4013 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4014 }
Sebastian Redlc3632732010-10-05 15:59:54 +00004015 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004016 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004017}
4018
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004019Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00004020 return GetDecl(ID);
4021}
4022
Douglas Gregore92b8a12011-08-04 00:01:48 +00004023uint64_t ASTReader::readCXXBaseSpecifiers(Module &M, const RecordData &Record,
4024 unsigned &Idx){
4025 if (Idx >= Record.size())
Douglas Gregor7c789c12010-10-29 22:39:52 +00004026 return 0;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004027
Douglas Gregore92b8a12011-08-04 00:01:48 +00004028 unsigned LocalID = Record[Idx++];
4029 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004030}
4031
4032CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004033 RecordLocation Loc = getLocalBitOffset(Offset);
4034 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004035 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004036 Cursor.JumpToBit(Loc.Offset);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004037 ReadingKindTracker ReadingKind(Read_Decl, *this);
4038 RecordData Record;
4039 unsigned Code = Cursor.ReadCode();
4040 unsigned RecCode = Cursor.ReadRecord(Code, Record);
4041 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4042 Error("Malformed AST file: missing C++ base specifiers");
4043 return 0;
4044 }
4045
4046 unsigned Idx = 0;
4047 unsigned NumBases = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00004048 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004049 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4050 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004051 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004052 return Bases;
4053}
4054
Douglas Gregor409448c2011-07-21 22:35:25 +00004055serialization::DeclID
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004056ASTReader::getGlobalDeclID(Module &F, unsigned LocalID) const {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004057 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregor496c7092011-08-03 15:48:04 +00004058 return LocalID;
4059
4060 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004061 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregor496c7092011-08-03 15:48:04 +00004062 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
4063
4064 return LocalID + I->second;
Douglas Gregor409448c2011-07-21 22:35:25 +00004065}
4066
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004067bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
4068 Module &M) const {
4069 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
4070 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4071 return &M == I->second;
4072}
4073
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004074SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
4075 if (ID < NUM_PREDEF_DECL_IDS)
4076 return SourceLocation();
4077
4078 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4079
4080 if (Index > DeclsLoaded.size()) {
4081 Error("declaration ID out-of-range for AST file");
4082 return SourceLocation();
4083 }
4084
4085 if (Decl *D = DeclsLoaded[Index])
4086 return D->getLocation();
4087
4088 unsigned RawLocation = 0;
4089 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
4090 return ReadSourceLocation(*Rec.F, RawLocation);
4091}
4092
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004093Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004094 if (ID < NUM_PREDEF_DECL_IDS) {
4095 switch ((PredefinedDeclIDs)ID) {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004096 case PREDEF_DECL_NULL_ID:
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004097 return 0;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004098
4099 case PREDEF_DECL_TRANSLATION_UNIT_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004100 return Context.getTranslationUnitDecl();
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00004101
4102 case PREDEF_DECL_OBJC_ID_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004103 return Context.getObjCIdDecl();
Douglas Gregor79d67262011-08-12 05:59:41 +00004104
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004105 case PREDEF_DECL_OBJC_SEL_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004106 return Context.getObjCSelDecl();
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004107
Douglas Gregor79d67262011-08-12 05:59:41 +00004108 case PREDEF_DECL_OBJC_CLASS_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004109 return Context.getObjCClassDecl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004110
4111 case PREDEF_DECL_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004112 return Context.getInt128Decl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004113
4114 case PREDEF_DECL_UNSIGNED_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004115 return Context.getUInt128Decl();
Douglas Gregore97179c2011-09-08 01:46:34 +00004116
4117 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004118 return Context.getObjCInstanceTypeDecl();
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004119 }
4120
Douglas Gregor2cf26342009-04-09 22:27:44 +00004121 return 0;
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004122 }
4123
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004124 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4125
4126 if (Index > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004127 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004128 return 0;
4129 }
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004130
4131if (!DeclsLoaded[Index]) {
Douglas Gregor496c7092011-08-03 15:48:04 +00004132 ReadDeclRecord(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00004133 if (DeserializationListener)
4134 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
4135 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004136
4137 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004138}
4139
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004140serialization::DeclID ASTReader::ReadDeclID(Module &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00004141 const RecordData &Record,
4142 unsigned &Idx) {
4143 if (Idx >= Record.size()) {
4144 Error("Corrupted AST file");
4145 return 0;
4146 }
4147
4148 return getGlobalDeclID(F, Record[Idx++]);
4149}
4150
Chris Lattner887e2b32009-04-27 05:46:25 +00004151/// \brief Resolve the offset of a statement into a statement.
4152///
4153/// This operation will read a new statement from the external
4154/// source each time it is called, and is meant to be used via a
4155/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004156Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004157 // Switch case IDs are per Decl.
4158 ClearSwitchCaseIDs();
4159
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004160 // Offset here is a global offset across the entire chain.
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004161 RecordLocation Loc = getLocalBitOffset(Offset);
4162 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
4163 return ReadStmtFromStream(*Loc.F);
Douglas Gregor250fc9c2009-04-18 00:07:54 +00004164}
4165
Douglas Gregor851c75a2011-08-24 21:27:34 +00004166namespace {
4167 class FindExternalLexicalDeclsVisitor {
4168 ASTReader &Reader;
4169 const DeclContext *DC;
4170 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004171
Douglas Gregor851c75a2011-08-24 21:27:34 +00004172 SmallVectorImpl<Decl*> &Decls;
4173 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
4174
4175 public:
4176 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
4177 bool (*isKindWeWant)(Decl::Kind),
4178 SmallVectorImpl<Decl*> &Decls)
4179 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
4180 {
4181 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
4182 PredefsVisited[I] = false;
4183 }
4184
4185 static bool visit(Module &M, bool Preorder, void *UserData) {
4186 if (Preorder)
4187 return false;
4188
4189 FindExternalLexicalDeclsVisitor *This
4190 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
4191
4192 Module::DeclContextInfosMap::iterator Info
4193 = M.DeclContextInfos.find(This->DC);
4194 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
4195 return false;
4196
4197 // Load all of the declaration IDs
4198 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
4199 *IDE = ID + Info->second.NumLexicalDecls;
4200 ID != IDE; ++ID) {
4201 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
4202 continue;
4203
4204 // Don't add predefined declarations to the lexical context more
4205 // than once.
4206 if (ID->second < NUM_PREDEF_DECL_IDS) {
4207 if (This->PredefsVisited[ID->second])
4208 continue;
4209
4210 This->PredefsVisited[ID->second] = true;
4211 }
4212
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004213 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
4214 if (!This->DC->isDeclInLexicalTraversal(D))
4215 This->Decls.push_back(D);
4216 }
Douglas Gregor851c75a2011-08-24 21:27:34 +00004217 }
4218
4219 return false;
4220 }
4221 };
4222}
4223
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004224ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00004225 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner5f9e2722011-07-23 10:55:15 +00004226 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00004227 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor851c75a2011-08-24 21:27:34 +00004228 // least. Walk all of the modules in the order they were loaded.
4229 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
4230 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00004231 ++NumLexicalDeclContextsRead;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004232 return ELR_Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004233}
4234
Douglas Gregor0d95f772011-08-24 19:03:07 +00004235namespace {
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004236
4237class DeclIDComp {
4238 ASTReader &Reader;
4239 Module &Mod;
4240
4241public:
4242 DeclIDComp(ASTReader &Reader, Module &M) : Reader(Reader), Mod(M) {}
4243
4244 bool operator()(LocalDeclID L, LocalDeclID R) const {
4245 SourceLocation LHS = getLocation(L);
4246 SourceLocation RHS = getLocation(R);
4247 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4248 }
4249
4250 bool operator()(SourceLocation LHS, LocalDeclID R) const {
4251 SourceLocation RHS = getLocation(R);
4252 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4253 }
4254
4255 bool operator()(LocalDeclID L, SourceLocation RHS) const {
4256 SourceLocation LHS = getLocation(L);
4257 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4258 }
4259
4260 SourceLocation getLocation(LocalDeclID ID) const {
4261 return Reader.getSourceManager().getFileLoc(
4262 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
4263 }
4264};
4265
4266}
4267
4268void ASTReader::FindFileRegionDecls(FileID File,
4269 unsigned Offset, unsigned Length,
4270 SmallVectorImpl<Decl *> &Decls) {
4271 SourceManager &SM = getSourceManager();
4272
4273 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
4274 if (I == FileDeclIDs.end())
4275 return;
4276
4277 FileDeclsInfo &DInfo = I->second;
4278 if (DInfo.Decls.empty())
4279 return;
4280
4281 SourceLocation
4282 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
4283 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
4284
4285 DeclIDComp DIDComp(*this, *DInfo.Mod);
4286 ArrayRef<serialization::LocalDeclID>::iterator
4287 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
4288 BeginLoc, DIDComp);
4289 if (BeginIt != DInfo.Decls.begin())
4290 --BeginIt;
4291
4292 ArrayRef<serialization::LocalDeclID>::iterator
4293 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
4294 EndLoc, DIDComp);
4295 if (EndIt != DInfo.Decls.end())
4296 ++EndIt;
4297
4298 for (ArrayRef<serialization::LocalDeclID>::iterator
4299 DIt = BeginIt; DIt != EndIt; ++DIt)
4300 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
4301}
4302
4303namespace {
Douglas Gregor0d95f772011-08-24 19:03:07 +00004304 /// \brief Module visitor used to perform name lookup into a
4305 /// declaration context.
4306 class DeclContextNameLookupVisitor {
4307 ASTReader &Reader;
4308 const DeclContext *DC;
4309 DeclarationName Name;
4310 SmallVectorImpl<NamedDecl *> &Decls;
4311
4312 public:
4313 DeclContextNameLookupVisitor(ASTReader &Reader,
4314 const DeclContext *DC, DeclarationName Name,
4315 SmallVectorImpl<NamedDecl *> &Decls)
4316 : Reader(Reader), DC(DC), Name(Name), Decls(Decls) { }
4317
4318 static bool visit(Module &M, void *UserData) {
4319 DeclContextNameLookupVisitor *This
4320 = static_cast<DeclContextNameLookupVisitor *>(UserData);
4321
4322 // Check whether we have any visible declaration information for
4323 // this context in this module.
4324 Module::DeclContextInfosMap::iterator Info
4325 = M.DeclContextInfos.find(This->DC);
4326 if (Info == M.DeclContextInfos.end() || !Info->second.NameLookupTableData)
4327 return false;
4328
4329 // Look for this name within this module.
4330 ASTDeclContextNameLookupTable *LookupTable =
4331 (ASTDeclContextNameLookupTable*)Info->second.NameLookupTableData;
4332 ASTDeclContextNameLookupTable::iterator Pos
4333 = LookupTable->find(This->Name);
4334 if (Pos == LookupTable->end())
4335 return false;
4336
4337 bool FoundAnything = false;
4338 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
4339 for (; Data.first != Data.second; ++Data.first) {
4340 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
4341 if (!ND)
4342 continue;
4343
4344 if (ND->getDeclName() != This->Name) {
4345 assert(!This->Name.getCXXNameType().isNull() &&
4346 "Name mismatch without a type");
4347 continue;
4348 }
4349
4350 // Record this declaration.
4351 FoundAnything = true;
4352 This->Decls.push_back(ND);
4353 }
4354
4355 return FoundAnything;
4356 }
4357 };
4358}
4359
John McCall76bd1f32010-06-01 09:23:16 +00004360DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004361ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00004362 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00004363 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00004364 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00004365 if (!Name)
4366 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
4367 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004368
Chris Lattner5f9e2722011-07-23 10:55:15 +00004369 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregor0d95f772011-08-24 19:03:07 +00004370 DeclContextNameLookupVisitor Visitor(*this, DC, Name, Decls);
4371 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00004372 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00004373 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00004374 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004375}
4376
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00004377/// \brief Under non-PCH compilation the consumer receives the objc methods
4378/// before receiving the implementation, and codegen depends on this.
4379/// We simulate this by deserializing and passing to consumer the methods of the
4380/// implementation before passing the deserialized implementation decl.
4381static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
4382 ASTConsumer *Consumer) {
4383 assert(ImplD && Consumer);
4384
4385 for (ObjCImplDecl::method_iterator
4386 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
4387 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
4388
4389 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
4390}
4391
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004392void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004393 assert(Consumer);
4394 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00004395 Decl *D = InterestingDecls.front();
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004396 InterestingDecls.pop_front();
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00004397
4398 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
4399 PassObjCImplDeclToConsumer(ImplD, Consumer);
4400 else
4401 Consumer->HandleInterestingDecl(DeclGroupRef(D));
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004402 }
4403}
4404
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004405void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00004406 this->Consumer = Consumer;
4407
Douglas Gregorfdd01722009-04-14 00:24:19 +00004408 if (!Consumer)
4409 return;
4410
4411 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004412 // Force deserialization of this decl, which will cause it to be queued for
4413 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00004414 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00004415 }
Douglas Gregor1a995dd2011-09-15 18:47:32 +00004416 ExternalDefinitions.clear();
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00004417
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004418 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00004419}
4420
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004421void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004422 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00004423
Mike Stump1eb44332009-09-09 15:08:12 +00004424 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004425 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00004426 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004427 unsigned NumDeclsLoaded
4428 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
4429 (Decl *)0);
4430 unsigned NumIdentifiersLoaded
4431 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
4432 IdentifiersLoaded.end(),
4433 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00004434 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004435 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
4436 SelectorsLoaded.end(),
4437 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00004438
Douglas Gregor4fed3f42009-04-27 18:38:38 +00004439 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
4440 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor0cdd7982011-07-21 18:46:38 +00004441 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00004442 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
4443 NumSLocEntriesRead, TotalNumSLocEntries,
4444 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004445 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004446 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004447 NumTypesLoaded, (unsigned)TypesLoaded.size(),
4448 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
4449 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004450 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004451 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
4452 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004453 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004454 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004455 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
4456 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00004457 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004458 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00004459 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
4460 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00004461 if (TotalNumStatements)
4462 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
4463 NumStatementsRead, TotalNumStatements,
4464 ((float)NumStatementsRead/TotalNumStatements * 100));
4465 if (TotalNumMacros)
4466 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
4467 NumMacrosRead, TotalNumMacros,
4468 ((float)NumMacrosRead/TotalNumMacros * 100));
4469 if (TotalLexicalDeclContexts)
4470 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
4471 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4472 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4473 * 100));
4474 if (TotalVisibleDeclContexts)
4475 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
4476 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4477 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4478 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004479 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004480 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004481 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4482 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00004483 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004484 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00004485 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004486 std::fprintf(stderr, "\n");
Douglas Gregor23d7df52011-07-21 19:50:14 +00004487 dump();
4488 std::fprintf(stderr, "\n");
4489}
4490
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004491template<typename Key, typename Module, unsigned InitialCapacity>
Douglas Gregor23d7df52011-07-21 19:50:14 +00004492static void
Chris Lattner5f9e2722011-07-23 10:55:15 +00004493dumpModuleIDMap(StringRef Name,
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004494 const ContinuousRangeMap<Key, Module *,
Douglas Gregor23d7df52011-07-21 19:50:14 +00004495 InitialCapacity> &Map) {
4496 if (Map.begin() == Map.end())
4497 return;
4498
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004499 typedef ContinuousRangeMap<Key, Module *, InitialCapacity> MapType;
Douglas Gregor23d7df52011-07-21 19:50:14 +00004500 llvm::errs() << Name << ":\n";
4501 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
4502 I != IEnd; ++I) {
4503 llvm::errs() << " " << I->first << " -> " << I->second->FileName
4504 << "\n";
4505 }
4506}
4507
Douglas Gregor23d7df52011-07-21 19:50:14 +00004508void ASTReader::dump() {
Douglas Gregor8df5c9b2011-08-02 11:12:41 +00004509 llvm::errs() << "*** PCH/Module Remappings:\n";
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004510 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor23d7df52011-07-21 19:50:14 +00004511 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor1e849b62011-07-29 00:21:44 +00004512 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00004513 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00004514 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
4515 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00004516 dumpModuleIDMap("Global preprocessed entity map",
4517 GlobalPreprocessedEntityMap);
Douglas Gregor8df5c9b2011-08-02 11:12:41 +00004518
4519 llvm::errs() << "\n*** PCH/Modules Loaded:";
4520 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
4521 MEnd = ModuleMgr.end();
4522 M != MEnd; ++M)
4523 (*M)->dump();
Douglas Gregor2cf26342009-04-09 22:27:44 +00004524}
4525
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00004526/// Return the amount of memory used by memory buffers, breaking down
4527/// by heap-backed versus mmap'ed memory.
4528void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004529 for (ModuleConstIterator I = ModuleMgr.begin(),
4530 E = ModuleMgr.end(); I != E; ++I) {
4531 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00004532 size_t bytes = buf->getBufferSize();
4533 switch (buf->getBufferKind()) {
4534 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
4535 sizes.malloc_bytes += bytes;
4536 break;
4537 case llvm::MemoryBuffer::MemoryBuffer_MMap:
4538 sizes.mmap_bytes += bytes;
4539 break;
4540 }
4541 }
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004542 }
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00004543}
4544
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004545void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00004546 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004547 S.ExternalSource = this;
4548
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004549 // Makes sure any declarations that were deserialized "too early"
4550 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00004551 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00004552 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
4553 PreloadedDecls[I]->getDeclName());
Douglas Gregor668c1a42009-04-21 22:25:48 +00004554 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004555 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004556
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004557 // Load the offsets of the declarations that Sema references.
4558 // They will be lazily deserialized when needed.
4559 if (!SemaDeclRefs.empty()) {
4560 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregor1e5b6f62011-07-28 00:57:24 +00004561 if (!SemaObj->StdNamespace)
4562 SemaObj->StdNamespace = SemaDeclRefs[0];
4563 if (!SemaObj->StdBadAlloc)
4564 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004565 }
4566
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004567 if (!FPPragmaOptions.empty()) {
4568 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4569 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4570 }
4571
4572 if (!OpenCLExtensions.empty()) {
4573 unsigned I = 0;
4574#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4575#include "clang/Basic/OpenCLExtensions.def"
4576
4577 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4578 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00004579}
4580
Douglas Gregor211f6e82011-08-20 04:39:52 +00004581IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregor211f6e82011-08-20 04:39:52 +00004582 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart));
4583 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Douglas Gregoreee242f2011-10-27 09:33:13 +00004584 IdentifierInfo *II = Visitor.getIdentifierInfo();
4585 if (II)
4586 II->setOutOfDate(false);
4587 return II;
Douglas Gregor668c1a42009-04-21 22:25:48 +00004588}
4589
Douglas Gregor95f42922010-10-14 22:11:03 +00004590namespace clang {
4591 /// \brief An identifier-lookup iterator that enumerates all of the
4592 /// identifiers stored within a set of AST files.
4593 class ASTIdentifierIterator : public IdentifierIterator {
4594 /// \brief The AST reader whose identifiers are being enumerated.
4595 const ASTReader &Reader;
4596
4597 /// \brief The current index into the chain of AST files stored in
4598 /// the AST reader.
4599 unsigned Index;
4600
4601 /// \brief The current position within the identifier lookup table
4602 /// of the current AST file.
4603 ASTIdentifierLookupTable::key_iterator Current;
4604
4605 /// \brief The end position within the identifier lookup table of
4606 /// the current AST file.
4607 ASTIdentifierLookupTable::key_iterator End;
4608
4609 public:
4610 explicit ASTIdentifierIterator(const ASTReader &Reader);
4611
Chris Lattner5f9e2722011-07-23 10:55:15 +00004612 virtual StringRef Next();
Douglas Gregor95f42922010-10-14 22:11:03 +00004613 };
4614}
4615
4616ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004617 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor95f42922010-10-14 22:11:03 +00004618 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004619 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00004620 Current = IdTable->key_begin();
4621 End = IdTable->key_end();
4622}
4623
Chris Lattner5f9e2722011-07-23 10:55:15 +00004624StringRef ASTIdentifierIterator::Next() {
Douglas Gregor95f42922010-10-14 22:11:03 +00004625 while (Current == End) {
4626 // If we have exhausted all of our AST files, we're done.
4627 if (Index == 0)
Chris Lattner5f9e2722011-07-23 10:55:15 +00004628 return StringRef();
Douglas Gregor95f42922010-10-14 22:11:03 +00004629
4630 --Index;
4631 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004632 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
4633 IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00004634 Current = IdTable->key_begin();
4635 End = IdTable->key_end();
4636 }
4637
4638 // We have any identifiers remaining in the current AST file; return
4639 // the next one.
4640 std::pair<const char*, unsigned> Key = *Current;
4641 ++Current;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004642 return StringRef(Key.first, Key.second);
Douglas Gregor95f42922010-10-14 22:11:03 +00004643}
4644
4645IdentifierIterator *ASTReader::getIdentifiers() const {
4646 return new ASTIdentifierIterator(*this);
4647}
4648
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004649namespace clang { namespace serialization {
4650 class ReadMethodPoolVisitor {
4651 ASTReader &Reader;
4652 Selector Sel;
4653 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
4654 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004655
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004656 /// \brief Build an ObjCMethodList from a vector of Objective-C method
4657 /// declarations.
4658 ObjCMethodList
4659 buildObjCMethodList(const SmallVectorImpl<ObjCMethodDecl *> &Vec) const
4660 {
4661 ObjCMethodList List;
4662 ObjCMethodList *Prev = 0;
4663 for (unsigned I = 0, N = Vec.size(); I != N; ++I) {
4664 if (!List.Method) {
4665 // This is the first method, which is the easy case.
4666 List.Method = Vec[I];
4667 Prev = &List;
4668 continue;
4669 }
4670
4671 ObjCMethodList *Mem =
4672 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
4673 Prev->Next = new (Mem) ObjCMethodList(Vec[I], 0);
4674 Prev = Prev->Next;
4675 }
4676
4677 return List;
4678 }
4679
4680 public:
4681 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel)
4682 : Reader(Reader), Sel(Sel) { }
4683
4684 static bool visit(Module &M, void *UserData) {
4685 ReadMethodPoolVisitor *This
4686 = static_cast<ReadMethodPoolVisitor *>(UserData);
4687
4688 if (!M.SelectorLookupTable)
4689 return false;
4690
4691 ASTSelectorLookupTable *PoolTable
4692 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
4693 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
4694 if (Pos == PoolTable->end())
4695 return false;
4696
4697 ++This->Reader.NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004698 // FIXME: Not quite happy with the statistics here. We probably should
4699 // disable this tracking when called via LoadSelector.
4700 // Also, should entries without methods count as misses?
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004701 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004702 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004703 if (This->Reader.DeserializationListener)
4704 This->Reader.DeserializationListener->SelectorRead(Data.ID,
4705 This->Sel);
4706
4707 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
4708 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
4709 return true;
Sebastian Redl725cd962010-08-04 20:40:17 +00004710 }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004711
4712 /// \brief Retrieve the instance methods found by this visitor.
4713 ObjCMethodList getInstanceMethods() const {
4714 return buildObjCMethodList(InstanceMethods);
4715 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004716
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004717 /// \brief Retrieve the instance methods found by this visitor.
4718 ObjCMethodList getFactoryMethods() const {
4719 return buildObjCMethodList(FactoryMethods);
4720 }
4721 };
4722} } // end namespace clang::serialization
4723
4724std::pair<ObjCMethodList, ObjCMethodList>
4725ASTReader::ReadMethodPool(Selector Sel) {
4726 ReadMethodPoolVisitor Visitor(*this, Sel);
4727 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
4728 std::pair<ObjCMethodList, ObjCMethodList> Result;
4729 Result.first = Visitor.getInstanceMethods();
4730 Result.second = Visitor.getFactoryMethods();
4731
4732 if (!Result.first.Method && !Result.second.Method)
4733 ++NumMethodPoolMisses;
4734 return Result;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004735}
4736
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004737void ASTReader::ReadKnownNamespaces(
Chris Lattner5f9e2722011-07-23 10:55:15 +00004738 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004739 Namespaces.clear();
4740
4741 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
4742 if (NamespaceDecl *Namespace
4743 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
4744 Namespaces.push_back(Namespace);
4745 }
4746}
4747
Douglas Gregora8623202011-07-27 20:58:46 +00004748void ASTReader::ReadTentativeDefinitions(
4749 SmallVectorImpl<VarDecl *> &TentativeDefs) {
4750 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4751 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
4752 if (Var)
4753 TentativeDefs.push_back(Var);
4754 }
4755 TentativeDefinitions.clear();
4756}
4757
Douglas Gregora2ee20a2011-07-27 21:45:57 +00004758void ASTReader::ReadUnusedFileScopedDecls(
4759 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
4760 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4761 DeclaratorDecl *D
4762 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4763 if (D)
4764 Decls.push_back(D);
4765 }
4766 UnusedFileScopedDecls.clear();
4767}
4768
Douglas Gregor0129b562011-07-27 21:57:17 +00004769void ASTReader::ReadDelegatingConstructors(
4770 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
4771 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
4772 CXXConstructorDecl *D
4773 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
4774 if (D)
4775 Decls.push_back(D);
4776 }
4777 DelegatingCtorDecls.clear();
4778}
4779
Douglas Gregord58a0a52011-07-28 00:39:29 +00004780void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
4781 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
4782 TypedefNameDecl *D
4783 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
4784 if (D)
4785 Decls.push_back(D);
4786 }
4787 ExtVectorDecls.clear();
4788}
4789
Douglas Gregora126f172011-07-28 00:53:40 +00004790void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
4791 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
4792 CXXRecordDecl *D
4793 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
4794 if (D)
4795 Decls.push_back(D);
4796 }
4797 DynamicClasses.clear();
4798}
4799
Douglas Gregorec12ce22011-07-28 14:20:37 +00004800void
4801ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
4802 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4803 NamedDecl *D
4804 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4805 if (D)
4806 Decls.push_back(D);
4807 }
4808 LocallyScopedExternalDecls.clear();
4809}
4810
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00004811void ASTReader::ReadReferencedSelectors(
4812 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
4813 if (ReferencedSelectorsData.empty())
4814 return;
4815
4816 // If there are @selector references added them to its pool. This is for
4817 // implementation of -Wselector.
4818 unsigned int DataSize = ReferencedSelectorsData.size()-1;
4819 unsigned I = 0;
4820 while (I < DataSize) {
4821 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
4822 SourceLocation SelLoc
4823 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
4824 Sels.push_back(std::make_pair(Sel, SelLoc));
4825 }
4826 ReferencedSelectorsData.clear();
4827}
4828
Douglas Gregor31e37b22011-07-28 18:09:57 +00004829void ASTReader::ReadWeakUndeclaredIdentifiers(
4830 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
4831 if (WeakUndeclaredIdentifiers.empty())
4832 return;
4833
4834 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
4835 IdentifierInfo *WeakId
4836 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4837 IdentifierInfo *AliasId
4838 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4839 SourceLocation Loc
4840 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
4841 bool Used = WeakUndeclaredIdentifiers[I++];
4842 WeakInfo WI(AliasId, Loc);
4843 WI.setUsed(Used);
4844 WeakIDs.push_back(std::make_pair(WeakId, WI));
4845 }
4846 WeakUndeclaredIdentifiers.clear();
4847}
4848
Douglas Gregordfe65432011-07-28 19:11:31 +00004849void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
4850 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
4851 ExternalVTableUse VT;
4852 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4853 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
4854 VT.DefinitionRequired = VTableUses[Idx++];
4855 VTables.push_back(VT);
4856 }
4857
4858 VTableUses.clear();
4859}
4860
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00004861void ASTReader::ReadPendingInstantiations(
4862 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
4863 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
4864 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
4865 SourceLocation Loc
4866 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
4867 Pending.push_back(std::make_pair(D, Loc));
4868 }
4869 PendingInstantiations.clear();
4870}
4871
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004872void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004873 // It would be complicated to avoid reading the methods anyway. So don't.
4874 ReadMethodPool(Sel);
4875}
4876
Douglas Gregor95eab172011-07-28 20:55:49 +00004877void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00004878 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00004879 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004880 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004881 if (DeserializationListener)
4882 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00004883}
4884
Douglas Gregord89275b2009-07-06 18:54:52 +00004885/// \brief Set the globally-visible declarations associated with the given
4886/// identifier.
4887///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004888/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00004889/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00004890/// them.
4891///
4892/// \param II an IdentifierInfo that refers to one or more globally-visible
4893/// declarations.
4894///
4895/// \param DeclIDs the set of declaration IDs with the name @p II that are
4896/// visible at global scope.
4897///
4898/// \param Nonrecursive should be true to indicate that the caller knows that
4899/// this call is non-recursive, and therefore the globally-visible declarations
4900/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00004901void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004902ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004903 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregord89275b2009-07-06 18:54:52 +00004904 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004905 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004906 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4907 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4908 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004909 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00004910 return;
4911 }
Mike Stump1eb44332009-09-09 15:08:12 +00004912
Douglas Gregord89275b2009-07-06 18:54:52 +00004913 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4914 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4915 if (SemaObj) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00004916 // Introduce this declaration into the translation-unit scope
4917 // and add it to the declaration chain for this identifier, so
4918 // that (unqualified) name lookup will find it.
4919 SemaObj->pushExternalDeclIntoScope(D, II);
Douglas Gregord89275b2009-07-06 18:54:52 +00004920 } else {
4921 // Queue this declaration so that it will be added to the
4922 // translation unit scope and identifier's declaration chain
4923 // once a Sema object is known.
4924 PreloadedDecls.push_back(D);
4925 }
4926 }
4927}
4928
Douglas Gregor95eab172011-07-28 20:55:49 +00004929IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00004930 if (ID == 0)
4931 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00004932
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004933 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004934 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00004935 return 0;
4936 }
Mike Stump1eb44332009-09-09 15:08:12 +00004937
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004938 ID -= 1;
4939 if (!IdentifiersLoaded[ID]) {
Douglas Gregor67268d02011-07-20 00:59:32 +00004940 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
4941 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregor9827a802011-07-29 00:56:45 +00004942 Module *M = I->second;
4943 unsigned Index = ID - M->BaseIdentifierID;
4944 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregord6595a42009-04-25 21:04:17 +00004945
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004946 // All of the strings in the AST file are preceded by a 16-bit length.
4947 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00004948 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4949 // unsigned integers. This is important to avoid integer overflow when
4950 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00004951 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00004952 unsigned StrLen = (((unsigned) StrLenPtr[0])
4953 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004954 IdentifiersLoaded[ID]
Douglas Gregor712f2fc2011-09-09 22:02:16 +00004955 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004956 if (DeserializationListener)
4957 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00004958 }
Mike Stump1eb44332009-09-09 15:08:12 +00004959
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004960 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004961}
4962
Douglas Gregor95eab172011-07-28 20:55:49 +00004963IdentifierInfo *ASTReader::getLocalIdentifier(Module &M, unsigned LocalID) {
4964 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
4965}
4966
4967IdentifierID ASTReader::getGlobalIdentifierID(Module &M, unsigned LocalID) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +00004968 if (LocalID < NUM_PREDEF_IDENT_IDS)
4969 return LocalID;
4970
4971 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4972 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
4973 assert(I != M.IdentifierRemap.end()
4974 && "Invalid index into identifier index remap");
4975
4976 return LocalID + I->second;
Douglas Gregor95eab172011-07-28 20:55:49 +00004977}
4978
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004979bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregore23ac652011-04-20 00:21:03 +00004980 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00004981}
4982
Douglas Gregor2d2689a2011-07-28 21:16:51 +00004983Selector ASTReader::getLocalSelector(Module &M, unsigned LocalID) {
4984 return DecodeSelector(getGlobalSelectorID(M, LocalID));
4985}
4986
4987Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004988 if (ID == 0)
4989 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00004990
Sebastian Redl725cd962010-08-04 20:40:17 +00004991 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004992 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004993 return Selector();
4994 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004995
Sebastian Redl725cd962010-08-04 20:40:17 +00004996 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004997 // Load this selector from the selector table.
Douglas Gregor96958cb2011-07-20 01:10:58 +00004998 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
4999 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregor9827a802011-07-29 00:56:45 +00005000 Module &M = *I->second;
5001 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005002 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor96958cb2011-07-20 01:10:58 +00005003 SelectorsLoaded[ID - 1] =
Douglas Gregor9827a802011-07-29 00:56:45 +00005004 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor96958cb2011-07-20 01:10:58 +00005005 if (DeserializationListener)
5006 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor83941df2009-04-25 17:48:32 +00005007 }
5008
Sebastian Redl725cd962010-08-04 20:40:17 +00005009 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005010}
5011
Douglas Gregor8451ec72011-07-28 14:41:43 +00005012Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00005013 return DecodeSelector(ID);
5014}
5015
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005016uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00005017 // ID 0 (the null selector) is considered an external selector.
5018 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00005019}
5020
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005021serialization::SelectorID
5022ASTReader::getGlobalSelectorID(Module &M, unsigned LocalID) const {
5023 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
5024 return LocalID;
5025
5026 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5027 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
5028 assert(I != M.SelectorRemap.end()
5029 && "Invalid index into identifier index remap");
5030
5031 return LocalID + I->second;
Douglas Gregor8451ec72011-07-28 14:41:43 +00005032}
5033
Mike Stump1eb44332009-09-09 15:08:12 +00005034DeclarationName
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005035ASTReader::ReadDeclarationName(Module &F,
Douglas Gregor393f2492011-07-22 00:38:23 +00005036 const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00005037 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
5038 switch (Kind) {
5039 case DeclarationName::Identifier:
Douglas Gregor95eab172011-07-28 20:55:49 +00005040 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005041
5042 case DeclarationName::ObjCZeroArgSelector:
5043 case DeclarationName::ObjCOneArgSelector:
5044 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor2d2689a2011-07-28 21:16:51 +00005045 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005046
5047 case DeclarationName::CXXConstructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005048 return Context.DeclarationNames.getCXXConstructorName(
5049 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005050
5051 case DeclarationName::CXXDestructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005052 return Context.DeclarationNames.getCXXDestructorName(
5053 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005054
5055 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor35942772011-09-09 21:34:22 +00005056 return Context.DeclarationNames.getCXXConversionFunctionName(
5057 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005058
5059 case DeclarationName::CXXOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005060 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00005061 (OverloadedOperatorKind)Record[Idx++]);
5062
Sean Hunt3e518bd2009-11-29 07:34:05 +00005063 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005064 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregor95eab172011-07-28 20:55:49 +00005065 GetIdentifierInfo(F, Record, Idx));
Sean Hunt3e518bd2009-11-29 07:34:05 +00005066
Douglas Gregor2cf26342009-04-09 22:27:44 +00005067 case DeclarationName::CXXUsingDirective:
5068 return DeclarationName::getUsingDirectiveName();
5069 }
5070
5071 // Required to silence GCC warning
5072 return DeclarationName();
5073}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00005074
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005075void ASTReader::ReadDeclarationNameLoc(Module &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005076 DeclarationNameLoc &DNLoc,
5077 DeclarationName Name,
5078 const RecordData &Record, unsigned &Idx) {
5079 switch (Name.getNameKind()) {
5080 case DeclarationName::CXXConstructorName:
5081 case DeclarationName::CXXDestructorName:
5082 case DeclarationName::CXXConversionFunctionName:
5083 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
5084 break;
5085
5086 case DeclarationName::CXXOperatorName:
5087 DNLoc.CXXOperatorName.BeginOpNameLoc
5088 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5089 DNLoc.CXXOperatorName.EndOpNameLoc
5090 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5091 break;
5092
5093 case DeclarationName::CXXLiteralOperatorName:
5094 DNLoc.CXXLiteralOperatorName.OpNameLoc
5095 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5096 break;
5097
5098 case DeclarationName::Identifier:
5099 case DeclarationName::ObjCZeroArgSelector:
5100 case DeclarationName::ObjCOneArgSelector:
5101 case DeclarationName::ObjCMultiArgSelector:
5102 case DeclarationName::CXXUsingDirective:
5103 break;
5104 }
5105}
5106
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005107void ASTReader::ReadDeclarationNameInfo(Module &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005108 DeclarationNameInfo &NameInfo,
5109 const RecordData &Record, unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00005110 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005111 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
5112 DeclarationNameLoc DNLoc;
5113 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
5114 NameInfo.setInfo(DNLoc);
5115}
5116
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005117void ASTReader::ReadQualifierInfo(Module &F, QualifierInfo &Info,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005118 const RecordData &Record, unsigned &Idx) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005119 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005120 unsigned NumTPLists = Record[Idx++];
5121 Info.NumTemplParamLists = NumTPLists;
5122 if (NumTPLists) {
Douglas Gregor35942772011-09-09 21:34:22 +00005123 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005124 for (unsigned i=0; i != NumTPLists; ++i)
5125 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
5126 }
5127}
5128
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005129TemplateName
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005130ASTReader::ReadTemplateName(Module &F, const RecordData &Record,
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005131 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00005132 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005133 switch (Kind) {
5134 case TemplateName::Template:
Douglas Gregor409448c2011-07-21 22:35:25 +00005135 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005136
5137 case TemplateName::OverloadedTemplate: {
5138 unsigned size = Record[Idx++];
5139 UnresolvedSet<8> Decls;
5140 while (size--)
Douglas Gregor409448c2011-07-21 22:35:25 +00005141 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005142
Douglas Gregor35942772011-09-09 21:34:22 +00005143 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005144 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005145
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005146 case TemplateName::QualifiedTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005147 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005148 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00005149 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005150 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005151 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005152
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005153 case TemplateName::DependentTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005154 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005155 if (Record[Idx++]) // isIdentifier
Douglas Gregor35942772011-09-09 21:34:22 +00005156 return Context.getDependentTemplateName(NNS,
Douglas Gregor95eab172011-07-28 20:55:49 +00005157 GetIdentifierInfo(F, Record,
5158 Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00005159 return Context.getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00005160 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005161 }
John McCall14606042011-06-30 08:33:18 +00005162
5163 case TemplateName::SubstTemplateTemplateParm: {
5164 TemplateTemplateParmDecl *param
Douglas Gregor409448c2011-07-21 22:35:25 +00005165 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCall14606042011-06-30 08:33:18 +00005166 if (!param) return TemplateName();
5167 TemplateName replacement = ReadTemplateName(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005168 return Context.getSubstTemplateTemplateParm(param, replacement);
John McCall14606042011-06-30 08:33:18 +00005169 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005170
5171 case TemplateName::SubstTemplateTemplateParmPack: {
5172 TemplateTemplateParmDecl *Param
Douglas Gregor409448c2011-07-21 22:35:25 +00005173 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005174 if (!Param)
5175 return TemplateName();
5176
5177 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
5178 if (ArgPack.getKind() != TemplateArgument::Pack)
5179 return TemplateName();
5180
Douglas Gregor35942772011-09-09 21:34:22 +00005181 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005182 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005183 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005184
David Blaikieb219cfc2011-09-23 05:06:16 +00005185 llvm_unreachable("Unhandled template name kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005186}
5187
5188TemplateArgument
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005189ASTReader::ReadTemplateArgument(Module &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00005190 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00005191 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
5192 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005193 case TemplateArgument::Null:
5194 return TemplateArgument();
5195 case TemplateArgument::Type:
Douglas Gregor393f2492011-07-22 00:38:23 +00005196 return TemplateArgument(readType(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005197 case TemplateArgument::Declaration:
Douglas Gregor409448c2011-07-21 22:35:25 +00005198 return TemplateArgument(ReadDecl(F, Record, Idx));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00005199 case TemplateArgument::Integral: {
5200 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00005201 QualType T = readType(F, Record, Idx);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00005202 return TemplateArgument(Value, T);
5203 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00005204 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005205 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00005206 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005207 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00005208 llvm::Optional<unsigned> NumTemplateExpansions;
5209 if (unsigned NumExpansions = Record[Idx++])
5210 NumTemplateExpansions = NumExpansions - 1;
5211 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00005212 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005213 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00005214 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005215 case TemplateArgument::Pack: {
5216 unsigned NumArgs = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00005217 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
Douglas Gregor910f8002010-11-07 23:05:16 +00005218 for (unsigned I = 0; I != NumArgs; ++I)
5219 Args[I] = ReadTemplateArgument(F, Record, Idx);
5220 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005221 }
5222 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005223
David Blaikieb219cfc2011-09-23 05:06:16 +00005224 llvm_unreachable("Unhandled template argument kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005225}
5226
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005227TemplateParameterList *
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005228ASTReader::ReadTemplateParameterList(Module &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00005229 const RecordData &Record, unsigned &Idx) {
5230 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
5231 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
5232 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005233
5234 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00005235 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005236 Params.reserve(NumParams);
5237 while (NumParams--)
Douglas Gregor409448c2011-07-21 22:35:25 +00005238 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer20249a12010-10-21 03:16:25 +00005239
5240 TemplateParameterList* TemplateParams =
Douglas Gregor35942772011-09-09 21:34:22 +00005241 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005242 Params.data(), Params.size(), RAngleLoc);
5243 return TemplateParams;
5244}
5245
5246void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005247ASTReader::
Chris Lattner5f9e2722011-07-23 10:55:15 +00005248ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005249 Module &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00005250 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005251 unsigned NumTemplateArgs = Record[Idx++];
5252 TemplArgs.reserve(NumTemplateArgs);
5253 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00005254 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005255}
5256
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005257/// \brief Read a UnresolvedSet structure.
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005258void ASTReader::ReadUnresolvedSet(Module &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005259 const RecordData &Record, unsigned &Idx) {
5260 unsigned NumDecls = Record[Idx++];
5261 while (NumDecls--) {
Douglas Gregor409448c2011-07-21 22:35:25 +00005262 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005263 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
5264 Set.addDecl(D, AS);
5265 }
5266}
5267
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005268CXXBaseSpecifier
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005269ASTReader::ReadCXXBaseSpecifier(Module &F,
Nick Lewycky56062202010-07-26 16:56:01 +00005270 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005271 bool isVirtual = static_cast<bool>(Record[Idx++]);
5272 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
5273 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00005274 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00005275 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
5276 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00005277 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00005278 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00005279 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00005280 Result.setInheritConstructors(inheritConstructors);
5281 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005282}
5283
Sean Huntcbb67482011-01-08 20:30:50 +00005284std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005285ASTReader::ReadCXXCtorInitializers(Module &F, const RecordData &Record,
Sean Huntcbb67482011-01-08 20:30:50 +00005286 unsigned &Idx) {
5287 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005288 unsigned NumInitializers = Record[Idx++];
5289 if (NumInitializers) {
Sean Huntcbb67482011-01-08 20:30:50 +00005290 CtorInitializers
Douglas Gregor35942772011-09-09 21:34:22 +00005291 = new (Context) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005292 for (unsigned i=0; i != NumInitializers; ++i) {
Douglas Gregor76852c22011-11-01 01:16:03 +00005293 TypeSourceInfo *TInfo = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005294 bool IsBaseVirtual = false;
5295 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00005296 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00005297
Sean Hunt156b6402011-05-04 01:19:08 +00005298 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
5299 switch (Type) {
Douglas Gregor76852c22011-11-01 01:16:03 +00005300 case CTOR_INITIALIZER_BASE:
5301 TInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005302 IsBaseVirtual = Record[Idx++];
Sean Hunt156b6402011-05-04 01:19:08 +00005303 break;
Douglas Gregor76852c22011-11-01 01:16:03 +00005304
5305 case CTOR_INITIALIZER_DELEGATING:
5306 TInfo = GetTypeSourceInfo(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00005307 break;
5308
5309 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00005310 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00005311 break;
5312
5313 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00005314 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00005315 break;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005316 }
Sean Hunt156b6402011-05-04 01:19:08 +00005317
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00005318 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00005319 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00005320 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
5321 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005322 bool IsWritten = Record[Idx++];
5323 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner5f9e2722011-07-23 10:55:15 +00005324 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005325 if (IsWritten) {
5326 SourceOrderOrNumArrayIndices = Record[Idx++];
5327 } else {
5328 SourceOrderOrNumArrayIndices = Record[Idx++];
5329 Indices.reserve(SourceOrderOrNumArrayIndices);
5330 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor409448c2011-07-21 22:35:25 +00005331 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005332 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005333
Sean Huntcbb67482011-01-08 20:30:50 +00005334 CXXCtorInitializer *BOMInit;
Sean Hunt156b6402011-05-04 01:19:08 +00005335 if (Type == CTOR_INITIALIZER_BASE) {
Douglas Gregor76852c22011-11-01 01:16:03 +00005336 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
Sean Huntcbb67482011-01-08 20:30:50 +00005337 LParenLoc, Init, RParenLoc,
5338 MemberOrEllipsisLoc);
Sean Hunt156b6402011-05-04 01:19:08 +00005339 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
Douglas Gregor76852c22011-11-01 01:16:03 +00005340 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
5341 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005342 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00005343 if (Member)
Douglas Gregor35942772011-09-09 21:34:22 +00005344 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00005345 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00005346 else
Douglas Gregor35942772011-09-09 21:34:22 +00005347 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
Sean Huntcbb67482011-01-08 20:30:50 +00005348 MemberOrEllipsisLoc, LParenLoc,
5349 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005350 } else {
Douglas Gregor35942772011-09-09 21:34:22 +00005351 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00005352 LParenLoc, Init, RParenLoc,
5353 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005354 }
5355
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00005356 if (IsWritten)
5357 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00005358 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005359 }
5360 }
5361
Sean Huntcbb67482011-01-08 20:30:50 +00005362 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005363}
5364
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005365NestedNameSpecifier *
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005366ASTReader::ReadNestedNameSpecifier(Module &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00005367 const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005368 unsigned N = Record[Idx++];
5369 NestedNameSpecifier *NNS = 0, *Prev = 0;
5370 for (unsigned I = 0; I != N; ++I) {
5371 NestedNameSpecifier::SpecifierKind Kind
5372 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5373 switch (Kind) {
5374 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00005375 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005376 NNS = NestedNameSpecifier::Create(Context, Prev, II);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005377 break;
5378 }
5379
5380 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005381 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005382 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005383 break;
5384 }
5385
Douglas Gregor14aba762011-02-24 02:36:08 +00005386 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005387 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005388 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
Douglas Gregor14aba762011-02-24 02:36:08 +00005389 break;
5390 }
5391
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005392 case NestedNameSpecifier::TypeSpec:
5393 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor393f2492011-07-22 00:38:23 +00005394 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00005395 if (!T)
5396 return 0;
5397
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005398 bool Template = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00005399 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005400 break;
5401 }
5402
5403 case NestedNameSpecifier::Global: {
Douglas Gregor35942772011-09-09 21:34:22 +00005404 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005405 // No associated value, and there can't be a prefix.
5406 break;
5407 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005408 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00005409 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005410 }
5411 return NNS;
5412}
5413
Douglas Gregordc355712011-02-25 00:36:19 +00005414NestedNameSpecifierLoc
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005415ASTReader::ReadNestedNameSpecifierLoc(Module &F, const RecordData &Record,
Douglas Gregordc355712011-02-25 00:36:19 +00005416 unsigned &Idx) {
5417 unsigned N = Record[Idx++];
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005418 NestedNameSpecifierLocBuilder Builder;
Douglas Gregordc355712011-02-25 00:36:19 +00005419 for (unsigned I = 0; I != N; ++I) {
5420 NestedNameSpecifier::SpecifierKind Kind
5421 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5422 switch (Kind) {
5423 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00005424 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00005425 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005426 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00005427 break;
5428 }
5429
5430 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005431 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00005432 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005433 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00005434 break;
5435 }
5436
5437 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005438 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00005439 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005440 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00005441 break;
5442 }
5443
5444 case NestedNameSpecifier::TypeSpec:
5445 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregordc355712011-02-25 00:36:19 +00005446 bool Template = Record[Idx++];
5447 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
5448 if (!T)
5449 return NestedNameSpecifierLoc();
Douglas Gregordc355712011-02-25 00:36:19 +00005450 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005451
5452 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
Douglas Gregor35942772011-09-09 21:34:22 +00005453 Builder.Extend(Context,
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005454 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
5455 T->getTypeLoc(), ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00005456 break;
5457 }
5458
5459 case NestedNameSpecifier::Global: {
Douglas Gregordc355712011-02-25 00:36:19 +00005460 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005461 Builder.MakeGlobal(Context, ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00005462 break;
5463 }
5464 }
Douglas Gregordc355712011-02-25 00:36:19 +00005465 }
5466
Douglas Gregor35942772011-09-09 21:34:22 +00005467 return Builder.getWithLocInContext(Context);
Douglas Gregordc355712011-02-25 00:36:19 +00005468}
5469
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005470SourceRange
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005471ASTReader::ReadSourceRange(Module &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00005472 unsigned &Idx) {
5473 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
5474 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00005475 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005476}
5477
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00005478/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005479llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00005480 unsigned BitWidth = Record[Idx++];
5481 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
5482 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
5483 Idx += NumWords;
5484 return Result;
5485}
5486
5487/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005488llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00005489 bool isUnsigned = Record[Idx++];
5490 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
5491}
5492
Douglas Gregor17fc2232009-04-14 21:55:33 +00005493/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005494llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00005495 return llvm::APFloat(ReadAPInt(Record, Idx));
5496}
5497
Douglas Gregor68a2eb02009-04-15 21:30:51 +00005498// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005499std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00005500 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00005501 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00005502 Idx += Len;
5503 return Result;
5504}
5505
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005506VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
5507 unsigned &Idx) {
5508 unsigned Major = Record[Idx++];
5509 unsigned Minor = Record[Idx++];
5510 unsigned Subminor = Record[Idx++];
5511 if (Minor == 0)
5512 return VersionTuple(Major);
5513 if (Subminor == 0)
5514 return VersionTuple(Major, Minor - 1);
5515 return VersionTuple(Major, Minor - 1, Subminor - 1);
5516}
5517
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005518CXXTemporary *ASTReader::ReadCXXTemporary(Module &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00005519 const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00005520 unsigned &Idx) {
Douglas Gregor409448c2011-07-21 22:35:25 +00005521 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005522 return CXXTemporary::Create(Context, Decl);
Chris Lattnerd2598362010-05-10 00:25:06 +00005523}
5524
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005525DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00005526 return Diag(SourceLocation(), DiagID);
5527}
5528
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005529DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00005530 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00005531}
Douglas Gregor025452f2009-04-17 00:04:06 +00005532
Douglas Gregor668c1a42009-04-21 22:25:48 +00005533/// \brief Retrieve the identifier table associated with the
5534/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005535IdentifierTable &ASTReader::getIdentifierTable() {
Douglas Gregor712f2fc2011-09-09 22:02:16 +00005536 return PP.getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00005537}
5538
Douglas Gregor025452f2009-04-17 00:04:06 +00005539/// \brief Record that the given ID maps to the given switch-case
5540/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005541void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00005542 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
5543 SwitchCaseStmts[ID] = SC;
5544}
5545
5546/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005547SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00005548 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
5549 return SwitchCaseStmts[ID];
5550}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00005551
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00005552void ASTReader::ClearSwitchCaseIDs() {
5553 SwitchCaseStmts.clear();
5554}
5555
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005556void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005557 assert(NumCurrentElementsDeserializing &&
5558 "FinishedDeserializing not paired with StartedDeserializing");
5559 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregord89275b2009-07-06 18:54:52 +00005560 // If any identifiers with corresponding top-level declarations have
5561 // been loaded, load those declarations now.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005562 while (!PendingIdentifierInfos.empty()) {
5563 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
5564 PendingIdentifierInfos.front().DeclIDs, true);
5565 PendingIdentifierInfos.pop_front();
Douglas Gregord89275b2009-07-06 18:54:52 +00005566 }
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005567
Argyrios Kyrtzidis0895d152011-02-12 07:50:47 +00005568 // Ready to load previous declarations of Decls that were delayed.
5569 while (!PendingPreviousDecls.empty()) {
5570 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
5571 PendingPreviousDecls.front().second);
5572 PendingPreviousDecls.pop_front();
5573 }
5574
Argyrios Kyrtzidis5a174992011-11-14 07:07:59 +00005575 for (std::vector<std::pair<ObjCInterfaceDecl *,
5576 serialization::DeclID> >::iterator
5577 I = PendingChainedObjCCategories.begin(),
5578 E = PendingChainedObjCCategories.end(); I != E; ++I) {
5579 loadObjCChainedCategories(I->second, I->first);
5580 }
5581 PendingChainedObjCCategories.clear();
5582
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005583 // We are not in recursive loading, so it's safe to pass the "interesting"
5584 // decls to the consumer.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005585 if (Consumer)
5586 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidis134db1f2010-10-24 17:26:31 +00005587
5588 assert(PendingForwardRefs.size() == 0 &&
5589 "Some forward refs did not get linked to the definition!");
Douglas Gregord89275b2009-07-06 18:54:52 +00005590 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005591 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00005592}
Douglas Gregor501c1032010-08-19 00:28:17 +00005593
Douglas Gregorf8a1e512011-09-02 00:26:20 +00005594ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
Douglas Gregor832d6202011-07-22 16:35:34 +00005595 StringRef isysroot, bool DisableValidation,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005596 bool DisableStatCache)
Sebastian Redle1dde812010-08-24 00:50:04 +00005597 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
5598 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00005599 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
Jonathan D. Turner1afb6612011-07-28 17:20:23 +00005600 Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
5601 RelocatablePCH(false), isysroot(isysroot),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005602 DisableValidation(DisableValidation),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005603 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005604 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005605 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
5606 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
5607 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
5608 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00005609 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
5610 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
5611 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005612{
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005613 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redle1dde812010-08-24 00:50:04 +00005614}
5615
Sebastian Redle1dde812010-08-24 00:50:04 +00005616ASTReader::~ASTReader() {
Sebastian Redle1dde812010-08-24 00:50:04 +00005617 for (DeclContextVisibleUpdatesPending::iterator
5618 I = PendingVisibleUpdates.begin(),
5619 E = PendingVisibleUpdates.end();
5620 I != E; ++I) {
5621 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5622 F = I->second.end();
5623 J != F; ++J)
Douglas Gregor496c7092011-08-03 15:48:04 +00005624 delete static_cast<ASTDeclContextNameLookupTable*>(J->first);
Sebastian Redle1dde812010-08-24 00:50:04 +00005625 }
5626}