blob: d70546dbbcf4bb9a813864c7801d9e9fec928a02 [file] [log] [blame]
Sebastian Redl3b3c8742010-08-18 23:57:11 +00001//===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===//
Douglas Gregoref84c4b2009-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 Redl2c499f62010-08-18 23:56:43 +000010// This file defines the ASTReader class, which reads AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner92ba5ff2009-04-27 05:14:47 +000013
Sebastian Redlf5b13462010-08-18 23:57:17 +000014#include "clang/Serialization/ASTReader.h"
15#include "clang/Serialization/ASTDeserializationListener.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregor55abb232009-04-10 20:39:37 +000017#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbar732ef8a2009-11-11 23:58:53 +000018#include "clang/Frontend/Utils.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000019#include "clang/Sema/Sema.h"
John McCallcc14d1f2010-08-24 08:50:51 +000020#include "clang/Sema/Scope.h"
Douglas Gregor1a0d0b92009-04-14 00:24:19 +000021#include "clang/AST/ASTConsumer.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000022#include "clang/AST/ASTContext.h"
John McCall19c1bfd2010-08-25 05:32:35 +000023#include "clang/AST/DeclTemplate.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Chris Lattner34321bc2009-04-10 21:41:48 +000028#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000029#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000030#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000031#include "clang/Lex/HeaderSearch.h"
Douglas Gregora868bbd2009-04-21 22:25:48 +000032#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000033#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000034#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000035#include "clang/Basic/FileManager.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000036#include "clang/Basic/TargetInfo.h"
Douglas Gregord54f3a12009-10-05 21:07:28 +000037#include "clang/Basic/Version.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000038#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000039#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000040#include "llvm/Support/MemoryBuffer.h"
John McCall0ad16662009-10-29 08:12:44 +000041#include "llvm/Support/ErrorHandling.h"
Daniel Dunbarf2ce9a22009-11-18 19:50:41 +000042#include "llvm/System/Path.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000043#include <algorithm>
Douglas Gregorc379c072009-04-28 18:58:38 +000044#include <iterator>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000045#include <cstdio>
Douglas Gregorc5046832009-04-27 18:38:38 +000046#include <sys/stat.h>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000047using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000048using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000049
50//===----------------------------------------------------------------------===//
Sebastian Redld44cd6a2010-08-18 23:57:06 +000051// PCH validator implementation
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000052//===----------------------------------------------------------------------===//
53
Sebastian Redl3e31c722010-08-18 23:56:56 +000054ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000055
56bool
57PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
58 const LangOptions &PPLangOpts = PP.getLangOptions();
59#define PARSE_LANGOPT_BENIGN(Option)
60#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
61 if (PPLangOpts.Option != LangOpts.Option) { \
62 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
63 return true; \
64 }
65
66 PARSE_LANGOPT_BENIGN(Trigraphs);
67 PARSE_LANGOPT_BENIGN(BCPLComment);
68 PARSE_LANGOPT_BENIGN(DollarIdents);
69 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
70 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carruthe03aa552010-04-17 20:17:31 +000071 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000072 PARSE_LANGOPT_BENIGN(ImplicitInt);
73 PARSE_LANGOPT_BENIGN(Digraphs);
74 PARSE_LANGOPT_BENIGN(HexFloats);
75 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
76 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +000077 PARSE_LANGOPT_BENIGN(MSCVersion);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000078 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
79 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
80 PARSE_LANGOPT_BENIGN(CXXOperatorName);
81 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
82 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
83 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian45878032010-02-09 19:31:38 +000084 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +000085 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian62c56022010-04-22 21:01:59 +000086 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000087 PARSE_LANGOPT_BENIGN(PascalStrings);
88 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump11289f42009-09-09 15:08:12 +000089 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000090 diag::warn_pch_lax_vector_conversions);
Nate Begeman9d905792009-06-25 22:57:40 +000091 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000092 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +000093 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000094 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
95 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
96 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump11289f42009-09-09 15:08:12 +000097 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000098 diag::warn_pch_thread_safe_statics);
Daniel Dunbara77eaeb2009-09-03 04:54:28 +000099 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000100 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
101 PARSE_LANGOPT_BENIGN(EmitAllDecls);
102 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattner51924e512010-06-26 21:25:03 +0000103 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump11289f42009-09-09 15:08:12 +0000104 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000105 diag::warn_pch_heinous_extensions);
106 // FIXME: Most of the options below are benign if the macro wasn't
107 // used. Unfortunately, this means that a PCH compiled without
108 // optimization can't be used with optimization turned on, even
109 // though the only thing that changes is whether __OPTIMIZE__ was
110 // defined... but if __OPTIMIZE__ never showed up in the header, it
111 // doesn't matter. We could consider making this some special kind
112 // of check.
113 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
114 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
115 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
116 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
117 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
118 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
119 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
120 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsoned4e2952009-11-05 20:14:16 +0000121 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +0000122 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000123 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump11289f42009-09-09 15:08:12 +0000124 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000125 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
126 return true;
127 }
128 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000129 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
130 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000131 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman9d905792009-06-25 22:57:40 +0000132 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Mike Stumpd9546382009-12-12 01:27:46 +0000133 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbar143021e2009-09-21 04:16:19 +0000134 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000135 PARSE_LANGOPT_BENIGN(SpellChecking);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +0000136#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000137#undef PARSE_LANGOPT_BENIGN
138
139 return false;
140}
141
Daniel Dunbar20a682d2009-11-11 00:52:11 +0000142bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
143 if (Triple == PP.getTargetInfo().getTriple().str())
144 return false;
145
146 Reader.Diag(diag::warn_pch_target_triple)
147 << Triple << PP.getTargetInfo().getTriple().str();
148 return true;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000149}
150
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000151struct EmptyStringRef {
Benjamin Kramer8d5609b2010-07-14 23:19:41 +0000152 bool operator ()(llvm::StringRef r) const { return r.empty(); }
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000153};
154struct EmptyBlock {
155 bool operator ()(const PCHPredefinesBlock &r) const { return r.Data.empty(); }
156};
157
158static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
159 PCHPredefinesBlocks R) {
160 // First, sum up the lengths.
161 unsigned LL = 0, RL = 0;
162 for (unsigned I = 0, N = L.size(); I != N; ++I) {
163 LL += L[I].size();
164 }
165 for (unsigned I = 0, N = R.size(); I != N; ++I) {
166 RL += R[I].Data.size();
167 }
168 if (LL != RL)
169 return false;
170 if (LL == 0 && RL == 0)
171 return true;
172
173 // Kick out empty parts, they confuse the algorithm below.
174 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
175 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
176
177 // Do it the hard way. At this point, both vectors must be non-empty.
178 llvm::StringRef LR = L[0], RR = R[0].Data;
179 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbar01ad0a72010-07-16 00:00:11 +0000180 (void) RN;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000181 for (;;) {
182 // Compare the current pieces.
183 if (LR.size() == RR.size()) {
184 // If they're the same length, it's pretty easy.
185 if (LR != RR)
186 return false;
187 // Both pieces are done, advance.
188 ++LI;
189 ++RI;
190 // If either string is done, they're both done, since they're the same
191 // length.
192 if (LI == LN) {
193 assert(RI == RN && "Strings not the same length after all?");
194 return true;
195 }
196 LR = L[LI];
197 RR = R[RI].Data;
198 } else if (LR.size() < RR.size()) {
199 // Right piece is longer.
200 if (!RR.startswith(LR))
201 return false;
202 ++LI;
203 assert(LI != LN && "Strings not the same length after all?");
204 RR = RR.substr(LR.size());
205 LR = L[LI];
206 } else {
207 // Left piece is longer.
208 if (!LR.startswith(RR))
209 return false;
210 ++RI;
211 assert(RI != RN && "Strings not the same length after all?");
212 LR = LR.substr(RR.size());
213 RR = R[RI].Data;
214 }
215 }
216}
217
218static std::pair<FileID, llvm::StringRef::size_type>
219FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
220 std::pair<FileID, llvm::StringRef::size_type> Res;
221 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
222 Res.second = Buffers[I].Data.find(MacroDef);
223 if (Res.second != llvm::StringRef::npos) {
224 Res.first = Buffers[I].BufferID;
225 break;
226 }
227 }
228 return Res;
229}
230
231bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000232 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000233 std::string &SuggestedPredefines) {
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000234 // We are in the context of an implicit include, so the predefines buffer will
235 // have a #include entry for the PCH file itself (as normalized by the
236 // preprocessor initialization). Find it and skip over it in the checking
237 // below.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000238 llvm::SmallString<256> PCHInclude;
239 PCHInclude += "#include \"";
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000240 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000241 PCHInclude += "\"\n";
242 std::pair<llvm::StringRef,llvm::StringRef> Split =
243 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
244 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000245 if (Left == PP.getPredefines()) {
246 Error("Missing PCH include entry!");
247 return true;
248 }
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000249
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000250 // If the concatenation of all the PCH buffers is equal to the adjusted
251 // command line, we're done.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000252 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
253 CommandLine.push_back(Left);
254 CommandLine.push_back(Right);
255 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000256 return false;
257
258 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000259
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000260 // The predefines buffers are different. Determine what the differences are,
261 // and whether they require us to reject the PCH file.
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000262 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000263 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
264 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000265
266 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
267 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000268
269 // Pick out implicit #includes after the PCH and don't consider them for
270 // validation; we will insert them into SuggestedPredefines so that the
271 // preprocessor includes them.
272 std::string IncludesAfterPCH;
273 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
274 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
275 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
276 if (AfterPCHLines[i].startswith("#include ")) {
277 IncludesAfterPCH += AfterPCHLines[i];
278 IncludesAfterPCH += '\n';
279 } else {
280 CmdLineLines.push_back(AfterPCHLines[i]);
281 }
282 }
283
284 // Make sure we add the includes last into SuggestedPredefines before we
285 // exit this function.
286 struct AddIncludesRAII {
287 std::string &SuggestedPredefines;
288 std::string &IncludesAfterPCH;
289
290 AddIncludesRAII(std::string &SuggestedPredefines,
291 std::string &IncludesAfterPCH)
292 : SuggestedPredefines(SuggestedPredefines),
293 IncludesAfterPCH(IncludesAfterPCH) { }
294 ~AddIncludesRAII() {
295 SuggestedPredefines += IncludesAfterPCH;
296 }
297 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000298
Daniel Dunbar499baed2009-11-11 05:26:28 +0000299 // Sort both sets of predefined buffer lines, since we allow some extra
300 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000301 std::sort(CmdLineLines.begin(), CmdLineLines.end());
302 std::sort(PCHLines.begin(), PCHLines.end());
303
Daniel Dunbar499baed2009-11-11 05:26:28 +0000304 // Determine which predefines that were used to build the PCH file are missing
305 // from the command line.
306 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000307 std::set_difference(PCHLines.begin(), PCHLines.end(),
308 CmdLineLines.begin(), CmdLineLines.end(),
309 std::back_inserter(MissingPredefines));
310
311 bool MissingDefines = false;
312 bool ConflictingDefines = false;
313 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000314 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000315 if (Missing.startswith("#include ")) {
316 // An -include was specified when generating the PCH; it is included in
317 // the PCH, just ignore it.
318 continue;
319 }
Daniel Dunbar499baed2009-11-11 05:26:28 +0000320 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000321 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
322 return true;
323 }
Mike Stump11289f42009-09-09 15:08:12 +0000324
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000325 // This is a macro definition. Determine the name of the macro we're
326 // defining.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000327 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000328 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000329 = Missing.find_first_of("( \n\r", StartOfMacroName);
330 assert(EndOfMacroName != std::string::npos &&
331 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000332 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000333
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000334 // Determine whether this macro was given a different definition on the
335 // command line.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000336 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000337 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000338 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000339 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
340 MacroDefStart);
341 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000342 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000343 // Different macro; we're done.
344 ConflictPos = CmdLineLines.end();
Mike Stump11289f42009-09-09 15:08:12 +0000345 break;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000346 }
Mike Stump11289f42009-09-09 15:08:12 +0000347
348 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000349 "Invalid #define in predefines buffer?");
Mike Stump11289f42009-09-09 15:08:12 +0000350 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000351 (*ConflictPos)[MacroDefLen] != '(')
352 continue; // Longer macro name; keep trying.
Mike Stump11289f42009-09-09 15:08:12 +0000353
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000354 // We found a conflicting macro definition.
355 break;
356 }
Mike Stump11289f42009-09-09 15:08:12 +0000357
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000358 if (ConflictPos != CmdLineLines.end()) {
359 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
360 << MacroName;
361
362 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000363 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
364 FindMacro(Buffers, Missing);
365 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
366 SourceLocation PCHMissingLoc =
367 SourceMgr.getLocForStartOfFile(MacroLoc.first)
368 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar499baed2009-11-11 05:26:28 +0000369 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000370
371 ConflictingDefines = true;
372 continue;
373 }
Mike Stump11289f42009-09-09 15:08:12 +0000374
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000375 // If the macro doesn't conflict, then we'll just pick up the macro
376 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000377 if (ConflictingDefines)
378 continue; // Don't complain if there are already conflicting defs
Mike Stump11289f42009-09-09 15:08:12 +0000379
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000380 if (!MissingDefines) {
381 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
382 MissingDefines = true;
383 }
384
385 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000386 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
387 FindMacro(Buffers, Missing);
388 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
389 SourceLocation PCHMissingLoc =
390 SourceMgr.getLocForStartOfFile(MacroLoc.first)
391 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000392 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
393 }
Mike Stump11289f42009-09-09 15:08:12 +0000394
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000395 if (ConflictingDefines)
396 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000397
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000398 // Determine what predefines were introduced based on command-line
399 // parameters that were not present when building the PCH
400 // file. Extra #defines are okay, so long as the identifiers being
401 // defined were not used within the precompiled header.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000402 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000403 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
404 PCHLines.begin(), PCHLines.end(),
Mike Stump11289f42009-09-09 15:08:12 +0000405 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000406 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000407 llvm::StringRef &Extra = ExtraPredefines[I];
408 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000409 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
410 return true;
411 }
412
413 // This is an extra macro definition. Determine the name of the
414 // macro we're defining.
415 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000416 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000417 = Extra.find_first_of("( \n\r", StartOfMacroName);
418 assert(EndOfMacroName != std::string::npos &&
419 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000420 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000421
422 // Check whether this name was used somewhere in the PCH file. If
423 // so, defining it as a macro could change behavior, so we reject
424 // the PCH file.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000425 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar045c92f2009-11-11 00:52:00 +0000426 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000427 return true;
428 }
429
430 // Add this definition to the suggested predefines buffer.
431 SuggestedPredefines += Extra;
432 SuggestedPredefines += '\n';
433 }
434
435 // If we get here, it's because the predefines buffer had compatible
436 // contents. Accept the PCH file.
437 return false;
438}
439
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000440void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
441 unsigned ID) {
442 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
443 ++NumHeaderInfos;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000444}
445
446void PCHValidator::ReadCounter(unsigned Value) {
447 PP.setCounterValue(Value);
448}
449
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000450//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +0000451// AST reader implementation
Douglas Gregora868bbd2009-04-21 22:25:48 +0000452//===----------------------------------------------------------------------===//
453
Sebastian Redl07a89a82010-07-30 00:29:29 +0000454void
Sebastian Redl3e31c722010-08-18 23:56:56 +0000455ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redl07a89a82010-07-30 00:29:29 +0000456 DeserializationListener = Listener;
Sebastian Redl07a89a82010-07-30 00:29:29 +0000457}
458
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000459
Douglas Gregora868bbd2009-04-21 22:25:48 +0000460namespace {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000461class ASTSelectorLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000462 ASTReader &Reader;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000463
464public:
Sebastian Redl834bb972010-08-04 17:20:04 +0000465 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +0000466 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +0000467 ObjCMethodList Instance, Factory;
468 };
Douglas Gregorc78d3462009-04-24 21:10:55 +0000469
470 typedef Selector external_key_type;
471 typedef external_key_type internal_key_type;
472
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000473 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump11289f42009-09-09 15:08:12 +0000474
Douglas Gregorc78d3462009-04-24 21:10:55 +0000475 static bool EqualKey(const internal_key_type& a,
476 const internal_key_type& b) {
477 return a == b;
478 }
Mike Stump11289f42009-09-09 15:08:12 +0000479
Douglas Gregorc78d3462009-04-24 21:10:55 +0000480 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +0000481 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000482 }
Mike Stump11289f42009-09-09 15:08:12 +0000483
Douglas Gregorc78d3462009-04-24 21:10:55 +0000484 // This hopefully will just get inlined and removed by the optimizer.
485 static const internal_key_type&
486 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000487
Douglas Gregorc78d3462009-04-24 21:10:55 +0000488 static std::pair<unsigned, unsigned>
489 ReadKeyDataLength(const unsigned char*& d) {
490 using namespace clang::io;
491 unsigned KeyLen = ReadUnalignedLE16(d);
492 unsigned DataLen = ReadUnalignedLE16(d);
493 return std::make_pair(KeyLen, DataLen);
494 }
Mike Stump11289f42009-09-09 15:08:12 +0000495
Douglas Gregor95c13f52009-04-25 17:48:32 +0000496 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000497 using namespace clang::io;
Chris Lattner8575daa2009-04-27 21:45:14 +0000498 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000499 unsigned N = ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000500 IdentifierInfo *FirstII
Douglas Gregorc78d3462009-04-24 21:10:55 +0000501 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
502 if (N == 0)
503 return SelTable.getNullarySelector(FirstII);
504 else if (N == 1)
505 return SelTable.getUnarySelector(FirstII);
506
507 llvm::SmallVector<IdentifierInfo *, 16> Args;
508 Args.push_back(FirstII);
509 for (unsigned I = 1; I != N; ++I)
510 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
511
Douglas Gregor038c3382009-05-22 22:45:36 +0000512 return SelTable.getSelector(N, Args.data());
Douglas Gregorc78d3462009-04-24 21:10:55 +0000513 }
Mike Stump11289f42009-09-09 15:08:12 +0000514
Douglas Gregorc78d3462009-04-24 21:10:55 +0000515 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
516 using namespace clang::io;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000517
518 data_type Result;
519
Sebastian Redl834bb972010-08-04 17:20:04 +0000520 Result.ID = ReadUnalignedLE32(d);
521 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
522 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
523
Douglas Gregorc78d3462009-04-24 21:10:55 +0000524 // Load instance methods
525 ObjCMethodList *Prev = 0;
526 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000527 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000528 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000529 if (!Result.Instance.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000530 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000531 Result.Instance.Method = Method;
532 Prev = &Result.Instance;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000533 continue;
534 }
535
Ted Kremenekda4abf12010-02-11 00:53:01 +0000536 ObjCMethodList *Mem =
537 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
538 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000539 Prev = Prev->Next;
540 }
541
542 // Load factory methods
543 Prev = 0;
544 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000545 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000546 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000547 if (!Result.Factory.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000548 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000549 Result.Factory.Method = Method;
550 Prev = &Result.Factory;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000551 continue;
552 }
553
Ted Kremenekda4abf12010-02-11 00:53:01 +0000554 ObjCMethodList *Mem =
555 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
556 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000557 Prev = Prev->Next;
558 }
559
560 return Result;
561 }
562};
Mike Stump11289f42009-09-09 15:08:12 +0000563
564} // end anonymous namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +0000565
566/// \brief The on-disk hash table used for the global method pool.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000567typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
568 ASTSelectorLookupTable;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000569
Sebastian Redl2c373b92010-10-05 15:59:54 +0000570namespace clang {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000571class ASTIdentifierLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000572 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +0000573 ASTReader::PerFileData &F;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000574
575 // If we know the IdentifierInfo in advance, it is here and we will
576 // not build a new one. Used when deserializing information about an
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000577 // identifier that was constructed before the AST file was read.
Douglas Gregora868bbd2009-04-21 22:25:48 +0000578 IdentifierInfo *KnownII;
579
580public:
581 typedef IdentifierInfo * data_type;
582
583 typedef const std::pair<const char*, unsigned> external_key_type;
584
585 typedef external_key_type internal_key_type;
586
Sebastian Redl2c373b92010-10-05 15:59:54 +0000587 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000588 IdentifierInfo *II = 0)
Sebastian Redl2c373b92010-10-05 15:59:54 +0000589 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump11289f42009-09-09 15:08:12 +0000590
Douglas Gregora868bbd2009-04-21 22:25:48 +0000591 static bool EqualKey(const internal_key_type& a,
592 const internal_key_type& b) {
593 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
594 : false;
595 }
Mike Stump11289f42009-09-09 15:08:12 +0000596
Douglas Gregora868bbd2009-04-21 22:25:48 +0000597 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000598 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregora868bbd2009-04-21 22:25:48 +0000599 }
Mike Stump11289f42009-09-09 15:08:12 +0000600
Douglas Gregora868bbd2009-04-21 22:25:48 +0000601 // This hopefully will just get inlined and removed by the optimizer.
602 static const internal_key_type&
603 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000604
Douglas Gregor57756ea2010-10-14 22:11:03 +0000605 // This hopefully will just get inlined and removed by the optimizer.
606 static const external_key_type&
607 GetExternalKey(const internal_key_type& x) { return x; }
608
Douglas Gregora868bbd2009-04-21 22:25:48 +0000609 static std::pair<unsigned, unsigned>
610 ReadKeyDataLength(const unsigned char*& d) {
611 using namespace clang::io;
Douglas Gregor6b7bf5a2009-04-25 20:26:24 +0000612 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor5287b4e2009-04-25 21:04:17 +0000613 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000614 return std::make_pair(KeyLen, DataLen);
615 }
Mike Stump11289f42009-09-09 15:08:12 +0000616
Douglas Gregora868bbd2009-04-21 22:25:48 +0000617 static std::pair<const char*, unsigned>
618 ReadKey(const unsigned char* d, unsigned n) {
619 assert(n >= 2 && d[n-1] == '\0');
620 return std::make_pair((const char*) d, n-1);
621 }
Mike Stump11289f42009-09-09 15:08:12 +0000622
623 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregora868bbd2009-04-21 22:25:48 +0000624 const unsigned char* d,
625 unsigned DataLen) {
626 using namespace clang::io;
Sebastian Redl539c5062010-08-18 23:57:32 +0000627 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000628 bool IsInteresting = ID & 0x01;
629
630 // Wipe out the "is interesting" bit.
631 ID = ID >> 1;
632
633 if (!IsInteresting) {
Sebastian Redl98912122010-07-27 23:01:28 +0000634 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregor1d583f22009-04-28 21:18:29 +0000635 // and associate it with the persistent ID.
636 IdentifierInfo *II = KnownII;
637 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000638 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000639 Reader.SetIdentifierInfo(ID, II);
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000640 II->setIsFromAST();
Douglas Gregor1d583f22009-04-28 21:18:29 +0000641 return II;
642 }
643
Douglas Gregorb9256522009-04-28 21:32:13 +0000644 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000645 bool CPlusPlusOperatorKeyword = Bits & 0x01;
646 Bits >>= 1;
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000647 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
648 Bits >>= 1;
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000649 bool Poisoned = Bits & 0x01;
650 Bits >>= 1;
651 bool ExtensionToken = Bits & 0x01;
652 Bits >>= 1;
653 bool hasMacroDefinition = Bits & 0x01;
654 Bits >>= 1;
655 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
656 Bits >>= 10;
Mike Stump11289f42009-09-09 15:08:12 +0000657
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000658 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorb9256522009-04-28 21:32:13 +0000659 DataLen -= 6;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000660
661 // Build the IdentifierInfo itself and link the identifier ID with
662 // the new IdentifierInfo.
663 IdentifierInfo *II = KnownII;
664 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000665 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000666 Reader.SetIdentifierInfo(ID, II);
667
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000668 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000669 // Token IDs are read-only.
670 if (HasRevertedTokenIDToIdentifier)
671 II->RevertTokenIDToIdentifier();
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000672 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump11289f42009-09-09 15:08:12 +0000673 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000674 "Incorrect extension token flag");
675 (void)ExtensionToken;
676 II->setIsPoisoned(Poisoned);
677 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
678 "Incorrect C++ operator keyword flag");
679 (void)CPlusPlusOperatorKeyword;
680
Douglas Gregorc3366a52009-04-21 23:56:24 +0000681 // If this identifier is a macro, deserialize the macro
682 // definition.
683 if (hasMacroDefinition) {
Douglas Gregorb9256522009-04-28 21:32:13 +0000684 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor5ef9e332010-10-30 00:23:06 +0000685 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregorb9256522009-04-28 21:32:13 +0000686 DataLen -= 4;
Douglas Gregorc3366a52009-04-21 23:56:24 +0000687 }
Douglas Gregora868bbd2009-04-21 22:25:48 +0000688
689 // Read all of the declarations visible at global scope with this
690 // name.
Chris Lattner1d728882009-04-27 22:17:41 +0000691 if (Reader.getContext() == 0) return II;
Douglas Gregor1342e842009-07-06 18:54:52 +0000692 if (DataLen > 0) {
693 llvm::SmallVector<uint32_t, 4> DeclIDs;
694 for (; DataLen > 0; DataLen -= 4)
695 DeclIDs.push_back(ReadUnalignedLE32(d));
696 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000697 }
Mike Stump11289f42009-09-09 15:08:12 +0000698
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000699 II->setIsFromAST();
Douglas Gregora868bbd2009-04-21 22:25:48 +0000700 return II;
701 }
702};
Mike Stump11289f42009-09-09 15:08:12 +0000703
704} // end anonymous namespace
Douglas Gregora868bbd2009-04-21 22:25:48 +0000705
706/// \brief The on-disk hash table used to contain information about
707/// all of the identifiers in the program.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000708typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
709 ASTIdentifierLookupTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000710
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000711namespace {
712class ASTDeclContextNameLookupTrait {
713 ASTReader &Reader;
714
715public:
716 /// \brief Pair of begin/end iterators for DeclIDs.
717 typedef std::pair<DeclID *, DeclID *> data_type;
718
719 /// \brief Special internal key for declaration names.
720 /// The hash table creates keys for comparison; we do not create
721 /// a DeclarationName for the internal key to avoid deserializing types.
722 struct DeclNameKey {
723 DeclarationName::NameKind Kind;
724 uint64_t Data;
725 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
726 };
727
728 typedef DeclarationName external_key_type;
729 typedef DeclNameKey internal_key_type;
730
731 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
732
733 static bool EqualKey(const internal_key_type& a,
734 const internal_key_type& b) {
735 return a.Kind == b.Kind && a.Data == b.Data;
736 }
737
738 unsigned ComputeHash(const DeclNameKey &Key) const {
739 llvm::FoldingSetNodeID ID;
740 ID.AddInteger(Key.Kind);
741
742 switch (Key.Kind) {
743 case DeclarationName::Identifier:
744 case DeclarationName::CXXLiteralOperatorName:
745 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
746 break;
747 case DeclarationName::ObjCZeroArgSelector:
748 case DeclarationName::ObjCOneArgSelector:
749 case DeclarationName::ObjCMultiArgSelector:
750 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
751 break;
752 case DeclarationName::CXXConstructorName:
753 case DeclarationName::CXXDestructorName:
754 case DeclarationName::CXXConversionFunctionName:
755 ID.AddInteger((TypeID)Key.Data);
756 break;
757 case DeclarationName::CXXOperatorName:
758 ID.AddInteger((OverloadedOperatorKind)Key.Data);
759 break;
760 case DeclarationName::CXXUsingDirective:
761 break;
762 }
763
764 return ID.ComputeHash();
765 }
766
767 internal_key_type GetInternalKey(const external_key_type& Name) const {
768 DeclNameKey Key;
769 Key.Kind = Name.getNameKind();
770 switch (Name.getNameKind()) {
771 case DeclarationName::Identifier:
772 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
773 break;
774 case DeclarationName::ObjCZeroArgSelector:
775 case DeclarationName::ObjCOneArgSelector:
776 case DeclarationName::ObjCMultiArgSelector:
777 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
778 break;
779 case DeclarationName::CXXConstructorName:
780 case DeclarationName::CXXDestructorName:
781 case DeclarationName::CXXConversionFunctionName:
782 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
783 break;
784 case DeclarationName::CXXOperatorName:
785 Key.Data = Name.getCXXOverloadedOperator();
786 break;
787 case DeclarationName::CXXLiteralOperatorName:
788 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
789 break;
790 case DeclarationName::CXXUsingDirective:
791 break;
792 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000793
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000794 return Key;
795 }
796
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000797 external_key_type GetExternalKey(const internal_key_type& Key) const {
798 ASTContext *Context = Reader.getContext();
799 switch (Key.Kind) {
800 case DeclarationName::Identifier:
801 return DeclarationName((IdentifierInfo*)Key.Data);
802
803 case DeclarationName::ObjCZeroArgSelector:
804 case DeclarationName::ObjCOneArgSelector:
805 case DeclarationName::ObjCMultiArgSelector:
806 return DeclarationName(Selector(Key.Data));
807
808 case DeclarationName::CXXConstructorName:
809 return Context->DeclarationNames.getCXXConstructorName(
810 Context->getCanonicalType(Reader.GetType(Key.Data)));
811
812 case DeclarationName::CXXDestructorName:
813 return Context->DeclarationNames.getCXXDestructorName(
814 Context->getCanonicalType(Reader.GetType(Key.Data)));
815
816 case DeclarationName::CXXConversionFunctionName:
817 return Context->DeclarationNames.getCXXConversionFunctionName(
818 Context->getCanonicalType(Reader.GetType(Key.Data)));
819
820 case DeclarationName::CXXOperatorName:
821 return Context->DeclarationNames.getCXXOperatorName(
822 (OverloadedOperatorKind)Key.Data);
823
824 case DeclarationName::CXXLiteralOperatorName:
825 return Context->DeclarationNames.getCXXLiteralOperatorName(
826 (IdentifierInfo*)Key.Data);
827
828 case DeclarationName::CXXUsingDirective:
829 return DeclarationName::getUsingDirectiveName();
830 }
831
832 llvm_unreachable("Invalid Name Kind ?");
833 }
834
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000835 static std::pair<unsigned, unsigned>
836 ReadKeyDataLength(const unsigned char*& d) {
837 using namespace clang::io;
838 unsigned KeyLen = ReadUnalignedLE16(d);
839 unsigned DataLen = ReadUnalignedLE16(d);
840 return std::make_pair(KeyLen, DataLen);
841 }
842
843 internal_key_type ReadKey(const unsigned char* d, unsigned) {
844 using namespace clang::io;
845
846 DeclNameKey Key;
847 Key.Kind = (DeclarationName::NameKind)*d++;
848 switch (Key.Kind) {
849 case DeclarationName::Identifier:
850 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
851 break;
852 case DeclarationName::ObjCZeroArgSelector:
853 case DeclarationName::ObjCOneArgSelector:
854 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000855 Key.Data =
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000856 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
857 break;
858 case DeclarationName::CXXConstructorName:
859 case DeclarationName::CXXDestructorName:
860 case DeclarationName::CXXConversionFunctionName:
861 Key.Data = ReadUnalignedLE32(d); // TypeID
862 break;
863 case DeclarationName::CXXOperatorName:
864 Key.Data = *d++; // OverloadedOperatorKind
865 break;
866 case DeclarationName::CXXLiteralOperatorName:
867 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
868 break;
869 case DeclarationName::CXXUsingDirective:
870 break;
871 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000872
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000873 return Key;
874 }
875
876 data_type ReadData(internal_key_type, const unsigned char* d,
877 unsigned DataLen) {
878 using namespace clang::io;
879 unsigned NumDecls = ReadUnalignedLE16(d);
880 DeclID *Start = (DeclID *)d;
881 return std::make_pair(Start, Start + NumDecls);
882 }
883};
884
885} // end anonymous namespace
886
887/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
888typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
889 ASTDeclContextNameLookupTable;
890
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000891bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
892 const std::pair<uint64_t, uint64_t> &Offsets,
893 DeclContextInfo &Info) {
894 SavedStreamPosition SavedPosition(Cursor);
895 // First the lexical decls.
896 if (Offsets.first != 0) {
897 Cursor.JumpToBit(Offsets.first);
898
899 RecordData Record;
900 const char *Blob;
901 unsigned BlobLen;
902 unsigned Code = Cursor.ReadCode();
903 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
904 if (RecCode != DECL_CONTEXT_LEXICAL) {
905 Error("Expected lexical block");
906 return true;
907 }
908
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000909 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
910 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000911 } else {
912 Info.LexicalDecls = 0;
913 Info.NumLexicalDecls = 0;
914 }
915
916 // Now the lookup table.
917 if (Offsets.second != 0) {
918 Cursor.JumpToBit(Offsets.second);
919
920 RecordData Record;
921 const char *Blob;
922 unsigned BlobLen;
923 unsigned Code = Cursor.ReadCode();
924 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
925 if (RecCode != DECL_CONTEXT_VISIBLE) {
926 Error("Expected visible lookup table block");
927 return true;
928 }
929 Info.NameLookupTableData
930 = ASTDeclContextNameLookupTable::Create(
931 (const unsigned char *)Blob + Record[0],
932 (const unsigned char *)Blob,
933 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl9d8f58b2010-08-24 00:50:00 +0000934 } else {
935 Info.NameLookupTableData = 0;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000936 }
937
938 return false;
939}
940
Sebastian Redl2c499f62010-08-18 23:56:43 +0000941void ASTReader::Error(const char *Msg) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000942 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000943}
944
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000945/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000946bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000947 if (Listener)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000948 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000949 ActualOriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000950 SuggestedPredefines);
Douglas Gregorc379c072009-04-28 18:58:38 +0000951 return false;
Douglas Gregor92863e42009-04-10 23:10:45 +0000952}
953
Douglas Gregorc5046832009-04-27 18:38:38 +0000954//===----------------------------------------------------------------------===//
955// Source Manager Deserialization
956//===----------------------------------------------------------------------===//
957
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000958/// \brief Read the line table in the source manager block.
Sebastian Redl2c373b92010-10-05 15:59:54 +0000959/// \returns true if there was an error.
960bool ASTReader::ParseLineTable(PerFileData &F,
961 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000962 unsigned Idx = 0;
963 LineTableInfo &LineTable = SourceMgr.getLineTable();
964
965 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +0000966 std::map<int, int> FileIDs;
967 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000968 // Extract the file name
969 unsigned FilenameLen = Record[Idx++];
970 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
971 Idx += FilenameLen;
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000972 MaybeAddSystemRootToFilename(Filename);
Mike Stump11289f42009-09-09 15:08:12 +0000973 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregora8854652009-04-13 17:12:42 +0000974 Filename.size());
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000975 }
976
977 // Parse the line entries
978 std::vector<LineEntry> Entries;
979 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000980 int FID = Record[Idx++];
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000981
982 // Extract the line entries
983 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000984 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000985 Entries.clear();
986 Entries.reserve(NumEntries);
987 for (unsigned I = 0; I != NumEntries; ++I) {
988 unsigned FileOffset = Record[Idx++];
989 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000990 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +0000991 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000992 = (SrcMgr::CharacteristicKind)Record[Idx++];
993 unsigned IncludeOffset = Record[Idx++];
994 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
995 FileKind, IncludeOffset));
996 }
997 LineTable.AddEntry(FID, Entries);
998 }
999
1000 return false;
1001}
1002
Douglas Gregorc5046832009-04-27 18:38:38 +00001003namespace {
1004
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001005class ASTStatData {
Douglas Gregorc5046832009-04-27 18:38:38 +00001006public:
1007 const bool hasStat;
1008 const ino_t ino;
1009 const dev_t dev;
1010 const mode_t mode;
1011 const time_t mtime;
1012 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +00001013
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001014 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump11289f42009-09-09 15:08:12 +00001015 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
1016
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001017 ASTStatData()
Douglas Gregorc5046832009-04-27 18:38:38 +00001018 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
1019};
1020
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001021class ASTStatLookupTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001022 public:
1023 typedef const char *external_key_type;
1024 typedef const char *internal_key_type;
1025
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001026 typedef ASTStatData data_type;
Douglas Gregorc5046832009-04-27 18:38:38 +00001027
1028 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001029 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001030 }
1031
1032 static internal_key_type GetInternalKey(const char *path) { return path; }
1033
1034 static bool EqualKey(internal_key_type a, internal_key_type b) {
1035 return strcmp(a, b) == 0;
1036 }
1037
1038 static std::pair<unsigned, unsigned>
1039 ReadKeyDataLength(const unsigned char*& d) {
1040 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1041 unsigned DataLen = (unsigned) *d++;
1042 return std::make_pair(KeyLen + 1, DataLen);
1043 }
1044
1045 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1046 return (const char *)d;
1047 }
1048
1049 static data_type ReadData(const internal_key_type, const unsigned char *d,
1050 unsigned /*DataLen*/) {
1051 using namespace clang::io;
1052
1053 if (*d++ == 1)
1054 return data_type();
1055
1056 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1057 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1058 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +00001059 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +00001060 off_t size = (off_t) ReadUnalignedLE64(d);
1061 return data_type(ino, dev, mode, mtime, size);
1062 }
1063};
1064
1065/// \brief stat() cache for precompiled headers.
1066///
1067/// This cache is very similar to the stat cache used by pretokenized
1068/// headers.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001069class ASTStatCache : public StatSysCallCache {
1070 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregorc5046832009-04-27 18:38:38 +00001071 CacheTy *Cache;
1072
1073 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +00001074public:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001075 ASTStatCache(const unsigned char *Buckets,
Douglas Gregorc5046832009-04-27 18:38:38 +00001076 const unsigned char *Base,
1077 unsigned &NumStatHits,
Mike Stump11289f42009-09-09 15:08:12 +00001078 unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +00001079 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1080 Cache = CacheTy::Create(Buckets, Base);
1081 }
1082
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001083 ~ASTStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +00001084
Douglas Gregorc5046832009-04-27 18:38:38 +00001085 int stat(const char *path, struct stat *buf) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001086 // Do the lookup for the file's data in the AST file.
Douglas Gregorc5046832009-04-27 18:38:38 +00001087 CacheTy::iterator I = Cache->find(path);
1088
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001089 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregorc5046832009-04-27 18:38:38 +00001090 if (I == Cache->end()) {
1091 ++NumStatMisses;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001092 return StatSysCallCache::stat(path, buf);
Douglas Gregorc5046832009-04-27 18:38:38 +00001093 }
Mike Stump11289f42009-09-09 15:08:12 +00001094
Douglas Gregorc5046832009-04-27 18:38:38 +00001095 ++NumStatHits;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001096 ASTStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001097
Douglas Gregorc5046832009-04-27 18:38:38 +00001098 if (!Data.hasStat)
1099 return 1;
1100
1101 buf->st_ino = Data.ino;
1102 buf->st_dev = Data.dev;
1103 buf->st_mtime = Data.mtime;
1104 buf->st_mode = Data.mode;
1105 buf->st_size = Data.size;
1106 return 0;
1107 }
1108};
1109} // end anonymous namespace
1110
1111
Sebastian Redl393f8b72010-07-19 20:52:06 +00001112/// \brief Read a source manager block
Sebastian Redl2c499f62010-08-18 23:56:43 +00001113ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001114 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +00001115
Sebastian Redl393f8b72010-07-19 20:52:06 +00001116 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001117
Douglas Gregor258ae542009-04-27 06:38:32 +00001118 // Set the source-location entry cursor to the current position in
1119 // the stream. This cursor will be used to read the contents of the
1120 // source manager block initially, and then lazily read
1121 // source-location entries as needed.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001122 SLocEntryCursor = F.Stream;
Douglas Gregor258ae542009-04-27 06:38:32 +00001123
1124 // The stream itself is going to skip over the source manager block.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001125 if (F.Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001126 Error("malformed block record in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001127 return Failure;
1128 }
1129
1130 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001131 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001132 Error("malformed source manager block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001133 return Failure;
1134 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001135
Douglas Gregora7f71a92009-04-10 03:52:48 +00001136 RecordData Record;
1137 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001138 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001139 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001140 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001141 Error("error at end of Source Manager block in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001142 return Failure;
1143 }
Douglas Gregor92863e42009-04-10 23:10:45 +00001144 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001145 }
Mike Stump11289f42009-09-09 15:08:12 +00001146
Douglas Gregora7f71a92009-04-10 03:52:48 +00001147 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1148 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +00001149 SLocEntryCursor.ReadSubBlockID();
1150 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001151 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001152 return Failure;
1153 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001154 continue;
1155 }
Mike Stump11289f42009-09-09 15:08:12 +00001156
Douglas Gregora7f71a92009-04-10 03:52:48 +00001157 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001158 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001159 continue;
1160 }
Mike Stump11289f42009-09-09 15:08:12 +00001161
Douglas Gregora7f71a92009-04-10 03:52:48 +00001162 // Read a record.
1163 const char *BlobStart;
1164 unsigned BlobLen;
1165 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +00001166 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001167 default: // Default behavior: ignore.
1168 break;
1169
Sebastian Redl539c5062010-08-18 23:57:32 +00001170 case SM_LINE_TABLE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00001171 if (ParseLineTable(F, Record))
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001172 return Failure;
Chris Lattner184e65d2009-04-14 23:22:57 +00001173 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00001174
Sebastian Redl539c5062010-08-18 23:57:32 +00001175 case SM_SLOC_FILE_ENTRY:
1176 case SM_SLOC_BUFFER_ENTRY:
1177 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor258ae542009-04-27 06:38:32 +00001178 // Once we hit one of the source location entries, we're done.
1179 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001180 }
1181 }
1182}
1183
Sebastian Redl06750302010-07-20 21:50:20 +00001184/// \brief Get a cursor that's correctly positioned for reading the source
1185/// location entry with the given ID.
Sebastian Redl2c373b92010-10-05 15:59:54 +00001186ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl06750302010-07-20 21:50:20 +00001187 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1188 "SLocCursorForID should only be called for real IDs.");
1189
1190 ID -= 1;
1191 PerFileData *F = 0;
1192 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1193 F = Chain[N - I - 1];
1194 if (ID < F->LocalNumSLocEntries)
1195 break;
1196 ID -= F->LocalNumSLocEntries;
1197 }
1198 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1199
1200 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00001201 return F;
Sebastian Redl06750302010-07-20 21:50:20 +00001202}
1203
Douglas Gregor258ae542009-04-27 06:38:32 +00001204/// \brief Read in the source location entry with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001205ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001206 if (ID == 0)
1207 return Success;
1208
1209 if (ID > TotalNumSLocEntries) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001210 Error("source location entry ID out-of-range for AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001211 return Failure;
1212 }
1213
Sebastian Redl2c373b92010-10-05 15:59:54 +00001214 PerFileData *F = SLocCursorForID(ID);
1215 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001216
Douglas Gregor258ae542009-04-27 06:38:32 +00001217 ++NumSLocEntriesRead;
Douglas Gregor258ae542009-04-27 06:38:32 +00001218 unsigned Code = SLocEntryCursor.ReadCode();
1219 if (Code == llvm::bitc::END_BLOCK ||
1220 Code == llvm::bitc::ENTER_SUBBLOCK ||
1221 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001222 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001223 return Failure;
1224 }
1225
Douglas Gregor258ae542009-04-27 06:38:32 +00001226 RecordData Record;
1227 const char *BlobStart;
1228 unsigned BlobLen;
1229 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1230 default:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001231 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001232 return Failure;
1233
Sebastian Redl539c5062010-08-18 23:57:32 +00001234 case SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001235 std::string Filename(BlobStart, BlobStart + BlobLen);
1236 MaybeAddSystemRootToFilename(Filename);
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00001237 const FileEntry *File = FileMgr.getFile(Filename, FileSystemOpts);
Chris Lattnerd20dc872009-06-15 04:35:16 +00001238 if (File == 0) {
1239 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001240 ErrorStr += Filename;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001241 ErrorStr += "' referenced by AST file";
Chris Lattnerd20dc872009-06-15 04:35:16 +00001242 Error(ErrorStr.c_str());
1243 return Failure;
1244 }
Mike Stump11289f42009-09-09 15:08:12 +00001245
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001246 if (Record.size() < 10) {
Ted Kremenekabb1ddd2010-03-18 21:23:05 +00001247 Error("source location entry is incorrect");
1248 return Failure;
1249 }
1250
Douglas Gregorce3a8292010-07-27 00:27:13 +00001251 if (!DisableValidation &&
1252 ((off_t)Record[4] != File->getSize()
Douglas Gregor08288f22010-04-09 15:54:22 +00001253#if !defined(LLVM_ON_WIN32)
1254 // In our regression testing, the Windows file system seems to
1255 // have inconsistent modification times that sometimes
1256 // erroneously trigger this error-handling path.
Douglas Gregorce3a8292010-07-27 00:27:13 +00001257 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor08288f22010-04-09 15:54:22 +00001258#endif
Douglas Gregorce3a8292010-07-27 00:27:13 +00001259 )) {
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001260 Diag(diag::err_fe_pch_file_modified)
1261 << Filename;
1262 return Failure;
1263 }
1264
Douglas Gregor258ae542009-04-27 06:38:32 +00001265 FileID FID = SourceMgr.createFileID(File,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001266 ReadSourceLocation(*F, Record[1]),
Douglas Gregor258ae542009-04-27 06:38:32 +00001267 (SrcMgr::CharacteristicKind)Record[2],
1268 ID, Record[0]);
1269 if (Record[3])
1270 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1271 .setHasLineDirectives();
1272
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001273 // Reconstruct header-search information for this file.
1274 HeaderFileInfo HFI;
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001275 HFI.isImport = Record[6];
1276 HFI.DirInfo = Record[7];
1277 HFI.NumIncludes = Record[8];
1278 HFI.ControllingMacroID = Record[9];
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001279 if (Listener)
1280 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor258ae542009-04-27 06:38:32 +00001281 break;
1282 }
1283
Sebastian Redl539c5062010-08-18 23:57:32 +00001284 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor258ae542009-04-27 06:38:32 +00001285 const char *Name = BlobStart;
1286 unsigned Offset = Record[0];
1287 unsigned Code = SLocEntryCursor.ReadCode();
1288 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001289 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +00001290 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001291
Sebastian Redl539c5062010-08-18 23:57:32 +00001292 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001293 Error("AST record has invalid code");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001294 return Failure;
1295 }
1296
Douglas Gregor258ae542009-04-27 06:38:32 +00001297 llvm::MemoryBuffer *Buffer
Chris Lattner58c79342010-04-05 22:42:27 +00001298 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1299 Name);
Douglas Gregor258ae542009-04-27 06:38:32 +00001300 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump11289f42009-09-09 15:08:12 +00001301
Douglas Gregore6648fb2009-04-28 20:33:11 +00001302 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001303 PCHPredefinesBlock Block = {
1304 BufferID,
1305 llvm::StringRef(BlobStart, BlobLen - 1)
1306 };
1307 PCHPredefinesBuffers.push_back(Block);
Douglas Gregore6648fb2009-04-28 20:33:11 +00001308 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001309
1310 break;
1311 }
1312
Sebastian Redl539c5062010-08-18 23:57:32 +00001313 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001314 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001315 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001316 ReadSourceLocation(*F, Record[2]),
1317 ReadSourceLocation(*F, Record[3]),
Douglas Gregor258ae542009-04-27 06:38:32 +00001318 Record[4],
1319 ID,
1320 Record[0]);
1321 break;
Mike Stump11289f42009-09-09 15:08:12 +00001322 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001323 }
1324
1325 return Success;
1326}
1327
Chris Lattnere78a6be2009-04-27 01:05:14 +00001328/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1329/// specified cursor. Read the abbreviations that are at the top of the block
1330/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001331bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001332 unsigned BlockID) {
1333 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001334 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001335 return Failure;
1336 }
Mike Stump11289f42009-09-09 15:08:12 +00001337
Chris Lattnere78a6be2009-04-27 01:05:14 +00001338 while (true) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001339 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattnere78a6be2009-04-27 01:05:14 +00001340 unsigned Code = Cursor.ReadCode();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001341
Chris Lattnere78a6be2009-04-27 01:05:14 +00001342 // We expect all abbrevs to be at the start of the block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001343 if (Code != llvm::bitc::DEFINE_ABBREV) {
1344 Cursor.JumpToBit(Offset);
Chris Lattnere78a6be2009-04-27 01:05:14 +00001345 return false;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001346 }
Chris Lattnere78a6be2009-04-27 01:05:14 +00001347 Cursor.ReadAbbrevRecord();
1348 }
1349}
1350
Sebastian Redl2c373b92010-10-05 15:59:54 +00001351void ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001352 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregor796d76a2010-10-20 22:00:55 +00001353 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump11289f42009-09-09 15:08:12 +00001354
Douglas Gregorc3366a52009-04-21 23:56:24 +00001355 // Keep track of where we are in the stream, then jump back there
1356 // after reading this macro.
1357 SavedStreamPosition SavedPosition(Stream);
1358
1359 Stream.JumpToBit(Offset);
1360 RecordData Record;
1361 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1362 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001363
Douglas Gregorc3366a52009-04-21 23:56:24 +00001364 while (true) {
1365 unsigned Code = Stream.ReadCode();
1366 switch (Code) {
1367 case llvm::bitc::END_BLOCK:
1368 return;
1369
1370 case llvm::bitc::ENTER_SUBBLOCK:
1371 // No known subblocks, always skip them.
1372 Stream.ReadSubBlockID();
1373 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001374 Error("malformed block record in AST file");
Douglas Gregorc3366a52009-04-21 23:56:24 +00001375 return;
1376 }
1377 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001378
Douglas Gregorc3366a52009-04-21 23:56:24 +00001379 case llvm::bitc::DEFINE_ABBREV:
1380 Stream.ReadAbbrevRecord();
1381 continue;
1382 default: break;
1383 }
1384
1385 // Read a record.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001386 const char *BlobStart = 0;
1387 unsigned BlobLen = 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001388 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001389 PreprocessorRecordTypes RecType =
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001390 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001391 BlobLen);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001392 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001393 case PP_MACRO_OBJECT_LIKE:
1394 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001395 // If we already have a macro, that means that we've hit the end
1396 // of the definition of the macro we were looking for. We're
1397 // done.
1398 if (Macro)
1399 return;
1400
1401 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1402 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001403 Error("macro must have a name in AST file");
Douglas Gregorc3366a52009-04-21 23:56:24 +00001404 return;
1405 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00001406 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001407 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001408
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001409 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001410 MI->setIsUsed(isUsed);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001411 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001412
Douglas Gregoraae92242010-03-19 21:51:54 +00001413 unsigned NextIndex = 3;
Sebastian Redl539c5062010-08-18 23:57:32 +00001414 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001415 // Decode function-like macro info.
1416 bool isC99VarArgs = Record[3];
1417 bool isGNUVarArgs = Record[4];
1418 MacroArgs.clear();
1419 unsigned NumArgs = Record[5];
Douglas Gregoraae92242010-03-19 21:51:54 +00001420 NextIndex = 6 + NumArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001421 for (unsigned i = 0; i != NumArgs; ++i)
1422 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1423
1424 // Install function-like macro info.
1425 MI->setIsFunctionLike();
1426 if (isC99VarArgs) MI->setIsC99Varargs();
1427 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001428 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001429 PP->getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001430 }
1431
1432 // Finally, install the macro.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001433 PP->setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001434
1435 // Remember that we saw this macro last so that we add the tokens that
1436 // form its body to it.
1437 Macro = MI;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001438
Douglas Gregoraae92242010-03-19 21:51:54 +00001439 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1440 // We have a macro definition. Load it now.
1441 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1442 getMacroDefinition(Record[NextIndex]));
1443 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001444
Douglas Gregorc3366a52009-04-21 23:56:24 +00001445 ++NumMacrosRead;
1446 break;
1447 }
Mike Stump11289f42009-09-09 15:08:12 +00001448
Sebastian Redl539c5062010-08-18 23:57:32 +00001449 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001450 // If we see a TOKEN before a PP_MACRO_*, then the file is
1451 // erroneous, just pretend we didn't see this.
1452 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001453
Douglas Gregorc3366a52009-04-21 23:56:24 +00001454 Token Tok;
1455 Tok.startToken();
Sebastian Redl2c373b92010-10-05 15:59:54 +00001456 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001457 Tok.setLength(Record[1]);
1458 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1459 Tok.setIdentifierInfo(II);
1460 Tok.setKind((tok::TokenKind)Record[3]);
1461 Tok.setFlag((Token::TokenFlags)Record[4]);
1462 Macro->AddTokenToBody(Tok);
1463 break;
1464 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001465
Sebastian Redl539c5062010-08-18 23:57:32 +00001466 case PP_MACRO_INSTANTIATION: {
Douglas Gregoraae92242010-03-19 21:51:54 +00001467 // If we already have a macro, that means that we've hit the end
1468 // of the definition of the macro we were looking for. We're
1469 // done.
1470 if (Macro)
1471 return;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001472
Douglas Gregoraae92242010-03-19 21:51:54 +00001473 if (!PP->getPreprocessingRecord()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001474 Error("missing preprocessing record in AST file");
Douglas Gregoraae92242010-03-19 21:51:54 +00001475 return;
1476 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001477
Douglas Gregoraae92242010-03-19 21:51:54 +00001478 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1479 if (PPRec.getPreprocessedEntity(Record[0]))
1480 return;
1481
1482 MacroInstantiation *MI
1483 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
Sebastian Redl2c373b92010-10-05 15:59:54 +00001484 SourceRange(ReadSourceLocation(F, Record[1]),
1485 ReadSourceLocation(F, Record[2])),
Douglas Gregoraae92242010-03-19 21:51:54 +00001486 getMacroDefinition(Record[4]));
1487 PPRec.SetPreallocatedEntity(Record[0], MI);
1488 return;
1489 }
1490
Sebastian Redl539c5062010-08-18 23:57:32 +00001491 case PP_MACRO_DEFINITION: {
Douglas Gregoraae92242010-03-19 21:51:54 +00001492 // If we already have a macro, that means that we've hit the end
1493 // of the definition of the macro we were looking for. We're
1494 // done.
1495 if (Macro)
1496 return;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001497
Douglas Gregoraae92242010-03-19 21:51:54 +00001498 if (!PP->getPreprocessingRecord()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001499 Error("missing preprocessing record in AST file");
Douglas Gregoraae92242010-03-19 21:51:54 +00001500 return;
1501 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001502
Douglas Gregoraae92242010-03-19 21:51:54 +00001503 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1504 if (PPRec.getPreprocessedEntity(Record[0]))
1505 return;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001506
Douglas Gregor91096292010-10-02 19:29:26 +00001507 if (Record[1] > MacroDefinitionsLoaded.size()) {
Douglas Gregoraae92242010-03-19 21:51:54 +00001508 Error("out-of-bounds macro definition record");
1509 return;
1510 }
1511
Douglas Gregor91096292010-10-02 19:29:26 +00001512 // Decode the identifier info and then check again; if the macro is
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001513 // still defined and associated with the identifier,
Douglas Gregor91096292010-10-02 19:29:26 +00001514 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1515 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1516 MacroDefinition *MD
1517 = new (PPRec) MacroDefinition(II,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001518 ReadSourceLocation(F, Record[5]),
Douglas Gregor36ea4d42010-10-01 20:33:34 +00001519 SourceRange(
Sebastian Redl2c373b92010-10-05 15:59:54 +00001520 ReadSourceLocation(F, Record[2]),
1521 ReadSourceLocation(F, Record[3])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001522
Douglas Gregor91096292010-10-02 19:29:26 +00001523 PPRec.SetPreallocatedEntity(Record[0], MD);
1524 MacroDefinitionsLoaded[Record[1] - 1] = MD;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001525
Douglas Gregor91096292010-10-02 19:29:26 +00001526 if (DeserializationListener)
1527 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1528 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001529
Douglas Gregoraae92242010-03-19 21:51:54 +00001530 return;
1531 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001532
Douglas Gregor796d76a2010-10-20 22:00:55 +00001533 case PP_INCLUSION_DIRECTIVE: {
1534 // If we already have a macro, that means that we've hit the end
1535 // of the definition of the macro we were looking for. We're
1536 // done.
1537 if (Macro)
1538 return;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001539
Douglas Gregor796d76a2010-10-20 22:00:55 +00001540 if (!PP->getPreprocessingRecord()) {
1541 Error("missing preprocessing record in AST file");
1542 return;
1543 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001544
Douglas Gregor796d76a2010-10-20 22:00:55 +00001545 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1546 if (PPRec.getPreprocessedEntity(Record[0]))
1547 return;
1548
1549 const char *FullFileNameStart = BlobStart + Record[3];
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001550 const FileEntry *File
Douglas Gregor796d76a2010-10-20 22:00:55 +00001551 = PP->getFileManager().getFile(FullFileNameStart,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00001552 FullFileNameStart + (BlobLen - Record[3]),
1553 FileSystemOpts);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001554
Douglas Gregor796d76a2010-10-20 22:00:55 +00001555 // FIXME: Stable encoding
1556 InclusionDirective::InclusionKind Kind
1557 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1558 InclusionDirective *ID
Douglas Gregorf09b6c92010-11-01 15:03:47 +00001559 = new (PPRec) InclusionDirective(PPRec, Kind,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001560 llvm::StringRef(BlobStart, Record[3]),
1561 Record[4],
1562 File,
1563 SourceRange(ReadSourceLocation(F, Record[1]),
1564 ReadSourceLocation(F, Record[2])));
1565 PPRec.SetPreallocatedEntity(Record[0], ID);
1566 return;
1567 }
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001568 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001569 }
1570}
1571
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001572void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1573 uint64_t Offset) {
1574 // Note that this identifier has a macro definition.
1575 II->setHasMacroDefinition(true);
1576
1577 // Adjust the offset based on our position in the chain.
1578 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1579 if (Chain[I] == &F)
1580 break;
1581
1582 Offset += Chain[I]->SizeInBits;
1583 }
1584
1585 UnreadMacroRecordOffsets[II] = Offset;
1586}
1587
Sebastian Redl2c499f62010-08-18 23:56:43 +00001588void ASTReader::ReadDefinedMacros() {
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001589 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001590 PerFileData &F = *Chain[N - I - 1];
1591 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001592
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001593 // If there was no preprocessor block, skip this file.
1594 if (!MacroCursor.getBitStreamReader())
1595 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001596
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001597 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001598 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001599
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001600 RecordData Record;
1601 while (true) {
Sebastian Redl4102dd52010-09-28 02:55:49 +00001602 uint64_t Offset = Cursor.GetCurrentBitNo();
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001603 unsigned Code = Cursor.ReadCode();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001604 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001605 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001606
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001607 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1608 // No known subblocks, always skip them.
1609 Cursor.ReadSubBlockID();
1610 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001611 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001612 return;
1613 }
1614 continue;
1615 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001616
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001617 if (Code == llvm::bitc::DEFINE_ABBREV) {
1618 Cursor.ReadAbbrevRecord();
1619 continue;
1620 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001621
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001622 // Read a record.
1623 const char *BlobStart;
1624 unsigned BlobLen;
1625 Record.clear();
1626 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1627 default: // Default behavior: ignore.
1628 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001629
Sebastian Redl539c5062010-08-18 23:57:32 +00001630 case PP_MACRO_OBJECT_LIKE:
1631 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001632 DecodeIdentifierInfo(Record[0]);
1633 break;
1634
Sebastian Redl539c5062010-08-18 23:57:32 +00001635 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001636 // Ignore tokens.
1637 break;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001638
Sebastian Redl539c5062010-08-18 23:57:32 +00001639 case PP_MACRO_INSTANTIATION:
1640 case PP_MACRO_DEFINITION:
Douglas Gregor796d76a2010-10-20 22:00:55 +00001641 case PP_INCLUSION_DIRECTIVE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001642 // Read the macro record.
Sebastian Redl4102dd52010-09-28 02:55:49 +00001643 // FIXME: That's a stupid way to do this. We should reuse this cursor.
Sebastian Redl2c373b92010-10-05 15:59:54 +00001644 ReadMacroRecord(F, Offset);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001645 break;
1646 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001647 }
1648 }
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001649
1650 // Drain the unread macro-record offsets map.
1651 while (!UnreadMacroRecordOffsets.empty())
1652 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1653}
1654
1655void ASTReader::LoadMacroDefinition(
1656 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1657 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1658 PerFileData *F = 0;
1659 uint64_t Offset = Pos->second;
1660 UnreadMacroRecordOffsets.erase(Pos);
1661
1662 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1663 if (Offset < Chain[I]->SizeInBits) {
1664 F = Chain[I];
1665 break;
1666 }
1667
1668 Offset -= Chain[I]->SizeInBits;
1669 }
1670 if (!F) {
1671 Error("Malformed macro record offset");
1672 return;
1673 }
1674
1675 ReadMacroRecord(*F, Offset);
1676}
1677
1678void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1679 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1680 = UnreadMacroRecordOffsets.find(II);
1681 LoadMacroDefinition(Pos);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001682}
1683
Sebastian Redl50e26582010-09-15 19:54:06 +00001684MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor91096292010-10-02 19:29:26 +00001685 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregoraae92242010-03-19 21:51:54 +00001686 return 0;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001687
Douglas Gregor91096292010-10-02 19:29:26 +00001688 if (!MacroDefinitionsLoaded[ID - 1]) {
1689 unsigned Index = ID - 1;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001690 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1691 PerFileData &F = *Chain[N - I - 1];
1692 if (Index < F.LocalNumMacroDefinitions) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001693 ReadMacroRecord(F, F.MacroDefinitionOffsets[Index]);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001694 break;
1695 }
1696 Index -= F.LocalNumMacroDefinitions;
1697 }
Douglas Gregor91096292010-10-02 19:29:26 +00001698 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001699 }
1700
Douglas Gregor91096292010-10-02 19:29:26 +00001701 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001702}
1703
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001704/// \brief If we are loading a relocatable PCH file, and the filename is
1705/// not an absolute path, add the system root to the beginning of the file
1706/// name.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001707void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001708 // If this is not a relocatable PCH file, there's nothing to do.
1709 if (!RelocatablePCH)
1710 return;
Mike Stump11289f42009-09-09 15:08:12 +00001711
Daniel Dunbarf2ce9a22009-11-18 19:50:41 +00001712 if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001713 return;
1714
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001715 if (isysroot == 0) {
1716 // If no system root was given, default to '/'
1717 Filename.insert(Filename.begin(), '/');
1718 return;
1719 }
Mike Stump11289f42009-09-09 15:08:12 +00001720
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001721 unsigned Length = strlen(isysroot);
1722 if (isysroot[Length - 1] != '/')
1723 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001724
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001725 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1726}
1727
Sebastian Redl2c499f62010-08-18 23:56:43 +00001728ASTReader::ASTReadResult
Sebastian Redl3e31c722010-08-18 23:56:56 +00001729ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001730 llvm::BitstreamCursor &Stream = F.Stream;
1731
Sebastian Redl539c5062010-08-18 23:57:32 +00001732 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001733 Error("malformed block record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001734 return Failure;
1735 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001736
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001737 // Read all of the records and blocks for the ASt file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001738 RecordData Record;
Sebastian Redl393f8b72010-07-19 20:52:06 +00001739 bool First = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001740 while (!Stream.AtEndOfStream()) {
1741 unsigned Code = Stream.ReadCode();
1742 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001743 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001744 Error("error at end of module block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001745 return Failure;
1746 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001747
Douglas Gregor55abb232009-04-10 20:39:37 +00001748 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001749 }
1750
1751 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1752 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001753 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001754 // We lazily load the decls block, but we want to set up the
1755 // DeclsCursor cursor to point into it. Clone our current bitcode
1756 // cursor to it, enter the block and read the abbrevs in that block.
1757 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001758 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001759 if (Stream.SkipBlock() || // Skip with the main cursor.
1760 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001761 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001762 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001763 return Failure;
1764 }
1765 break;
Mike Stump11289f42009-09-09 15:08:12 +00001766
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00001767 case DECL_UPDATES_BLOCK_ID:
1768 if (Stream.SkipBlock()) {
1769 Error("malformed block record in AST file");
1770 return Failure;
1771 }
1772 break;
1773
Sebastian Redl539c5062010-08-18 23:57:32 +00001774 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001775 F.MacroCursor = Stream;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001776 if (PP)
1777 PP->setExternalSource(this);
1778
Douglas Gregor796d76a2010-10-20 22:00:55 +00001779 if (Stream.SkipBlock() ||
1780 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001781 Error("malformed block record in AST file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001782 return Failure;
1783 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001784 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001785 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001786
Sebastian Redl539c5062010-08-18 23:57:32 +00001787 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001788 switch (ReadSourceManagerBlock(F)) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001789 case Success:
1790 break;
1791
1792 case Failure:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001793 Error("malformed source manager block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001794 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001795
1796 case IgnorePCH:
1797 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001798 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001799 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001800 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00001801 First = false;
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001802 continue;
1803 }
1804
1805 if (Code == llvm::bitc::DEFINE_ABBREV) {
1806 Stream.ReadAbbrevRecord();
1807 continue;
1808 }
1809
1810 // Read and process a record.
1811 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001812 const char *BlobStart = 0;
1813 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001814 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001815 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001816 default: // Default behavior: ignore.
1817 break;
1818
Sebastian Redl539c5062010-08-18 23:57:32 +00001819 case METADATA: {
1820 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1821 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001822 : diag::warn_pch_version_too_new);
1823 return IgnorePCH;
1824 }
1825
1826 RelocatablePCH = Record[4];
1827 if (Listener) {
1828 std::string TargetTriple(BlobStart, BlobLen);
1829 if (Listener->ReadTargetTriple(TargetTriple))
1830 return IgnorePCH;
1831 }
1832 break;
1833 }
1834
Sebastian Redl539c5062010-08-18 23:57:32 +00001835 case CHAINED_METADATA: {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001836 if (!First) {
1837 Error("CHAINED_METADATA is not first record in block");
1838 return Failure;
1839 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001840 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1841 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001842 : diag::warn_pch_version_too_new);
1843 return IgnorePCH;
1844 }
1845
Sebastian Redl009e7f22010-10-05 16:15:19 +00001846 // Load the chained file, which is always a PCH file.
1847 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001848 case Failure: return Failure;
1849 // If we have to ignore the dependency, we'll have to ignore this too.
1850 case IgnorePCH: return IgnorePCH;
1851 case Success: break;
1852 }
1853 break;
1854 }
1855
Sebastian Redl539c5062010-08-18 23:57:32 +00001856 case TYPE_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001857 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001858 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001859 return Failure;
1860 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001861 F.TypeOffsets = (const uint32_t *)BlobStart;
1862 F.LocalNumTypes = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001863 break;
1864
Sebastian Redl539c5062010-08-18 23:57:32 +00001865 case DECL_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001866 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001867 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001868 return Failure;
1869 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001870 F.DeclOffsets = (const uint32_t *)BlobStart;
1871 F.LocalNumDecls = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001872 break;
Douglas Gregor55abb232009-04-10 20:39:37 +00001873
Sebastian Redl539c5062010-08-18 23:57:32 +00001874 case TU_UPDATE_LEXICAL: {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001875 DeclContextInfo Info = {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001876 /* No visible information */ 0,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001877 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
1878 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001879 };
Douglas Gregoraa433012010-10-01 01:18:02 +00001880 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
1881 .push_back(Info);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001882 break;
1883 }
1884
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001885 case UPDATE_VISIBLE: {
1886 serialization::DeclID ID = Record[0];
1887 void *Table = ASTDeclContextNameLookupTable::Create(
1888 (const unsigned char *)BlobStart + Record[1],
1889 (const unsigned char *)BlobStart,
1890 ASTDeclContextNameLookupTrait(*this));
Douglas Gregoraa433012010-10-01 01:18:02 +00001891 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001892 DeclContextInfo Info = {
1893 Table, /* No lexical inforamtion */ 0, 0
1894 };
1895 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1896 } else
1897 PendingVisibleUpdates[ID].push_back(Table);
1898 break;
1899 }
1900
Sebastian Redl539c5062010-08-18 23:57:32 +00001901 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001902 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1903 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001904 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001905 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1906 Latest > FirstLatestDeclIDs[First]) &&
1907 "The new latest is supposed to come after the previous latest");
1908 FirstLatestDeclIDs[First] = Latest;
1909 }
1910 break;
1911 }
1912
Sebastian Redl539c5062010-08-18 23:57:32 +00001913 case LANGUAGE_OPTIONS:
Douglas Gregorce3a8292010-07-27 00:27:13 +00001914 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor55abb232009-04-10 20:39:37 +00001915 return IgnorePCH;
1916 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00001917
Sebastian Redl539c5062010-08-18 23:57:32 +00001918 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001919 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00001920 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001921 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001922 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00001923 (const unsigned char *)F.IdentifierTableData + Record[0],
1924 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001925 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001926 if (PP)
1927 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00001928 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001929 break;
1930
Sebastian Redl539c5062010-08-18 23:57:32 +00001931 case IDENTIFIER_OFFSET:
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001932 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001933 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001934 return Failure;
1935 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001936 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1937 F.LocalNumIdentifiers = Record[0];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001938 break;
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001939
Sebastian Redl539c5062010-08-18 23:57:32 +00001940 case EXTERNAL_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001941 // Optimization for the first block.
1942 if (ExternalDefinitions.empty())
1943 ExternalDefinitions.swap(Record);
1944 else
1945 ExternalDefinitions.insert(ExternalDefinitions.end(),
1946 Record.begin(), Record.end());
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001947 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00001948
Sebastian Redl539c5062010-08-18 23:57:32 +00001949 case SPECIAL_TYPES:
Sebastian Redlb293a452010-07-20 21:20:32 +00001950 // Optimization for the first block
1951 if (SpecialTypes.empty())
1952 SpecialTypes.swap(Record);
1953 else
1954 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregor652d82a2009-04-18 05:55:16 +00001955 break;
1956
Sebastian Redl539c5062010-08-18 23:57:32 +00001957 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001958 TotalNumStatements += Record[0];
1959 TotalNumMacros += Record[1];
1960 TotalLexicalDeclContexts += Record[2];
1961 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00001962 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00001963
Sebastian Redl539c5062010-08-18 23:57:32 +00001964 case TENTATIVE_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001965 // Optimization for the first block.
1966 if (TentativeDefinitions.empty())
1967 TentativeDefinitions.swap(Record);
1968 else
1969 TentativeDefinitions.insert(TentativeDefinitions.end(),
1970 Record.begin(), Record.end());
Douglas Gregord4df8652009-04-22 22:02:47 +00001971 break;
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001972
Sebastian Redl539c5062010-08-18 23:57:32 +00001973 case UNUSED_FILESCOPED_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001974 // Optimization for the first block.
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00001975 if (UnusedFileScopedDecls.empty())
1976 UnusedFileScopedDecls.swap(Record);
Sebastian Redlb293a452010-07-20 21:20:32 +00001977 else
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00001978 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1979 Record.begin(), Record.end());
Tanya Lattner90073802010-02-12 00:07:30 +00001980 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001981
Sebastian Redl539c5062010-08-18 23:57:32 +00001982 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001983 // Later blocks overwrite earlier ones.
1984 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00001985 break;
1986
Sebastian Redl539c5062010-08-18 23:57:32 +00001987 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001988 // Optimization for the first block.
1989 if (LocallyScopedExternalDecls.empty())
1990 LocallyScopedExternalDecls.swap(Record);
1991 else
1992 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1993 Record.begin(), Record.end());
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001994 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001995
Sebastian Redl539c5062010-08-18 23:57:32 +00001996 case SELECTOR_OFFSETS:
Sebastian Redla19a67f2010-08-03 21:58:15 +00001997 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00001998 F.LocalNumSelectors = Record[0];
Douglas Gregor95c13f52009-04-25 17:48:32 +00001999 break;
2000
Sebastian Redl539c5062010-08-18 23:57:32 +00002001 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00002002 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002003 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00002004 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002005 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00002006 F.SelectorLookupTableData + Record[0],
2007 F.SelectorLookupTableData,
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002008 ASTSelectorLookupTrait(*this));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002009 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002010 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00002011
Sebastian Redl96371b42010-09-22 00:42:30 +00002012 case REFERENCED_SELECTOR_POOL:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002013 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002014 break;
2015
Sebastian Redl539c5062010-08-18 23:57:32 +00002016 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002017 if (!Record.empty() && Listener)
2018 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002019 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002020
Sebastian Redl539c5062010-08-18 23:57:32 +00002021 case SOURCE_LOCATION_OFFSETS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002022 F.SLocOffsets = (const uint32_t *)BlobStart;
2023 F.LocalNumSLocEntries = Record[0];
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002024 F.LocalSLocSize = Record[1];
Douglas Gregor258ae542009-04-27 06:38:32 +00002025 break;
2026
Sebastian Redl539c5062010-08-18 23:57:32 +00002027 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl96371b42010-09-22 00:42:30 +00002028 if (PreloadSLocEntries.empty())
2029 PreloadSLocEntries.swap(Record);
2030 else
2031 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2032 Record.begin(), Record.end());
Douglas Gregor258ae542009-04-27 06:38:32 +00002033 break;
Douglas Gregorc5046832009-04-27 18:38:38 +00002034
Sebastian Redl539c5062010-08-18 23:57:32 +00002035 case STAT_CACHE: {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002036 ASTStatCache *MyStatCache =
2037 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
Douglas Gregord2eb58a2009-10-16 18:18:30 +00002038 (const unsigned char *)BlobStart,
2039 NumStatHits, NumStatMisses);
2040 FileMgr.addStatCache(MyStatCache);
Sebastian Redl34522812010-07-16 17:50:48 +00002041 F.StatCache = MyStatCache;
Douglas Gregorc5046832009-04-27 18:38:38 +00002042 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00002043 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002044
Sebastian Redl539c5062010-08-18 23:57:32 +00002045 case EXT_VECTOR_DECLS:
Sebastian Redl04f5c312010-07-28 21:38:49 +00002046 // Optimization for the first block.
2047 if (ExtVectorDecls.empty())
2048 ExtVectorDecls.swap(Record);
2049 else
2050 ExtVectorDecls.insert(ExtVectorDecls.end(),
2051 Record.begin(), Record.end());
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002052 break;
2053
Sebastian Redl539c5062010-08-18 23:57:32 +00002054 case VTABLE_USES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002055 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002056 VTableUses.swap(Record);
2057 break;
2058
Sebastian Redl539c5062010-08-18 23:57:32 +00002059 case DYNAMIC_CLASSES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002060 // Optimization for the first block.
2061 if (DynamicClasses.empty())
2062 DynamicClasses.swap(Record);
2063 else
2064 DynamicClasses.insert(DynamicClasses.end(),
2065 Record.begin(), Record.end());
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002066 break;
2067
Sebastian Redl539c5062010-08-18 23:57:32 +00002068 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002069 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002070 break;
2071
Sebastian Redl539c5062010-08-18 23:57:32 +00002072 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002073 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002074 SemaDeclRefs.swap(Record);
2075 break;
2076
Sebastian Redl539c5062010-08-18 23:57:32 +00002077 case ORIGINAL_FILE_NAME:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002078 // The primary AST will be the last to get here, so it will be the one
Sebastian Redlb293a452010-07-20 21:20:32 +00002079 // that's used.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00002080 ActualOriginalFileName.assign(BlobStart, BlobLen);
2081 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002082 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00002083 break;
Mike Stump11289f42009-09-09 15:08:12 +00002084
Sebastian Redl539c5062010-08-18 23:57:32 +00002085 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00002086 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002087 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2088 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2089 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregord54f3a12009-10-05 21:07:28 +00002090 return IgnorePCH;
2091 }
2092 break;
2093 }
Sebastian Redlfa061442010-07-21 20:07:32 +00002094
Sebastian Redl539c5062010-08-18 23:57:32 +00002095 case MACRO_DEFINITION_OFFSETS:
Sebastian Redlfa061442010-07-21 20:07:32 +00002096 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2097 F.NumPreallocatedPreprocessingEntities = Record[0];
2098 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregoraae92242010-03-19 21:51:54 +00002099 break;
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002100
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002101 case DECL_UPDATE_OFFSETS: {
2102 if (Record.size() % 2 != 0) {
2103 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2104 return Failure;
2105 }
2106 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2107 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2108 .push_back(std::make_pair(&F, Record[I+1]));
2109 break;
2110 }
2111
Sebastian Redl539c5062010-08-18 23:57:32 +00002112 case DECL_REPLACEMENTS: {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002113 if (Record.size() % 2 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002114 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002115 return Failure;
2116 }
2117 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl539c5062010-08-18 23:57:32 +00002118 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002119 std::make_pair(&F, Record[I+1]);
2120 break;
2121 }
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002122
2123 case CXX_BASE_SPECIFIER_OFFSETS: {
2124 if (F.LocalNumCXXBaseSpecifiers != 0) {
2125 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2126 return Failure;
2127 }
2128
2129 F.LocalNumCXXBaseSpecifiers = Record[0];
2130 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2131 break;
2132 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002133
2134 case DIAG_USER_MAPPINGS:
2135 if (Record.size() % 2 != 0) {
2136 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2137 return Failure;
2138 }
2139 if (UserDiagMappings.empty())
2140 UserDiagMappings.swap(Record);
2141 else
2142 UserDiagMappings.insert(UserDiagMappings.end(),
2143 Record.begin(), Record.end());
2144 break;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002145 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00002146 First = false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002147 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002148 Error("premature end of bitstream in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002149 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002150}
2151
Sebastian Redl009e7f22010-10-05 16:15:19 +00002152ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2153 ASTFileType Type) {
2154 switch(ReadASTCore(FileName, Type)) {
Sebastian Redl2abc0382010-07-16 20:41:52 +00002155 case Failure: return Failure;
2156 case IgnorePCH: return IgnorePCH;
2157 case Success: break;
2158 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002159
2160 // Here comes stuff that we only do once the entire chain is loaded.
2161
Sebastian Redl96371b42010-09-22 00:42:30 +00002162 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redlfa061442010-07-21 20:07:32 +00002163 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redlada023c2010-08-04 20:40:17 +00002164 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2165 TotalNumSelectors = 0;
Sebastian Redl9e687992010-07-19 22:06:55 +00002166 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl96371b42010-09-22 00:42:30 +00002167 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002168 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002169 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl9e687992010-07-19 22:06:55 +00002170 TotalNumTypes += Chain[I]->LocalNumTypes;
2171 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redlfa061442010-07-21 20:07:32 +00002172 TotalNumPreallocatedPreprocessingEntities +=
2173 Chain[I]->NumPreallocatedPreprocessingEntities;
2174 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redlada023c2010-08-04 20:40:17 +00002175 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl9e687992010-07-19 22:06:55 +00002176 }
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002177 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002178 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl9e687992010-07-19 22:06:55 +00002179 TypesLoaded.resize(TotalNumTypes);
2180 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redlfa061442010-07-21 20:07:32 +00002181 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2182 if (PP) {
2183 if (TotalNumIdentifiers > 0)
2184 PP->getHeaderSearchInfo().SetExternalLookup(this);
2185 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2186 if (!PP->getPreprocessingRecord())
2187 PP->createPreprocessingRecord();
2188 PP->getPreprocessingRecord()->SetExternalSource(*this,
2189 TotalNumPreallocatedPreprocessingEntities);
2190 }
2191 }
Sebastian Redlada023c2010-08-04 20:40:17 +00002192 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl96371b42010-09-22 00:42:30 +00002193 // Preload SLocEntries.
2194 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2195 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2196 if (Result != Success)
2197 return Result;
2198 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002199
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002200 // Check the predefines buffers.
Douglas Gregorce3a8292010-07-27 00:27:13 +00002201 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002202 return IgnorePCH;
2203
2204 if (PP) {
2205 // Initialization of keywords and pragmas occurs before the
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002206 // AST file is read, so there may be some identifiers that were
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002207 // loaded into the IdentifierTable before we intercepted the
2208 // creation of identifiers. Iterate through the list of known
2209 // identifiers and determine whether we have to establish
2210 // preprocessor definitions or top-level identifier declaration
2211 // chains for those identifiers.
2212 //
2213 // We copy the IdentifierInfo pointers to a small vector first,
2214 // since de-serializing declarations or macro definitions can add
2215 // new entries into the identifier table, invalidating the
2216 // iterators.
2217 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2218 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2219 IdEnd = PP->getIdentifierTable().end();
2220 Id != IdEnd; ++Id)
2221 Identifiers.push_back(Id->second);
Sebastian Redlfa061442010-07-21 20:07:32 +00002222 // We need to search the tables in all files.
Sebastian Redlfa061442010-07-21 20:07:32 +00002223 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002224 ASTIdentifierLookupTable *IdTable
2225 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2226 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl5c415f32010-07-22 17:01:13 +00002227 // ones.
2228 if (!IdTable)
2229 continue;
Sebastian Redlfa061442010-07-21 20:07:32 +00002230 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2231 IdentifierInfo *II = Identifiers[I];
2232 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00002233 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redlfa061442010-07-21 20:07:32 +00002234 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002235 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redlb293a452010-07-20 21:20:32 +00002236 if (Pos == IdTable->end())
2237 continue;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002238
Sebastian Redlb293a452010-07-20 21:20:32 +00002239 // Dereferencing the iterator has the effect of populating the
2240 // IdentifierInfo node with the various declarations it needs.
2241 (void)*Pos;
2242 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002243 }
2244 }
2245
2246 if (Context)
2247 InitializeContext(*Context);
2248
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002249 if (DeserializationListener)
2250 DeserializationListener->ReaderInitialized(this);
2251
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002252 return Success;
2253}
2254
Sebastian Redl009e7f22010-10-05 16:15:19 +00002255ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2256 ASTFileType Type) {
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002257 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl009e7f22010-10-05 16:15:19 +00002258 Chain.push_back(new PerFileData(Type));
Sebastian Redl34522812010-07-16 17:50:48 +00002259 PerFileData &F = *Chain.back();
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002260 if (Prev)
2261 Prev->NextInSource = &F;
2262 else
2263 FirstInSource = &F;
2264 F.Loaders.push_back(Prev);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002265
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002266 // Set the AST file name.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002267 F.FileName = FileName;
2268
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002269 // Open the AST file.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002270 //
2271 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2272 std::string ErrStr;
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00002273 if (FileName == "-")
2274 F.Buffer.reset(llvm::MemoryBuffer::getSTDIN(&ErrStr));
2275 else
2276 F.Buffer.reset(FileMgr.getBufferForFile(FileName, FileSystemOpts, &ErrStr));
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002277 if (!F.Buffer) {
2278 Error(ErrStr.c_str());
2279 return IgnorePCH;
2280 }
2281
2282 // Initialize the stream
2283 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2284 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl34522812010-07-16 17:50:48 +00002285 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002286 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002287 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002288
2289 // Sniff for the signature.
2290 if (Stream.Read(8) != 'C' ||
2291 Stream.Read(8) != 'P' ||
2292 Stream.Read(8) != 'C' ||
2293 Stream.Read(8) != 'H') {
2294 Diag(diag::err_not_a_pch_file) << FileName;
2295 return Failure;
2296 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002297
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002298 while (!Stream.AtEndOfStream()) {
2299 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002300
Douglas Gregor92863e42009-04-10 23:10:45 +00002301 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002302 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002303 return Failure;
2304 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002305
2306 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002307
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002308 // We only know the AST subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002309 switch (BlockID) {
2310 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002311 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002312 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002313 return Failure;
2314 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002315 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00002316 case AST_BLOCK_ID:
Sebastian Redl3e31c722010-08-18 23:56:56 +00002317 switch (ReadASTBlock(F)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002318 case Success:
2319 break;
2320
2321 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00002322 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00002323
2324 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00002325 // FIXME: We could consider reading through to the end of this
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002326 // AST block, skipping subblocks, to see if there are other
2327 // AST blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00002328
2329 // Clear out any preallocated source location entries, so that
2330 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002331 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00002332
2333 // Remove the stat cache.
Sebastian Redl34522812010-07-16 17:50:48 +00002334 if (F.StatCache)
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002335 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00002336
Douglas Gregor92863e42009-04-10 23:10:45 +00002337 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002338 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002339 break;
2340 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002341 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002342 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002343 return Failure;
2344 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002345 break;
2346 }
Mike Stump11289f42009-09-09 15:08:12 +00002347 }
2348
Sebastian Redl2abc0382010-07-16 20:41:52 +00002349 return Success;
2350}
2351
Sebastian Redl2c499f62010-08-18 23:56:43 +00002352void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002353 PP = &pp;
Sebastian Redlfa061442010-07-21 20:07:32 +00002354
2355 unsigned TotalNum = 0;
2356 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2357 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2358 if (TotalNum) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002359 if (!PP->getPreprocessingRecord())
2360 PP->createPreprocessingRecord();
Sebastian Redlfa061442010-07-21 20:07:32 +00002361 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregoraae92242010-03-19 21:51:54 +00002362 }
2363}
2364
Sebastian Redl2c499f62010-08-18 23:56:43 +00002365void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002366 Context = &Ctx;
2367 assert(Context && "Passed null context!");
2368
2369 assert(PP && "Forgot to set Preprocessor ?");
2370 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2371 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00002372 PP->setExternalSource(this);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002373
Douglas Gregoraa433012010-10-01 01:18:02 +00002374 // If we have an update block for the TU waiting, we have to add it before
2375 // deserializing the decl.
2376 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2377 if (DCU != DeclContextOffsets.end()) {
2378 // Insertion could invalidate map, so grab vector.
2379 DeclContextInfos T;
2380 T.swap(DCU->second);
2381 DeclContextOffsets.erase(DCU);
2382 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2383 }
2384
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002385 // Load the translation unit declaration
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00002386 GetTranslationUnitDecl();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002387
2388 // Load the special types.
2389 Context->setBuiltinVaListType(
Sebastian Redl539c5062010-08-18 23:57:32 +00002390 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2391 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002392 Context->setObjCIdType(GetType(Id));
Sebastian Redl539c5062010-08-18 23:57:32 +00002393 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002394 Context->setObjCSelType(GetType(Sel));
Sebastian Redl539c5062010-08-18 23:57:32 +00002395 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002396 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl539c5062010-08-18 23:57:32 +00002397 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002398 Context->setObjCClassType(GetType(Class));
Steve Naroff7cae42b2009-07-10 23:34:53 +00002399
Sebastian Redl539c5062010-08-18 23:57:32 +00002400 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002401 Context->setCFConstantStringType(GetType(String));
Mike Stump11289f42009-09-09 15:08:12 +00002402 if (unsigned FastEnum
Sebastian Redl539c5062010-08-18 23:57:32 +00002403 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002404 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl539c5062010-08-18 23:57:32 +00002405 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregor27821ce2009-07-07 16:35:42 +00002406 QualType FileType = GetType(File);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002407 if (FileType.isNull()) {
2408 Error("FILE type is NULL");
2409 return;
2410 }
John McCall9dd450b2009-09-21 23:43:11 +00002411 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregor27821ce2009-07-07 16:35:42 +00002412 Context->setFILEDecl(Typedef->getDecl());
2413 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002414 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002415 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002416 Error("Invalid FILE type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002417 return;
2418 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002419 Context->setFILEDecl(Tag->getDecl());
2420 }
2421 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002422 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002423 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002424 if (Jmp_bufType.isNull()) {
2425 Error("jmp_bug type is NULL");
2426 return;
2427 }
John McCall9dd450b2009-09-21 23:43:11 +00002428 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002429 Context->setjmp_bufDecl(Typedef->getDecl());
2430 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002431 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002432 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002433 Error("Invalid jmp_buf type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002434 return;
2435 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002436 Context->setjmp_bufDecl(Tag->getDecl());
2437 }
2438 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002439 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002440 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002441 if (Sigjmp_bufType.isNull()) {
2442 Error("sigjmp_buf type is NULL");
2443 return;
2444 }
John McCall9dd450b2009-09-21 23:43:11 +00002445 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002446 Context->setsigjmp_bufDecl(Typedef->getDecl());
2447 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002448 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002449 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stumpa4de80b2009-07-28 02:25:19 +00002450 Context->setsigjmp_bufDecl(Tag->getDecl());
2451 }
2452 }
Mike Stump11289f42009-09-09 15:08:12 +00002453 if (unsigned ObjCIdRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002454 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002455 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump11289f42009-09-09 15:08:12 +00002456 if (unsigned ObjCClassRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002457 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002458 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002459 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpd0153282009-10-20 02:12:22 +00002460 Context->setBlockDescriptorType(GetType(String));
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002461 if (unsigned String
Sebastian Redl539c5062010-08-18 23:57:32 +00002462 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002463 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002464 if (unsigned ObjCSelRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002465 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002466 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002467 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002468 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002469
Sebastian Redl539c5062010-08-18 23:57:32 +00002470 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002471 Context->setInt128Installed();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002472
2473 ReadUserDiagnosticMappings(Context->getDiagnostics());
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002474}
2475
Douglas Gregor45fe0362009-05-12 01:31:05 +00002476/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002477/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00002478/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002479std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00002480 FileManager &FileMgr,
2481 const FileSystemOptions &FSOpts,
Daniel Dunbar3b951482009-12-03 09:13:06 +00002482 Diagnostic &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002483 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002484 std::string ErrStr;
2485 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00002486 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, FSOpts, &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00002487 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00002488 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002489 return std::string();
2490 }
2491
2492 // Initialize the stream
2493 llvm::BitstreamReader StreamFile;
2494 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00002495 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00002496 (const unsigned char *)Buffer->getBufferEnd());
2497 Stream.init(StreamFile);
2498
2499 // Sniff for the signature.
2500 if (Stream.Read(8) != 'C' ||
2501 Stream.Read(8) != 'P' ||
2502 Stream.Read(8) != 'C' ||
2503 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002504 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002505 return std::string();
2506 }
2507
2508 RecordData Record;
2509 while (!Stream.AtEndOfStream()) {
2510 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002511
Douglas Gregor45fe0362009-05-12 01:31:05 +00002512 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2513 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00002514
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002515 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002516 switch (BlockID) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002517 case AST_BLOCK_ID:
2518 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002519 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002520 return std::string();
2521 }
2522 break;
Mike Stump11289f42009-09-09 15:08:12 +00002523
Douglas Gregor45fe0362009-05-12 01:31:05 +00002524 default:
2525 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002526 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002527 return std::string();
2528 }
2529 break;
2530 }
2531 continue;
2532 }
2533
2534 if (Code == llvm::bitc::END_BLOCK) {
2535 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002536 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002537 return std::string();
2538 }
2539 continue;
2540 }
2541
2542 if (Code == llvm::bitc::DEFINE_ABBREV) {
2543 Stream.ReadAbbrevRecord();
2544 continue;
2545 }
2546
2547 Record.clear();
2548 const char *BlobStart = 0;
2549 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002550 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl539c5062010-08-18 23:57:32 +00002551 == ORIGINAL_FILE_NAME)
Douglas Gregor45fe0362009-05-12 01:31:05 +00002552 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00002553 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00002554
2555 return std::string();
2556}
2557
Douglas Gregor55abb232009-04-10 20:39:37 +00002558/// \brief Parse the record that corresponds to a LangOptions data
2559/// structure.
2560///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002561/// This routine parses the language options from the AST file and then gives
2562/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00002563///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002564/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002565bool ASTReader::ParseLanguageOptions(
Douglas Gregor55abb232009-04-10 20:39:37 +00002566 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002567 if (Listener) {
2568 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00002569
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002570 #define PARSE_LANGOPT(Option) \
2571 LangOpts.Option = Record[Idx]; \
2572 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00002573
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002574 unsigned Idx = 0;
2575 PARSE_LANGOPT(Trigraphs);
2576 PARSE_LANGOPT(BCPLComment);
2577 PARSE_LANGOPT(DollarIdents);
2578 PARSE_LANGOPT(AsmPreprocessor);
2579 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00002580 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002581 PARSE_LANGOPT(ImplicitInt);
2582 PARSE_LANGOPT(Digraphs);
2583 PARSE_LANGOPT(HexFloats);
2584 PARSE_LANGOPT(C99);
2585 PARSE_LANGOPT(Microsoft);
2586 PARSE_LANGOPT(CPlusPlus);
2587 PARSE_LANGOPT(CPlusPlus0x);
2588 PARSE_LANGOPT(CXXOperatorNames);
2589 PARSE_LANGOPT(ObjC1);
2590 PARSE_LANGOPT(ObjC2);
2591 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00002592 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00002593 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002594 PARSE_LANGOPT(PascalStrings);
2595 PARSE_LANGOPT(WritableStrings);
2596 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00002597 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002598 PARSE_LANGOPT(Exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +00002599 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002600 PARSE_LANGOPT(NeXTRuntime);
2601 PARSE_LANGOPT(Freestanding);
2602 PARSE_LANGOPT(NoBuiltin);
2603 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00002604 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002605 PARSE_LANGOPT(Blocks);
2606 PARSE_LANGOPT(EmitAllDecls);
2607 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00002608 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2609 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002610 PARSE_LANGOPT(HeinousExtensions);
2611 PARSE_LANGOPT(Optimize);
2612 PARSE_LANGOPT(OptimizeSize);
2613 PARSE_LANGOPT(Static);
2614 PARSE_LANGOPT(PICLevel);
2615 PARSE_LANGOPT(GNUInline);
2616 PARSE_LANGOPT(NoInline);
2617 PARSE_LANGOPT(AccessControl);
2618 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00002619 PARSE_LANGOPT(ShortWChar);
Chris Lattner51924e512010-06-26 21:25:03 +00002620 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall457a04e2010-10-22 21:05:15 +00002621 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00002622 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00002623 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002624 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00002625 PARSE_LANGOPT(OpenCL);
Mike Stumpd9546382009-12-12 01:27:46 +00002626 PARSE_LANGOPT(CatchUndefined);
2627 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002628 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00002629
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002630 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00002631 }
Douglas Gregor55abb232009-04-10 20:39:37 +00002632
2633 return false;
2634}
2635
Sebastian Redl2c499f62010-08-18 23:56:43 +00002636void ASTReader::ReadPreprocessedEntities() {
Douglas Gregoraae92242010-03-19 21:51:54 +00002637 ReadDefinedMacros();
2638}
2639
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002640void ASTReader::ReadUserDiagnosticMappings(Diagnostic &Diag) {
2641 unsigned Idx = 0;
2642 while (Idx < UserDiagMappings.size()) {
2643 unsigned DiagID = UserDiagMappings[Idx++];
2644 unsigned Map = UserDiagMappings[Idx++];
2645 Diag.setDiagnosticMappingInternal(DiagID, Map, /*isUser=*/true);
2646 }
2647}
2648
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002649/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002650ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002651 PerFileData *F = 0;
2652 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2653 F = Chain[N - I - 1];
2654 if (Index < F->LocalNumTypes)
2655 break;
2656 Index -= F->LocalNumTypes;
2657 }
2658 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redl2c373b92010-10-05 15:59:54 +00002659 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002660}
2661
2662/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002663///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002664/// The index is the type ID, shifted and minus the number of predefs. This
2665/// routine actually reads the record corresponding to the type at the given
2666/// location. It is a helper routine for GetType, which deals with reading type
2667/// IDs.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002668QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002669 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00002670 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00002671
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002672 // Keep track of where we are in the stream, then jump back there
2673 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002674 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002675
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002676 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00002677
Douglas Gregor1342e842009-07-06 18:54:52 +00002678 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00002679 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00002680
Sebastian Redl2c373b92010-10-05 15:59:54 +00002681 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002682 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002683 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00002684 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2685 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002686 if (Record.size() != 2) {
2687 Error("Incorrect encoding of extended qualifier type");
2688 return QualType();
2689 }
Douglas Gregor455b8f42009-04-15 22:00:08 +00002690 QualType Base = GetType(Record[0]);
John McCall8ccfcb52009-09-24 19:53:00 +00002691 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2692 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00002693 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002694
Sebastian Redl539c5062010-08-18 23:57:32 +00002695 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002696 if (Record.size() != 1) {
2697 Error("Incorrect encoding of complex type");
2698 return QualType();
2699 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002700 QualType ElemType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002701 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002702 }
2703
Sebastian Redl539c5062010-08-18 23:57:32 +00002704 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002705 if (Record.size() != 1) {
2706 Error("Incorrect encoding of pointer type");
2707 return QualType();
2708 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002709 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002710 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002711 }
2712
Sebastian Redl539c5062010-08-18 23:57:32 +00002713 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002714 if (Record.size() != 1) {
2715 Error("Incorrect encoding of block pointer type");
2716 return QualType();
2717 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002718 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002719 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002720 }
2721
Sebastian Redl539c5062010-08-18 23:57:32 +00002722 case TYPE_LVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002723 if (Record.size() != 1) {
2724 Error("Incorrect encoding of lvalue reference type");
2725 return QualType();
2726 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002727 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002728 return Context->getLValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002729 }
2730
Sebastian Redl539c5062010-08-18 23:57:32 +00002731 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002732 if (Record.size() != 1) {
2733 Error("Incorrect encoding of rvalue reference type");
2734 return QualType();
2735 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002736 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002737 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002738 }
2739
Sebastian Redl539c5062010-08-18 23:57:32 +00002740 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00002741 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002742 Error("Incorrect encoding of member pointer type");
2743 return QualType();
2744 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002745 QualType PointeeType = GetType(Record[0]);
2746 QualType ClassType = GetType(Record[1]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002747 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002748 }
2749
Sebastian Redl539c5062010-08-18 23:57:32 +00002750 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002751 QualType ElementType = GetType(Record[0]);
2752 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2753 unsigned IndexTypeQuals = Record[2];
2754 unsigned Idx = 3;
2755 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00002756 return Context->getConstantArrayType(ElementType, Size,
2757 ASM, IndexTypeQuals);
2758 }
2759
Sebastian Redl539c5062010-08-18 23:57:32 +00002760 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002761 QualType ElementType = GetType(Record[0]);
2762 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2763 unsigned IndexTypeQuals = Record[2];
Chris Lattner8575daa2009-04-27 21:45:14 +00002764 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002765 }
2766
Sebastian Redl539c5062010-08-18 23:57:32 +00002767 case TYPE_VARIABLE_ARRAY: {
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002768 QualType ElementType = GetType(Record[0]);
2769 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2770 unsigned IndexTypeQuals = Record[2];
Sebastian Redl2c373b92010-10-05 15:59:54 +00002771 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
2772 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
2773 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor04318252009-07-06 15:59:29 +00002774 ASM, IndexTypeQuals,
2775 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002776 }
2777
Sebastian Redl539c5062010-08-18 23:57:32 +00002778 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002779 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002780 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002781 return QualType();
2782 }
2783
2784 QualType ElementType = GetType(Record[0]);
2785 unsigned NumElements = Record[1];
Chris Lattner37141f42010-06-23 06:00:24 +00002786 unsigned AltiVecSpec = Record[2];
2787 return Context->getVectorType(ElementType, NumElements,
2788 (VectorType::AltiVecSpecific)AltiVecSpec);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002789 }
2790
Sebastian Redl539c5062010-08-18 23:57:32 +00002791 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002792 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002793 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002794 return QualType();
2795 }
2796
2797 QualType ElementType = GetType(Record[0]);
2798 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00002799 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002800 }
2801
Sebastian Redl539c5062010-08-18 23:57:32 +00002802 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002803 if (Record.size() != 4) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002804 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002805 return QualType();
2806 }
2807 QualType ResultType = GetType(Record[0]);
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002808 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00002809 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002810 }
2811
Sebastian Redl539c5062010-08-18 23:57:32 +00002812 case TYPE_FUNCTION_PROTO: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002813 QualType ResultType = GetType(Record[0]);
Douglas Gregordc728752009-12-22 18:11:50 +00002814 bool NoReturn = Record[1];
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002815 unsigned RegParm = Record[2];
2816 CallingConv CallConv = (CallingConv)Record[3];
2817 unsigned Idx = 4;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002818 unsigned NumParams = Record[Idx++];
2819 llvm::SmallVector<QualType, 16> ParamTypes;
2820 for (unsigned I = 0; I != NumParams; ++I)
2821 ParamTypes.push_back(GetType(Record[Idx++]));
2822 bool isVariadic = Record[Idx++];
2823 unsigned Quals = Record[Idx++];
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002824 bool hasExceptionSpec = Record[Idx++];
2825 bool hasAnyExceptionSpec = Record[Idx++];
2826 unsigned NumExceptions = Record[Idx++];
2827 llvm::SmallVector<QualType, 2> Exceptions;
2828 for (unsigned I = 0; I != NumExceptions; ++I)
2829 Exceptions.push_back(GetType(Record[Idx++]));
Jay Foad7d0479f2009-05-21 09:52:38 +00002830 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002831 isVariadic, Quals, hasExceptionSpec,
2832 hasAnyExceptionSpec, NumExceptions,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00002833 Exceptions.data(),
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002834 FunctionType::ExtInfo(NoReturn, RegParm,
2835 CallConv));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002836 }
2837
Sebastian Redl539c5062010-08-18 23:57:32 +00002838 case TYPE_UNRESOLVED_USING:
John McCallb96ec562009-12-04 22:46:56 +00002839 return Context->getTypeDeclType(
2840 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2841
Sebastian Redl539c5062010-08-18 23:57:32 +00002842 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002843 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002844 Error("incorrect encoding of typedef type");
2845 return QualType();
2846 }
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002847 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2848 QualType Canonical = GetType(Record[1]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00002849 if (!Canonical.isNull())
2850 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002851 return Context->getTypedefType(Decl, Canonical);
2852 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002853
Sebastian Redl539c5062010-08-18 23:57:32 +00002854 case TYPE_TYPEOF_EXPR:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002855 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002856
Sebastian Redl539c5062010-08-18 23:57:32 +00002857 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002858 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002859 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002860 return QualType();
2861 }
2862 QualType UnderlyingType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002863 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002864 }
Mike Stump11289f42009-09-09 15:08:12 +00002865
Sebastian Redl539c5062010-08-18 23:57:32 +00002866 case TYPE_DECLTYPE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002867 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson81df7b82009-06-24 19:06:50 +00002868
Sebastian Redl539c5062010-08-18 23:57:32 +00002869 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002870 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002871 Error("incorrect encoding of record type");
2872 return QualType();
2873 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002874 bool IsDependent = Record[0];
2875 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCall25c9d112010-10-14 21:48:26 +00002876 T->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002877 return T;
2878 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002879
Sebastian Redl539c5062010-08-18 23:57:32 +00002880 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002881 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002882 Error("incorrect encoding of enum type");
2883 return QualType();
2884 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002885 bool IsDependent = Record[0];
2886 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCall25c9d112010-10-14 21:48:26 +00002887 T->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002888 return T;
2889 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00002890
Sebastian Redl539c5062010-08-18 23:57:32 +00002891 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002892 unsigned Idx = 0;
2893 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2894 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2895 QualType NamedType = GetType(Record[Idx++]);
2896 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00002897 }
2898
Sebastian Redl539c5062010-08-18 23:57:32 +00002899 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00002900 unsigned Idx = 0;
2901 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00002902 return Context->getObjCInterfaceType(ItfD);
2903 }
2904
Sebastian Redl539c5062010-08-18 23:57:32 +00002905 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00002906 unsigned Idx = 0;
2907 QualType Base = GetType(Record[Idx++]);
Chris Lattner587cbe12009-04-22 06:45:28 +00002908 unsigned NumProtos = Record[Idx++];
2909 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2910 for (unsigned I = 0; I != NumProtos; ++I)
2911 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002912 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00002913 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002914
Sebastian Redl539c5062010-08-18 23:57:32 +00002915 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00002916 unsigned Idx = 0;
John McCall8b07ec22010-05-15 11:32:37 +00002917 QualType Pointee = GetType(Record[Idx++]);
2918 return Context->getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00002919 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00002920
Sebastian Redl539c5062010-08-18 23:57:32 +00002921 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00002922 unsigned Idx = 0;
2923 QualType Parm = GetType(Record[Idx++]);
2924 QualType Replacement = GetType(Record[Idx++]);
2925 return
2926 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2927 Replacement);
2928 }
John McCalle78aac42010-03-10 03:28:59 +00002929
Sebastian Redl539c5062010-08-18 23:57:32 +00002930 case TYPE_INJECTED_CLASS_NAME: {
John McCalle78aac42010-03-10 03:28:59 +00002931 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2932 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00002933 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002934 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00002935 return
2936 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00002937 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002938
Sebastian Redl539c5062010-08-18 23:57:32 +00002939 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002940 unsigned Idx = 0;
2941 unsigned Depth = Record[Idx++];
2942 unsigned Index = Record[Idx++];
2943 bool Pack = Record[Idx++];
2944 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
2945 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
2946 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002947
Sebastian Redl539c5062010-08-18 23:57:32 +00002948 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002949 unsigned Idx = 0;
2950 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2951 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2952 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00002953 QualType Canon = GetType(Record[Idx++]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00002954 if (!Canon.isNull())
2955 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00002956 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002957 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002958
Sebastian Redl539c5062010-08-18 23:57:32 +00002959 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002960 unsigned Idx = 0;
2961 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2962 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2963 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2964 unsigned NumArgs = Record[Idx++];
2965 llvm::SmallVector<TemplateArgument, 8> Args;
2966 Args.reserve(NumArgs);
2967 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00002968 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002969 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
2970 Args.size(), Args.data());
2971 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002972
Sebastian Redl539c5062010-08-18 23:57:32 +00002973 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00002974 unsigned Idx = 0;
2975
2976 // ArrayType
2977 QualType ElementType = GetType(Record[Idx++]);
2978 ArrayType::ArraySizeModifier ASM
2979 = (ArrayType::ArraySizeModifier)Record[Idx++];
2980 unsigned IndexTypeQuals = Record[Idx++];
2981
2982 // DependentSizedArrayType
Sebastian Redl2c373b92010-10-05 15:59:54 +00002983 Expr *NumElts = ReadExpr(*Loc.F);
2984 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00002985
2986 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
2987 IndexTypeQuals, Brackets);
2988 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002989
Sebastian Redl539c5062010-08-18 23:57:32 +00002990 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002991 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002992 bool IsDependent = Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002993 TemplateName Name = ReadTemplateName(Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002994 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redl2c373b92010-10-05 15:59:54 +00002995 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002996 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002997 QualType T;
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002998 if (Canon.isNull())
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002999 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3000 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003001 else
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003002 T = Context->getTemplateSpecializationType(Name, Args.data(),
3003 Args.size(), Canon);
John McCall25c9d112010-10-14 21:48:26 +00003004 T->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003005 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003006 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003007 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003008 // Suppress a GCC warning
3009 return QualType();
3010}
3011
Sebastian Redl2c373b92010-10-05 15:59:54 +00003012class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00003013 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003014 ASTReader::PerFileData &F;
Sebastian Redlc67764e2010-07-22 22:43:28 +00003015 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redl2c499f62010-08-18 23:56:43 +00003016 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00003017 unsigned &Idx;
3018
Sebastian Redl2c373b92010-10-05 15:59:54 +00003019 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3020 unsigned &I) {
3021 return Reader.ReadSourceLocation(F, R, I);
3022 }
3023
John McCall8f115c62009-10-16 21:56:05 +00003024public:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003025 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl2c499f62010-08-18 23:56:43 +00003026 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003027 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3028 { }
John McCall8f115c62009-10-16 21:56:05 +00003029
John McCall17001972009-10-18 01:05:36 +00003030 // We want compile-time assurance that we've enumerated all of
3031 // these, so unfortunately we have to declare them first, then
3032 // define them out-of-line.
3033#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00003034#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00003035 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00003036#include "clang/AST/TypeLocNodes.def"
3037
John McCall17001972009-10-18 01:05:36 +00003038 void VisitFunctionTypeLoc(FunctionTypeLoc);
3039 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00003040};
3041
John McCall17001972009-10-18 01:05:36 +00003042void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00003043 // nothing to do
3044}
John McCall17001972009-10-18 01:05:36 +00003045void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003046 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorc9b7a592010-01-18 18:04:31 +00003047 if (TL.needsExtraLocalData()) {
3048 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3049 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3050 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3051 TL.setModeAttr(Record[Idx++]);
3052 }
John McCall8f115c62009-10-16 21:56:05 +00003053}
John McCall17001972009-10-18 01:05:36 +00003054void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003055 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003056}
John McCall17001972009-10-18 01:05:36 +00003057void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003058 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003059}
John McCall17001972009-10-18 01:05:36 +00003060void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003061 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003062}
John McCall17001972009-10-18 01:05:36 +00003063void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003064 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003065}
John McCall17001972009-10-18 01:05:36 +00003066void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003067 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003068}
John McCall17001972009-10-18 01:05:36 +00003069void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003070 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003071}
John McCall17001972009-10-18 01:05:36 +00003072void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003073 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3074 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003075 if (Record[Idx++])
Sebastian Redl2c373b92010-10-05 15:59:54 +00003076 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003077 else
John McCall17001972009-10-18 01:05:36 +00003078 TL.setSizeExpr(0);
3079}
3080void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3081 VisitArrayTypeLoc(TL);
3082}
3083void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3084 VisitArrayTypeLoc(TL);
3085}
3086void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3087 VisitArrayTypeLoc(TL);
3088}
3089void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3090 DependentSizedArrayTypeLoc TL) {
3091 VisitArrayTypeLoc(TL);
3092}
3093void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3094 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003095 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003096}
3097void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003098 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003099}
3100void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003101 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003102}
3103void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003104 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3105 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor7fb25412010-10-01 18:44:50 +00003106 TL.setTrailingReturn(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00003107 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCalle6347002009-10-23 01:28:53 +00003108 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall17001972009-10-18 01:05:36 +00003109 }
3110}
3111void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3112 VisitFunctionTypeLoc(TL);
3113}
3114void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3115 VisitFunctionTypeLoc(TL);
3116}
John McCallb96ec562009-12-04 22:46:56 +00003117void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003118 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallb96ec562009-12-04 22:46:56 +00003119}
John McCall17001972009-10-18 01:05:36 +00003120void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003121 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003122}
3123void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003124 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3125 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3126 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003127}
3128void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003129 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3130 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3131 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3132 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003133}
3134void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003135 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003136}
3137void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003138 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003139}
3140void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003141 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003142}
John McCall17001972009-10-18 01:05:36 +00003143void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003144 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003145}
John McCallcebee162009-10-18 09:09:24 +00003146void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3147 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003148 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallcebee162009-10-18 09:09:24 +00003149}
John McCall17001972009-10-18 01:05:36 +00003150void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3151 TemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003152 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3153 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3154 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall0ad16662009-10-29 08:12:44 +00003155 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3156 TL.setArgLocInfo(i,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003157 Reader.GetTemplateArgumentLocInfo(F,
3158 TL.getTypePtr()->getArg(i).getKind(),
3159 Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003160}
Abramo Bagnara6150c882010-05-11 21:36:43 +00003161void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003162 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3163 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003164}
John McCalle78aac42010-03-10 03:28:59 +00003165void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003166 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalle78aac42010-03-10 03:28:59 +00003167}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00003168void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003169 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3170 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3171 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003172}
John McCallc392f372010-06-11 00:33:02 +00003173void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3174 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003175 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3176 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3177 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3178 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3179 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003180 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3181 TL.setArgLocInfo(I,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003182 Reader.GetTemplateArgumentLocInfo(F,
3183 TL.getTypePtr()->getArg(I).getKind(),
3184 Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003185}
John McCall17001972009-10-18 01:05:36 +00003186void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003187 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8b07ec22010-05-15 11:32:37 +00003188}
3189void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3190 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003191 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3192 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003193 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003194 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003195}
John McCallfc93cf92009-10-22 22:37:11 +00003196void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003197 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCallfc93cf92009-10-22 22:37:11 +00003198}
John McCall8f115c62009-10-16 21:56:05 +00003199
Sebastian Redl2c373b92010-10-05 15:59:54 +00003200TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003201 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00003202 unsigned &Idx) {
3203 QualType InfoTy = GetType(Record[Idx++]);
3204 if (InfoTy.isNull())
3205 return 0;
3206
John McCallbcd03502009-12-07 02:54:59 +00003207 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003208 TypeLocReader TLR(*this, F, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00003209 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00003210 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00003211 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00003212}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003213
Sebastian Redl539c5062010-08-18 23:57:32 +00003214QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00003215 unsigned FastQuals = ID & Qualifiers::FastMask;
3216 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003217
Sebastian Redl539c5062010-08-18 23:57:32 +00003218 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003219 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00003220 switch ((PredefinedTypeIDs)Index) {
3221 case PREDEF_TYPE_NULL_ID: return QualType();
3222 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3223 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003224
Sebastian Redl539c5062010-08-18 23:57:32 +00003225 case PREDEF_TYPE_CHAR_U_ID:
3226 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003227 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00003228 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003229 break;
3230
Sebastian Redl539c5062010-08-18 23:57:32 +00003231 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3232 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3233 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3234 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3235 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3236 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3237 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3238 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3239 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3240 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3241 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3242 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3243 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3244 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3245 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3246 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3247 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3248 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3249 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3250 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3251 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3252 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3253 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3254 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003255 }
3256
3257 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00003258 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003259 }
3260
Sebastian Redl539c5062010-08-18 23:57:32 +00003261 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003262 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00003263 if (TypesLoaded[Index].isNull()) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003264 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003265 if (TypesLoaded[Index].isNull())
3266 return QualType();
3267
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003268 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003269 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003270 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003271 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003272 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00003273 }
Mike Stump11289f42009-09-09 15:08:12 +00003274
John McCall8ccfcb52009-09-24 19:53:00 +00003275 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003276}
3277
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003278TypeID ASTReader::GetTypeID(QualType T) const {
3279 return MakeTypeID(T,
3280 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3281}
3282
3283TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3284 if (T.isNull())
3285 return TypeIdx();
3286 assert(!T.getLocalFastQualifiers());
3287
3288 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3289 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3290 // comparing keys of ASTDeclContextNameLookupTable.
3291 // If the type didn't come from the AST file use a specially marked index
3292 // so that any hash/key comparison fail since no such index is stored
3293 // in a AST file.
3294 if (I == TypeIdxs.end())
3295 return TypeIdx(-1);
3296 return I->second;
3297}
3298
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003299unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3300 unsigned Result = 0;
3301 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3302 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3303
3304 return Result;
3305}
3306
John McCall0ad16662009-10-29 08:12:44 +00003307TemplateArgumentLocInfo
Sebastian Redl2c373b92010-10-05 15:59:54 +00003308ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3309 TemplateArgument::ArgKind Kind,
John McCall0ad16662009-10-29 08:12:44 +00003310 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003311 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00003312 switch (Kind) {
3313 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003314 return ReadExpr(F);
John McCall0ad16662009-10-29 08:12:44 +00003315 case TemplateArgument::Type:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003316 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003317 case TemplateArgument::Template: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003318 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3319 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003320 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003321 }
John McCall0ad16662009-10-29 08:12:44 +00003322 case TemplateArgument::Null:
3323 case TemplateArgument::Integral:
3324 case TemplateArgument::Declaration:
3325 case TemplateArgument::Pack:
3326 return TemplateArgumentLocInfo();
3327 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00003328 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00003329 return TemplateArgumentLocInfo();
3330}
3331
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003332TemplateArgumentLoc
Sebastian Redl2c373b92010-10-05 15:59:54 +00003333ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003334 const RecordData &Record, unsigned &Index) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003335 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003336
3337 if (Arg.getKind() == TemplateArgument::Expression) {
3338 if (Record[Index++]) // bool InfoHasSameExpr.
3339 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3340 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00003341 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003342 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003343}
3344
Sebastian Redl2c499f62010-08-18 23:56:43 +00003345Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00003346 return GetDecl(ID);
3347}
3348
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003349uint64_t
3350ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3351 if (ID == 0)
3352 return 0;
3353
3354 --ID;
3355 uint64_t Offset = 0;
3356 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3357 if (ID < Chain[I]->LocalNumCXXBaseSpecifiers)
3358 return Offset + Chain[I]->CXXBaseSpecifiersOffsets[ID];
3359
3360 ID -= Chain[I]->LocalNumCXXBaseSpecifiers;
3361 Offset += Chain[I]->SizeInBits;
3362 }
3363
3364 assert(false && "CXXBaseSpecifiers not found");
3365 return 0;
3366}
3367
3368CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3369 // Figure out which AST file contains this offset.
3370 PerFileData *F = 0;
3371 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3372 if (Offset < Chain[I]->SizeInBits) {
3373 F = Chain[I];
3374 break;
3375 }
3376
3377 Offset -= Chain[I]->SizeInBits;
3378 }
3379
3380 if (!F) {
3381 Error("Malformed AST file: C++ base specifiers at impossible offset");
3382 return 0;
3383 }
3384
3385 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3386 SavedStreamPosition SavedPosition(Cursor);
3387 Cursor.JumpToBit(Offset);
3388 ReadingKindTracker ReadingKind(Read_Decl, *this);
3389 RecordData Record;
3390 unsigned Code = Cursor.ReadCode();
3391 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3392 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3393 Error("Malformed AST file: missing C++ base specifiers");
3394 return 0;
3395 }
3396
3397 unsigned Idx = 0;
3398 unsigned NumBases = Record[Idx++];
3399 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3400 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3401 for (unsigned I = 0; I != NumBases; ++I)
3402 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3403 return Bases;
3404}
3405
Sebastian Redl2c499f62010-08-18 23:56:43 +00003406TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003407 if (!DeclsLoaded[0]) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00003408 ReadDeclRecord(0, 1);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003409 if (DeserializationListener)
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003410 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003411 }
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00003412
3413 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3414}
3415
Sebastian Redl539c5062010-08-18 23:57:32 +00003416Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003417 if (ID == 0)
3418 return 0;
3419
Douglas Gregor745ed142009-04-25 18:35:21 +00003420 if (ID > DeclsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003421 Error("declaration ID out-of-range for AST file");
Douglas Gregor745ed142009-04-25 18:35:21 +00003422 return 0;
3423 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003424
Douglas Gregor745ed142009-04-25 18:35:21 +00003425 unsigned Index = ID - 1;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003426 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003427 ReadDeclRecord(Index, ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003428 if (DeserializationListener)
3429 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3430 }
Douglas Gregor745ed142009-04-25 18:35:21 +00003431
3432 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003433}
3434
Chris Lattner9c28af02009-04-27 05:46:25 +00003435/// \brief Resolve the offset of a statement into a statement.
3436///
3437/// This operation will read a new statement from the external
3438/// source each time it is called, and is meant to be used via a
3439/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00003440Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00003441 // Switch case IDs are per Decl.
3442 ClearSwitchCaseIDs();
3443
Sebastian Redl5c415f32010-07-22 17:01:13 +00003444 // Offset here is a global offset across the entire chain.
3445 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3446 PerFileData &F = *Chain[N - I - 1];
3447 if (Offset < F.SizeInBits) {
3448 // Since we know that this statement is part of a decl, make sure to use
3449 // the decl cursor to read it.
3450 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003451 return ReadStmtFromStream(F);
Sebastian Redl5c415f32010-07-22 17:01:13 +00003452 }
3453 Offset -= F.SizeInBits;
3454 }
3455 llvm_unreachable("Broken chain");
Douglas Gregor3c3aa612009-04-18 00:07:54 +00003456}
3457
Sebastian Redl2c499f62010-08-18 23:56:43 +00003458bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003459 bool (*isKindWeWant)(Decl::Kind),
John McCall75b960e2010-06-01 09:23:16 +00003460 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump11289f42009-09-09 15:08:12 +00003461 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003462 "DeclContext has no lexical decls in storage");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003463
Sebastian Redl5c415f32010-07-22 17:01:13 +00003464 // There might be lexical decls in multiple parts of the chain, for the TU
3465 // at least.
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003466 // DeclContextOffsets might reallocate as we load additional decls below,
3467 // so make a copy of the vector.
3468 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl5c415f32010-07-22 17:01:13 +00003469 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3470 I != E; ++I) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003471 // IDs can be 0 if this context doesn't contain declarations.
3472 if (!I->LexicalDecls)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003473 continue;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003474
3475 // Load all of the declaration IDs
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003476 for (const KindDeclIDPair *ID = I->LexicalDecls,
3477 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3478 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3479 continue;
3480
3481 Decl *D = GetDecl(ID->second);
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003482 assert(D && "Null decl in lexical decls");
3483 Decls.push_back(D);
3484 }
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003485 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003486
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003487 ++NumLexicalDeclContextsRead;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003488 return false;
3489}
3490
John McCall75b960e2010-06-01 09:23:16 +00003491DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00003492ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00003493 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003494 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003495 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003496 if (!Name)
3497 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3498 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003499
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003500 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl471ac2f2010-08-24 00:49:55 +00003501 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003502 // and namespaces. For any given name, the last available results replace
3503 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl5c415f32010-07-22 17:01:13 +00003504 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003505 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl5c415f32010-07-22 17:01:13 +00003506 I != E; ++I) {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003507 if (!I->NameLookupTableData)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003508 continue;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003509
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003510 ASTDeclContextNameLookupTable *LookupTable =
3511 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3512 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3513 if (Pos == LookupTable->end())
Sebastian Redl5c415f32010-07-22 17:01:13 +00003514 continue;
3515
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003516 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3517 for (; Data.first != Data.second; ++Data.first)
3518 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003519 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003520 }
3521
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003522 ++NumVisibleDeclContextsRead;
John McCall75b960e2010-06-01 09:23:16 +00003523
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003524 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00003525 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003526}
3527
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00003528void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3529 assert(DC->hasExternalVisibleStorage() &&
3530 "DeclContext has no visible decls in storage");
3531
3532 llvm::SmallVector<NamedDecl *, 64> Decls;
3533 // There might be visible decls in multiple parts of the chain, for the TU
3534 // and namespaces.
3535 DeclContextInfos &Infos = DeclContextOffsets[DC];
3536 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3537 I != E; ++I) {
3538 if (!I->NameLookupTableData)
3539 continue;
3540
3541 ASTDeclContextNameLookupTable *LookupTable =
3542 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3543 for (ASTDeclContextNameLookupTable::item_iterator
3544 ItemI = LookupTable->item_begin(),
3545 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3546 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3547 = *ItemI;
3548 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3549 Decls.clear();
3550 for (; Data.first != Data.second; ++Data.first)
3551 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3552 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3553 }
3554 }
3555}
3556
Sebastian Redl2c499f62010-08-18 23:56:43 +00003557void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003558 assert(Consumer);
3559 while (!InterestingDecls.empty()) {
3560 DeclGroupRef DG(InterestingDecls.front());
3561 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00003562 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003563 }
3564}
3565
Sebastian Redl2c499f62010-08-18 23:56:43 +00003566void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00003567 this->Consumer = Consumer;
3568
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003569 if (!Consumer)
3570 return;
3571
3572 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003573 // Force deserialization of this decl, which will cause it to be queued for
3574 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00003575 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003576 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00003577
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003578 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003579}
3580
Sebastian Redl2c499f62010-08-18 23:56:43 +00003581void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003582 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003583
Mike Stump11289f42009-09-09 15:08:12 +00003584 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003585 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00003586 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00003587 unsigned NumDeclsLoaded
3588 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3589 (Decl *)0);
3590 unsigned NumIdentifiersLoaded
3591 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3592 IdentifiersLoaded.end(),
3593 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00003594 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003595 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3596 SelectorsLoaded.end(),
3597 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00003598
Douglas Gregorc5046832009-04-27 18:38:38 +00003599 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3600 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor258ae542009-04-27 06:38:32 +00003601 if (TotalNumSLocEntries)
3602 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3603 NumSLocEntriesRead, TotalNumSLocEntries,
3604 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00003605 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003606 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003607 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3608 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3609 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003610 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003611 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3612 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00003613 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003614 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00003615 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3616 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00003617 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003618 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00003619 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3620 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003621 if (TotalNumStatements)
3622 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3623 NumStatementsRead, TotalNumStatements,
3624 ((float)NumStatementsRead/TotalNumStatements * 100));
3625 if (TotalNumMacros)
3626 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3627 NumMacrosRead, TotalNumMacros,
3628 ((float)NumMacrosRead/TotalNumMacros * 100));
3629 if (TotalLexicalDeclContexts)
3630 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3631 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3632 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3633 * 100));
3634 if (TotalVisibleDeclContexts)
3635 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3636 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3637 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3638 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003639 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003640 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003641 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3642 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00003643 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003644 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003645 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003646 std::fprintf(stderr, "\n");
3647}
3648
Sebastian Redl2c499f62010-08-18 23:56:43 +00003649void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00003650 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003651 S.ExternalSource = this;
3652
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003653 // Makes sure any declarations that were deserialized "too early"
3654 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003655 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3656 if (SemaObj->TUScope)
John McCall48871652010-08-21 09:40:31 +00003657 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003658
3659 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003660 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003661 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00003662
3663 // If there were any tentative definitions, deserialize them and add
Sebastian Redl35351a92010-01-31 22:27:38 +00003664 // them to Sema's list of tentative definitions.
Douglas Gregord4df8652009-04-22 22:02:47 +00003665 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3666 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redl35351a92010-01-31 22:27:38 +00003667 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregord4df8652009-04-22 22:02:47 +00003668 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00003669
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003670 // If there were any unused file scoped decls, deserialize them and add to
3671 // Sema's list of unused file scoped decls.
3672 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3673 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3674 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattner90073802010-02-12 00:07:30 +00003675 }
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003676
3677 // If there were any locally-scoped external declarations,
3678 // deserialize them and add them to Sema's table of locally-scoped
3679 // external declarations.
3680 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3681 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3682 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3683 }
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003684
3685 // If there were any ext_vector type declarations, deserialize them
3686 // and add them to Sema's vector of such declarations.
3687 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3688 SemaObj->ExtVectorDecls.push_back(
3689 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003690
3691 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3692 // Can we cut them down before writing them ?
3693
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003694 // If there were any dynamic classes declarations, deserialize them
3695 // and add them to Sema's vector of such declarations.
3696 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3697 SemaObj->DynamicClasses.push_back(
3698 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003699
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003700 // Load the offsets of the declarations that Sema references.
3701 // They will be lazily deserialized when needed.
3702 if (!SemaDeclRefs.empty()) {
3703 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3704 SemaObj->StdNamespace = SemaDeclRefs[0];
3705 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3706 }
3707
Sebastian Redl2c373b92010-10-05 15:59:54 +00003708 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
3709
3710 // If there are @selector references added them to its pool. This is for
3711 // implementation of -Wselector.
3712 if (!F->ReferencedSelectorsData.empty()) {
3713 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
3714 unsigned I = 0;
3715 while (I < DataSize) {
3716 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
3717 SourceLocation SelLoc = ReadSourceLocation(
3718 *F, F->ReferencedSelectorsData, I);
3719 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3720 }
3721 }
3722
3723 // If there were any pending implicit instantiations, deserialize them
3724 // and add them to Sema's queue of such instantiations.
3725 assert(F->PendingInstantiations.size() % 2 == 0 &&
3726 "Expected pairs of entries");
3727 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
3728 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
3729 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
3730 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
3731 }
3732 }
3733
3734 // The two special data sets below always come from the most recent PCH,
3735 // which is at the front of the chain.
3736 PerFileData &F = *Chain.front();
3737
3738 // If there were any weak undeclared identifiers, deserialize them and add to
3739 // Sema's list of weak undeclared identifiers.
3740 if (!WeakUndeclaredIdentifiers.empty()) {
3741 unsigned Idx = 0;
3742 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3743 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3744 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3745 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
3746 bool Used = WeakUndeclaredIdentifiers[Idx++];
3747 Sema::WeakInfo WI(AliasId, Loc);
3748 WI.setUsed(Used);
3749 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3750 }
3751 }
3752
3753 // If there were any VTable uses, deserialize the information and add it
3754 // to Sema's vector and map of VTable uses.
3755 if (!VTableUses.empty()) {
3756 unsigned Idx = 0;
3757 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3758 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3759 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
3760 bool DefinitionRequired = VTableUses[Idx++];
3761 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3762 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003763 }
3764 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00003765}
3766
Sebastian Redl2c499f62010-08-18 23:56:43 +00003767IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redl78f51772010-08-02 18:30:12 +00003768 // Try to find this name within our on-disk hash tables. We start with the
3769 // most recent one, since that one contains the most up-to-date info.
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003770 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003771 ASTIdentifierLookupTable *IdTable
3772 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003773 if (!IdTable)
3774 continue;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003775 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003776 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003777 if (Pos == IdTable->end())
3778 continue;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003779
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003780 // Dereferencing the iterator has the effect of building the
3781 // IdentifierInfo node and populating it with the various
3782 // declarations it needs.
Sebastian Redl78f51772010-08-02 18:30:12 +00003783 return *Pos;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003784 }
Sebastian Redl78f51772010-08-02 18:30:12 +00003785 return 0;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003786}
3787
Douglas Gregor57756ea2010-10-14 22:11:03 +00003788namespace clang {
3789 /// \brief An identifier-lookup iterator that enumerates all of the
3790 /// identifiers stored within a set of AST files.
3791 class ASTIdentifierIterator : public IdentifierIterator {
3792 /// \brief The AST reader whose identifiers are being enumerated.
3793 const ASTReader &Reader;
3794
3795 /// \brief The current index into the chain of AST files stored in
3796 /// the AST reader.
3797 unsigned Index;
3798
3799 /// \brief The current position within the identifier lookup table
3800 /// of the current AST file.
3801 ASTIdentifierLookupTable::key_iterator Current;
3802
3803 /// \brief The end position within the identifier lookup table of
3804 /// the current AST file.
3805 ASTIdentifierLookupTable::key_iterator End;
3806
3807 public:
3808 explicit ASTIdentifierIterator(const ASTReader &Reader);
3809
3810 virtual llvm::StringRef Next();
3811 };
3812}
3813
3814ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
3815 : Reader(Reader), Index(Reader.Chain.size() - 1) {
3816 ASTIdentifierLookupTable *IdTable
3817 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3818 Current = IdTable->key_begin();
3819 End = IdTable->key_end();
3820}
3821
3822llvm::StringRef ASTIdentifierIterator::Next() {
3823 while (Current == End) {
3824 // If we have exhausted all of our AST files, we're done.
3825 if (Index == 0)
3826 return llvm::StringRef();
3827
3828 --Index;
3829 ASTIdentifierLookupTable *IdTable
3830 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3831 Current = IdTable->key_begin();
3832 End = IdTable->key_end();
3833 }
3834
3835 // We have any identifiers remaining in the current AST file; return
3836 // the next one.
3837 std::pair<const char*, unsigned> Key = *Current;
3838 ++Current;
3839 return llvm::StringRef(Key.first, Key.second);
3840}
3841
3842IdentifierIterator *ASTReader::getIdentifiers() const {
3843 return new ASTIdentifierIterator(*this);
3844}
3845
Mike Stump11289f42009-09-09 15:08:12 +00003846std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redl2c499f62010-08-18 23:56:43 +00003847ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redlada023c2010-08-04 20:40:17 +00003848 // Find this selector in a hash table. We want to find the most recent entry.
3849 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3850 PerFileData &F = *Chain[I];
3851 if (!F.SelectorLookupTable)
3852 continue;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003853
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003854 ASTSelectorLookupTable *PoolTable
3855 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3856 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redlada023c2010-08-04 20:40:17 +00003857 if (Pos != PoolTable->end()) {
3858 ++NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003859 // FIXME: Not quite happy with the statistics here. We probably should
3860 // disable this tracking when called via LoadSelector.
3861 // Also, should entries without methods count as misses?
3862 ++NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003863 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redlada023c2010-08-04 20:40:17 +00003864 if (DeserializationListener)
3865 DeserializationListener->SelectorRead(Data.ID, Sel);
3866 return std::make_pair(Data.Instance, Data.Factory);
3867 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003868 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003869
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003870 ++NumMethodPoolMisses;
Sebastian Redlada023c2010-08-04 20:40:17 +00003871 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorc78d3462009-04-24 21:10:55 +00003872}
3873
Sebastian Redl2c499f62010-08-18 23:56:43 +00003874void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003875 // It would be complicated to avoid reading the methods anyway. So don't.
3876 ReadMethodPool(Sel);
3877}
3878
Sebastian Redl2c499f62010-08-18 23:56:43 +00003879void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00003880 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00003881 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00003882 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00003883 if (DeserializationListener)
3884 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003885}
3886
Douglas Gregor1342e842009-07-06 18:54:52 +00003887/// \brief Set the globally-visible declarations associated with the given
3888/// identifier.
3889///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003890/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00003891/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00003892/// them.
3893///
3894/// \param II an IdentifierInfo that refers to one or more globally-visible
3895/// declarations.
3896///
3897/// \param DeclIDs the set of declaration IDs with the name @p II that are
3898/// visible at global scope.
3899///
3900/// \param Nonrecursive should be true to indicate that the caller knows that
3901/// this call is non-recursive, and therefore the globally-visible declarations
3902/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00003903void
Sebastian Redl2c499f62010-08-18 23:56:43 +00003904ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregor1342e842009-07-06 18:54:52 +00003905 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
3906 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003907 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00003908 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
3909 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
3910 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003911 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00003912 return;
3913 }
Mike Stump11289f42009-09-09 15:08:12 +00003914
Douglas Gregor1342e842009-07-06 18:54:52 +00003915 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
3916 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
3917 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00003918 if (SemaObj->TUScope) {
3919 // Introduce this declaration into the translation-unit scope
3920 // and add it to the declaration chain for this identifier, so
3921 // that (unqualified) name lookup will find it.
John McCall48871652010-08-21 09:40:31 +00003922 SemaObj->TUScope->AddDecl(D);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00003923 }
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003924 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregor1342e842009-07-06 18:54:52 +00003925 } else {
3926 // Queue this declaration so that it will be added to the
3927 // translation unit scope and identifier's declaration chain
3928 // once a Sema object is known.
3929 PreloadedDecls.push_back(D);
3930 }
3931 }
3932}
3933
Sebastian Redl2c499f62010-08-18 23:56:43 +00003934IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003935 if (ID == 0)
3936 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00003937
Sebastian Redlc713b962010-07-21 00:46:22 +00003938 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003939 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003940 return 0;
3941 }
Mike Stump11289f42009-09-09 15:08:12 +00003942
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003943 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc713b962010-07-21 00:46:22 +00003944 ID -= 1;
3945 if (!IdentifiersLoaded[ID]) {
3946 unsigned Index = ID;
3947 const char *Str = 0;
3948 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3949 PerFileData *F = Chain[N - I - 1];
3950 if (Index < F->LocalNumIdentifiers) {
3951 uint32_t Offset = F->IdentifierOffsets[Index];
3952 Str = F->IdentifierTableData + Offset;
3953 break;
3954 }
3955 Index -= F->LocalNumIdentifiers;
3956 }
3957 assert(Str && "Broken Chain");
Douglas Gregor5287b4e2009-04-25 21:04:17 +00003958
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003959 // All of the strings in the AST file are preceded by a 16-bit length.
3960 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00003961 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
3962 // unsigned integers. This is important to avoid integer overflow when
3963 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00003964 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00003965 unsigned StrLen = (((unsigned) StrLenPtr[0])
3966 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00003967 IdentifiersLoaded[ID]
Kovarththanan Rajaratnama3b09592010-03-12 10:32:27 +00003968 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003969 if (DeserializationListener)
3970 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003971 }
Mike Stump11289f42009-09-09 15:08:12 +00003972
Sebastian Redlc713b962010-07-21 00:46:22 +00003973 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003974}
3975
Sebastian Redl2c499f62010-08-18 23:56:43 +00003976void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00003977 ReadSLocEntryRecord(ID);
3978}
3979
Sebastian Redl2c499f62010-08-18 23:56:43 +00003980Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00003981 if (ID == 0)
3982 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00003983
Sebastian Redlada023c2010-08-04 20:40:17 +00003984 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003985 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00003986 return Selector();
3987 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003988
Sebastian Redlada023c2010-08-04 20:40:17 +00003989 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003990 // Load this selector from the selector table.
Sebastian Redlada023c2010-08-04 20:40:17 +00003991 unsigned Idx = ID - 1;
3992 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3993 PerFileData &F = *Chain[N - I - 1];
3994 if (Idx < F.LocalNumSelectors) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003995 ASTSelectorLookupTrait Trait(*this);
Sebastian Redlada023c2010-08-04 20:40:17 +00003996 SelectorsLoaded[ID - 1] =
3997 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
3998 if (DeserializationListener)
3999 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4000 break;
4001 }
4002 Idx -= F.LocalNumSelectors;
4003 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004004 }
4005
Sebastian Redlada023c2010-08-04 20:40:17 +00004006 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00004007}
4008
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004009Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00004010 return DecodeSelector(ID);
4011}
4012
Sebastian Redl2c499f62010-08-18 23:56:43 +00004013uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00004014 // ID 0 (the null selector) is considered an external selector.
4015 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00004016}
4017
Mike Stump11289f42009-09-09 15:08:12 +00004018DeclarationName
Sebastian Redl2c499f62010-08-18 23:56:43 +00004019ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004020 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4021 switch (Kind) {
4022 case DeclarationName::Identifier:
4023 return DeclarationName(GetIdentifierInfo(Record, Idx));
4024
4025 case DeclarationName::ObjCZeroArgSelector:
4026 case DeclarationName::ObjCOneArgSelector:
4027 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff3c301dc2009-04-23 15:15:40 +00004028 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004029
4030 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004031 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004032 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004033
4034 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004035 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004036 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004037
4038 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004039 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004040 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004041
4042 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004043 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004044 (OverloadedOperatorKind)Record[Idx++]);
4045
Alexis Hunt3d221f22009-11-29 07:34:05 +00004046 case DeclarationName::CXXLiteralOperatorName:
4047 return Context->DeclarationNames.getCXXLiteralOperatorName(
4048 GetIdentifierInfo(Record, Idx));
4049
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004050 case DeclarationName::CXXUsingDirective:
4051 return DeclarationName::getUsingDirectiveName();
4052 }
4053
4054 // Required to silence GCC warning
4055 return DeclarationName();
4056}
Douglas Gregor55abb232009-04-10 20:39:37 +00004057
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004058void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4059 DeclarationNameLoc &DNLoc,
4060 DeclarationName Name,
4061 const RecordData &Record, unsigned &Idx) {
4062 switch (Name.getNameKind()) {
4063 case DeclarationName::CXXConstructorName:
4064 case DeclarationName::CXXDestructorName:
4065 case DeclarationName::CXXConversionFunctionName:
4066 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4067 break;
4068
4069 case DeclarationName::CXXOperatorName:
4070 DNLoc.CXXOperatorName.BeginOpNameLoc
4071 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4072 DNLoc.CXXOperatorName.EndOpNameLoc
4073 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4074 break;
4075
4076 case DeclarationName::CXXLiteralOperatorName:
4077 DNLoc.CXXLiteralOperatorName.OpNameLoc
4078 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4079 break;
4080
4081 case DeclarationName::Identifier:
4082 case DeclarationName::ObjCZeroArgSelector:
4083 case DeclarationName::ObjCOneArgSelector:
4084 case DeclarationName::ObjCMultiArgSelector:
4085 case DeclarationName::CXXUsingDirective:
4086 break;
4087 }
4088}
4089
4090void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4091 DeclarationNameInfo &NameInfo,
4092 const RecordData &Record, unsigned &Idx) {
4093 NameInfo.setName(ReadDeclarationName(Record, Idx));
4094 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4095 DeclarationNameLoc DNLoc;
4096 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4097 NameInfo.setInfo(DNLoc);
4098}
4099
4100void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4101 const RecordData &Record, unsigned &Idx) {
4102 Info.NNS = ReadNestedNameSpecifier(Record, Idx);
4103 Info.NNSRange = ReadSourceRange(F, Record, Idx);
4104 unsigned NumTPLists = Record[Idx++];
4105 Info.NumTemplParamLists = NumTPLists;
4106 if (NumTPLists) {
4107 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4108 for (unsigned i=0; i != NumTPLists; ++i)
4109 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4110 }
4111}
4112
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004113TemplateName
Sebastian Redl2c499f62010-08-18 23:56:43 +00004114ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004115 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004116 switch (Kind) {
4117 case TemplateName::Template:
4118 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4119
4120 case TemplateName::OverloadedTemplate: {
4121 unsigned size = Record[Idx++];
4122 UnresolvedSet<8> Decls;
4123 while (size--)
4124 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4125
4126 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4127 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004128
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004129 case TemplateName::QualifiedTemplate: {
4130 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4131 bool hasTemplKeyword = Record[Idx++];
4132 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4133 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4134 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004135
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004136 case TemplateName::DependentTemplate: {
4137 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4138 if (Record[Idx++]) // isIdentifier
4139 return Context->getDependentTemplateName(NNS,
4140 GetIdentifierInfo(Record, Idx));
4141 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004142 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004143 }
4144 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004145
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004146 assert(0 && "Unhandled template name kind!");
4147 return TemplateName();
4148}
4149
4150TemplateArgument
Sebastian Redl2c373b92010-10-05 15:59:54 +00004151ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00004152 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004153 switch ((TemplateArgument::ArgKind)Record[Idx++]) {
4154 case TemplateArgument::Null:
4155 return TemplateArgument();
4156 case TemplateArgument::Type:
4157 return TemplateArgument(GetType(Record[Idx++]));
4158 case TemplateArgument::Declaration:
4159 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00004160 case TemplateArgument::Integral: {
4161 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4162 QualType T = GetType(Record[Idx++]);
4163 return TemplateArgument(Value, T);
4164 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004165 case TemplateArgument::Template:
4166 return TemplateArgument(ReadTemplateName(Record, Idx));
4167 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004168 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004169 case TemplateArgument::Pack: {
4170 unsigned NumArgs = Record[Idx++];
4171 llvm::SmallVector<TemplateArgument, 8> Args;
4172 Args.reserve(NumArgs);
4173 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004174 Args.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004175 TemplateArgument TemplArg;
4176 TemplArg.setArgumentPack(Args.data(), Args.size(), /*CopyArgs=*/true);
4177 return TemplArg;
4178 }
4179 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004180
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004181 assert(0 && "Unhandled template argument kind!");
4182 return TemplateArgument();
4183}
4184
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004185TemplateParameterList *
Sebastian Redl2c373b92010-10-05 15:59:54 +00004186ASTReader::ReadTemplateParameterList(PerFileData &F,
4187 const RecordData &Record, unsigned &Idx) {
4188 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4189 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4190 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004191
4192 unsigned NumParams = Record[Idx++];
4193 llvm::SmallVector<NamedDecl *, 16> Params;
4194 Params.reserve(NumParams);
4195 while (NumParams--)
4196 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004197
4198 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004199 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4200 Params.data(), Params.size(), RAngleLoc);
4201 return TemplateParams;
4202}
4203
4204void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004205ASTReader::
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004206ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004207 PerFileData &F, const RecordData &Record,
4208 unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004209 unsigned NumTemplateArgs = Record[Idx++];
4210 TemplArgs.reserve(NumTemplateArgs);
4211 while (NumTemplateArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004212 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004213}
4214
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004215/// \brief Read a UnresolvedSet structure.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004216void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004217 const RecordData &Record, unsigned &Idx) {
4218 unsigned NumDecls = Record[Idx++];
4219 while (NumDecls--) {
4220 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4221 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4222 Set.addDecl(D, AS);
4223 }
4224}
4225
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004226CXXBaseSpecifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00004227ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky19b9f952010-07-26 16:56:01 +00004228 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004229 bool isVirtual = static_cast<bool>(Record[Idx++]);
4230 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4231 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004232 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4233 SourceRange Range = ReadSourceRange(F, Record, Idx);
Nick Lewycky19b9f952010-07-26 16:56:01 +00004234 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004235}
4236
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004237std::pair<CXXBaseOrMemberInitializer **, unsigned>
Sebastian Redl2c373b92010-10-05 15:59:54 +00004238ASTReader::ReadCXXBaseOrMemberInitializers(PerFileData &F,
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004239 const RecordData &Record,
4240 unsigned &Idx) {
4241 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0;
4242 unsigned NumInitializers = Record[Idx++];
4243 if (NumInitializers) {
4244 ASTContext &C = *getContext();
4245
4246 BaseOrMemberInitializers
4247 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
4248 for (unsigned i=0; i != NumInitializers; ++i) {
4249 TypeSourceInfo *BaseClassInfo = 0;
4250 bool IsBaseVirtual = false;
4251 FieldDecl *Member = 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004252
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004253 bool IsBaseInitializer = Record[Idx++];
4254 if (IsBaseInitializer) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004255 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004256 IsBaseVirtual = Record[Idx++];
4257 } else {
4258 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
4259 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00004260 SourceLocation MemberLoc = ReadSourceLocation(F, Record, Idx);
4261 Expr *Init = ReadExpr(F);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004262 FieldDecl *AnonUnionMember
4263 = cast_or_null<FieldDecl>(GetDecl(Record[Idx++]));
Sebastian Redl2c373b92010-10-05 15:59:54 +00004264 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4265 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004266 bool IsWritten = Record[Idx++];
4267 unsigned SourceOrderOrNumArrayIndices;
4268 llvm::SmallVector<VarDecl *, 8> Indices;
4269 if (IsWritten) {
4270 SourceOrderOrNumArrayIndices = Record[Idx++];
4271 } else {
4272 SourceOrderOrNumArrayIndices = Record[Idx++];
4273 Indices.reserve(SourceOrderOrNumArrayIndices);
4274 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4275 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4276 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004277
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004278 CXXBaseOrMemberInitializer *BOMInit;
4279 if (IsBaseInitializer) {
4280 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
4281 IsBaseVirtual, LParenLoc,
4282 Init, RParenLoc);
4283 } else if (IsWritten) {
4284 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc,
4285 LParenLoc, Init, RParenLoc);
4286 } else {
4287 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc,
4288 LParenLoc, Init, RParenLoc,
4289 Indices.data(),
4290 Indices.size());
4291 }
4292
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00004293 if (IsWritten)
4294 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004295 BOMInit->setAnonUnionMember(AnonUnionMember);
4296 BaseOrMemberInitializers[i] = BOMInit;
4297 }
4298 }
4299
4300 return std::make_pair(BaseOrMemberInitializers, NumInitializers);
4301}
4302
Chris Lattnerca025db2010-05-07 21:43:38 +00004303NestedNameSpecifier *
Sebastian Redl2c499f62010-08-18 23:56:43 +00004304ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004305 unsigned N = Record[Idx++];
4306 NestedNameSpecifier *NNS = 0, *Prev = 0;
4307 for (unsigned I = 0; I != N; ++I) {
4308 NestedNameSpecifier::SpecifierKind Kind
4309 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4310 switch (Kind) {
4311 case NestedNameSpecifier::Identifier: {
4312 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4313 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4314 break;
4315 }
4316
4317 case NestedNameSpecifier::Namespace: {
4318 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4319 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4320 break;
4321 }
4322
4323 case NestedNameSpecifier::TypeSpec:
4324 case NestedNameSpecifier::TypeSpecWithTemplate: {
4325 Type *T = GetType(Record[Idx++]).getTypePtr();
4326 bool Template = Record[Idx++];
4327 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4328 break;
4329 }
4330
4331 case NestedNameSpecifier::Global: {
4332 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4333 // No associated value, and there can't be a prefix.
4334 break;
4335 }
Chris Lattnerca025db2010-05-07 21:43:38 +00004336 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00004337 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00004338 }
4339 return NNS;
4340}
4341
4342SourceRange
Sebastian Redl2c373b92010-10-05 15:59:54 +00004343ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4344 unsigned &Idx) {
4345 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4346 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00004347 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00004348}
4349
Douglas Gregor1daeb692009-04-13 18:14:40 +00004350/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004351llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004352 unsigned BitWidth = Record[Idx++];
4353 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4354 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4355 Idx += NumWords;
4356 return Result;
4357}
4358
4359/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004360llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004361 bool isUnsigned = Record[Idx++];
4362 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4363}
4364
Douglas Gregore0a3a512009-04-14 21:55:33 +00004365/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004366llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004367 return llvm::APFloat(ReadAPInt(Record, Idx));
4368}
4369
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004370// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00004371std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004372 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00004373 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004374 Idx += Len;
4375 return Result;
4376}
4377
Sebastian Redl2c499f62010-08-18 23:56:43 +00004378CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00004379 unsigned &Idx) {
4380 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4381 return CXXTemporary::Create(*Context, Decl);
4382}
4383
Sebastian Redl2c499f62010-08-18 23:56:43 +00004384DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00004385 return Diag(SourceLocation(), DiagID);
4386}
4387
Sebastian Redl2c499f62010-08-18 23:56:43 +00004388DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004389 return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00004390}
Douglas Gregora9af1d12009-04-17 00:04:06 +00004391
Douglas Gregora868bbd2009-04-21 22:25:48 +00004392/// \brief Retrieve the identifier table associated with the
4393/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004394IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004395 assert(PP && "Forgot to set Preprocessor ?");
4396 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00004397}
4398
Douglas Gregora9af1d12009-04-17 00:04:06 +00004399/// \brief Record that the given ID maps to the given switch-case
4400/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004401void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004402 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4403 SwitchCaseStmts[ID] = SC;
4404}
4405
4406/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004407SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004408 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4409 return SwitchCaseStmts[ID];
4410}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004411
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00004412void ASTReader::ClearSwitchCaseIDs() {
4413 SwitchCaseStmts.clear();
4414}
4415
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004416/// \brief Record that the given label statement has been
4417/// deserialized and has the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004418void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump11289f42009-09-09 15:08:12 +00004419 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004420 "Deserialized label twice");
4421 LabelStmts[ID] = S;
4422
4423 // If we've already seen any goto statements that point to this
4424 // label, resolve them now.
4425 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
4426 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
4427 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
4428 Goto->second->setLabel(S);
4429 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor779d8652009-04-17 18:58:21 +00004430
4431 // If we've already seen any address-label statements that point to
4432 // this label, resolve them now.
4433 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump11289f42009-09-09 15:08:12 +00004434 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor779d8652009-04-17 18:58:21 +00004435 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump11289f42009-09-09 15:08:12 +00004436 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor779d8652009-04-17 18:58:21 +00004437 AddrLabel != AddrLabels.second; ++AddrLabel)
4438 AddrLabel->second->setLabel(S);
4439 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004440}
4441
4442/// \brief Set the label of the given statement to the label
4443/// identified by ID.
4444///
4445/// Depending on the order in which the label and other statements
4446/// referencing that label occur, this operation may complete
4447/// immediately (updating the statement) or it may queue the
4448/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004449void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004450 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4451 if (Label != LabelStmts.end()) {
4452 // We've already seen this label, so set the label of the goto and
4453 // we're done.
4454 S->setLabel(Label->second);
4455 } else {
4456 // We haven't seen this label yet, so add this goto to the set of
4457 // unresolved goto statements.
4458 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
4459 }
4460}
Douglas Gregor779d8652009-04-17 18:58:21 +00004461
4462/// \brief Set the label of the given expression to the label
4463/// identified by ID.
4464///
4465/// Depending on the order in which the label and other statements
4466/// referencing that label occur, this operation may complete
4467/// immediately (updating the statement) or it may queue the
4468/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004469void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor779d8652009-04-17 18:58:21 +00004470 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4471 if (Label != LabelStmts.end()) {
4472 // We've already seen this label, so set the label of the
4473 // label-address expression and we're done.
4474 S->setLabel(Label->second);
4475 } else {
4476 // We haven't seen this label yet, so add this label-address
4477 // expression to the set of unresolved label-address expressions.
4478 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
4479 }
4480}
Douglas Gregor1342e842009-07-06 18:54:52 +00004481
Sebastian Redl2c499f62010-08-18 23:56:43 +00004482void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004483 assert(NumCurrentElementsDeserializing &&
4484 "FinishedDeserializing not paired with StartedDeserializing");
4485 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004486 // If any identifiers with corresponding top-level declarations have
4487 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004488 while (!PendingIdentifierInfos.empty()) {
4489 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4490 PendingIdentifierInfos.front().DeclIDs, true);
4491 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00004492 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004493
4494 // We are not in recursive loading, so it's safe to pass the "interesting"
4495 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004496 if (Consumer)
4497 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidisad5f95c2010-10-24 17:26:31 +00004498
4499 assert(PendingForwardRefs.size() == 0 &&
4500 "Some forward refs did not get linked to the definition!");
Douglas Gregor1342e842009-07-06 18:54:52 +00004501 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004502 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00004503}
Douglas Gregorb473b072010-08-19 00:28:17 +00004504
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004505ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
4506 const char *isysroot, bool DisableValidation)
4507 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4508 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00004509 FileSystemOpts(PP.getFileSystemOpts()),
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004510 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4511 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
4512 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00004513 TotalNumSLocEntries(0), NextSLocOffset(0), NumStatementsRead(0),
4514 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
4515 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004516 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4517 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4518 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
4519 RelocatablePCH = false;
4520}
4521
4522ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00004523 const FileSystemOptions &FileSystemOpts,
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004524 Diagnostic &Diags, const char *isysroot,
4525 bool DisableValidation)
4526 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00004527 FileSystemOpts(FileSystemOpts),
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004528 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
4529 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
4530 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00004531 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4532 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4533 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4534 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4535 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4536 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004537 RelocatablePCH = false;
4538}
4539
4540ASTReader::~ASTReader() {
4541 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4542 delete Chain[e - i - 1];
4543 // Delete all visible decl lookup tables
4544 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4545 E = DeclContextOffsets.end();
4546 I != E; ++I) {
4547 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4548 J != F; ++J) {
4549 if (J->NameLookupTableData)
4550 delete static_cast<ASTDeclContextNameLookupTable*>(
4551 J->NameLookupTableData);
4552 }
4553 }
4554 for (DeclContextVisibleUpdatesPending::iterator
4555 I = PendingVisibleUpdates.begin(),
4556 E = PendingVisibleUpdates.end();
4557 I != E; ++I) {
4558 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4559 F = I->second.end();
4560 J != F; ++J)
4561 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4562 }
4563}
4564
Sebastian Redl009e7f22010-10-05 16:15:19 +00004565ASTReader::PerFileData::PerFileData(ASTFileType Ty)
4566 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00004567 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
4568 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
4569 MacroDefinitionOffsets(0), LocalNumSelectors(0), SelectorOffsets(0),
4570 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004571 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
4572 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redl3f6b7532010-10-01 19:59:12 +00004573 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregorb473b072010-08-19 00:28:17 +00004574{}
4575
4576ASTReader::PerFileData::~PerFileData() {
4577 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
4578 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4579}
4580