blob: 258baebc05cfbf8bc0ed10135aff12b119cf6775 [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 Gregor1a4761e2011-11-30 23:21:26 +0000734bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000735 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 Gregor1a4761e2011-11-30 23:21:26 +0000808bool ASTReader::ParseLineTable(ModuleFile &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 Gregor1a4761e2011-11-30 23:21:26 +0000952ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(ModuleFile &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 Gregor1a4761e2011-11-30 23:21:26 +00001061 ModuleFile *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 Gregora4581a12011-11-17 19:08:51 +00001094 const FileEntry *File =
1095 OverriddenBuffer? FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1096 (time_t)Record[5])
1097 : FileMgr.getFile(Filename, /*OpenFile=*/false);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001098 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1099 OriginalDir != CurrentDir) {
1100 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1101 OriginalDir,
1102 CurrentDir);
1103 if (!resolved.empty())
1104 File = FileMgr.getFile(resolved);
1105 }
Axel Naumann04331162011-01-27 10:55:51 +00001106 if (File == 0)
1107 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1108 (time_t)Record[5]);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001109 if (File == 0) {
1110 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001111 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001112 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001113 Error(ErrorStr.c_str());
1114 return Failure;
1115 }
Mike Stump1eb44332009-09-09 15:08:12 +00001116
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001117 if (!DisableValidation &&
1118 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001119#if !defined(LLVM_ON_WIN32)
1120 // In our regression testing, the Windows file system seems to
1121 // have inconsistent modification times that sometimes
1122 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001123 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001124#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001125 )) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +00001126 Error(diag::err_fe_pch_file_modified, Filename);
Douglas Gregor2d52be52010-03-21 22:49:54 +00001127 return Failure;
1128 }
1129
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001130 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001131 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001132 // This is the module's main file.
1133 IncludeLoc = getImportLocation(F);
1134 }
1135 FileID FID = SourceMgr.createFileID(File, IncludeLoc,
Chris Lattner75dfb652010-11-23 09:19:42 +00001136 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001137 ID, BaseOffset + Record[0]);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001138 SrcMgr::FileInfo &FileInfo =
1139 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
Douglas Gregora081da52011-11-16 20:05:18 +00001140 FileInfo.NumCreatedFIDs = Record[7];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001141 if (Record[3])
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001142 FileInfo.setHasLineDirectives();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001143
Douglas Gregora081da52011-11-16 20:05:18 +00001144 const DeclID *FirstDecl = F->FileSortedDecls + Record[8];
1145 unsigned NumFileDecls = Record[9];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001146 if (NumFileDecls) {
1147 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
Argyrios Kyrtzidis9d128d02011-10-31 07:20:08 +00001148 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1149 NumFileDecls));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001150 }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001151
Douglas Gregor35f9ae62011-11-17 01:44:33 +00001152 const SrcMgr::ContentCache *ContentCache
1153 = SourceMgr.getOrCreateContentCache(File);
1154 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1155 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
Douglas Gregora081da52011-11-16 20:05:18 +00001156 unsigned Code = SLocEntryCursor.ReadCode();
1157 Record.clear();
1158 unsigned RecCode
1159 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1160
1161 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1162 Error("AST record has invalid code");
1163 return Failure;
1164 }
1165
1166 llvm::MemoryBuffer *Buffer
1167 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
1168 Filename);
1169 SourceMgr.overrideFileContents(File, Buffer);
1170 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001171 break;
1172 }
1173
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001174 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001175 const char *Name = BlobStart;
1176 unsigned Offset = Record[0];
1177 unsigned Code = SLocEntryCursor.ReadCode();
1178 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001179 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001180 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001181
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001182 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001183 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001184 return Failure;
1185 }
1186
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001187 llvm::MemoryBuffer *Buffer
Douglas Gregora081da52011-11-16 20:05:18 +00001188 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
1189 Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001190 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID,
1191 BaseOffset + Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001192
Douglas Gregor92b059e2009-04-28 20:33:11 +00001193 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001194 PCHPredefinesBlock Block = {
1195 BufferID,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001196 StringRef(BlobStart, BlobLen - 1)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001197 };
1198 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001199 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001200
1201 break;
1202 }
1203
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001204 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001205 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruthbf340e42011-07-26 03:03:05 +00001206 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001207 ReadSourceLocation(*F, Record[2]),
1208 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001209 Record[4],
1210 ID,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001211 BaseOffset + Record[0]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001212 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001213 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001214 }
1215
1216 return Success;
1217}
1218
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001219/// \brief Find the location where the module F is imported.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001220SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001221 if (F->ImportLoc.isValid())
1222 return F->ImportLoc;
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001223
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001224 // Otherwise we have a PCH. It's considered to be "imported" at the first
1225 // location of its includer.
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001226 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001227 // Main file is the importer. We assume that it is the first entry in the
1228 // entry table. We can't ask the manager, because at the time of PCH loading
1229 // the main file entry doesn't exist yet.
1230 // The very first entry is the invalid instantiation loc, which takes up
1231 // offsets 0 and 1.
1232 return SourceLocation::getFromRawEncoding(2U);
1233 }
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001234 //return F->Loaders[0]->FirstLoc;
1235 return F->ImportedBy[0]->FirstLoc;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001236}
1237
Chris Lattner6367f6d2009-04-27 01:05:14 +00001238/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1239/// specified cursor. Read the abbreviations that are at the top of the block
1240/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001241bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001242 unsigned BlockID) {
1243 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001244 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001245 return Failure;
1246 }
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Chris Lattner6367f6d2009-04-27 01:05:14 +00001248 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001249 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001250 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001251
Chris Lattner6367f6d2009-04-27 01:05:14 +00001252 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001253 if (Code != llvm::bitc::DEFINE_ABBREV) {
1254 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001255 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001256 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001257 Cursor.ReadAbbrevRecord();
1258 }
1259}
1260
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001261void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001262 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001263
Douglas Gregor37e26842009-04-21 23:56:24 +00001264 // Keep track of where we are in the stream, then jump back there
1265 // after reading this macro.
1266 SavedStreamPosition SavedPosition(Stream);
1267
1268 Stream.JumpToBit(Offset);
1269 RecordData Record;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001270 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001271 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Douglas Gregor37e26842009-04-21 23:56:24 +00001273 while (true) {
1274 unsigned Code = Stream.ReadCode();
1275 switch (Code) {
1276 case llvm::bitc::END_BLOCK:
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001277 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001278
1279 case llvm::bitc::ENTER_SUBBLOCK:
1280 // No known subblocks, always skip them.
1281 Stream.ReadSubBlockID();
1282 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001283 Error("malformed block record in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001284 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001285 }
1286 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001287
Douglas Gregor37e26842009-04-21 23:56:24 +00001288 case llvm::bitc::DEFINE_ABBREV:
1289 Stream.ReadAbbrevRecord();
1290 continue;
1291 default: break;
1292 }
1293
1294 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001295 const char *BlobStart = 0;
1296 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001297 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001298 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001299 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001300 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001301 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001302 case PP_MACRO_OBJECT_LIKE:
1303 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001304 // If we already have a macro, that means that we've hit the end
1305 // of the definition of the macro we were looking for. We're
1306 // done.
1307 if (Macro)
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001308 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001309
Douglas Gregor95eab172011-07-28 20:55:49 +00001310 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001311 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001312 Error("macro must have a name in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001313 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001314 }
Douglas Gregoreee242f2011-10-27 09:33:13 +00001315
Sebastian Redlc3632732010-10-05 15:59:54 +00001316 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001317 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001319 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001320 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001321 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001322
Douglas Gregoraa93a872011-10-17 15:32:29 +00001323 bool IsPublic = Record[3];
1324 unsigned NextIndex = 4;
1325 MI->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex));
Douglas Gregor7143aab2011-09-01 17:04:32 +00001326
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001327 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001328 // Decode function-like macro info.
Douglas Gregor7143aab2011-09-01 17:04:32 +00001329 bool isC99VarArgs = Record[NextIndex++];
1330 bool isGNUVarArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001331 MacroArgs.clear();
Douglas Gregor7143aab2011-09-01 17:04:32 +00001332 unsigned NumArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001333 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregor7143aab2011-09-01 17:04:32 +00001334 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001335
1336 // Install function-like macro info.
1337 MI->setIsFunctionLike();
1338 if (isC99VarArgs) MI->setIsC99Varargs();
1339 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001340 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001341 PP.getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001342 }
1343
1344 // Finally, install the macro.
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001345 PP.setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001346
1347 // Remember that we saw this macro last so that we add the tokens that
1348 // form its body to it.
1349 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001350
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001351 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1352 Record[NextIndex]) {
1353 // We have a macro definition. Register the association
1354 PreprocessedEntityID
1355 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1356 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1357 PPRec.RegisterMacroDefinition(Macro,
1358 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001359 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001360
Douglas Gregor37e26842009-04-21 23:56:24 +00001361 ++NumMacrosRead;
1362 break;
1363 }
Mike Stump1eb44332009-09-09 15:08:12 +00001364
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001365 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001366 // If we see a TOKEN before a PP_MACRO_*, then the file is
1367 // erroneous, just pretend we didn't see this.
1368 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001369
Douglas Gregor37e26842009-04-21 23:56:24 +00001370 Token Tok;
1371 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001372 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001373 Tok.setLength(Record[1]);
Douglas Gregor95eab172011-07-28 20:55:49 +00001374 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregor37e26842009-04-21 23:56:24 +00001375 Tok.setIdentifierInfo(II);
1376 Tok.setKind((tok::TokenKind)Record[3]);
1377 Tok.setFlag((Token::TokenFlags)Record[4]);
1378 Macro->AddTokenToBody(Tok);
1379 break;
1380 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001381 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001382 }
Douglas Gregor89d99802010-11-30 06:16:57 +00001383
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001384 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001385}
1386
Douglas Gregor86c67d82011-07-28 22:39:26 +00001387PreprocessedEntityID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001388ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
Argyrios Kyrtzidis1f6d2252011-09-19 20:40:02 +00001389 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001390 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1391 assert(I != M.PreprocessedEntityRemap.end()
1392 && "Invalid index into preprocessed entity index remap");
1393
1394 return LocalID + I->second;
Douglas Gregor86c67d82011-07-28 22:39:26 +00001395}
1396
Douglas Gregor98339b92011-08-25 20:47:51 +00001397unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1398 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001399}
Douglas Gregor98339b92011-08-25 20:47:51 +00001400
1401HeaderFileInfoTrait::internal_key_type
1402HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1403
1404bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1405 if (strcmp(a, b) == 0)
1406 return true;
1407
1408 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1409 return false;
1410
1411 // The file names match, but the path names don't. stat() the files to
1412 // see if they are the same.
1413 struct stat StatBufA, StatBufB;
1414 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1415 return false;
1416
1417 return StatBufA.st_ino == StatBufB.st_ino;
1418}
1419
1420std::pair<unsigned, unsigned>
1421HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1422 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1423 unsigned DataLen = (unsigned) *d++;
1424 return std::make_pair(KeyLen + 1, DataLen);
1425}
1426
1427HeaderFileInfoTrait::data_type
1428HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1429 unsigned DataLen) {
1430 const unsigned char *End = d + DataLen;
1431 using namespace clang::io;
1432 HeaderFileInfo HFI;
1433 unsigned Flags = *d++;
1434 HFI.isImport = (Flags >> 5) & 0x01;
1435 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1436 HFI.DirInfo = (Flags >> 2) & 0x03;
1437 HFI.Resolved = (Flags >> 1) & 0x01;
1438 HFI.IndexHeaderMapHeader = Flags & 0x01;
1439 HFI.NumIncludes = ReadUnalignedLE16(d);
Douglas Gregor541ba162011-10-17 18:53:12 +00001440 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1441 ReadUnalignedLE32(d));
Douglas Gregor98339b92011-08-25 20:47:51 +00001442 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1443 // The framework offset is 1 greater than the actual offset,
1444 // since 0 is used as an indicator for "no framework name".
1445 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1446 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1447 }
1448
1449 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1450 (void)End;
1451
1452 // This HeaderFileInfo was externally loaded.
1453 HFI.External = true;
1454 return HFI;
1455}
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001456
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001457void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, ModuleFile &F,
Douglas Gregor2d2689a2011-07-28 21:16:51 +00001458 uint64_t LocalOffset) {
Douglas Gregor295a2a62010-10-30 00:23:06 +00001459 // Note that this identifier has a macro definition.
1460 II->setHasMacroDefinition(true);
1461
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001462 // Adjust the offset to a global offset.
Douglas Gregor2d2689a2011-07-28 21:16:51 +00001463 UnreadMacroRecordOffsets[II] = F.GlobalBitOffset + LocalOffset;
Douglas Gregor295a2a62010-10-30 00:23:06 +00001464}
1465
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001466void ASTReader::ReadDefinedMacros() {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001467 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1468 E = ModuleMgr.rend(); I != E; ++I) {
1469 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001470
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001471 // If there was no preprocessor block, skip this file.
1472 if (!MacroCursor.getBitStreamReader())
1473 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001474
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001475 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001476 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001477
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001478 RecordData Record;
1479 while (true) {
1480 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001481 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001482 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001483
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001484 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1485 // No known subblocks, always skip them.
1486 Cursor.ReadSubBlockID();
1487 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001488 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001489 return;
1490 }
1491 continue;
1492 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001493
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001494 if (Code == llvm::bitc::DEFINE_ABBREV) {
1495 Cursor.ReadAbbrevRecord();
1496 continue;
1497 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001498
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001499 // Read a record.
1500 const char *BlobStart;
1501 unsigned BlobLen;
1502 Record.clear();
1503 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1504 default: // Default behavior: ignore.
1505 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001506
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001507 case PP_MACRO_OBJECT_LIKE:
1508 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregor95eab172011-07-28 20:55:49 +00001509 getLocalIdentifier(**I, Record[0]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001510 break;
1511
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001512 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001513 // Ignore tokens.
1514 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001515 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001516 }
1517 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001518
1519 // Drain the unread macro-record offsets map.
1520 while (!UnreadMacroRecordOffsets.empty())
1521 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1522}
1523
1524void ASTReader::LoadMacroDefinition(
1525 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1526 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
Douglas Gregor295a2a62010-10-30 00:23:06 +00001527 uint64_t Offset = Pos->second;
1528 UnreadMacroRecordOffsets.erase(Pos);
1529
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001530 RecordLocation Loc = getLocalBitOffset(Offset);
1531 ReadMacroRecord(*Loc.F, Loc.Offset);
Douglas Gregor295a2a62010-10-30 00:23:06 +00001532}
1533
1534void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1535 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1536 = UnreadMacroRecordOffsets.find(II);
1537 LoadMacroDefinition(Pos);
Douglas Gregor88a35862010-01-04 19:18:44 +00001538}
1539
Douglas Gregoreee242f2011-10-27 09:33:13 +00001540namespace {
1541 /// \brief Visitor class used to look up identifirs in an AST file.
1542 class IdentifierLookupVisitor {
1543 StringRef Name;
1544 IdentifierInfo *Found;
1545 public:
1546 explicit IdentifierLookupVisitor(StringRef Name) : Name(Name), Found() { }
1547
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001548 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00001549 IdentifierLookupVisitor *This
1550 = static_cast<IdentifierLookupVisitor *>(UserData);
1551
1552 ASTIdentifierLookupTable *IdTable
1553 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1554 if (!IdTable)
1555 return false;
1556
1557 std::pair<const char*, unsigned> Key(This->Name.begin(),
1558 This->Name.size());
1559 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
1560 if (Pos == IdTable->end())
1561 return false;
1562
1563 // Dereferencing the iterator has the effect of building the
1564 // IdentifierInfo node and populating it with the various
1565 // declarations it needs.
1566 This->Found = *Pos;
1567 return true;
1568 }
1569
1570 // \brief Retrieve the identifier info found within the module
1571 // files.
1572 IdentifierInfo *getIdentifierInfo() const { return Found; }
1573 };
1574}
1575
1576void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1577 get(II.getName());
1578}
1579
Chris Lattner5f9e2722011-07-23 10:55:15 +00001580const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001581 std::string Filename = filenameStrRef;
1582 MaybeAddSystemRootToFilename(Filename);
1583 const FileEntry *File = FileMgr.getFile(Filename);
1584 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1585 OriginalDir != CurrentDir) {
1586 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1587 OriginalDir,
1588 CurrentDir);
1589 if (!resolved.empty())
1590 File = FileMgr.getFile(resolved);
1591 }
1592
1593 return File;
1594}
1595
Douglas Gregore650c8c2009-07-07 00:12:59 +00001596/// \brief If we are loading a relocatable PCH file, and the filename is
1597/// not an absolute path, add the system root to the beginning of the file
1598/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001599void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001600 // If this is not a relocatable PCH file, there's nothing to do.
1601 if (!RelocatablePCH)
1602 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Michael J. Spencer256053b2010-12-17 21:22:22 +00001604 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregore650c8c2009-07-07 00:12:59 +00001605 return;
1606
Douglas Gregor832d6202011-07-22 16:35:34 +00001607 if (isysroot.empty()) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001608 // If no system root was given, default to '/'
1609 Filename.insert(Filename.begin(), '/');
1610 return;
1611 }
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Douglas Gregor832d6202011-07-22 16:35:34 +00001613 unsigned Length = isysroot.size();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001614 if (isysroot[Length - 1] != '/')
1615 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001616
Douglas Gregor832d6202011-07-22 16:35:34 +00001617 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregore650c8c2009-07-07 00:12:59 +00001618}
1619
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001620ASTReader::ASTReadResult
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001621ASTReader::ReadASTBlock(ModuleFile &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001622 llvm::BitstreamCursor &Stream = F.Stream;
1623
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001624 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001625 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001626 return Failure;
1627 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001628
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001629 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001630 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001631 while (!Stream.AtEndOfStream()) {
1632 unsigned Code = Stream.ReadCode();
1633 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001634 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001635 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001636 return Failure;
1637 }
Chris Lattner7356a312009-04-11 21:15:38 +00001638
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001639 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001640 }
1641
1642 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1643 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001644 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001645 // We lazily load the decls block, but we want to set up the
1646 // DeclsCursor cursor to point into it. Clone our current bitcode
1647 // cursor to it, enter the block and read the abbrevs in that block.
1648 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001649 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001650 if (Stream.SkipBlock() || // Skip with the main cursor.
1651 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001652 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001653 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001654 return Failure;
1655 }
1656 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001657
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001658 case DECL_UPDATES_BLOCK_ID:
1659 if (Stream.SkipBlock()) {
1660 Error("malformed block record in AST file");
1661 return Failure;
1662 }
1663 break;
1664
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001665 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001666 F.MacroCursor = Stream;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001667 if (!PP.getExternalSource())
1668 PP.setExternalSource(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00001669
Douglas Gregorecdcb882010-10-20 22:00:55 +00001670 if (Stream.SkipBlock() ||
1671 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001672 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001673 return Failure;
1674 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001675 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001676 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001677
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001678 case PREPROCESSOR_DETAIL_BLOCK_ID:
1679 F.PreprocessorDetailCursor = Stream;
1680 if (Stream.SkipBlock() ||
1681 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1682 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1683 Error("malformed preprocessor detail record in AST file");
1684 return Failure;
1685 }
1686 F.PreprocessorDetailStartOffset
1687 = F.PreprocessorDetailCursor.GetCurrentBitNo();
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001688
1689 if (!PP.getPreprocessingRecord())
1690 PP.createPreprocessingRecord(true);
1691 if (!PP.getPreprocessingRecord()->getExternalSource())
1692 PP.getPreprocessingRecord()->SetExternalSource(*this);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001693 break;
1694
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001695 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001696 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001697 case Success:
1698 break;
1699
1700 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001701 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001702 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001703
1704 case IgnorePCH:
1705 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001706 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001707 break;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001708
1709 case SUBMODULE_BLOCK_ID:
1710 switch (ReadSubmoduleBlock(F)) {
1711 case Success:
1712 break;
1713
1714 case Failure:
1715 Error("malformed submodule block in AST file");
1716 return Failure;
1717
1718 case IgnorePCH:
1719 return IgnorePCH;
1720 }
1721 break;
1722
1723 default:
1724 if (!Stream.SkipBlock())
1725 break;
1726 Error("malformed block record in AST file");
1727 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001728 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001729 continue;
1730 }
1731
1732 if (Code == llvm::bitc::DEFINE_ABBREV) {
1733 Stream.ReadAbbrevRecord();
1734 continue;
1735 }
1736
1737 // Read and process a record.
1738 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001739 const char *BlobStart = 0;
1740 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001741 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001742 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001743 default: // Default behavior: ignore.
1744 break;
1745
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001746 case METADATA: {
1747 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1748 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001749 : diag::warn_pch_version_too_new);
1750 return IgnorePCH;
1751 }
1752
1753 RelocatablePCH = Record[4];
1754 if (Listener) {
1755 std::string TargetTriple(BlobStart, BlobLen);
1756 if (Listener->ReadTargetTriple(TargetTriple))
1757 return IgnorePCH;
1758 }
1759 break;
1760 }
1761
Douglas Gregore95b9192011-08-17 21:07:30 +00001762 case IMPORTS: {
1763 // Load each of the imported PCH files.
1764 unsigned Idx = 0, N = Record.size();
1765 while (Idx < N) {
1766 // Read information about the AST file.
1767 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1768 unsigned Length = Record[Idx++];
1769 llvm::SmallString<128> ImportedFile(Record.begin() + Idx,
1770 Record.begin() + Idx + Length);
1771 Idx += Length;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001772
Douglas Gregore95b9192011-08-17 21:07:30 +00001773 // Load the AST file.
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001774 switch(ReadASTCore(ImportedFile, ImportedKind, &F)) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001775 case Failure: return Failure;
1776 // If we have to ignore the dependency, we'll have to ignore this too.
1777 case IgnorePCH: return IgnorePCH;
1778 case Success: break;
1779 }
1780 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001781 break;
1782 }
1783
Douglas Gregora119da02011-08-02 16:26:37 +00001784 case TYPE_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001785 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001786 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001787 return Failure;
1788 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001789 F.TypeOffsets = (const uint32_t *)BlobStart;
1790 F.LocalNumTypes = Record[0];
Douglas Gregore3605012011-08-02 18:32:54 +00001791 unsigned LocalBaseTypeIndex = Record[1];
1792 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor1e849b62011-07-29 00:21:44 +00001793
Douglas Gregora119da02011-08-02 16:26:37 +00001794 if (F.LocalNumTypes > 0) {
1795 // Introduce the global -> local mapping for types within this module.
1796 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
1797
1798 // Introduce the local -> global mapping for types within this module.
Douglas Gregore3605012011-08-02 18:32:54 +00001799 F.TypeRemap.insert(std::make_pair(LocalBaseTypeIndex,
1800 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregora119da02011-08-02 16:26:37 +00001801
1802 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
1803 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001804 break;
Douglas Gregora119da02011-08-02 16:26:37 +00001805 }
1806
Douglas Gregor496c7092011-08-03 15:48:04 +00001807 case DECL_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001808 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001809 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001810 return Failure;
1811 }
Argyrios Kyrtzidis9d31fa72011-10-27 18:47:35 +00001812 F.DeclOffsets = (const DeclOffset *)BlobStart;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001813 F.LocalNumDecls = Record[0];
Douglas Gregor496c7092011-08-03 15:48:04 +00001814 unsigned LocalBaseDeclID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001815 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor96e973f2011-07-20 00:27:43 +00001816
Douglas Gregor496c7092011-08-03 15:48:04 +00001817 if (F.LocalNumDecls > 0) {
1818 // Introduce the global -> local mapping for declarations within this
1819 // module.
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001820 GlobalDeclMap.insert(
1821 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregor496c7092011-08-03 15:48:04 +00001822
1823 // Introduce the local -> global mapping for declarations within this
1824 // module.
1825 F.DeclRemap.insert(std::make_pair(LocalBaseDeclID,
1826 F.BaseDeclID - LocalBaseDeclID));
1827
1828 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
1829 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001830 break;
Douglas Gregor496c7092011-08-03 15:48:04 +00001831 }
1832
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001833 case TU_UPDATE_LEXICAL: {
Douglas Gregor35942772011-09-09 21:34:22 +00001834 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001835 DeclContextInfo &Info = F.DeclContextInfos[TU];
1836 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
1837 Info.NumLexicalDecls
1838 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregor35942772011-09-09 21:34:22 +00001839 TU->setHasExternalLexicalStorage(true);
Sebastian Redld692af72010-07-27 18:24:41 +00001840 break;
1841 }
1842
Sebastian Redle1dde812010-08-24 00:50:04 +00001843 case UPDATE_VISIBLE: {
Douglas Gregor496c7092011-08-03 15:48:04 +00001844 unsigned Idx = 0;
1845 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Sebastian Redle1dde812010-08-24 00:50:04 +00001846 void *Table = ASTDeclContextNameLookupTable::Create(
Douglas Gregor496c7092011-08-03 15:48:04 +00001847 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redle1dde812010-08-24 00:50:04 +00001848 (const unsigned char *)BlobStart,
Douglas Gregor393f2492011-07-22 00:38:23 +00001849 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregor35942772011-09-09 21:34:22 +00001850 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
1851 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001852 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001853 TU->setHasExternalVisibleStorage(true);
Sebastian Redle1dde812010-08-24 00:50:04 +00001854 } else
Douglas Gregor496c7092011-08-03 15:48:04 +00001855 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redle1dde812010-08-24 00:50:04 +00001856 break;
1857 }
1858
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001859 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001860 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
Douglas Gregor496c7092011-08-03 15:48:04 +00001861 for (unsigned i = 0, e = Record.size(); i < e; /* in loop */) {
1862 DeclID First = ReadDeclID(F, Record, i);
1863 DeclID Latest = ReadDeclID(F, Record, i);
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001864 FirstLatestDeclIDs[First] = Latest;
1865 }
1866 break;
1867 }
1868
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001869 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001870 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001871 return IgnorePCH;
1872 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001873
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001874 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001875 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001876 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001877 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001878 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001879 (const unsigned char *)F.IdentifierTableData + Record[0],
1880 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00001881 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001882
1883 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001884 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001885 break;
1886
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001887 case IDENTIFIER_OFFSET: {
Sebastian Redl2da08f92010-07-19 22:28:42 +00001888 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001889 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001890 return Failure;
1891 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001892 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1893 F.LocalNumIdentifiers = Record[0];
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001894 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001895 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor67268d02011-07-20 00:59:32 +00001896
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001897 if (F.LocalNumIdentifiers > 0) {
1898 // Introduce the global -> local mapping for identifiers within this
1899 // module.
1900 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
1901 &F));
1902
1903 // Introduce the local -> global mapping for identifiers within this
1904 // module.
1905 F.IdentifierRemap.insert(
1906 std::make_pair(LocalBaseIdentifierID,
1907 F.BaseIdentifierID - LocalBaseIdentifierID));
1908
1909 IdentifiersLoaded.resize(IdentifiersLoaded.size()
1910 + F.LocalNumIdentifiers);
1911 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001912 break;
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001913 }
1914
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001915 case EXTERNAL_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001916 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1917 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorfdd01722009-04-14 00:24:19 +00001918 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001919
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001920 case SPECIAL_TYPES:
Douglas Gregor393f2492011-07-22 00:38:23 +00001921 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1922 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregorad1de002009-04-18 05:55:16 +00001923 break;
1924
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001925 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001926 TotalNumStatements += Record[0];
1927 TotalNumMacros += Record[1];
1928 TotalLexicalDeclContexts += Record[2];
1929 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001930 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001931
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001932 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001933 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1934 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001935 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001936
Sean Huntebcbe1d2011-05-04 23:29:54 +00001937 case DELEGATING_CTORS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001938 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1939 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00001940 break;
1941
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001942 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor31e37b22011-07-28 18:09:57 +00001943 if (Record.size() % 4 != 0) {
1944 Error("invalid weak identifiers record");
1945 return Failure;
1946 }
1947
1948 // FIXME: Ignore weak undeclared identifiers from non-original PCH
1949 // files. This isn't the way to do it :)
1950 WeakUndeclaredIdentifiers.clear();
1951
1952 // Translate the weak, undeclared identifiers into global IDs.
1953 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
1954 WeakUndeclaredIdentifiers.push_back(
1955 getGlobalIdentifierID(F, Record[I++]));
1956 WeakUndeclaredIdentifiers.push_back(
1957 getGlobalIdentifierID(F, Record[I++]));
1958 WeakUndeclaredIdentifiers.push_back(
1959 ReadSourceLocation(F, Record, I).getRawEncoding());
1960 WeakUndeclaredIdentifiers.push_back(Record[I++]);
1961 }
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00001962 break;
1963
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001964 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001965 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1966 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor14c22f22009-04-22 22:18:58 +00001967 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001968
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00001969 case SELECTOR_OFFSETS: {
Sebastian Redl059612d2010-08-03 21:58:15 +00001970 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00001971 F.LocalNumSelectors = Record[0];
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00001972 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001973 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor96958cb2011-07-20 01:10:58 +00001974
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00001975 if (F.LocalNumSelectors > 0) {
1976 // Introduce the global -> local mapping for selectors within this
1977 // module.
1978 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
1979
1980 // Introduce the local -> global mapping for selectors within this
1981 // module.
1982 F.SelectorRemap.insert(std::make_pair(LocalBaseSelectorID,
1983 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor83941df2009-04-25 17:48:32 +00001984
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00001985 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
1986 }
1987 break;
1988 }
1989
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001990 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00001991 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00001992 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00001993 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001994 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00001995 F.SelectorLookupTableData + Record[0],
1996 F.SelectorLookupTableData,
Douglas Gregor409448c2011-07-21 22:35:25 +00001997 ASTSelectorLookupTrait(*this, F));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00001998 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001999 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002000
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002001 case REFERENCED_SELECTOR_POOL:
Douglas Gregor8451ec72011-07-28 14:41:43 +00002002 if (!Record.empty()) {
2003 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2004 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2005 Record[Idx++]));
2006 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2007 getRawEncoding());
2008 }
2009 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00002010 break;
2011
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002012 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002013 if (!Record.empty() && Listener)
2014 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002015 break;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002016
2017 case FILE_SORTED_DECLS:
2018 F.FileSortedDecls = (const DeclID *)BlobStart;
2019 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002020
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002021 case SOURCE_LOCATION_OFFSETS: {
2022 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002023 F.LocalNumSLocEntries = Record[0];
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002024 unsigned SLocSpaceSize = Record[1];
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002025 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002026 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2027 SLocSpaceSize);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002028 // Make our entry in the range map. BaseID is negative and growing, so
2029 // we invert it. Because we invert it, though, we need the other end of
2030 // the range.
2031 unsigned RangeStart =
2032 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2033 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2034 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2035
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002036 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2037 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2038 GlobalSLocOffsetMap.insert(
2039 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2040 - SLocSpaceSize,&F));
2041
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002042 // Initialize the remapping table.
2043 // Invalid stays invalid.
2044 F.SLocRemap.insert(std::make_pair(0U, 0));
2045 // This module. Base was 2 when being compiled.
2046 F.SLocRemap.insert(std::make_pair(2U,
2047 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002048
2049 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002050 break;
2051 }
2052
Douglas Gregor5d51a1d2011-08-01 16:01:55 +00002053 case MODULE_OFFSET_MAP: {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002054 // Additional remapping information.
2055 const unsigned char *Data = (const unsigned char*)BlobStart;
2056 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregorf33740e2011-08-02 10:56:51 +00002057
2058 // Continuous range maps we may be updating in our module.
2059 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002060 ContinuousRangeMap<uint32_t, int, 2>::Builder
2061 IdentifierRemap(F.IdentifierRemap);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002062 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002063 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2064 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor26ced122011-12-01 00:59:36 +00002065 SubmoduleRemap(F.SubmoduleRemap);
2066 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002067 SelectorRemap(F.SelectorRemap);
Douglas Gregor496c7092011-08-03 15:48:04 +00002068 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregora119da02011-08-02 16:26:37 +00002069 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2070
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002071 while(Data < DataEnd) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002072 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002073 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002074 Data += Len;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002075 ModuleFile *OM = ModuleMgr.lookup(Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002076 if (!OM) {
2077 Error("SourceLocation remap refers to unknown module");
2078 return Failure;
2079 }
Douglas Gregorf33740e2011-08-02 10:56:51 +00002080
2081 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2082 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2083 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor26ced122011-12-01 00:59:36 +00002084 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002085 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2086 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora119da02011-08-02 16:26:37 +00002087 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002088
2089 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2090 SLocRemap.insert(std::make_pair(SLocOffset,
2091 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002092 IdentifierRemap.insert(
2093 std::make_pair(IdentifierIDOffset,
2094 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002095 PreprocessedEntityRemap.insert(
2096 std::make_pair(PreprocessedEntityIDOffset,
2097 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregor26ced122011-12-01 00:59:36 +00002098 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2099 OM->BaseSubmoduleID - SubmoduleIDOffset));
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002100 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2101 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregor496c7092011-08-03 15:48:04 +00002102 DeclRemap.insert(std::make_pair(DeclIDOffset,
2103 OM->BaseDeclID - DeclIDOffset));
2104
Douglas Gregora119da02011-08-02 16:26:37 +00002105 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregore3605012011-08-02 18:32:54 +00002106 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002107 }
2108 break;
2109 }
2110
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002111 case SOURCE_MANAGER_LINE_TABLE:
2112 if (ParseLineTable(F, Record))
2113 return Failure;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002114 break;
2115
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00002116 case FILE_SOURCE_LOCATION_OFFSETS:
2117 F.SLocFileOffsets = (const uint32_t *)BlobStart;
2118 F.LocalNumSLocFileEntries = Record[0];
2119 break;
2120
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002121 case SOURCE_LOCATION_PRELOADS: {
2122 // Need to transform from the local view (1-based IDs) to the global view,
2123 // which is based off F.SLocEntryBaseID.
Douglas Gregorf249bf32011-08-25 21:09:44 +00002124 if (!F.PreloadSLocEntries.empty()) {
2125 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2126 return Failure;
2127 }
2128
2129 F.PreloadSLocEntries.swap(Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002130 break;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002131 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002132
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002133 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002134 if (!DisableStatCache) {
2135 ASTStatCache *MyStatCache =
2136 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2137 (const unsigned char *)BlobStart,
2138 NumStatHits, NumStatMisses);
2139 FileMgr.addStatCache(MyStatCache);
2140 F.StatCache = MyStatCache;
2141 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002142 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002143 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002144
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002145 case EXT_VECTOR_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002146 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2147 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorb81c1702009-04-27 20:06:05 +00002148 break;
2149
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002150 case VTABLE_USES:
Douglas Gregordfe65432011-07-28 19:11:31 +00002151 if (Record.size() % 3 != 0) {
2152 Error("Invalid VTABLE_USES record");
2153 return Failure;
2154 }
2155
Sebastian Redl40566802010-08-05 18:21:25 +00002156 // Later tables overwrite earlier ones.
Douglas Gregordfe65432011-07-28 19:11:31 +00002157 // FIXME: Modules will have some trouble with this. This is clearly not
2158 // the right way to do this.
Douglas Gregor409448c2011-07-21 22:35:25 +00002159 VTableUses.clear();
Douglas Gregordfe65432011-07-28 19:11:31 +00002160
2161 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2162 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2163 VTableUses.push_back(
2164 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2165 VTableUses.push_back(Record[Idx++]);
2166 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002167 break;
2168
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002169 case DYNAMIC_CLASSES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002170 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2171 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002172 break;
2173
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002174 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorf2abb522011-07-28 19:26:52 +00002175 if (PendingInstantiations.size() % 2 != 0) {
2176 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2177 return Failure;
2178 }
2179
2180 // Later lists of pending instantiations overwrite earlier ones.
2181 // FIXME: This is most certainly wrong for modules.
2182 PendingInstantiations.clear();
2183 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2184 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2185 PendingInstantiations.push_back(
2186 ReadSourceLocation(F, Record, I).getRawEncoding());
2187 }
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002188 break;
2189
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002190 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002191 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002192 // FIXME: Modules will have some trouble with this.
2193 SemaDeclRefs.clear();
2194 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2195 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002196 break;
2197
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002198 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002199 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002200 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002201 ActualOriginalFileName.assign(BlobStart, BlobLen);
2202 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002203 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002204 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002205
Douglas Gregor31d375f2011-05-06 21:43:30 +00002206 case ORIGINAL_FILE_ID:
2207 OriginalFileID = FileID::get(Record[0]);
2208 break;
2209
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002210 case ORIGINAL_PCH_DIR:
2211 // The primary AST will be the last to get here, so it will be the one
2212 // that's used.
2213 OriginalDir.assign(BlobStart, BlobLen);
2214 break;
2215
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002216 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002217 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002218 StringRef ASTBranch(BlobStart, BlobLen);
2219 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002220 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002221 return IgnorePCH;
2222 }
2223 break;
2224 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002225
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002226 case PPD_ENTITIES_OFFSETS: {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002227 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart;
2228 assert(BlobLen % sizeof(PPEntityOffset) == 0);
2229 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002230
2231 unsigned LocalBasePreprocessedEntityID = Record[0];
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002232
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002233 unsigned StartingID;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002234 if (!PP.getPreprocessingRecord())
2235 PP.createPreprocessingRecord(true);
2236 if (!PP.getPreprocessingRecord()->getExternalSource())
2237 PP.getPreprocessingRecord()->SetExternalSource(*this);
2238 StartingID
2239 = PP.getPreprocessingRecord()
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002240 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Douglas Gregor9827a802011-07-29 00:56:45 +00002241 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002242
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002243 if (F.NumPreprocessedEntities > 0) {
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002244 // Introduce the global -> local mapping for preprocessed entities in
2245 // this module.
2246 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2247
2248 // Introduce the local -> global mapping for preprocessed entities in
2249 // this module.
2250 F.PreprocessedEntityRemap.insert(
2251 std::make_pair(LocalBasePreprocessedEntityID,
2252 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2253 }
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002254
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002255 break;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002256 }
2257
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002258 case DECL_UPDATE_OFFSETS: {
2259 if (Record.size() % 2 != 0) {
2260 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2261 return Failure;
2262 }
2263 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregor496c7092011-08-03 15:48:04 +00002264 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2265 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002266 break;
2267 }
2268
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002269 case DECL_REPLACEMENTS: {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002270 if (Record.size() % 3 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002271 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002272 return Failure;
2273 }
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002274 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
Douglas Gregor496c7092011-08-03 15:48:04 +00002275 ReplacedDecls[getGlobalDeclID(F, Record[I])]
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002276 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002277 break;
2278 }
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002279
2280 case OBJC_CHAINED_CATEGORIES: {
2281 if (Record.size() % 3 != 0) {
2282 Error("invalid OBJC_CHAINED_CATEGORIES block in AST file");
2283 return Failure;
2284 }
2285 for (unsigned I = 0, N = Record.size(); I != N; I += 3) {
2286 serialization::GlobalDeclID GlobID = getGlobalDeclID(F, Record[I]);
2287 F.ChainedObjCCategories[GlobID] = std::make_pair(Record[I+1],
2288 Record[I+2]);
2289 ObjCChainedCategoriesInterfaces.insert(GlobID);
2290 }
2291 break;
2292 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002293
2294 case CXX_BASE_SPECIFIER_OFFSETS: {
2295 if (F.LocalNumCXXBaseSpecifiers != 0) {
2296 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2297 return Failure;
2298 }
2299
2300 F.LocalNumCXXBaseSpecifiers = Record[0];
2301 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner1da90142011-07-21 21:15:19 +00002302 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002303 break;
2304 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002305
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002306 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002307 if (Record.size() % 2 != 0) {
2308 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2309 return Failure;
2310 }
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002311
2312 if (F.PragmaDiagMappings.empty())
2313 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002314 else
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002315 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2316 Record.begin(), Record.end());
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002317 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002318
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002319 case CUDA_SPECIAL_DECL_REFS:
2320 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002321 // FIXME: Modules will have trouble with this.
2322 CUDASpecialDeclRefs.clear();
2323 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2324 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002325 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002326
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002327 case HEADER_SEARCH_TABLE: {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002328 F.HeaderFileInfoTableData = BlobStart;
2329 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002330 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002331 if (Record[0]) {
2332 F.HeaderFileInfoTable
2333 = HeaderFileInfoLookupTable::Create(
2334 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002335 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregor95eab172011-07-28 20:55:49 +00002336 HeaderFileInfoTrait(*this, F,
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002337 &PP.getHeaderSearchInfo(),
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002338 BlobStart + Record[2]));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002339
2340 PP.getHeaderSearchInfo().SetExternalSource(this);
2341 if (!PP.getHeaderSearchInfo().getExternalLookup())
2342 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002343 }
2344 break;
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002345 }
2346
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002347 case FP_PRAGMA_OPTIONS:
2348 // Later tables overwrite earlier ones.
2349 FPPragmaOptions.swap(Record);
2350 break;
2351
2352 case OPENCL_EXTENSIONS:
2353 // Later tables overwrite earlier ones.
2354 OpenCLExtensions.swap(Record);
2355 break;
Sean Huntebcbe1d2011-05-04 23:29:54 +00002356
2357 case TENTATIVE_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002358 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2359 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002360 break;
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002361
2362 case KNOWN_NAMESPACES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002363 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2364 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002365 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002366 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002367 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002368 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002369 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002370}
2371
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002372ASTReader::ASTReadResult ASTReader::validateFileEntries(ModuleFile &M) {
Douglas Gregorc69a2922011-08-25 20:58:51 +00002373 llvm::BitstreamCursor &SLocEntryCursor = M.SLocEntryCursor;
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002374
Douglas Gregorc69a2922011-08-25 20:58:51 +00002375 for (unsigned i = 0, e = M.LocalNumSLocFileEntries; i != e; ++i) {
2376 SLocEntryCursor.JumpToBit(M.SLocFileOffsets[i]);
2377 unsigned Code = SLocEntryCursor.ReadCode();
2378 if (Code == llvm::bitc::END_BLOCK ||
2379 Code == llvm::bitc::ENTER_SUBBLOCK ||
2380 Code == llvm::bitc::DEFINE_ABBREV) {
2381 Error("incorrectly-formatted source location entry in AST file");
2382 return Failure;
2383 }
2384
2385 RecordData Record;
2386 const char *BlobStart;
2387 unsigned BlobLen;
2388 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2389 default:
2390 Error("incorrectly-formatted source location entry in AST file");
2391 return Failure;
2392
2393 case SM_SLOC_FILE_ENTRY: {
Douglas Gregora081da52011-11-16 20:05:18 +00002394 // If the buffer was overridden, the file need not exist.
2395 if (Record[6])
2396 break;
2397
Douglas Gregorc69a2922011-08-25 20:58:51 +00002398 StringRef Filename(BlobStart, BlobLen);
2399 const FileEntry *File = getFileEntry(Filename);
2400
2401 if (File == 0) {
2402 std::string ErrorStr = "could not find file '";
2403 ErrorStr += Filename;
2404 ErrorStr += "' referenced by AST file";
2405 Error(ErrorStr.c_str());
2406 return IgnorePCH;
2407 }
2408
Douglas Gregora081da52011-11-16 20:05:18 +00002409 if (Record.size() < 7) {
Douglas Gregorc69a2922011-08-25 20:58:51 +00002410 Error("source location entry is incorrect");
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002411 return Failure;
2412 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002413
Douglas Gregorc69a2922011-08-25 20:58:51 +00002414 // The stat info from the FileEntry came from the cached stat
2415 // info of the PCH, so we cannot trust it.
2416 struct stat StatBuf;
2417 if (::stat(File->getName(), &StatBuf) != 0) {
2418 StatBuf.st_size = File->getSize();
2419 StatBuf.st_mtime = File->getModificationTime();
2420 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002421
Douglas Gregorc69a2922011-08-25 20:58:51 +00002422 if (((off_t)Record[4] != StatBuf.st_size
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002423#if !defined(LLVM_ON_WIN32)
Douglas Gregorc69a2922011-08-25 20:58:51 +00002424 // In our regression testing, the Windows file system seems to
2425 // have inconsistent modification times that sometimes
2426 // erroneously trigger this error-handling path.
2427 || (time_t)Record[5] != StatBuf.st_mtime
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002428#endif
Douglas Gregorc69a2922011-08-25 20:58:51 +00002429 )) {
2430 Error(diag::err_fe_pch_file_modified, Filename);
2431 return IgnorePCH;
2432 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002433
Douglas Gregorc69a2922011-08-25 20:58:51 +00002434 break;
2435 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002436 }
2437 }
2438
2439 return Success;
2440}
2441
Douglas Gregor5e356932011-12-01 17:11:21 +00002442void ASTReader::makeModuleVisible(Module *Mod,
2443 Module::NameVisibilityKind NameVisibility) {
2444 llvm::SmallPtrSet<Module *, 4> Visited;
2445 llvm::SmallVector<Module *, 4> Stack;
2446 Stack.push_back(Mod);
2447 while (!Stack.empty()) {
2448 Mod = Stack.back();
2449 Stack.pop_back();
2450
2451 if (NameVisibility <= Mod->NameVisibility) {
2452 // This module already has this level of visibility (or greater), so
2453 // there is nothing more to do.
2454 continue;
2455 }
2456
2457 // Update the module's name visibility.
2458 Mod->NameVisibility = NameVisibility;
2459
2460 // FIXME: If we've already deserialized any names from this module,
2461 // mark them as visible.
2462
2463 // Push any non-explicit submodules onto the stack to be marked as
2464 // visible.
2465 for (llvm::StringMap<Module *>::iterator Sub = Mod->SubModules.begin(),
2466 SubEnd = Mod->SubModules.end();
2467 Sub != SubEnd; ++Sub) {
2468 if (!Sub->getValue()->IsExplicit && Visited.insert(Sub->getValue()))
2469 Stack.push_back(Sub->getValue());
2470 }
2471 }
2472}
2473
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002474ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregor72a9ae12011-07-22 16:00:58 +00002475 ModuleKind Type) {
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002476 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002477 case Failure: return Failure;
2478 case IgnorePCH: return IgnorePCH;
2479 case Success: break;
2480 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002481
2482 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002483
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002484 // Check the predefines buffers.
Argyrios Kyrtzidis26d43cd2011-09-12 18:09:38 +00002485 if (!DisableValidation && Type != MK_Module && Type != MK_Preamble &&
2486 // FIXME: CheckPredefinesBuffers also sets the SuggestedPredefines;
2487 // if DisableValidation is true, defines that were set on command-line
2488 // but not in the PCH file will not be added to SuggestedPredefines.
2489 CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002490 return IgnorePCH;
2491
Douglas Gregoreee242f2011-10-27 09:33:13 +00002492 // Mark all of the identifiers in the identifier table as being out of date,
2493 // so that various accessors know to check the loaded modules when the
2494 // identifier is used.
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002495 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2496 IdEnd = PP.getIdentifierTable().end();
2497 Id != IdEnd; ++Id)
Douglas Gregoreee242f2011-10-27 09:33:13 +00002498 Id->second->setOutOfDate(true);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002499
Douglas Gregor35942772011-09-09 21:34:22 +00002500 InitializeContext();
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002501
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002502 if (DeserializationListener)
2503 DeserializationListener->ReaderInitialized(this);
2504
Argyrios Kyrtzidis507097e2011-09-19 20:40:35 +00002505 // If this AST file is a precompiled preamble, then set the preamble file ID
2506 // of the source manager to the file source file from which the preamble was
2507 // built.
Douglas Gregor72a9ae12011-07-22 16:00:58 +00002508 if (Type == MK_Preamble) {
Argyrios Kyrtzidis507097e2011-09-19 20:40:35 +00002509 if (!OriginalFileID.isInvalid()) {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00002510 OriginalFileID = FileID::get(ModuleMgr.getPrimaryModule().SLocEntryBaseID
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002511 + OriginalFileID.getOpaqueValue() - 1);
Argyrios Kyrtzidis507097e2011-09-19 20:40:35 +00002512 SourceMgr.setPreambleFileID(OriginalFileID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002513 }
Douglas Gregor414cb642010-11-30 05:23:00 +00002514 }
2515
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002516 return Success;
2517}
2518
Chris Lattner5f9e2722011-07-23 10:55:15 +00002519ASTReader::ASTReadResult ASTReader::ReadASTCore(StringRef FileName,
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002520 ModuleKind Type,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002521 ModuleFile *ImportedBy) {
2522 ModuleFile *M;
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002523 bool NewModule;
2524 std::string ErrorStr;
2525 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
2526 ErrorStr);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002527
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002528 if (!M) {
2529 // We couldn't load the module.
2530 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2531 + ErrorStr;
2532 Error(Msg);
2533 return Failure;
2534 }
2535
2536 if (!NewModule) {
2537 // We've already loaded this module.
2538 return Success;
2539 }
2540
2541 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2542 // module?
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002543 if (FileName != "-") {
2544 CurrentDir = llvm::sys::path::parent_path(FileName);
2545 if (CurrentDir.empty()) CurrentDir = ".";
2546 }
2547
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002548 ModuleFile &F = *M;
Sebastian Redl9137a522010-07-16 17:50:48 +00002549 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002550 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002551 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002552
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002553 // Sniff for the signature.
2554 if (Stream.Read(8) != 'C' ||
2555 Stream.Read(8) != 'P' ||
2556 Stream.Read(8) != 'C' ||
2557 Stream.Read(8) != 'H') {
2558 Diag(diag::err_not_a_pch_file) << FileName;
2559 return Failure;
2560 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002561
Douglas Gregor2cf26342009-04-09 22:27:44 +00002562 while (!Stream.AtEndOfStream()) {
2563 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002564
Douglas Gregore1d918e2009-04-10 23:10:45 +00002565 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002566 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002567 return Failure;
2568 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002569
2570 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002571
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002572 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002573 switch (BlockID) {
2574 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002575 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002576 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002577 return Failure;
2578 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002579 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002580 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002581 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002582 case Success:
2583 break;
2584
2585 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002586 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002587
2588 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002589 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002590 // AST block, skipping subblocks, to see if there are other
2591 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002592
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002593 // FIXME: We can't clear loaded slocentries anymore.
2594 //SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002595
2596 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002597 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002598 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002599
Douglas Gregore1d918e2009-04-10 23:10:45 +00002600 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002601 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002602 break;
2603 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002604 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002605 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002606 return Failure;
2607 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002608 break;
2609 }
Mike Stump1eb44332009-09-09 15:08:12 +00002610 }
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002611
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002612 // Once read, set the ModuleFile bit base offset and update the size in
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002613 // bits of all files we've seen.
2614 F.GlobalBitOffset = TotalModulesSizeInBits;
2615 TotalModulesSizeInBits += F.SizeInBits;
2616 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
Douglas Gregorc69a2922011-08-25 20:58:51 +00002617
2618 // Make sure that the files this module was built against are still available.
2619 if (!DisableValidation) {
2620 switch(validateFileEntries(*M)) {
2621 case Failure: return Failure;
2622 case IgnorePCH: return IgnorePCH;
2623 case Success: break;
2624 }
2625 }
Douglas Gregorf249bf32011-08-25 21:09:44 +00002626
2627 // Preload SLocEntries.
2628 for (unsigned I = 0, N = M->PreloadSLocEntries.size(); I != N; ++I) {
2629 int Index = int(M->PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Argyrios Kyrtzidisac1ffcc2011-09-19 20:39:54 +00002630 // Load it through the SourceManager and don't call ReadSLocEntryRecord()
2631 // directly because the entry may have already been loaded in which case
2632 // calling ReadSLocEntryRecord() directly would trigger an assertion in
2633 // SourceManager.
2634 SourceMgr.getLoadedSLocEntryByID(Index);
Douglas Gregorf249bf32011-08-25 21:09:44 +00002635 }
2636
Douglas Gregorc69a2922011-08-25 20:58:51 +00002637
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002638 return Success;
2639}
2640
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002641void ASTReader::InitializeContext() {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002642 // If there's a listener, notify them that we "read" the translation unit.
2643 if (DeserializationListener)
Douglas Gregor35942772011-09-09 21:34:22 +00002644 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
2645 Context.getTranslationUnitDecl());
Douglas Gregor3747ee72010-10-01 01:18:02 +00002646
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002647 // Make sure we load the declaration update records for the translation unit,
2648 // if there are any.
Douglas Gregor35942772011-09-09 21:34:22 +00002649 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
2650 Context.getTranslationUnitDecl());
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002651
Douglas Gregor5f957282011-08-11 22:18:49 +00002652 // FIXME: Find a better way to deal with collisions between these
2653 // built-in types. Right now, we just ignore the problem.
2654
2655 // Load the special types.
Douglas Gregor02a5e872011-09-10 00:30:18 +00002656 if (SpecialTypes.size() > NumSpecialTypeIDs) {
2657 if (Context.getBuiltinVaListType().isNull()) {
2658 Context.setBuiltinVaListType(
2659 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
Douglas Gregor5f957282011-08-11 22:18:49 +00002660 }
2661
Douglas Gregor02a5e872011-09-10 00:30:18 +00002662 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL]) {
2663 if (Context.ObjCProtoType.isNull())
2664 Context.ObjCProtoType = GetType(Proto);
2665 }
2666
2667 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2668 if (!Context.CFConstantStringTypeDecl)
2669 Context.setCFConstantStringType(GetType(String));
2670 }
2671
2672 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2673 QualType FileType = GetType(File);
2674 if (FileType.isNull()) {
2675 Error("FILE type is NULL");
2676 return;
2677 }
2678
2679 if (!Context.FILEDecl) {
2680 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2681 Context.setFILEDecl(Typedef->getDecl());
2682 else {
2683 const TagType *Tag = FileType->getAs<TagType>();
2684 if (!Tag) {
2685 Error("Invalid FILE type in AST file");
2686 return;
2687 }
2688 Context.setFILEDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002689 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002690 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002691 }
Douglas Gregor5f957282011-08-11 22:18:49 +00002692
Douglas Gregor72cd7a02011-11-11 19:13:12 +00002693 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002694 QualType Jmp_bufType = GetType(Jmp_buf);
2695 if (Jmp_bufType.isNull()) {
2696 Error("jmp_buf type is NULL");
2697 return;
2698 }
2699
2700 if (!Context.jmp_bufDecl) {
2701 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2702 Context.setjmp_bufDecl(Typedef->getDecl());
2703 else {
2704 const TagType *Tag = Jmp_bufType->getAs<TagType>();
2705 if (!Tag) {
2706 Error("Invalid jmp_buf type in AST file");
2707 return;
2708 }
2709 Context.setjmp_bufDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002710 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002711 }
Mike Stump782fa302009-07-28 02:25:19 +00002712 }
Douglas Gregor02a5e872011-09-10 00:30:18 +00002713
Douglas Gregor72cd7a02011-11-11 19:13:12 +00002714 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002715 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2716 if (Sigjmp_bufType.isNull()) {
2717 Error("sigjmp_buf type is NULL");
2718 return;
2719 }
2720
2721 if (!Context.sigjmp_bufDecl) {
2722 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2723 Context.setsigjmp_bufDecl(Typedef->getDecl());
2724 else {
2725 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
2726 assert(Tag && "Invalid sigjmp_buf type in AST file");
2727 Context.setsigjmp_bufDecl(Tag->getDecl());
2728 }
2729 }
2730 }
2731
2732 if (unsigned ObjCIdRedef
2733 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
2734 if (Context.ObjCIdRedefinitionType.isNull())
2735 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
2736 }
2737
2738 if (unsigned ObjCClassRedef
2739 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
2740 if (Context.ObjCClassRedefinitionType.isNull())
2741 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
2742 }
2743
2744 if (unsigned ObjCSelRedef
2745 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
2746 if (Context.ObjCSelRedefinitionType.isNull())
2747 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
2748 }
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00002749
2750 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
2751 QualType Ucontext_tType = GetType(Ucontext_t);
2752 if (Ucontext_tType.isNull()) {
2753 Error("ucontext_t type is NULL");
2754 return;
2755 }
2756
2757 if (!Context.ucontext_tDecl) {
2758 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
2759 Context.setucontext_tDecl(Typedef->getDecl());
2760 else {
2761 const TagType *Tag = Ucontext_tType->getAs<TagType>();
2762 assert(Tag && "Invalid ucontext_t type in AST file");
2763 Context.setucontext_tDecl(Tag->getDecl());
2764 }
2765 }
2766 }
Douglas Gregor5f957282011-08-11 22:18:49 +00002767 }
2768
Douglas Gregor35942772011-09-09 21:34:22 +00002769 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002770
2771 // If there were any CUDA special declarations, deserialize them.
2772 if (!CUDASpecialDeclRefs.empty()) {
2773 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
Douglas Gregor35942772011-09-09 21:34:22 +00002774 Context.setcudaConfigureCallDecl(
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002775 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2776 }
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002777}
2778
Douglas Gregorb64c1932009-05-12 01:31:05 +00002779/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002780/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002781/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002782std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002783 FileManager &FileMgr,
David Blaikied6471f72011-09-25 23:23:43 +00002784 DiagnosticsEngine &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002785 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002786 std::string ErrStr;
2787 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00002788 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002789 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002790 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002791 return std::string();
2792 }
2793
2794 // Initialize the stream
2795 llvm::BitstreamReader StreamFile;
2796 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002797 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002798 (const unsigned char *)Buffer->getBufferEnd());
2799 Stream.init(StreamFile);
2800
2801 // Sniff for the signature.
2802 if (Stream.Read(8) != 'C' ||
2803 Stream.Read(8) != 'P' ||
2804 Stream.Read(8) != 'C' ||
2805 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002806 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002807 return std::string();
2808 }
2809
2810 RecordData Record;
2811 while (!Stream.AtEndOfStream()) {
2812 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002813
Douglas Gregorb64c1932009-05-12 01:31:05 +00002814 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2815 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002816
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002817 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002818 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002819 case AST_BLOCK_ID:
2820 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002821 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002822 return std::string();
2823 }
2824 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002825
Douglas Gregorb64c1932009-05-12 01:31:05 +00002826 default:
2827 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002828 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002829 return std::string();
2830 }
2831 break;
2832 }
2833 continue;
2834 }
2835
2836 if (Code == llvm::bitc::END_BLOCK) {
2837 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002838 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002839 return std::string();
2840 }
2841 continue;
2842 }
2843
2844 if (Code == llvm::bitc::DEFINE_ABBREV) {
2845 Stream.ReadAbbrevRecord();
2846 continue;
2847 }
2848
2849 Record.clear();
2850 const char *BlobStart = 0;
2851 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002852 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002853 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00002854 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00002855 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00002856
2857 return std::string();
2858}
2859
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002860ASTReader::ASTReadResult ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002861 // Enter the submodule block.
2862 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
2863 Error("malformed submodule block record in AST file");
2864 return Failure;
2865 }
2866
2867 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
Douglas Gregor26ced122011-12-01 00:59:36 +00002868 bool First = true;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002869 Module *CurrentModule = 0;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002870 RecordData Record;
Douglas Gregor26ced122011-12-01 00:59:36 +00002871 SubmoduleID CurrentModuleGlobalIndex = 0;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002872 while (true) {
2873 unsigned Code = F.Stream.ReadCode();
2874 if (Code == llvm::bitc::END_BLOCK) {
2875 if (F.Stream.ReadBlockEnd()) {
2876 Error("error at end of submodule block in AST file");
2877 return Failure;
2878 }
2879 return Success;
2880 }
2881
2882 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2883 // No known subblocks, always skip them.
2884 F.Stream.ReadSubBlockID();
2885 if (F.Stream.SkipBlock()) {
2886 Error("malformed block record in AST file");
2887 return Failure;
2888 }
2889 continue;
2890 }
2891
2892 if (Code == llvm::bitc::DEFINE_ABBREV) {
2893 F.Stream.ReadAbbrevRecord();
2894 continue;
2895 }
2896
2897 // Read a record.
2898 const char *BlobStart;
2899 unsigned BlobLen;
2900 Record.clear();
2901 switch (F.Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2902 default: // Default behavior: ignore.
2903 break;
2904
2905 case SUBMODULE_DEFINITION: {
Douglas Gregor26ced122011-12-01 00:59:36 +00002906 if (First) {
2907 Error("missing submodule metadata record at beginning of block");
2908 return Failure;
2909 }
2910
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002911 StringRef Name(BlobStart, BlobLen);
Douglas Gregor26ced122011-12-01 00:59:36 +00002912 unsigned Parent = getGlobalSubmoduleID(F, Record[0]);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002913 bool IsFramework = Record[1];
2914 bool IsExplicit = Record[2];
2915
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002916 Module *ParentModule = 0;
Douglas Gregor26ced122011-12-01 00:59:36 +00002917 if (Parent)
2918 ParentModule = getSubmodule(Parent);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002919
2920 // Retrieve this (sub)module from the module map, creating it if
2921 // necessary.
2922 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
2923 IsFramework,
2924 IsExplicit).first;
Douglas Gregor26ced122011-12-01 00:59:36 +00002925
2926 if (CurrentModuleGlobalIndex >= SubmodulesLoaded.size() ||
2927 SubmodulesLoaded[CurrentModuleGlobalIndex]) {
2928 Error("too many submodules");
2929 return Failure;
2930 }
2931 SubmodulesLoaded[CurrentModuleGlobalIndex++] = CurrentModule;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002932 break;
2933 }
2934
2935 case SUBMODULE_UMBRELLA: {
Douglas Gregor26ced122011-12-01 00:59:36 +00002936 if (First) {
2937 Error("missing submodule metadata record at beginning of block");
2938 return Failure;
2939 }
2940
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002941 if (!CurrentModule)
2942 break;
2943
2944 StringRef FileName(BlobStart, BlobLen);
2945 if (const FileEntry *Umbrella = PP.getFileManager().getFile(FileName)) {
2946 if (!CurrentModule->UmbrellaHeader)
2947 CurrentModule->UmbrellaHeader = Umbrella;
2948 else if (CurrentModule->UmbrellaHeader != Umbrella) {
2949 Error("mismatched umbrella headers in submodule");
2950 return Failure;
2951 }
2952 }
2953 break;
2954 }
2955
2956 case SUBMODULE_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00002957 if (First) {
2958 Error("missing submodule metadata record at beginning of block");
2959 return Failure;
2960 }
2961
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002962 if (!CurrentModule)
2963 break;
2964
2965 // FIXME: Be more lazy about this!
2966 StringRef FileName(BlobStart, BlobLen);
2967 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
2968 if (std::find(CurrentModule->Headers.begin(),
2969 CurrentModule->Headers.end(),
2970 File) == CurrentModule->Headers.end())
2971 CurrentModule->Headers.push_back(File);
2972 }
2973 break;
2974 }
Douglas Gregor26ced122011-12-01 00:59:36 +00002975
2976 case SUBMODULE_METADATA: {
2977 if (!First) {
2978 Error("submodule metadata record not at beginning of block");
2979 return Failure;
2980 }
2981 First = false;
2982
2983 F.BaseSubmoduleID = getTotalNumSubmodules();
2984 CurrentModuleGlobalIndex = F.BaseSubmoduleID;
2985 F.LocalNumSubmodules = Record[0];
2986 unsigned LocalBaseSubmoduleID = Record[1];
2987 if (F.LocalNumSubmodules > 0) {
2988 // Introduce the global -> local mapping for submodules within this
2989 // module.
2990 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
2991
2992 // Introduce the local -> global mapping for submodules within this
2993 // module.
2994 F.SubmoduleRemap.insert(
2995 std::make_pair(LocalBaseSubmoduleID,
2996 F.BaseSubmoduleID - LocalBaseSubmoduleID));
2997
2998 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
2999 }
3000 break;
3001 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003002 }
3003 }
3004
3005 return Success;
3006}
3007
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003008/// \brief Parse the record that corresponds to a LangOptions data
3009/// structure.
3010///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003011/// This routine parses the language options from the AST file and then gives
3012/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003013///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003014/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003015bool ASTReader::ParseLanguageOptions(
Chris Lattner5f9e2722011-07-23 10:55:15 +00003016 const SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003017 if (Listener) {
3018 LangOptions LangOpts;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003019 unsigned Idx = 0;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003020#define LANGOPT(Name, Bits, Default, Description) \
3021 LangOpts.Name = Record[Idx++];
3022#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3023 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3024#include "clang/Basic/LangOptions.def"
3025
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00003026 unsigned Length = Record[Idx++];
3027 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3028 Record.begin() + Idx + Length);
3029 Idx += Length;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003030 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003031 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003032
3033 return false;
3034}
3035
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003036std::pair<ModuleFile *, unsigned>
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003037ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003038 GlobalPreprocessedEntityMapType::iterator
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003039 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003040 assert(I != GlobalPreprocessedEntityMap.end() &&
3041 "Corrupted global preprocessed entity map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003042 ModuleFile *M = I->second;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003043 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
3044 return std::make_pair(M, LocalIndex);
3045}
3046
3047PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
3048 PreprocessedEntityID PPID = Index+1;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003049 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3050 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003051 unsigned LocalIndex = PPInfo.second;
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003052 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
Douglas Gregor4800a5c2011-02-08 21:58:10 +00003053
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003054 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003055 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003056
3057 unsigned Code = M.PreprocessorDetailCursor.ReadCode();
3058 switch (Code) {
3059 case llvm::bitc::END_BLOCK:
3060 return 0;
3061
3062 case llvm::bitc::ENTER_SUBBLOCK:
3063 Error("unexpected subblock record in preprocessor detail block");
3064 return 0;
3065
3066 case llvm::bitc::DEFINE_ABBREV:
3067 Error("unexpected abbrevation record in preprocessor detail block");
3068 return 0;
3069
3070 default:
3071 break;
3072 }
3073
3074 if (!PP.getPreprocessingRecord()) {
3075 Error("no preprocessing record");
3076 return 0;
3077 }
3078
3079 // Read the record.
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003080 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
3081 ReadSourceLocation(M, PPOffs.End));
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003082 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
3083 const char *BlobStart = 0;
3084 unsigned BlobLen = 0;
3085 RecordData Record;
3086 PreprocessorDetailRecordTypes RecType =
3087 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord(
3088 Code, Record, BlobStart, BlobLen);
3089 switch (RecType) {
3090 case PPD_MACRO_EXPANSION: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003091 bool isBuiltin = Record[0];
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003092 IdentifierInfo *Name = 0;
3093 MacroDefinition *Def = 0;
3094 if (isBuiltin)
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003095 Name = getLocalIdentifier(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003096 else {
3097 PreprocessedEntityID
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003098 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003099 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
3100 }
3101
3102 MacroExpansion *ME;
3103 if (isBuiltin)
3104 ME = new (PPRec) MacroExpansion(Name, Range);
3105 else
3106 ME = new (PPRec) MacroExpansion(Def, Range);
3107
3108 return ME;
3109 }
3110
3111 case PPD_MACRO_DEFINITION: {
3112 // Decode the identifier info and then check again; if the macro is
3113 // still defined and associated with the identifier,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003114 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003115 MacroDefinition *MD
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003116 = new (PPRec) MacroDefinition(II, Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003117
3118 if (DeserializationListener)
3119 DeserializationListener->MacroDefinitionRead(PPID, MD);
3120
3121 return MD;
3122 }
3123
3124 case PPD_INCLUSION_DIRECTIVE: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003125 const char *FullFileNameStart = BlobStart + Record[0];
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003126 const FileEntry *File
3127 = PP.getFileManager().getFile(StringRef(FullFileNameStart,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003128 BlobLen - Record[0]));
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003129
3130 // FIXME: Stable encoding
3131 InclusionDirective::InclusionKind Kind
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003132 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003133 InclusionDirective *ID
3134 = new (PPRec) InclusionDirective(PPRec, Kind,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003135 StringRef(BlobStart, Record[0]),
3136 Record[1],
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003137 File,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003138 Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003139 return ID;
3140 }
3141 }
3142
3143 Error("invalid offset in preprocessor detail block");
3144 return 0;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003145}
3146
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003147/// \brief \arg SLocMapI points at a chunk of a module that contains no
3148/// preprocessed entities or the entities it contains are not the ones we are
3149/// looking for. Find the next module that contains entities and return the ID
3150/// of the first entry.
3151PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
3152 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
3153 ++SLocMapI;
3154 for (GlobalSLocOffsetMapType::const_iterator
3155 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003156 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003157 if (M.NumPreprocessedEntities)
3158 return getGlobalPreprocessedEntityID(M, M.BasePreprocessedEntityID);
3159 }
3160
3161 return getTotalNumPreprocessedEntities();
3162}
3163
3164namespace {
3165
3166template <unsigned PPEntityOffset::*PPLoc>
3167struct PPEntityComp {
3168 const ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003169 ModuleFile &M;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003170
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003171 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003172
Benjamin Kramer88df1252011-09-21 06:42:26 +00003173 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
3174 SourceLocation LHS = getLoc(L);
3175 SourceLocation RHS = getLoc(R);
3176 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3177 }
3178
3179 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003180 SourceLocation LHS = getLoc(L);
3181 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3182 }
3183
Benjamin Kramer88df1252011-09-21 06:42:26 +00003184 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003185 SourceLocation RHS = getLoc(R);
3186 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3187 }
3188
3189 SourceLocation getLoc(const PPEntityOffset &PPE) const {
3190 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
3191 }
3192};
3193
3194}
3195
3196/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
3197PreprocessedEntityID
3198ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
3199 if (SourceMgr.isLocalSourceLocation(BLoc))
3200 return getTotalNumPreprocessedEntities();
3201
3202 GlobalSLocOffsetMapType::const_iterator
3203 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3204 BLoc.getOffset());
3205 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3206 "Corrupted global sloc offset map");
3207
3208 if (SLocMapI->second->NumPreprocessedEntities == 0)
3209 return findNextPreprocessedEntity(SLocMapI);
3210
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003211 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003212 typedef const PPEntityOffset *pp_iterator;
3213 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3214 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
Argyrios Kyrtzidis4cd06342011-09-22 21:17:02 +00003215
3216 size_t Count = M.NumPreprocessedEntities;
3217 size_t Half;
3218 pp_iterator First = pp_begin;
3219 pp_iterator PPI;
3220
3221 // Do a binary search manually instead of using std::lower_bound because
3222 // The end locations of entities may be unordered (when a macro expansion
3223 // is inside another macro argument), but for this case it is not important
3224 // whether we get the first macro expansion or its containing macro.
3225 while (Count > 0) {
3226 Half = Count/2;
3227 PPI = First;
3228 std::advance(PPI, Half);
3229 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
3230 BLoc)){
3231 First = PPI;
3232 ++First;
3233 Count = Count - Half - 1;
3234 } else
3235 Count = Half;
3236 }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003237
3238 if (PPI == pp_end)
3239 return findNextPreprocessedEntity(SLocMapI);
3240
3241 return getGlobalPreprocessedEntityID(M,
3242 M.BasePreprocessedEntityID + (PPI - pp_begin));
3243}
3244
3245/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
3246PreprocessedEntityID
3247ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
3248 if (SourceMgr.isLocalSourceLocation(ELoc))
3249 return getTotalNumPreprocessedEntities();
3250
3251 GlobalSLocOffsetMapType::const_iterator
3252 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3253 ELoc.getOffset());
3254 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3255 "Corrupted global sloc offset map");
3256
3257 if (SLocMapI->second->NumPreprocessedEntities == 0)
3258 return findNextPreprocessedEntity(SLocMapI);
3259
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003260 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003261 typedef const PPEntityOffset *pp_iterator;
3262 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3263 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
3264 pp_iterator PPI =
3265 std::upper_bound(pp_begin, pp_end, ELoc,
3266 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
3267
3268 if (PPI == pp_end)
3269 return findNextPreprocessedEntity(SLocMapI);
3270
3271 return getGlobalPreprocessedEntityID(M,
3272 M.BasePreprocessedEntityID + (PPI - pp_begin));
3273}
3274
3275/// \brief Returns a pair of [Begin, End) indices of preallocated
3276/// preprocessed entities that \arg Range encompasses.
3277std::pair<unsigned, unsigned>
3278 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
3279 if (Range.isInvalid())
3280 return std::make_pair(0,0);
3281 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
3282
3283 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
3284 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
3285 return std::make_pair(BeginID, EndID);
3286}
3287
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003288/// \brief Optionally returns true or false if the preallocated preprocessed
3289/// entity with index \arg Index came from file \arg FID.
3290llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
3291 FileID FID) {
3292 if (FID.isInvalid())
3293 return false;
3294
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003295 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3296 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003297 unsigned LocalIndex = PPInfo.second;
3298 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
3299
3300 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
3301 if (Loc.isInvalid())
3302 return false;
3303
3304 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
3305 return true;
3306 else
3307 return false;
3308}
3309
Douglas Gregord10a3812011-08-25 18:14:34 +00003310namespace {
3311 /// \brief Visitor used to search for information about a header file.
3312 class HeaderFileInfoVisitor {
3313 ASTReader &Reader;
3314 const FileEntry *FE;
3315
3316 llvm::Optional<HeaderFileInfo> HFI;
3317
3318 public:
3319 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
3320 : Reader(Reader), FE(FE) { }
3321
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003322 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003323 HeaderFileInfoVisitor *This
3324 = static_cast<HeaderFileInfoVisitor *>(UserData);
3325
3326 HeaderFileInfoTrait Trait(This->Reader, M,
3327 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
3328 M.HeaderFileFrameworkStrings,
3329 This->FE->getName());
3330
3331 HeaderFileInfoLookupTable *Table
3332 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
3333 if (!Table)
3334 return false;
3335
3336 // Look in the on-disk hash table for an entry for this file name.
3337 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
3338 &Trait);
3339 if (Pos == Table->end())
3340 return false;
3341
3342 This->HFI = *Pos;
3343 return true;
3344 }
3345
3346 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
3347 };
3348}
3349
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003350HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003351 HeaderFileInfoVisitor Visitor(*this, FE);
3352 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
3353 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003354 if (Listener)
Douglas Gregord10a3812011-08-25 18:14:34 +00003355 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
3356 return *HFI;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003357 }
3358
3359 return HeaderFileInfo();
3360}
3361
David Blaikied6471f72011-09-25 23:23:43 +00003362void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00003363 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003364 ModuleFile &F = *(*I);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003365 unsigned Idx = 0;
3366 while (Idx < F.PragmaDiagMappings.size()) {
3367 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00003368 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
3369 Diag.DiagStatePoints.push_back(
3370 DiagnosticsEngine::DiagStatePoint(&Diag.DiagStates.back(),
3371 FullSourceLoc(Loc, SourceMgr)));
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003372 while (1) {
3373 assert(Idx < F.PragmaDiagMappings.size() &&
3374 "Invalid data, didn't find '-1' marking end of diag/map pairs");
3375 if (Idx >= F.PragmaDiagMappings.size()) {
3376 break; // Something is messed up but at least avoid infinite loop in
3377 // release build.
3378 }
3379 unsigned DiagID = F.PragmaDiagMappings[Idx++];
3380 if (DiagID == (unsigned)-1) {
3381 break; // no more diag/map pairs for this location.
3382 }
3383 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00003384 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
3385 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003386 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003387 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00003388 }
3389}
3390
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003391/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003392ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregora119da02011-08-02 16:26:37 +00003393 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turnere9b76c12011-07-20 21:31:32 +00003394 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003395 ModuleFile *M = I->second;
Douglas Gregore3605012011-08-02 18:32:54 +00003396 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003397}
3398
3399/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00003400///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003401/// The index is the type ID, shifted and minus the number of predefs. This
3402/// routine actually reads the record corresponding to the type at the given
3403/// location. It is a helper routine for GetType, which deals with reading type
3404/// IDs.
Douglas Gregor393f2492011-07-22 00:38:23 +00003405QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003406 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00003407 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00003408
Douglas Gregor0b748912009-04-14 21:18:50 +00003409 // Keep track of where we are in the stream, then jump back there
3410 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003411 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00003412
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003413 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00003414
Douglas Gregord89275b2009-07-06 18:54:52 +00003415 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003416 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00003417
Douglas Gregor393f2492011-07-22 00:38:23 +00003418 unsigned Idx = 0;
Sebastian Redlc3632732010-10-05 15:59:54 +00003419 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003420 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003421 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003422 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3423 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003424 if (Record.size() != 2) {
3425 Error("Incorrect encoding of extended qualifier type");
3426 return QualType();
3427 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003428 QualType Base = readType(*Loc.F, Record, Idx);
3429 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
Douglas Gregor35942772011-09-09 21:34:22 +00003430 return Context.getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00003431 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003432
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003433 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003434 if (Record.size() != 1) {
3435 Error("Incorrect encoding of complex type");
3436 return QualType();
3437 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003438 QualType ElemType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003439 return Context.getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003440 }
3441
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003442 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003443 if (Record.size() != 1) {
3444 Error("Incorrect encoding of pointer type");
3445 return QualType();
3446 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003447 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003448 return Context.getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003449 }
3450
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003451 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003452 if (Record.size() != 1) {
3453 Error("Incorrect encoding of block pointer type");
3454 return QualType();
3455 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003456 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003457 return Context.getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003458 }
3459
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003460 case TYPE_LVALUE_REFERENCE: {
Richard Smithdf1550f2011-04-12 10:38:03 +00003461 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003462 Error("Incorrect encoding of lvalue reference type");
3463 return QualType();
3464 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003465 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003466 return Context.getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003467 }
3468
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003469 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003470 if (Record.size() != 1) {
3471 Error("Incorrect encoding of rvalue reference type");
3472 return QualType();
3473 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003474 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003475 return Context.getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003476 }
3477
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003478 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00003479 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003480 Error("Incorrect encoding of member pointer type");
3481 return QualType();
3482 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003483 QualType PointeeType = readType(*Loc.F, Record, Idx);
3484 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00003485 if (PointeeType.isNull() || ClassType.isNull())
3486 return QualType();
3487
Douglas Gregor35942772011-09-09 21:34:22 +00003488 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003489 }
3490
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003491 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003492 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003493 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3494 unsigned IndexTypeQuals = Record[2];
3495 unsigned Idx = 3;
3496 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003497 return Context.getConstantArrayType(ElementType, Size,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003498 ASM, IndexTypeQuals);
3499 }
3500
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003501 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003502 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003503 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3504 unsigned IndexTypeQuals = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003505 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003506 }
3507
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003508 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003509 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00003510 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3511 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00003512 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3513 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
Douglas Gregor35942772011-09-09 21:34:22 +00003514 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003515 ASM, IndexTypeQuals,
3516 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003517 }
3518
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003519 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003520 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003521 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003522 return QualType();
3523 }
3524
Douglas Gregor393f2492011-07-22 00:38:23 +00003525 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003526 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00003527 unsigned VecKind = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003528 return Context.getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00003529 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003530 }
3531
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003532 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003533 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003534 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003535 return QualType();
3536 }
3537
Douglas Gregor393f2492011-07-22 00:38:23 +00003538 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003539 unsigned NumElements = Record[1];
Douglas Gregor35942772011-09-09 21:34:22 +00003540 return Context.getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003541 }
3542
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003543 case TYPE_FUNCTION_NO_PROTO: {
John McCallf85e1932011-06-15 23:02:42 +00003544 if (Record.size() != 6) {
Douglas Gregora02b1472009-04-28 21:53:25 +00003545 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003546 return QualType();
3547 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003548 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCallf85e1932011-06-15 23:02:42 +00003549 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
3550 (CallingConv)Record[4], Record[5]);
Douglas Gregor35942772011-09-09 21:34:22 +00003551 return Context.getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003552 }
3553
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003554 case TYPE_FUNCTION_PROTO: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003555 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalle23cf432010-12-14 08:05:40 +00003556
3557 FunctionProtoType::ExtProtoInfo EPI;
3558 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmana49218e2011-04-09 08:18:08 +00003559 /*hasregparm*/ Record[2],
3560 /*regparm*/ Record[3],
John McCallf85e1932011-06-15 23:02:42 +00003561 static_cast<CallingConv>(Record[4]),
3562 /*produces*/ Record[5]);
John McCalle23cf432010-12-14 08:05:40 +00003563
John McCallf85e1932011-06-15 23:02:42 +00003564 unsigned Idx = 6;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003565 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003566 SmallVector<QualType, 16> ParamTypes;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003567 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00003568 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalle23cf432010-12-14 08:05:40 +00003569
3570 EPI.Variadic = Record[Idx++];
3571 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00003572 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redl60618fa2011-03-12 11:50:43 +00003573 ExceptionSpecificationType EST =
3574 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3575 EPI.ExceptionSpecType = EST;
3576 if (EST == EST_Dynamic) {
3577 EPI.NumExceptions = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003578 SmallVector<QualType, 2> Exceptions;
Sebastian Redl60618fa2011-03-12 11:50:43 +00003579 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00003580 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redl60618fa2011-03-12 11:50:43 +00003581 EPI.Exceptions = Exceptions.data();
3582 } else if (EST == EST_ComputedNoexcept) {
3583 EPI.NoexceptExpr = ReadExpr(*Loc.F);
3584 }
Douglas Gregor35942772011-09-09 21:34:22 +00003585 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00003586 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003587 }
3588
Douglas Gregor409448c2011-07-21 22:35:25 +00003589 case TYPE_UNRESOLVED_USING: {
3590 unsigned Idx = 0;
Douglas Gregor35942772011-09-09 21:34:22 +00003591 return Context.getTypeDeclType(
Douglas Gregor409448c2011-07-21 22:35:25 +00003592 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
3593 }
3594
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003595 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003596 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003597 Error("incorrect encoding of typedef type");
3598 return QualType();
3599 }
Douglas Gregor409448c2011-07-21 22:35:25 +00003600 unsigned Idx = 0;
3601 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003602 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003603 if (!Canonical.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00003604 Canonical = Context.getCanonicalType(Canonical);
3605 return Context.getTypedefType(Decl, Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003606 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003607
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003608 case TYPE_TYPEOF_EXPR:
Douglas Gregor35942772011-09-09 21:34:22 +00003609 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003610
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003611 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003612 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003613 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003614 return QualType();
3615 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003616 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003617 return Context.getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003618 }
Mike Stump1eb44332009-09-09 15:08:12 +00003619
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003620 case TYPE_DECLTYPE:
Douglas Gregor35942772011-09-09 21:34:22 +00003621 return Context.getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson395b4752009-06-24 19:06:50 +00003622
Sean Huntca63c202011-05-24 22:41:36 +00003623 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003624 QualType BaseType = readType(*Loc.F, Record, Idx);
3625 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Sean Huntca63c202011-05-24 22:41:36 +00003626 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003627 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
Sean Huntca63c202011-05-24 22:41:36 +00003628 }
3629
Richard Smith34b41d92011-02-20 03:19:35 +00003630 case TYPE_AUTO:
Douglas Gregor35942772011-09-09 21:34:22 +00003631 return Context.getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith34b41d92011-02-20 03:19:35 +00003632
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003633 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003634 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003635 Error("incorrect encoding of record type");
3636 return QualType();
3637 }
Douglas Gregor409448c2011-07-21 22:35:25 +00003638 unsigned Idx = 0;
3639 bool IsDependent = Record[Idx++];
3640 QualType T
Douglas Gregor35942772011-09-09 21:34:22 +00003641 = Context.getRecordType(ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00003642 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003643 return T;
3644 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003645
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003646 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003647 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003648 Error("incorrect encoding of enum type");
3649 return QualType();
3650 }
Douglas Gregor409448c2011-07-21 22:35:25 +00003651 unsigned Idx = 0;
3652 bool IsDependent = Record[Idx++];
3653 QualType T
Douglas Gregor35942772011-09-09 21:34:22 +00003654 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00003655 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003656 return T;
3657 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003658
John McCall9d156a72011-01-06 01:58:22 +00003659 case TYPE_ATTRIBUTED: {
3660 if (Record.size() != 3) {
3661 Error("incorrect encoding of attributed type");
3662 return QualType();
3663 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003664 QualType modifiedType = readType(*Loc.F, Record, Idx);
3665 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall9d156a72011-01-06 01:58:22 +00003666 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
Douglas Gregor35942772011-09-09 21:34:22 +00003667 return Context.getAttributedType(kind, modifiedType, equivalentType);
John McCall9d156a72011-01-06 01:58:22 +00003668 }
3669
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003670 case TYPE_PAREN: {
3671 if (Record.size() != 1) {
3672 Error("incorrect encoding of paren type");
3673 return QualType();
3674 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003675 QualType InnerType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003676 return Context.getParenType(InnerType);
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003677 }
3678
Douglas Gregor7536dd52010-12-20 02:24:11 +00003679 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00003680 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00003681 Error("incorrect encoding of pack expansion type");
3682 return QualType();
3683 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003684 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregor7536dd52010-12-20 02:24:11 +00003685 if (Pattern.isNull())
3686 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00003687 llvm::Optional<unsigned> NumExpansions;
3688 if (Record[1])
3689 NumExpansions = Record[1] - 1;
Douglas Gregor35942772011-09-09 21:34:22 +00003690 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00003691 }
3692
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003693 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003694 unsigned Idx = 0;
3695 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00003696 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003697 QualType NamedType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003698 return Context.getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00003699 }
3700
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003701 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003702 unsigned Idx = 0;
Douglas Gregor409448c2011-07-21 22:35:25 +00003703 ObjCInterfaceDecl *ItfD
3704 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003705 return Context.getObjCInterfaceType(ItfD);
John McCallc12c5bb2010-05-15 11:32:37 +00003706 }
3707
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003708 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00003709 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00003710 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003711 unsigned NumProtos = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003712 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003713 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor409448c2011-07-21 22:35:25 +00003714 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00003715 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003716 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003717
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003718 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003719 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00003720 QualType Pointee = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003721 return Context.getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003722 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00003723
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003724 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00003725 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00003726 QualType Parm = readType(*Loc.F, Record, Idx);
3727 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCall49a832b2009-10-18 09:09:24 +00003728 return
Douglas Gregor35942772011-09-09 21:34:22 +00003729 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
John McCall49a832b2009-10-18 09:09:24 +00003730 Replacement);
3731 }
John McCall3cb0ebd2010-03-10 03:28:59 +00003732
Douglas Gregorc3069d62011-01-14 02:55:32 +00003733 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3734 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00003735 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorc3069d62011-01-14 02:55:32 +00003736 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003737 return Context.getSubstTemplateTypeParmPackType(
Douglas Gregorc3069d62011-01-14 02:55:32 +00003738 cast<TemplateTypeParmType>(Parm),
3739 ArgPack);
3740 }
3741
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003742 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor409448c2011-07-21 22:35:25 +00003743 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003744 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003745 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003746 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003747 return
Douglas Gregor35942772011-09-09 21:34:22 +00003748 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00003749 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003750
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003751 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003752 unsigned Idx = 0;
3753 unsigned Depth = Record[Idx++];
3754 unsigned Index = Record[Idx++];
3755 bool Pack = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00003756 TemplateTypeParmDecl *D
3757 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003758 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003759 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003760
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003761 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003762 unsigned Idx = 0;
3763 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00003764 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00003765 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003766 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003767 if (!Canon.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00003768 Canon = Context.getCanonicalType(Canon);
3769 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003770 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003771
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003772 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003773 unsigned Idx = 0;
3774 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00003775 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00003776 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003777 unsigned NumArgs = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003778 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003779 Args.reserve(NumArgs);
3780 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00003781 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00003782 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003783 Args.size(), Args.data());
3784 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003785
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003786 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003787 unsigned Idx = 0;
3788
3789 // ArrayType
Douglas Gregor393f2492011-07-22 00:38:23 +00003790 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003791 ArrayType::ArraySizeModifier ASM
3792 = (ArrayType::ArraySizeModifier)Record[Idx++];
3793 unsigned IndexTypeQuals = Record[Idx++];
3794
3795 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00003796 Expr *NumElts = ReadExpr(*Loc.F);
3797 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003798
Douglas Gregor35942772011-09-09 21:34:22 +00003799 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003800 IndexTypeQuals, Brackets);
3801 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003802
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003803 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003804 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003805 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003806 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner5f9e2722011-07-23 10:55:15 +00003807 SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00003808 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003809 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003810 QualType T;
Richard Smith3e4c6c42011-05-05 21:57:07 +00003811 if (Underlying.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00003812 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003813 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003814 else
Douglas Gregor35942772011-09-09 21:34:22 +00003815 T = Context.getTemplateSpecializationType(Name, Args.data(),
Richard Smith3e4c6c42011-05-05 21:57:07 +00003816 Args.size(), Underlying);
John McCallf4c73712011-01-19 06:33:43 +00003817 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003818 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003819 }
Eli Friedmanb001de72011-10-06 23:00:33 +00003820
3821 case TYPE_ATOMIC: {
3822 if (Record.size() != 1) {
3823 Error("Incorrect encoding of atomic type");
3824 return QualType();
3825 }
3826 QualType ValueType = readType(*Loc.F, Record, Idx);
3827 return Context.getAtomicType(ValueType);
3828 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003829 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003830 // Suppress a GCC warning
3831 return QualType();
3832}
3833
Sebastian Redlc3632732010-10-05 15:59:54 +00003834class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003835 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003836 ModuleFile &F;
Sebastian Redl577d4792010-07-22 22:43:28 +00003837 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003838 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00003839 unsigned &Idx;
3840
Sebastian Redlc3632732010-10-05 15:59:54 +00003841 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3842 unsigned &I) {
3843 return Reader.ReadSourceLocation(F, R, I);
3844 }
3845
Douglas Gregor409448c2011-07-21 22:35:25 +00003846 template<typename T>
3847 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
3848 return Reader.ReadDeclAs<T>(F, Record, Idx);
3849 }
3850
John McCalla1ee0c52009-10-16 21:56:05 +00003851public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003852 TypeLocReader(ASTReader &Reader, ModuleFile &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003853 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00003854 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3855 { }
John McCalla1ee0c52009-10-16 21:56:05 +00003856
John McCall51bd8032009-10-18 01:05:36 +00003857 // We want compile-time assurance that we've enumerated all of
3858 // these, so unfortunately we have to declare them first, then
3859 // define them out-of-line.
3860#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00003861#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00003862 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003863#include "clang/AST/TypeLocNodes.def"
3864
John McCall51bd8032009-10-18 01:05:36 +00003865 void VisitFunctionTypeLoc(FunctionTypeLoc);
3866 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003867};
3868
John McCall51bd8032009-10-18 01:05:36 +00003869void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00003870 // nothing to do
3871}
John McCall51bd8032009-10-18 01:05:36 +00003872void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003873 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00003874 if (TL.needsExtraLocalData()) {
3875 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3876 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3877 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3878 TL.setModeAttr(Record[Idx++]);
3879 }
John McCalla1ee0c52009-10-16 21:56:05 +00003880}
John McCall51bd8032009-10-18 01:05:36 +00003881void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003882 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003883}
John McCall51bd8032009-10-18 01:05:36 +00003884void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003885 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003886}
John McCall51bd8032009-10-18 01:05:36 +00003887void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003888 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003889}
John McCall51bd8032009-10-18 01:05:36 +00003890void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003891 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003892}
John McCall51bd8032009-10-18 01:05:36 +00003893void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003894 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003895}
John McCall51bd8032009-10-18 01:05:36 +00003896void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003897 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00003898 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003899}
John McCall51bd8032009-10-18 01:05:36 +00003900void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003901 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3902 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003903 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00003904 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003905 else
John McCall51bd8032009-10-18 01:05:36 +00003906 TL.setSizeExpr(0);
3907}
3908void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3909 VisitArrayTypeLoc(TL);
3910}
3911void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3912 VisitArrayTypeLoc(TL);
3913}
3914void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3915 VisitArrayTypeLoc(TL);
3916}
3917void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3918 DependentSizedArrayTypeLoc TL) {
3919 VisitArrayTypeLoc(TL);
3920}
3921void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3922 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003923 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003924}
3925void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003926 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003927}
3928void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003929 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003930}
3931void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +00003932 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
3933 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Douglas Gregordab60ad2010-10-01 18:44:50 +00003934 TL.setTrailingReturn(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00003935 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor409448c2011-07-21 22:35:25 +00003936 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003937 }
3938}
3939void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3940 VisitFunctionTypeLoc(TL);
3941}
3942void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3943 VisitFunctionTypeLoc(TL);
3944}
John McCalled976492009-12-04 22:46:56 +00003945void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003946 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00003947}
John McCall51bd8032009-10-18 01:05:36 +00003948void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003949 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003950}
3951void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003952 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3953 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3954 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003955}
3956void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003957 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3958 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3959 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3960 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003961}
3962void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003963 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003964}
Sean Huntca63c202011-05-24 22:41:36 +00003965void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3966 TL.setKWLoc(ReadSourceLocation(Record, Idx));
3967 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3968 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3969 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3970}
Richard Smith34b41d92011-02-20 03:19:35 +00003971void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3972 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3973}
John McCall51bd8032009-10-18 01:05:36 +00003974void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003975 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003976}
3977void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003978 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003979}
John McCall9d156a72011-01-06 01:58:22 +00003980void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3981 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3982 if (TL.hasAttrOperand()) {
3983 SourceRange range;
3984 range.setBegin(ReadSourceLocation(Record, Idx));
3985 range.setEnd(ReadSourceLocation(Record, Idx));
3986 TL.setAttrOperandParensRange(range);
3987 }
3988 if (TL.hasAttrExprOperand()) {
3989 if (Record[Idx++])
3990 TL.setAttrExprOperand(Reader.ReadExpr(F));
3991 else
3992 TL.setAttrExprOperand(0);
3993 } else if (TL.hasAttrEnumOperand())
3994 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3995}
John McCall51bd8032009-10-18 01:05:36 +00003996void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003997 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003998}
John McCall49a832b2009-10-18 09:09:24 +00003999void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4000 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004001 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00004002}
Douglas Gregorc3069d62011-01-14 02:55:32 +00004003void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
4004 SubstTemplateTypeParmPackTypeLoc TL) {
4005 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4006}
John McCall51bd8032009-10-18 01:05:36 +00004007void TypeLocReader::VisitTemplateSpecializationTypeLoc(
4008 TemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004009 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4010 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4011 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00004012 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
4013 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00004014 Reader.GetTemplateArgumentLocInfo(F,
4015 TL.getTypePtr()->getArg(i).getKind(),
4016 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004017}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004018void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
4019 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4020 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4021}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004022void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004023 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor9e876872011-03-01 18:12:44 +00004024 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004025}
John McCall3cb0ebd2010-03-10 03:28:59 +00004026void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004027 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00004028}
Douglas Gregor4714c122010-03-31 17:34:00 +00004029void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004030 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor2494dd02011-03-01 01:34:45 +00004031 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004032 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004033}
John McCall33500952010-06-11 00:33:02 +00004034void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
4035 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004036 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004037 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004038 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4039 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4040 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004041 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
4042 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00004043 Reader.GetTemplateArgumentLocInfo(F,
4044 TL.getTypePtr()->getArg(I).getKind(),
4045 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004046}
Douglas Gregor7536dd52010-12-20 02:24:11 +00004047void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
4048 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
4049}
John McCall51bd8032009-10-18 01:05:36 +00004050void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004051 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00004052}
4053void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
4054 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004055 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4056 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004057 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00004058 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004059}
John McCall54e14c42009-10-22 22:37:11 +00004060void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004061 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00004062}
Eli Friedmanb001de72011-10-06 23:00:33 +00004063void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
4064 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4065 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4066 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4067}
John McCalla1ee0c52009-10-16 21:56:05 +00004068
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004069TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004070 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00004071 unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004072 QualType InfoTy = readType(F, Record, Idx);
John McCalla1ee0c52009-10-16 21:56:05 +00004073 if (InfoTy.isNull())
4074 return 0;
4075
Douglas Gregor35942772011-09-09 21:34:22 +00004076 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00004077 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00004078 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00004079 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00004080 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00004081}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004082
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004083QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00004084 unsigned FastQuals = ID & Qualifiers::FastMask;
4085 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004086
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004087 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004088 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004089 switch ((PredefinedTypeIDs)Index) {
4090 case PREDEF_TYPE_NULL_ID: return QualType();
Douglas Gregor35942772011-09-09 21:34:22 +00004091 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
4092 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004093
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004094 case PREDEF_TYPE_CHAR_U_ID:
4095 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00004096 // FIXME: Check that the signedness of CharTy is correct!
Douglas Gregor35942772011-09-09 21:34:22 +00004097 T = Context.CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004098 break;
4099
Douglas Gregor35942772011-09-09 21:34:22 +00004100 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
4101 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
4102 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
4103 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
4104 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
4105 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
4106 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
4107 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
4108 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
4109 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
4110 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
4111 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
4112 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00004113 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004114 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
4115 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
4116 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
4117 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
4118 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
John McCall3c3b7f92011-10-25 17:37:35 +00004119 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004120 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
4121 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
4122 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
4123 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
4124 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
4125 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
4126 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
4127 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
4128 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004129
4130 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
Douglas Gregor35942772011-09-09 21:34:22 +00004131 T = Context.getAutoRRefDeductType();
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004132 break;
John McCall0ddaeb92011-10-17 18:09:15 +00004133
4134 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
4135 T = Context.ARCUnbridgedCastTy;
4136 break;
4137
Douglas Gregor2cf26342009-04-09 22:27:44 +00004138 }
4139
4140 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00004141 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004142 }
4143
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004144 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004145 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00004146 if (TypesLoaded[Index].isNull()) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004147 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00004148 if (TypesLoaded[Index].isNull())
4149 return QualType();
4150
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004151 TypesLoaded[Index]->setFromAST();
Sebastian Redl30c514c2010-07-14 23:45:08 +00004152 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004153 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00004154 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00004155 }
Mike Stump1eb44332009-09-09 15:08:12 +00004156
John McCall0953e762009-09-24 19:53:00 +00004157 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004158}
4159
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004160QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004161 return GetType(getGlobalTypeID(F, LocalID));
4162}
4163
4164serialization::TypeID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004165ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
Douglas Gregora119da02011-08-02 16:26:37 +00004166 unsigned FastQuals = LocalID & Qualifiers::FastMask;
4167 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
4168
4169 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
4170 return LocalID;
4171
4172 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4173 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
4174 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
4175
4176 unsigned GlobalIndex = LocalIndex + I->second;
4177 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
4178}
4179
John McCall833ca992009-10-29 08:12:44 +00004180TemplateArgumentLocInfo
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004181ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00004182 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00004183 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004184 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00004185 switch (Kind) {
4186 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004187 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00004188 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00004189 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00004190 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004191 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4192 Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00004193 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004194 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregora7fc9012011-01-05 18:58:31 +00004195 SourceLocation());
4196 }
4197 case TemplateArgument::TemplateExpansion: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004198 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4199 Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004200 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004201 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004202 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregorba68eca2011-01-05 17:40:24 +00004203 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00004204 }
John McCall833ca992009-10-29 08:12:44 +00004205 case TemplateArgument::Null:
4206 case TemplateArgument::Integral:
4207 case TemplateArgument::Declaration:
4208 case TemplateArgument::Pack:
4209 return TemplateArgumentLocInfo();
4210 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00004211 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00004212 return TemplateArgumentLocInfo();
4213}
4214
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004215TemplateArgumentLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004216ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004217 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004218 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004219
4220 if (Arg.getKind() == TemplateArgument::Expression) {
4221 if (Record[Index++]) // bool InfoHasSameExpr.
4222 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4223 }
Sebastian Redlc3632732010-10-05 15:59:54 +00004224 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004225 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004226}
4227
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004228Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00004229 return GetDecl(ID);
4230}
4231
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004232uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
Douglas Gregore92b8a12011-08-04 00:01:48 +00004233 unsigned &Idx){
4234 if (Idx >= Record.size())
Douglas Gregor7c789c12010-10-29 22:39:52 +00004235 return 0;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004236
Douglas Gregore92b8a12011-08-04 00:01:48 +00004237 unsigned LocalID = Record[Idx++];
4238 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004239}
4240
4241CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004242 RecordLocation Loc = getLocalBitOffset(Offset);
4243 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004244 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004245 Cursor.JumpToBit(Loc.Offset);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004246 ReadingKindTracker ReadingKind(Read_Decl, *this);
4247 RecordData Record;
4248 unsigned Code = Cursor.ReadCode();
4249 unsigned RecCode = Cursor.ReadRecord(Code, Record);
4250 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4251 Error("Malformed AST file: missing C++ base specifiers");
4252 return 0;
4253 }
4254
4255 unsigned Idx = 0;
4256 unsigned NumBases = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00004257 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004258 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4259 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004260 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004261 return Bases;
4262}
4263
Douglas Gregor409448c2011-07-21 22:35:25 +00004264serialization::DeclID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004265ASTReader::getGlobalDeclID(ModuleFile &F, unsigned LocalID) const {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004266 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregor496c7092011-08-03 15:48:04 +00004267 return LocalID;
4268
4269 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004270 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregor496c7092011-08-03 15:48:04 +00004271 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
4272
4273 return LocalID + I->second;
Douglas Gregor409448c2011-07-21 22:35:25 +00004274}
4275
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004276bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004277 ModuleFile &M) const {
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004278 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
4279 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4280 return &M == I->second;
4281}
4282
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004283SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
4284 if (ID < NUM_PREDEF_DECL_IDS)
4285 return SourceLocation();
4286
4287 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4288
4289 if (Index > DeclsLoaded.size()) {
4290 Error("declaration ID out-of-range for AST file");
4291 return SourceLocation();
4292 }
4293
4294 if (Decl *D = DeclsLoaded[Index])
4295 return D->getLocation();
4296
4297 unsigned RawLocation = 0;
4298 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
4299 return ReadSourceLocation(*Rec.F, RawLocation);
4300}
4301
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004302Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004303 if (ID < NUM_PREDEF_DECL_IDS) {
4304 switch ((PredefinedDeclIDs)ID) {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004305 case PREDEF_DECL_NULL_ID:
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004306 return 0;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004307
4308 case PREDEF_DECL_TRANSLATION_UNIT_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004309 return Context.getTranslationUnitDecl();
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00004310
4311 case PREDEF_DECL_OBJC_ID_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004312 return Context.getObjCIdDecl();
Douglas Gregor79d67262011-08-12 05:59:41 +00004313
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004314 case PREDEF_DECL_OBJC_SEL_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004315 return Context.getObjCSelDecl();
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004316
Douglas Gregor79d67262011-08-12 05:59:41 +00004317 case PREDEF_DECL_OBJC_CLASS_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004318 return Context.getObjCClassDecl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004319
4320 case PREDEF_DECL_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004321 return Context.getInt128Decl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004322
4323 case PREDEF_DECL_UNSIGNED_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004324 return Context.getUInt128Decl();
Douglas Gregore97179c2011-09-08 01:46:34 +00004325
4326 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004327 return Context.getObjCInstanceTypeDecl();
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004328 }
4329
Douglas Gregor2cf26342009-04-09 22:27:44 +00004330 return 0;
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004331 }
4332
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004333 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4334
4335 if (Index > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004336 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004337 return 0;
4338 }
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004339
4340if (!DeclsLoaded[Index]) {
Douglas Gregor496c7092011-08-03 15:48:04 +00004341 ReadDeclRecord(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00004342 if (DeserializationListener)
4343 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
4344 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004345
4346 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004347}
4348
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004349serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00004350 const RecordData &Record,
4351 unsigned &Idx) {
4352 if (Idx >= Record.size()) {
4353 Error("Corrupted AST file");
4354 return 0;
4355 }
4356
4357 return getGlobalDeclID(F, Record[Idx++]);
4358}
4359
Chris Lattner887e2b32009-04-27 05:46:25 +00004360/// \brief Resolve the offset of a statement into a statement.
4361///
4362/// This operation will read a new statement from the external
4363/// source each time it is called, and is meant to be used via a
4364/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004365Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004366 // Switch case IDs are per Decl.
4367 ClearSwitchCaseIDs();
4368
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004369 // Offset here is a global offset across the entire chain.
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004370 RecordLocation Loc = getLocalBitOffset(Offset);
4371 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
4372 return ReadStmtFromStream(*Loc.F);
Douglas Gregor250fc9c2009-04-18 00:07:54 +00004373}
4374
Douglas Gregor851c75a2011-08-24 21:27:34 +00004375namespace {
4376 class FindExternalLexicalDeclsVisitor {
4377 ASTReader &Reader;
4378 const DeclContext *DC;
4379 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004380
Douglas Gregor851c75a2011-08-24 21:27:34 +00004381 SmallVectorImpl<Decl*> &Decls;
4382 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
4383
4384 public:
4385 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
4386 bool (*isKindWeWant)(Decl::Kind),
4387 SmallVectorImpl<Decl*> &Decls)
4388 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
4389 {
4390 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
4391 PredefsVisited[I] = false;
4392 }
4393
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004394 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
Douglas Gregor851c75a2011-08-24 21:27:34 +00004395 if (Preorder)
4396 return false;
4397
4398 FindExternalLexicalDeclsVisitor *This
4399 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
4400
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004401 ModuleFile::DeclContextInfosMap::iterator Info
Douglas Gregor851c75a2011-08-24 21:27:34 +00004402 = M.DeclContextInfos.find(This->DC);
4403 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
4404 return false;
4405
4406 // Load all of the declaration IDs
4407 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
4408 *IDE = ID + Info->second.NumLexicalDecls;
4409 ID != IDE; ++ID) {
4410 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
4411 continue;
4412
4413 // Don't add predefined declarations to the lexical context more
4414 // than once.
4415 if (ID->second < NUM_PREDEF_DECL_IDS) {
4416 if (This->PredefsVisited[ID->second])
4417 continue;
4418
4419 This->PredefsVisited[ID->second] = true;
4420 }
4421
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004422 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
4423 if (!This->DC->isDeclInLexicalTraversal(D))
4424 This->Decls.push_back(D);
4425 }
Douglas Gregor851c75a2011-08-24 21:27:34 +00004426 }
4427
4428 return false;
4429 }
4430 };
4431}
4432
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004433ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00004434 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner5f9e2722011-07-23 10:55:15 +00004435 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00004436 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor851c75a2011-08-24 21:27:34 +00004437 // least. Walk all of the modules in the order they were loaded.
4438 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
4439 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00004440 ++NumLexicalDeclContextsRead;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004441 return ELR_Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004442}
4443
Douglas Gregor0d95f772011-08-24 19:03:07 +00004444namespace {
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004445
4446class DeclIDComp {
4447 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004448 ModuleFile &Mod;
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004449
4450public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004451 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004452
4453 bool operator()(LocalDeclID L, LocalDeclID R) const {
4454 SourceLocation LHS = getLocation(L);
4455 SourceLocation RHS = getLocation(R);
4456 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4457 }
4458
4459 bool operator()(SourceLocation LHS, LocalDeclID R) const {
4460 SourceLocation RHS = getLocation(R);
4461 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4462 }
4463
4464 bool operator()(LocalDeclID L, SourceLocation RHS) const {
4465 SourceLocation LHS = getLocation(L);
4466 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4467 }
4468
4469 SourceLocation getLocation(LocalDeclID ID) const {
4470 return Reader.getSourceManager().getFileLoc(
4471 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
4472 }
4473};
4474
4475}
4476
4477void ASTReader::FindFileRegionDecls(FileID File,
4478 unsigned Offset, unsigned Length,
4479 SmallVectorImpl<Decl *> &Decls) {
4480 SourceManager &SM = getSourceManager();
4481
4482 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
4483 if (I == FileDeclIDs.end())
4484 return;
4485
4486 FileDeclsInfo &DInfo = I->second;
4487 if (DInfo.Decls.empty())
4488 return;
4489
4490 SourceLocation
4491 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
4492 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
4493
4494 DeclIDComp DIDComp(*this, *DInfo.Mod);
4495 ArrayRef<serialization::LocalDeclID>::iterator
4496 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
4497 BeginLoc, DIDComp);
4498 if (BeginIt != DInfo.Decls.begin())
4499 --BeginIt;
4500
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00004501 // If we are pointing at a top-level decl inside an objc container, we need
4502 // to backtrack until we find it otherwise we will fail to report that the
4503 // region overlaps with an objc container.
4504 while (BeginIt != DInfo.Decls.begin() &&
4505 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
4506 ->isTopLevelDeclInObjCContainer())
4507 --BeginIt;
4508
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004509 ArrayRef<serialization::LocalDeclID>::iterator
4510 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
4511 EndLoc, DIDComp);
4512 if (EndIt != DInfo.Decls.end())
4513 ++EndIt;
4514
4515 for (ArrayRef<serialization::LocalDeclID>::iterator
4516 DIt = BeginIt; DIt != EndIt; ++DIt)
4517 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
4518}
4519
4520namespace {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004521 /// \brief ModuleFile visitor used to perform name lookup into a
Douglas Gregor0d95f772011-08-24 19:03:07 +00004522 /// declaration context.
4523 class DeclContextNameLookupVisitor {
4524 ASTReader &Reader;
4525 const DeclContext *DC;
4526 DeclarationName Name;
4527 SmallVectorImpl<NamedDecl *> &Decls;
4528
4529 public:
4530 DeclContextNameLookupVisitor(ASTReader &Reader,
4531 const DeclContext *DC, DeclarationName Name,
4532 SmallVectorImpl<NamedDecl *> &Decls)
4533 : Reader(Reader), DC(DC), Name(Name), Decls(Decls) { }
4534
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004535 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00004536 DeclContextNameLookupVisitor *This
4537 = static_cast<DeclContextNameLookupVisitor *>(UserData);
4538
4539 // Check whether we have any visible declaration information for
4540 // this context in this module.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004541 ModuleFile::DeclContextInfosMap::iterator Info
Douglas Gregor0d95f772011-08-24 19:03:07 +00004542 = M.DeclContextInfos.find(This->DC);
4543 if (Info == M.DeclContextInfos.end() || !Info->second.NameLookupTableData)
4544 return false;
4545
4546 // Look for this name within this module.
4547 ASTDeclContextNameLookupTable *LookupTable =
4548 (ASTDeclContextNameLookupTable*)Info->second.NameLookupTableData;
4549 ASTDeclContextNameLookupTable::iterator Pos
4550 = LookupTable->find(This->Name);
4551 if (Pos == LookupTable->end())
4552 return false;
4553
4554 bool FoundAnything = false;
4555 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
4556 for (; Data.first != Data.second; ++Data.first) {
4557 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
4558 if (!ND)
4559 continue;
4560
4561 if (ND->getDeclName() != This->Name) {
4562 assert(!This->Name.getCXXNameType().isNull() &&
4563 "Name mismatch without a type");
4564 continue;
4565 }
4566
4567 // Record this declaration.
4568 FoundAnything = true;
4569 This->Decls.push_back(ND);
4570 }
4571
4572 return FoundAnything;
4573 }
4574 };
4575}
4576
John McCall76bd1f32010-06-01 09:23:16 +00004577DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004578ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00004579 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00004580 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00004581 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00004582 if (!Name)
4583 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
4584 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004585
Chris Lattner5f9e2722011-07-23 10:55:15 +00004586 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregor0d95f772011-08-24 19:03:07 +00004587 DeclContextNameLookupVisitor Visitor(*this, DC, Name, Decls);
4588 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00004589 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00004590 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00004591 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004592}
4593
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00004594/// \brief Under non-PCH compilation the consumer receives the objc methods
4595/// before receiving the implementation, and codegen depends on this.
4596/// We simulate this by deserializing and passing to consumer the methods of the
4597/// implementation before passing the deserialized implementation decl.
4598static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
4599 ASTConsumer *Consumer) {
4600 assert(ImplD && Consumer);
4601
4602 for (ObjCImplDecl::method_iterator
4603 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
4604 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
4605
4606 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
4607}
4608
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004609void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004610 assert(Consumer);
4611 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00004612 Decl *D = InterestingDecls.front();
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004613 InterestingDecls.pop_front();
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00004614
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00004615 PassInterestingDeclToConsumer(D);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004616 }
4617}
4618
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00004619void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
4620 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
4621 PassObjCImplDeclToConsumer(ImplD, Consumer);
4622 else
4623 Consumer->HandleInterestingDecl(DeclGroupRef(D));
4624}
4625
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004626void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00004627 this->Consumer = Consumer;
4628
Douglas Gregorfdd01722009-04-14 00:24:19 +00004629 if (!Consumer)
4630 return;
4631
4632 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004633 // Force deserialization of this decl, which will cause it to be queued for
4634 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00004635 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00004636 }
Douglas Gregor1a995dd2011-09-15 18:47:32 +00004637 ExternalDefinitions.clear();
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00004638
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004639 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00004640}
4641
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004642void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004643 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00004644
Mike Stump1eb44332009-09-09 15:08:12 +00004645 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004646 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00004647 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004648 unsigned NumDeclsLoaded
4649 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
4650 (Decl *)0);
4651 unsigned NumIdentifiersLoaded
4652 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
4653 IdentifiersLoaded.end(),
4654 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00004655 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004656 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
4657 SelectorsLoaded.end(),
4658 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00004659
Douglas Gregor4fed3f42009-04-27 18:38:38 +00004660 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
4661 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor0cdd7982011-07-21 18:46:38 +00004662 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00004663 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
4664 NumSLocEntriesRead, TotalNumSLocEntries,
4665 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004666 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004667 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004668 NumTypesLoaded, (unsigned)TypesLoaded.size(),
4669 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
4670 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004671 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004672 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
4673 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004674 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004675 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004676 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
4677 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00004678 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004679 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00004680 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
4681 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00004682 if (TotalNumStatements)
4683 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
4684 NumStatementsRead, TotalNumStatements,
4685 ((float)NumStatementsRead/TotalNumStatements * 100));
4686 if (TotalNumMacros)
4687 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
4688 NumMacrosRead, TotalNumMacros,
4689 ((float)NumMacrosRead/TotalNumMacros * 100));
4690 if (TotalLexicalDeclContexts)
4691 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
4692 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4693 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4694 * 100));
4695 if (TotalVisibleDeclContexts)
4696 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
4697 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4698 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4699 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004700 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004701 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004702 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4703 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00004704 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004705 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00004706 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004707 std::fprintf(stderr, "\n");
Douglas Gregor23d7df52011-07-21 19:50:14 +00004708 dump();
4709 std::fprintf(stderr, "\n");
4710}
4711
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004712template<typename Key, typename ModuleFile, unsigned InitialCapacity>
Douglas Gregor23d7df52011-07-21 19:50:14 +00004713static void
Chris Lattner5f9e2722011-07-23 10:55:15 +00004714dumpModuleIDMap(StringRef Name,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004715 const ContinuousRangeMap<Key, ModuleFile *,
Douglas Gregor23d7df52011-07-21 19:50:14 +00004716 InitialCapacity> &Map) {
4717 if (Map.begin() == Map.end())
4718 return;
4719
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004720 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
Douglas Gregor23d7df52011-07-21 19:50:14 +00004721 llvm::errs() << Name << ":\n";
4722 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
4723 I != IEnd; ++I) {
4724 llvm::errs() << " " << I->first << " -> " << I->second->FileName
4725 << "\n";
4726 }
4727}
4728
Douglas Gregor23d7df52011-07-21 19:50:14 +00004729void ASTReader::dump() {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004730 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004731 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor23d7df52011-07-21 19:50:14 +00004732 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor1e849b62011-07-29 00:21:44 +00004733 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00004734 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00004735 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
Douglas Gregor26ced122011-12-01 00:59:36 +00004736 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00004737 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00004738 dumpModuleIDMap("Global preprocessed entity map",
4739 GlobalPreprocessedEntityMap);
Douglas Gregor8df5c9b2011-08-02 11:12:41 +00004740
4741 llvm::errs() << "\n*** PCH/Modules Loaded:";
4742 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
4743 MEnd = ModuleMgr.end();
4744 M != MEnd; ++M)
4745 (*M)->dump();
Douglas Gregor2cf26342009-04-09 22:27:44 +00004746}
4747
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00004748/// Return the amount of memory used by memory buffers, breaking down
4749/// by heap-backed versus mmap'ed memory.
4750void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004751 for (ModuleConstIterator I = ModuleMgr.begin(),
4752 E = ModuleMgr.end(); I != E; ++I) {
4753 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00004754 size_t bytes = buf->getBufferSize();
4755 switch (buf->getBufferKind()) {
4756 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
4757 sizes.malloc_bytes += bytes;
4758 break;
4759 case llvm::MemoryBuffer::MemoryBuffer_MMap:
4760 sizes.mmap_bytes += bytes;
4761 break;
4762 }
4763 }
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004764 }
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00004765}
4766
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004767void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00004768 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004769 S.ExternalSource = this;
4770
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004771 // Makes sure any declarations that were deserialized "too early"
4772 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00004773 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00004774 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
4775 PreloadedDecls[I]->getDeclName());
Douglas Gregor668c1a42009-04-21 22:25:48 +00004776 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004777 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004778
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004779 // Load the offsets of the declarations that Sema references.
4780 // They will be lazily deserialized when needed.
4781 if (!SemaDeclRefs.empty()) {
4782 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregor1e5b6f62011-07-28 00:57:24 +00004783 if (!SemaObj->StdNamespace)
4784 SemaObj->StdNamespace = SemaDeclRefs[0];
4785 if (!SemaObj->StdBadAlloc)
4786 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004787 }
4788
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004789 if (!FPPragmaOptions.empty()) {
4790 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4791 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4792 }
4793
4794 if (!OpenCLExtensions.empty()) {
4795 unsigned I = 0;
4796#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4797#include "clang/Basic/OpenCLExtensions.def"
4798
4799 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4800 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00004801}
4802
Douglas Gregor211f6e82011-08-20 04:39:52 +00004803IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregor211f6e82011-08-20 04:39:52 +00004804 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart));
4805 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Douglas Gregoreee242f2011-10-27 09:33:13 +00004806 IdentifierInfo *II = Visitor.getIdentifierInfo();
4807 if (II)
4808 II->setOutOfDate(false);
4809 return II;
Douglas Gregor668c1a42009-04-21 22:25:48 +00004810}
4811
Douglas Gregor95f42922010-10-14 22:11:03 +00004812namespace clang {
4813 /// \brief An identifier-lookup iterator that enumerates all of the
4814 /// identifiers stored within a set of AST files.
4815 class ASTIdentifierIterator : public IdentifierIterator {
4816 /// \brief The AST reader whose identifiers are being enumerated.
4817 const ASTReader &Reader;
4818
4819 /// \brief The current index into the chain of AST files stored in
4820 /// the AST reader.
4821 unsigned Index;
4822
4823 /// \brief The current position within the identifier lookup table
4824 /// of the current AST file.
4825 ASTIdentifierLookupTable::key_iterator Current;
4826
4827 /// \brief The end position within the identifier lookup table of
4828 /// the current AST file.
4829 ASTIdentifierLookupTable::key_iterator End;
4830
4831 public:
4832 explicit ASTIdentifierIterator(const ASTReader &Reader);
4833
Chris Lattner5f9e2722011-07-23 10:55:15 +00004834 virtual StringRef Next();
Douglas Gregor95f42922010-10-14 22:11:03 +00004835 };
4836}
4837
4838ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004839 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor95f42922010-10-14 22:11:03 +00004840 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004841 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00004842 Current = IdTable->key_begin();
4843 End = IdTable->key_end();
4844}
4845
Chris Lattner5f9e2722011-07-23 10:55:15 +00004846StringRef ASTIdentifierIterator::Next() {
Douglas Gregor95f42922010-10-14 22:11:03 +00004847 while (Current == End) {
4848 // If we have exhausted all of our AST files, we're done.
4849 if (Index == 0)
Chris Lattner5f9e2722011-07-23 10:55:15 +00004850 return StringRef();
Douglas Gregor95f42922010-10-14 22:11:03 +00004851
4852 --Index;
4853 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004854 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
4855 IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00004856 Current = IdTable->key_begin();
4857 End = IdTable->key_end();
4858 }
4859
4860 // We have any identifiers remaining in the current AST file; return
4861 // the next one.
4862 std::pair<const char*, unsigned> Key = *Current;
4863 ++Current;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004864 return StringRef(Key.first, Key.second);
Douglas Gregor95f42922010-10-14 22:11:03 +00004865}
4866
4867IdentifierIterator *ASTReader::getIdentifiers() const {
4868 return new ASTIdentifierIterator(*this);
4869}
4870
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004871namespace clang { namespace serialization {
4872 class ReadMethodPoolVisitor {
4873 ASTReader &Reader;
4874 Selector Sel;
4875 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
4876 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004877
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004878 /// \brief Build an ObjCMethodList from a vector of Objective-C method
4879 /// declarations.
4880 ObjCMethodList
4881 buildObjCMethodList(const SmallVectorImpl<ObjCMethodDecl *> &Vec) const
4882 {
4883 ObjCMethodList List;
4884 ObjCMethodList *Prev = 0;
4885 for (unsigned I = 0, N = Vec.size(); I != N; ++I) {
4886 if (!List.Method) {
4887 // This is the first method, which is the easy case.
4888 List.Method = Vec[I];
4889 Prev = &List;
4890 continue;
4891 }
4892
4893 ObjCMethodList *Mem =
4894 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
4895 Prev->Next = new (Mem) ObjCMethodList(Vec[I], 0);
4896 Prev = Prev->Next;
4897 }
4898
4899 return List;
4900 }
4901
4902 public:
4903 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel)
4904 : Reader(Reader), Sel(Sel) { }
4905
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004906 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004907 ReadMethodPoolVisitor *This
4908 = static_cast<ReadMethodPoolVisitor *>(UserData);
4909
4910 if (!M.SelectorLookupTable)
4911 return false;
4912
4913 ASTSelectorLookupTable *PoolTable
4914 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
4915 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
4916 if (Pos == PoolTable->end())
4917 return false;
4918
4919 ++This->Reader.NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004920 // FIXME: Not quite happy with the statistics here. We probably should
4921 // disable this tracking when called via LoadSelector.
4922 // Also, should entries without methods count as misses?
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004923 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004924 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004925 if (This->Reader.DeserializationListener)
4926 This->Reader.DeserializationListener->SelectorRead(Data.ID,
4927 This->Sel);
4928
4929 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
4930 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
4931 return true;
Sebastian Redl725cd962010-08-04 20:40:17 +00004932 }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004933
4934 /// \brief Retrieve the instance methods found by this visitor.
4935 ObjCMethodList getInstanceMethods() const {
4936 return buildObjCMethodList(InstanceMethods);
4937 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004938
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004939 /// \brief Retrieve the instance methods found by this visitor.
4940 ObjCMethodList getFactoryMethods() const {
4941 return buildObjCMethodList(FactoryMethods);
4942 }
4943 };
4944} } // end namespace clang::serialization
4945
4946std::pair<ObjCMethodList, ObjCMethodList>
4947ASTReader::ReadMethodPool(Selector Sel) {
4948 ReadMethodPoolVisitor Visitor(*this, Sel);
4949 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
4950 std::pair<ObjCMethodList, ObjCMethodList> Result;
4951 Result.first = Visitor.getInstanceMethods();
4952 Result.second = Visitor.getFactoryMethods();
4953
4954 if (!Result.first.Method && !Result.second.Method)
4955 ++NumMethodPoolMisses;
4956 return Result;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004957}
4958
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004959void ASTReader::ReadKnownNamespaces(
Chris Lattner5f9e2722011-07-23 10:55:15 +00004960 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004961 Namespaces.clear();
4962
4963 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
4964 if (NamespaceDecl *Namespace
4965 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
4966 Namespaces.push_back(Namespace);
4967 }
4968}
4969
Douglas Gregora8623202011-07-27 20:58:46 +00004970void ASTReader::ReadTentativeDefinitions(
4971 SmallVectorImpl<VarDecl *> &TentativeDefs) {
4972 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4973 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
4974 if (Var)
4975 TentativeDefs.push_back(Var);
4976 }
4977 TentativeDefinitions.clear();
4978}
4979
Douglas Gregora2ee20a2011-07-27 21:45:57 +00004980void ASTReader::ReadUnusedFileScopedDecls(
4981 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
4982 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4983 DeclaratorDecl *D
4984 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4985 if (D)
4986 Decls.push_back(D);
4987 }
4988 UnusedFileScopedDecls.clear();
4989}
4990
Douglas Gregor0129b562011-07-27 21:57:17 +00004991void ASTReader::ReadDelegatingConstructors(
4992 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
4993 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
4994 CXXConstructorDecl *D
4995 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
4996 if (D)
4997 Decls.push_back(D);
4998 }
4999 DelegatingCtorDecls.clear();
5000}
5001
Douglas Gregord58a0a52011-07-28 00:39:29 +00005002void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
5003 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
5004 TypedefNameDecl *D
5005 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
5006 if (D)
5007 Decls.push_back(D);
5008 }
5009 ExtVectorDecls.clear();
5010}
5011
Douglas Gregora126f172011-07-28 00:53:40 +00005012void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
5013 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
5014 CXXRecordDecl *D
5015 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
5016 if (D)
5017 Decls.push_back(D);
5018 }
5019 DynamicClasses.clear();
5020}
5021
Douglas Gregorec12ce22011-07-28 14:20:37 +00005022void
5023ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
5024 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
5025 NamedDecl *D
5026 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
5027 if (D)
5028 Decls.push_back(D);
5029 }
5030 LocallyScopedExternalDecls.clear();
5031}
5032
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00005033void ASTReader::ReadReferencedSelectors(
5034 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
5035 if (ReferencedSelectorsData.empty())
5036 return;
5037
5038 // If there are @selector references added them to its pool. This is for
5039 // implementation of -Wselector.
5040 unsigned int DataSize = ReferencedSelectorsData.size()-1;
5041 unsigned I = 0;
5042 while (I < DataSize) {
5043 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
5044 SourceLocation SelLoc
5045 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
5046 Sels.push_back(std::make_pair(Sel, SelLoc));
5047 }
5048 ReferencedSelectorsData.clear();
5049}
5050
Douglas Gregor31e37b22011-07-28 18:09:57 +00005051void ASTReader::ReadWeakUndeclaredIdentifiers(
5052 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
5053 if (WeakUndeclaredIdentifiers.empty())
5054 return;
5055
5056 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
5057 IdentifierInfo *WeakId
5058 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5059 IdentifierInfo *AliasId
5060 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5061 SourceLocation Loc
5062 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
5063 bool Used = WeakUndeclaredIdentifiers[I++];
5064 WeakInfo WI(AliasId, Loc);
5065 WI.setUsed(Used);
5066 WeakIDs.push_back(std::make_pair(WeakId, WI));
5067 }
5068 WeakUndeclaredIdentifiers.clear();
5069}
5070
Douglas Gregordfe65432011-07-28 19:11:31 +00005071void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
5072 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
5073 ExternalVTableUse VT;
5074 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
5075 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
5076 VT.DefinitionRequired = VTableUses[Idx++];
5077 VTables.push_back(VT);
5078 }
5079
5080 VTableUses.clear();
5081}
5082
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00005083void ASTReader::ReadPendingInstantiations(
5084 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
5085 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
5086 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
5087 SourceLocation Loc
5088 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
5089 Pending.push_back(std::make_pair(D, Loc));
5090 }
5091 PendingInstantiations.clear();
5092}
5093
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005094void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00005095 // It would be complicated to avoid reading the methods anyway. So don't.
5096 ReadMethodPool(Sel);
5097}
5098
Douglas Gregor95eab172011-07-28 20:55:49 +00005099void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005100 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00005101 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005102 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005103 if (DeserializationListener)
5104 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00005105}
5106
Douglas Gregord89275b2009-07-06 18:54:52 +00005107/// \brief Set the globally-visible declarations associated with the given
5108/// identifier.
5109///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005110/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00005111/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00005112/// them.
5113///
5114/// \param II an IdentifierInfo that refers to one or more globally-visible
5115/// declarations.
5116///
5117/// \param DeclIDs the set of declaration IDs with the name @p II that are
5118/// visible at global scope.
5119///
5120/// \param Nonrecursive should be true to indicate that the caller knows that
5121/// this call is non-recursive, and therefore the globally-visible declarations
5122/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00005123void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005124ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner5f9e2722011-07-23 10:55:15 +00005125 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregord89275b2009-07-06 18:54:52 +00005126 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005127 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00005128 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
5129 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
5130 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00005131 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00005132 return;
5133 }
Mike Stump1eb44332009-09-09 15:08:12 +00005134
Douglas Gregord89275b2009-07-06 18:54:52 +00005135 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
5136 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
5137 if (SemaObj) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005138 // Introduce this declaration into the translation-unit scope
5139 // and add it to the declaration chain for this identifier, so
5140 // that (unqualified) name lookup will find it.
5141 SemaObj->pushExternalDeclIntoScope(D, II);
Douglas Gregord89275b2009-07-06 18:54:52 +00005142 } else {
5143 // Queue this declaration so that it will be added to the
5144 // translation unit scope and identifier's declaration chain
5145 // once a Sema object is known.
5146 PreloadedDecls.push_back(D);
5147 }
5148 }
5149}
5150
Douglas Gregor95eab172011-07-28 20:55:49 +00005151IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00005152 if (ID == 0)
5153 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00005154
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005155 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005156 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00005157 return 0;
5158 }
Mike Stump1eb44332009-09-09 15:08:12 +00005159
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005160 ID -= 1;
5161 if (!IdentifiersLoaded[ID]) {
Douglas Gregor67268d02011-07-20 00:59:32 +00005162 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
5163 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005164 ModuleFile *M = I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00005165 unsigned Index = ID - M->BaseIdentifierID;
5166 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregord6595a42009-04-25 21:04:17 +00005167
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005168 // All of the strings in the AST file are preceded by a 16-bit length.
5169 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00005170 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
5171 // unsigned integers. This is important to avoid integer overflow when
5172 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00005173 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00005174 unsigned StrLen = (((unsigned) StrLenPtr[0])
5175 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005176 IdentifiersLoaded[ID]
Douglas Gregor712f2fc2011-09-09 22:02:16 +00005177 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005178 if (DeserializationListener)
5179 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00005180 }
Mike Stump1eb44332009-09-09 15:08:12 +00005181
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005182 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00005183}
5184
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005185IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
Douglas Gregor95eab172011-07-28 20:55:49 +00005186 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
5187}
5188
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005189IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +00005190 if (LocalID < NUM_PREDEF_IDENT_IDS)
5191 return LocalID;
5192
5193 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5194 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
5195 assert(I != M.IdentifierRemap.end()
5196 && "Invalid index into identifier index remap");
5197
5198 return LocalID + I->second;
Douglas Gregor95eab172011-07-28 20:55:49 +00005199}
5200
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005201bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregore23ac652011-04-20 00:21:03 +00005202 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005203}
5204
Douglas Gregor26ced122011-12-01 00:59:36 +00005205serialization::SubmoduleID
5206ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
5207 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
5208 return LocalID;
5209
5210 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5211 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
5212 assert(I != M.SubmoduleRemap.end()
5213 && "Invalid index into identifier index remap");
5214
5215 return LocalID + I->second;
5216}
5217
5218Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
5219 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
5220 assert(GlobalID == 0 && "Unhandled global submodule ID");
5221 return 0;
5222 }
5223
5224 if (GlobalID > SubmodulesLoaded.size()) {
5225 Error("submodule ID out of range in AST file");
5226 return 0;
5227 }
5228
5229 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
5230}
5231
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005232Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
Douglas Gregor2d2689a2011-07-28 21:16:51 +00005233 return DecodeSelector(getGlobalSelectorID(M, LocalID));
5234}
5235
5236Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005237 if (ID == 0)
5238 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00005239
Sebastian Redl725cd962010-08-04 20:40:17 +00005240 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005241 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005242 return Selector();
5243 }
Douglas Gregor83941df2009-04-25 17:48:32 +00005244
Sebastian Redl725cd962010-08-04 20:40:17 +00005245 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005246 // Load this selector from the selector table.
Douglas Gregor96958cb2011-07-20 01:10:58 +00005247 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
5248 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005249 ModuleFile &M = *I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00005250 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005251 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor96958cb2011-07-20 01:10:58 +00005252 SelectorsLoaded[ID - 1] =
Douglas Gregor9827a802011-07-29 00:56:45 +00005253 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor96958cb2011-07-20 01:10:58 +00005254 if (DeserializationListener)
5255 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor83941df2009-04-25 17:48:32 +00005256 }
5257
Sebastian Redl725cd962010-08-04 20:40:17 +00005258 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005259}
5260
Douglas Gregor8451ec72011-07-28 14:41:43 +00005261Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00005262 return DecodeSelector(ID);
5263}
5264
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005265uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00005266 // ID 0 (the null selector) is considered an external selector.
5267 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00005268}
5269
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005270serialization::SelectorID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005271ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005272 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
5273 return LocalID;
5274
5275 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5276 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
5277 assert(I != M.SelectorRemap.end()
5278 && "Invalid index into identifier index remap");
5279
5280 return LocalID + I->second;
Douglas Gregor8451ec72011-07-28 14:41:43 +00005281}
5282
Mike Stump1eb44332009-09-09 15:08:12 +00005283DeclarationName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005284ASTReader::ReadDeclarationName(ModuleFile &F,
Douglas Gregor393f2492011-07-22 00:38:23 +00005285 const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00005286 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
5287 switch (Kind) {
5288 case DeclarationName::Identifier:
Douglas Gregor95eab172011-07-28 20:55:49 +00005289 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005290
5291 case DeclarationName::ObjCZeroArgSelector:
5292 case DeclarationName::ObjCOneArgSelector:
5293 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor2d2689a2011-07-28 21:16:51 +00005294 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005295
5296 case DeclarationName::CXXConstructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005297 return Context.DeclarationNames.getCXXConstructorName(
5298 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005299
5300 case DeclarationName::CXXDestructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005301 return Context.DeclarationNames.getCXXDestructorName(
5302 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005303
5304 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor35942772011-09-09 21:34:22 +00005305 return Context.DeclarationNames.getCXXConversionFunctionName(
5306 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005307
5308 case DeclarationName::CXXOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005309 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00005310 (OverloadedOperatorKind)Record[Idx++]);
5311
Sean Hunt3e518bd2009-11-29 07:34:05 +00005312 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005313 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregor95eab172011-07-28 20:55:49 +00005314 GetIdentifierInfo(F, Record, Idx));
Sean Hunt3e518bd2009-11-29 07:34:05 +00005315
Douglas Gregor2cf26342009-04-09 22:27:44 +00005316 case DeclarationName::CXXUsingDirective:
5317 return DeclarationName::getUsingDirectiveName();
5318 }
5319
5320 // Required to silence GCC warning
5321 return DeclarationName();
5322}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00005323
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005324void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005325 DeclarationNameLoc &DNLoc,
5326 DeclarationName Name,
5327 const RecordData &Record, unsigned &Idx) {
5328 switch (Name.getNameKind()) {
5329 case DeclarationName::CXXConstructorName:
5330 case DeclarationName::CXXDestructorName:
5331 case DeclarationName::CXXConversionFunctionName:
5332 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
5333 break;
5334
5335 case DeclarationName::CXXOperatorName:
5336 DNLoc.CXXOperatorName.BeginOpNameLoc
5337 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5338 DNLoc.CXXOperatorName.EndOpNameLoc
5339 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5340 break;
5341
5342 case DeclarationName::CXXLiteralOperatorName:
5343 DNLoc.CXXLiteralOperatorName.OpNameLoc
5344 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5345 break;
5346
5347 case DeclarationName::Identifier:
5348 case DeclarationName::ObjCZeroArgSelector:
5349 case DeclarationName::ObjCOneArgSelector:
5350 case DeclarationName::ObjCMultiArgSelector:
5351 case DeclarationName::CXXUsingDirective:
5352 break;
5353 }
5354}
5355
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005356void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005357 DeclarationNameInfo &NameInfo,
5358 const RecordData &Record, unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00005359 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005360 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
5361 DeclarationNameLoc DNLoc;
5362 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
5363 NameInfo.setInfo(DNLoc);
5364}
5365
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005366void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005367 const RecordData &Record, unsigned &Idx) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005368 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005369 unsigned NumTPLists = Record[Idx++];
5370 Info.NumTemplParamLists = NumTPLists;
5371 if (NumTPLists) {
Douglas Gregor35942772011-09-09 21:34:22 +00005372 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005373 for (unsigned i=0; i != NumTPLists; ++i)
5374 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
5375 }
5376}
5377
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005378TemplateName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005379ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005380 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00005381 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005382 switch (Kind) {
5383 case TemplateName::Template:
Douglas Gregor409448c2011-07-21 22:35:25 +00005384 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005385
5386 case TemplateName::OverloadedTemplate: {
5387 unsigned size = Record[Idx++];
5388 UnresolvedSet<8> Decls;
5389 while (size--)
Douglas Gregor409448c2011-07-21 22:35:25 +00005390 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005391
Douglas Gregor35942772011-09-09 21:34:22 +00005392 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005393 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005394
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005395 case TemplateName::QualifiedTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005396 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005397 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00005398 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005399 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005400 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005401
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005402 case TemplateName::DependentTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005403 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005404 if (Record[Idx++]) // isIdentifier
Douglas Gregor35942772011-09-09 21:34:22 +00005405 return Context.getDependentTemplateName(NNS,
Douglas Gregor95eab172011-07-28 20:55:49 +00005406 GetIdentifierInfo(F, Record,
5407 Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00005408 return Context.getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00005409 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005410 }
John McCall14606042011-06-30 08:33:18 +00005411
5412 case TemplateName::SubstTemplateTemplateParm: {
5413 TemplateTemplateParmDecl *param
Douglas Gregor409448c2011-07-21 22:35:25 +00005414 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCall14606042011-06-30 08:33:18 +00005415 if (!param) return TemplateName();
5416 TemplateName replacement = ReadTemplateName(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005417 return Context.getSubstTemplateTemplateParm(param, replacement);
John McCall14606042011-06-30 08:33:18 +00005418 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005419
5420 case TemplateName::SubstTemplateTemplateParmPack: {
5421 TemplateTemplateParmDecl *Param
Douglas Gregor409448c2011-07-21 22:35:25 +00005422 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005423 if (!Param)
5424 return TemplateName();
5425
5426 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
5427 if (ArgPack.getKind() != TemplateArgument::Pack)
5428 return TemplateName();
5429
Douglas Gregor35942772011-09-09 21:34:22 +00005430 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005431 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005432 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005433
David Blaikieb219cfc2011-09-23 05:06:16 +00005434 llvm_unreachable("Unhandled template name kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005435}
5436
5437TemplateArgument
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005438ASTReader::ReadTemplateArgument(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00005439 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00005440 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
5441 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005442 case TemplateArgument::Null:
5443 return TemplateArgument();
5444 case TemplateArgument::Type:
Douglas Gregor393f2492011-07-22 00:38:23 +00005445 return TemplateArgument(readType(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005446 case TemplateArgument::Declaration:
Douglas Gregor409448c2011-07-21 22:35:25 +00005447 return TemplateArgument(ReadDecl(F, Record, Idx));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00005448 case TemplateArgument::Integral: {
5449 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00005450 QualType T = readType(F, Record, Idx);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00005451 return TemplateArgument(Value, T);
5452 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00005453 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005454 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00005455 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005456 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00005457 llvm::Optional<unsigned> NumTemplateExpansions;
5458 if (unsigned NumExpansions = Record[Idx++])
5459 NumTemplateExpansions = NumExpansions - 1;
5460 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00005461 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005462 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00005463 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005464 case TemplateArgument::Pack: {
5465 unsigned NumArgs = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00005466 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
Douglas Gregor910f8002010-11-07 23:05:16 +00005467 for (unsigned I = 0; I != NumArgs; ++I)
5468 Args[I] = ReadTemplateArgument(F, Record, Idx);
5469 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005470 }
5471 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005472
David Blaikieb219cfc2011-09-23 05:06:16 +00005473 llvm_unreachable("Unhandled template argument kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005474}
5475
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005476TemplateParameterList *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005477ASTReader::ReadTemplateParameterList(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00005478 const RecordData &Record, unsigned &Idx) {
5479 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
5480 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
5481 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005482
5483 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00005484 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005485 Params.reserve(NumParams);
5486 while (NumParams--)
Douglas Gregor409448c2011-07-21 22:35:25 +00005487 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer20249a12010-10-21 03:16:25 +00005488
5489 TemplateParameterList* TemplateParams =
Douglas Gregor35942772011-09-09 21:34:22 +00005490 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005491 Params.data(), Params.size(), RAngleLoc);
5492 return TemplateParams;
5493}
5494
5495void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005496ASTReader::
Chris Lattner5f9e2722011-07-23 10:55:15 +00005497ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005498 ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00005499 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005500 unsigned NumTemplateArgs = Record[Idx++];
5501 TemplArgs.reserve(NumTemplateArgs);
5502 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00005503 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005504}
5505
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005506/// \brief Read a UnresolvedSet structure.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005507void ASTReader::ReadUnresolvedSet(ModuleFile &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005508 const RecordData &Record, unsigned &Idx) {
5509 unsigned NumDecls = Record[Idx++];
5510 while (NumDecls--) {
Douglas Gregor409448c2011-07-21 22:35:25 +00005511 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005512 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
5513 Set.addDecl(D, AS);
5514 }
5515}
5516
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005517CXXBaseSpecifier
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005518ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
Nick Lewycky56062202010-07-26 16:56:01 +00005519 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005520 bool isVirtual = static_cast<bool>(Record[Idx++]);
5521 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
5522 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00005523 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00005524 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
5525 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00005526 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00005527 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00005528 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00005529 Result.setInheritConstructors(inheritConstructors);
5530 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005531}
5532
Sean Huntcbb67482011-01-08 20:30:50 +00005533std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005534ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
Sean Huntcbb67482011-01-08 20:30:50 +00005535 unsigned &Idx) {
5536 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005537 unsigned NumInitializers = Record[Idx++];
5538 if (NumInitializers) {
Sean Huntcbb67482011-01-08 20:30:50 +00005539 CtorInitializers
Douglas Gregor35942772011-09-09 21:34:22 +00005540 = new (Context) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005541 for (unsigned i=0; i != NumInitializers; ++i) {
Douglas Gregor76852c22011-11-01 01:16:03 +00005542 TypeSourceInfo *TInfo = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005543 bool IsBaseVirtual = false;
5544 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00005545 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00005546
Sean Hunt156b6402011-05-04 01:19:08 +00005547 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
5548 switch (Type) {
Douglas Gregor76852c22011-11-01 01:16:03 +00005549 case CTOR_INITIALIZER_BASE:
5550 TInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005551 IsBaseVirtual = Record[Idx++];
Sean Hunt156b6402011-05-04 01:19:08 +00005552 break;
Douglas Gregor76852c22011-11-01 01:16:03 +00005553
5554 case CTOR_INITIALIZER_DELEGATING:
5555 TInfo = GetTypeSourceInfo(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00005556 break;
5557
5558 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00005559 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00005560 break;
5561
5562 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00005563 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00005564 break;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005565 }
Sean Hunt156b6402011-05-04 01:19:08 +00005566
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00005567 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00005568 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00005569 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
5570 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005571 bool IsWritten = Record[Idx++];
5572 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner5f9e2722011-07-23 10:55:15 +00005573 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005574 if (IsWritten) {
5575 SourceOrderOrNumArrayIndices = Record[Idx++];
5576 } else {
5577 SourceOrderOrNumArrayIndices = Record[Idx++];
5578 Indices.reserve(SourceOrderOrNumArrayIndices);
5579 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor409448c2011-07-21 22:35:25 +00005580 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005581 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005582
Sean Huntcbb67482011-01-08 20:30:50 +00005583 CXXCtorInitializer *BOMInit;
Sean Hunt156b6402011-05-04 01:19:08 +00005584 if (Type == CTOR_INITIALIZER_BASE) {
Douglas Gregor76852c22011-11-01 01:16:03 +00005585 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
Sean Huntcbb67482011-01-08 20:30:50 +00005586 LParenLoc, Init, RParenLoc,
5587 MemberOrEllipsisLoc);
Sean Hunt156b6402011-05-04 01:19:08 +00005588 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
Douglas Gregor76852c22011-11-01 01:16:03 +00005589 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
5590 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005591 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00005592 if (Member)
Douglas Gregor35942772011-09-09 21:34:22 +00005593 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00005594 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00005595 else
Douglas Gregor35942772011-09-09 21:34:22 +00005596 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
Sean Huntcbb67482011-01-08 20:30:50 +00005597 MemberOrEllipsisLoc, LParenLoc,
5598 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005599 } else {
Douglas Gregor35942772011-09-09 21:34:22 +00005600 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00005601 LParenLoc, Init, RParenLoc,
5602 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005603 }
5604
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00005605 if (IsWritten)
5606 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00005607 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005608 }
5609 }
5610
Sean Huntcbb67482011-01-08 20:30:50 +00005611 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005612}
5613
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005614NestedNameSpecifier *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005615ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00005616 const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005617 unsigned N = Record[Idx++];
5618 NestedNameSpecifier *NNS = 0, *Prev = 0;
5619 for (unsigned I = 0; I != N; ++I) {
5620 NestedNameSpecifier::SpecifierKind Kind
5621 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5622 switch (Kind) {
5623 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00005624 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005625 NNS = NestedNameSpecifier::Create(Context, Prev, II);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005626 break;
5627 }
5628
5629 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005630 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005631 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005632 break;
5633 }
5634
Douglas Gregor14aba762011-02-24 02:36:08 +00005635 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005636 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005637 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
Douglas Gregor14aba762011-02-24 02:36:08 +00005638 break;
5639 }
5640
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005641 case NestedNameSpecifier::TypeSpec:
5642 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor393f2492011-07-22 00:38:23 +00005643 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00005644 if (!T)
5645 return 0;
5646
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005647 bool Template = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00005648 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005649 break;
5650 }
5651
5652 case NestedNameSpecifier::Global: {
Douglas Gregor35942772011-09-09 21:34:22 +00005653 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005654 // No associated value, and there can't be a prefix.
5655 break;
5656 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005657 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00005658 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005659 }
5660 return NNS;
5661}
5662
Douglas Gregordc355712011-02-25 00:36:19 +00005663NestedNameSpecifierLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005664ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
Douglas Gregordc355712011-02-25 00:36:19 +00005665 unsigned &Idx) {
5666 unsigned N = Record[Idx++];
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005667 NestedNameSpecifierLocBuilder Builder;
Douglas Gregordc355712011-02-25 00:36:19 +00005668 for (unsigned I = 0; I != N; ++I) {
5669 NestedNameSpecifier::SpecifierKind Kind
5670 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5671 switch (Kind) {
5672 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00005673 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00005674 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005675 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00005676 break;
5677 }
5678
5679 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005680 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00005681 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005682 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00005683 break;
5684 }
5685
5686 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005687 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00005688 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005689 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00005690 break;
5691 }
5692
5693 case NestedNameSpecifier::TypeSpec:
5694 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregordc355712011-02-25 00:36:19 +00005695 bool Template = Record[Idx++];
5696 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
5697 if (!T)
5698 return NestedNameSpecifierLoc();
Douglas Gregordc355712011-02-25 00:36:19 +00005699 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005700
5701 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
Douglas Gregor35942772011-09-09 21:34:22 +00005702 Builder.Extend(Context,
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005703 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
5704 T->getTypeLoc(), ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00005705 break;
5706 }
5707
5708 case NestedNameSpecifier::Global: {
Douglas Gregordc355712011-02-25 00:36:19 +00005709 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005710 Builder.MakeGlobal(Context, ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00005711 break;
5712 }
5713 }
Douglas Gregordc355712011-02-25 00:36:19 +00005714 }
5715
Douglas Gregor35942772011-09-09 21:34:22 +00005716 return Builder.getWithLocInContext(Context);
Douglas Gregordc355712011-02-25 00:36:19 +00005717}
5718
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005719SourceRange
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005720ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00005721 unsigned &Idx) {
5722 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
5723 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00005724 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005725}
5726
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00005727/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005728llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00005729 unsigned BitWidth = Record[Idx++];
5730 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
5731 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
5732 Idx += NumWords;
5733 return Result;
5734}
5735
5736/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005737llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00005738 bool isUnsigned = Record[Idx++];
5739 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
5740}
5741
Douglas Gregor17fc2232009-04-14 21:55:33 +00005742/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005743llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00005744 return llvm::APFloat(ReadAPInt(Record, Idx));
5745}
5746
Douglas Gregor68a2eb02009-04-15 21:30:51 +00005747// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005748std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00005749 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00005750 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00005751 Idx += Len;
5752 return Result;
5753}
5754
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005755VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
5756 unsigned &Idx) {
5757 unsigned Major = Record[Idx++];
5758 unsigned Minor = Record[Idx++];
5759 unsigned Subminor = Record[Idx++];
5760 if (Minor == 0)
5761 return VersionTuple(Major);
5762 if (Subminor == 0)
5763 return VersionTuple(Major, Minor - 1);
5764 return VersionTuple(Major, Minor - 1, Subminor - 1);
5765}
5766
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005767CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00005768 const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00005769 unsigned &Idx) {
Douglas Gregor409448c2011-07-21 22:35:25 +00005770 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005771 return CXXTemporary::Create(Context, Decl);
Chris Lattnerd2598362010-05-10 00:25:06 +00005772}
5773
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005774DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00005775 return Diag(SourceLocation(), DiagID);
5776}
5777
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005778DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00005779 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00005780}
Douglas Gregor025452f2009-04-17 00:04:06 +00005781
Douglas Gregor668c1a42009-04-21 22:25:48 +00005782/// \brief Retrieve the identifier table associated with the
5783/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005784IdentifierTable &ASTReader::getIdentifierTable() {
Douglas Gregor712f2fc2011-09-09 22:02:16 +00005785 return PP.getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00005786}
5787
Douglas Gregor025452f2009-04-17 00:04:06 +00005788/// \brief Record that the given ID maps to the given switch-case
5789/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005790void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00005791 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
5792 SwitchCaseStmts[ID] = SC;
5793}
5794
5795/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005796SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00005797 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
5798 return SwitchCaseStmts[ID];
5799}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00005800
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00005801void ASTReader::ClearSwitchCaseIDs() {
5802 SwitchCaseStmts.clear();
5803}
5804
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005805void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005806 assert(NumCurrentElementsDeserializing &&
5807 "FinishedDeserializing not paired with StartedDeserializing");
5808 if (NumCurrentElementsDeserializing == 1) {
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005809 do {
5810 // If any identifiers with corresponding top-level declarations have
5811 // been loaded, load those declarations now.
5812 while (!PendingIdentifierInfos.empty()) {
5813 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
5814 PendingIdentifierInfos.front().DeclIDs, true);
5815 PendingIdentifierInfos.pop_front();
5816 }
5817
5818 // Ready to load previous declarations of Decls that were delayed.
5819 while (!PendingPreviousDecls.empty()) {
5820 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
5821 PendingPreviousDecls.front().second);
5822 PendingPreviousDecls.pop_front();
5823 }
5824
5825 for (std::vector<std::pair<ObjCInterfaceDecl *,
5826 serialization::DeclID> >::iterator
5827 I = PendingChainedObjCCategories.begin(),
5828 E = PendingChainedObjCCategories.end(); I != E; ++I) {
5829 loadObjCChainedCategories(I->second, I->first);
5830 }
5831 PendingChainedObjCCategories.clear();
5832
5833 // We are not in recursive loading, so it's safe to pass the "interesting"
5834 // decls to the consumer.
5835 if (Consumer && !InterestingDecls.empty()) {
5836 Decl *D = InterestingDecls.front();
5837 InterestingDecls.pop_front();
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005838
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005839 PassInterestingDeclToConsumer(D);
5840 }
Argyrios Kyrtzidis0895d152011-02-12 07:50:47 +00005841
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005842 } while ((Consumer && !InterestingDecls.empty()) ||
5843 !PendingIdentifierInfos.empty() ||
5844 !PendingPreviousDecls.empty() ||
5845 !PendingChainedObjCCategories.empty());
Argyrios Kyrtzidis134db1f2010-10-24 17:26:31 +00005846
5847 assert(PendingForwardRefs.size() == 0 &&
5848 "Some forward refs did not get linked to the definition!");
Douglas Gregord89275b2009-07-06 18:54:52 +00005849 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005850 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00005851}
Douglas Gregor501c1032010-08-19 00:28:17 +00005852
Douglas Gregorf8a1e512011-09-02 00:26:20 +00005853ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
Douglas Gregor832d6202011-07-22 16:35:34 +00005854 StringRef isysroot, bool DisableValidation,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005855 bool DisableStatCache)
Sebastian Redle1dde812010-08-24 00:50:04 +00005856 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
5857 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00005858 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
Jonathan D. Turner1afb6612011-07-28 17:20:23 +00005859 Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
5860 RelocatablePCH(false), isysroot(isysroot),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005861 DisableValidation(DisableValidation),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005862 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005863 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005864 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
5865 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
5866 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
5867 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00005868 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
5869 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
5870 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005871{
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005872 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redle1dde812010-08-24 00:50:04 +00005873}
5874
Sebastian Redle1dde812010-08-24 00:50:04 +00005875ASTReader::~ASTReader() {
Sebastian Redle1dde812010-08-24 00:50:04 +00005876 for (DeclContextVisibleUpdatesPending::iterator
5877 I = PendingVisibleUpdates.begin(),
5878 E = PendingVisibleUpdates.end();
5879 I != E; ++I) {
5880 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5881 F = I->second.end();
5882 J != F; ++J)
Douglas Gregor496c7092011-08-03 15:48:04 +00005883 delete static_cast<ASTDeclContextNameLookupTable*>(J->first);
Sebastian Redle1dde812010-08-24 00:50:04 +00005884 }
5885}