blob: 3dcc62ebc1bc1830025d3a3f4f044f885b4a6aaf [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"
Chris Lattner226efd32010-11-23 19:19:34 +000036#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000037#include "clang/Basic/TargetInfo.h"
Douglas Gregord54f3a12009-10-05 21:07:28 +000038#include "clang/Basic/Version.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000039#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000040#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000041#include "llvm/Support/MemoryBuffer.h"
John McCall0ad16662009-10-29 08:12:44 +000042#include "llvm/Support/ErrorHandling.h"
Douglas Gregor09b69892011-02-10 17:09:37 +000043#include "llvm/Support/FileSystem.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000044#include "llvm/Support/Path.h"
Michael J. Spencerf25faaa2010-12-09 17:36:38 +000045#include "llvm/Support/system_error.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000046#include <algorithm>
Douglas Gregorc379c072009-04-28 18:58:38 +000047#include <iterator>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000048#include <cstdio>
Douglas Gregorc5046832009-04-27 18:38:38 +000049#include <sys/stat.h>
Douglas Gregor09b69892011-02-10 17:09:37 +000050
Douglas Gregoref84c4b2009-04-09 22:27:44 +000051using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000052using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000053
54//===----------------------------------------------------------------------===//
Sebastian Redld44cd6a2010-08-18 23:57:06 +000055// PCH validator implementation
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000056//===----------------------------------------------------------------------===//
57
Sebastian Redl3e31c722010-08-18 23:56:56 +000058ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000059
60bool
61PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
62 const LangOptions &PPLangOpts = PP.getLangOptions();
63#define PARSE_LANGOPT_BENIGN(Option)
64#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
65 if (PPLangOpts.Option != LangOpts.Option) { \
66 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
67 return true; \
68 }
69
70 PARSE_LANGOPT_BENIGN(Trigraphs);
71 PARSE_LANGOPT_BENIGN(BCPLComment);
72 PARSE_LANGOPT_BENIGN(DollarIdents);
73 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
74 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carruthe03aa552010-04-17 20:17:31 +000075 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000076 PARSE_LANGOPT_BENIGN(ImplicitInt);
77 PARSE_LANGOPT_BENIGN(Digraphs);
78 PARSE_LANGOPT_BENIGN(HexFloats);
79 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
80 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +000081 PARSE_LANGOPT_BENIGN(MSCVersion);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000082 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
83 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
84 PARSE_LANGOPT_BENIGN(CXXOperatorName);
85 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
86 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
87 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian45878032010-02-09 19:31:38 +000088 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +000089 PARSE_LANGOPT_IMPORTANT(AppleKext, diag::warn_pch_apple_kext);
Ted Kremenek1d56c9e2010-12-23 21:35:43 +000090 PARSE_LANGOPT_IMPORTANT(ObjCDefaultSynthProperties,
91 diag::warn_pch_objc_auto_properties);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +000092 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian62c56022010-04-22 21:01:59 +000093 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000094 PARSE_LANGOPT_BENIGN(PascalStrings);
95 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump11289f42009-09-09 15:08:12 +000096 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000097 diag::warn_pch_lax_vector_conversions);
Nate Begeman9d905792009-06-25 22:57:40 +000098 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000099 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +0000100 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Douglas Gregordbe39272011-02-01 15:15:22 +0000101 PARSE_LANGOPT_IMPORTANT(MSBitfields, diag::warn_pch_ms_bitfields);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000102 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
103 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
104 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump11289f42009-09-09 15:08:12 +0000105 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000106 diag::warn_pch_thread_safe_statics);
Daniel Dunbara77eaeb2009-09-03 04:54:28 +0000107 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000108 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
109 PARSE_LANGOPT_BENIGN(EmitAllDecls);
110 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattner51924e512010-06-26 21:25:03 +0000111 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump11289f42009-09-09 15:08:12 +0000112 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000113 diag::warn_pch_heinous_extensions);
114 // FIXME: Most of the options below are benign if the macro wasn't
115 // used. Unfortunately, this means that a PCH compiled without
116 // optimization can't be used with optimization turned on, even
117 // though the only thing that changes is whether __OPTIMIZE__ was
118 // defined... but if __OPTIMIZE__ never showed up in the header, it
119 // doesn't matter. We could consider making this some special kind
120 // of check.
121 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
122 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
123 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
124 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
125 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
126 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
127 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
128 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsoned4e2952009-11-05 20:14:16 +0000129 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +0000130 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000131 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump11289f42009-09-09 15:08:12 +0000132 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000133 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
134 return true;
135 }
136 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000137 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
138 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000139 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman9d905792009-06-25 22:57:40 +0000140 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Peter Collingbourne546d0792010-12-01 19:14:57 +0000141 PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda);
Mike Stumpd9546382009-12-12 01:27:46 +0000142 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbar143021e2009-09-21 04:16:19 +0000143 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000144 PARSE_LANGOPT_BENIGN(SpellChecking);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +0000145#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000146#undef PARSE_LANGOPT_BENIGN
147
148 return false;
149}
150
Daniel Dunbar20a682d2009-11-11 00:52:11 +0000151bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
152 if (Triple == PP.getTargetInfo().getTriple().str())
153 return false;
154
155 Reader.Diag(diag::warn_pch_target_triple)
156 << Triple << PP.getTargetInfo().getTriple().str();
157 return true;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000158}
159
Benjamin Kramer90b5b682010-11-25 18:29:30 +0000160namespace {
161 struct EmptyStringRef {
162 bool operator ()(llvm::StringRef r) const { return r.empty(); }
163 };
164 struct EmptyBlock {
165 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
166 };
167}
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000168
169static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
170 PCHPredefinesBlocks R) {
171 // First, sum up the lengths.
172 unsigned LL = 0, RL = 0;
173 for (unsigned I = 0, N = L.size(); I != N; ++I) {
174 LL += L[I].size();
175 }
176 for (unsigned I = 0, N = R.size(); I != N; ++I) {
177 RL += R[I].Data.size();
178 }
179 if (LL != RL)
180 return false;
181 if (LL == 0 && RL == 0)
182 return true;
183
184 // Kick out empty parts, they confuse the algorithm below.
185 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
186 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
187
188 // Do it the hard way. At this point, both vectors must be non-empty.
189 llvm::StringRef LR = L[0], RR = R[0].Data;
190 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbar01ad0a72010-07-16 00:00:11 +0000191 (void) RN;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000192 for (;;) {
193 // Compare the current pieces.
194 if (LR.size() == RR.size()) {
195 // If they're the same length, it's pretty easy.
196 if (LR != RR)
197 return false;
198 // Both pieces are done, advance.
199 ++LI;
200 ++RI;
201 // If either string is done, they're both done, since they're the same
202 // length.
203 if (LI == LN) {
204 assert(RI == RN && "Strings not the same length after all?");
205 return true;
206 }
207 LR = L[LI];
208 RR = R[RI].Data;
209 } else if (LR.size() < RR.size()) {
210 // Right piece is longer.
211 if (!RR.startswith(LR))
212 return false;
213 ++LI;
214 assert(LI != LN && "Strings not the same length after all?");
215 RR = RR.substr(LR.size());
216 LR = L[LI];
217 } else {
218 // Left piece is longer.
219 if (!LR.startswith(RR))
220 return false;
221 ++RI;
222 assert(RI != RN && "Strings not the same length after all?");
223 LR = LR.substr(RR.size());
224 RR = R[RI].Data;
225 }
226 }
227}
228
229static std::pair<FileID, llvm::StringRef::size_type>
230FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
231 std::pair<FileID, llvm::StringRef::size_type> Res;
232 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
233 Res.second = Buffers[I].Data.find(MacroDef);
234 if (Res.second != llvm::StringRef::npos) {
235 Res.first = Buffers[I].BufferID;
236 break;
237 }
238 }
239 return Res;
240}
241
242bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000243 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000244 std::string &SuggestedPredefines) {
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000245 // We are in the context of an implicit include, so the predefines buffer will
246 // have a #include entry for the PCH file itself (as normalized by the
247 // preprocessor initialization). Find it and skip over it in the checking
248 // below.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000249 llvm::SmallString<256> PCHInclude;
250 PCHInclude += "#include \"";
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000251 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000252 PCHInclude += "\"\n";
253 std::pair<llvm::StringRef,llvm::StringRef> Split =
254 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
255 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000256 if (Left == PP.getPredefines()) {
257 Error("Missing PCH include entry!");
258 return true;
259 }
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000260
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000261 // If the concatenation of all the PCH buffers is equal to the adjusted
262 // command line, we're done.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000263 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
264 CommandLine.push_back(Left);
265 CommandLine.push_back(Right);
266 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000267 return false;
268
269 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000270
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000271 // The predefines buffers are different. Determine what the differences are,
272 // and whether they require us to reject the PCH file.
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000273 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000274 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
275 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000276
277 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
278 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000279
280 // Pick out implicit #includes after the PCH and don't consider them for
281 // validation; we will insert them into SuggestedPredefines so that the
282 // preprocessor includes them.
283 std::string IncludesAfterPCH;
284 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
285 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
286 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
287 if (AfterPCHLines[i].startswith("#include ")) {
288 IncludesAfterPCH += AfterPCHLines[i];
289 IncludesAfterPCH += '\n';
290 } else {
291 CmdLineLines.push_back(AfterPCHLines[i]);
292 }
293 }
294
295 // Make sure we add the includes last into SuggestedPredefines before we
296 // exit this function.
297 struct AddIncludesRAII {
298 std::string &SuggestedPredefines;
299 std::string &IncludesAfterPCH;
300
301 AddIncludesRAII(std::string &SuggestedPredefines,
302 std::string &IncludesAfterPCH)
303 : SuggestedPredefines(SuggestedPredefines),
304 IncludesAfterPCH(IncludesAfterPCH) { }
305 ~AddIncludesRAII() {
306 SuggestedPredefines += IncludesAfterPCH;
307 }
308 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000309
Daniel Dunbar499baed2009-11-11 05:26:28 +0000310 // Sort both sets of predefined buffer lines, since we allow some extra
311 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000312 std::sort(CmdLineLines.begin(), CmdLineLines.end());
313 std::sort(PCHLines.begin(), PCHLines.end());
314
Daniel Dunbar499baed2009-11-11 05:26:28 +0000315 // Determine which predefines that were used to build the PCH file are missing
316 // from the command line.
317 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000318 std::set_difference(PCHLines.begin(), PCHLines.end(),
319 CmdLineLines.begin(), CmdLineLines.end(),
320 std::back_inserter(MissingPredefines));
321
322 bool MissingDefines = false;
323 bool ConflictingDefines = false;
324 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000325 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000326 if (Missing.startswith("#include ")) {
327 // An -include was specified when generating the PCH; it is included in
328 // the PCH, just ignore it.
329 continue;
330 }
Daniel Dunbar499baed2009-11-11 05:26:28 +0000331 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000332 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
333 return true;
334 }
Mike Stump11289f42009-09-09 15:08:12 +0000335
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000336 // This is a macro definition. Determine the name of the macro we're
337 // defining.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000338 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000339 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000340 = Missing.find_first_of("( \n\r", StartOfMacroName);
341 assert(EndOfMacroName != std::string::npos &&
342 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000343 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000344
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000345 // Determine whether this macro was given a different definition on the
346 // command line.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000347 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000348 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000349 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000350 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
351 MacroDefStart);
352 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000353 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000354 // Different macro; we're done.
355 ConflictPos = CmdLineLines.end();
Mike Stump11289f42009-09-09 15:08:12 +0000356 break;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000357 }
Mike Stump11289f42009-09-09 15:08:12 +0000358
359 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000360 "Invalid #define in predefines buffer?");
Mike Stump11289f42009-09-09 15:08:12 +0000361 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000362 (*ConflictPos)[MacroDefLen] != '(')
363 continue; // Longer macro name; keep trying.
Mike Stump11289f42009-09-09 15:08:12 +0000364
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000365 // We found a conflicting macro definition.
366 break;
367 }
Mike Stump11289f42009-09-09 15:08:12 +0000368
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000369 if (ConflictPos != CmdLineLines.end()) {
370 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
371 << MacroName;
372
373 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000374 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
375 FindMacro(Buffers, Missing);
376 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
377 SourceLocation PCHMissingLoc =
378 SourceMgr.getLocForStartOfFile(MacroLoc.first)
379 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar499baed2009-11-11 05:26:28 +0000380 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000381
382 ConflictingDefines = true;
383 continue;
384 }
Mike Stump11289f42009-09-09 15:08:12 +0000385
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000386 // If the macro doesn't conflict, then we'll just pick up the macro
387 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000388 if (ConflictingDefines)
389 continue; // Don't complain if there are already conflicting defs
Mike Stump11289f42009-09-09 15:08:12 +0000390
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000391 if (!MissingDefines) {
392 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
393 MissingDefines = true;
394 }
395
396 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000397 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
398 FindMacro(Buffers, Missing);
399 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
400 SourceLocation PCHMissingLoc =
401 SourceMgr.getLocForStartOfFile(MacroLoc.first)
402 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000403 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
404 }
Mike Stump11289f42009-09-09 15:08:12 +0000405
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000406 if (ConflictingDefines)
407 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000408
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000409 // Determine what predefines were introduced based on command-line
410 // parameters that were not present when building the PCH
411 // file. Extra #defines are okay, so long as the identifiers being
412 // defined were not used within the precompiled header.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000413 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000414 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
415 PCHLines.begin(), PCHLines.end(),
Mike Stump11289f42009-09-09 15:08:12 +0000416 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000417 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000418 llvm::StringRef &Extra = ExtraPredefines[I];
419 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000420 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
421 return true;
422 }
423
424 // This is an extra macro definition. Determine the name of the
425 // macro we're defining.
426 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000427 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000428 = Extra.find_first_of("( \n\r", StartOfMacroName);
429 assert(EndOfMacroName != std::string::npos &&
430 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000431 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000432
433 // Check whether this name was used somewhere in the PCH file. If
434 // so, defining it as a macro could change behavior, so we reject
435 // the PCH file.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000436 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar045c92f2009-11-11 00:52:00 +0000437 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000438 return true;
439 }
440
441 // Add this definition to the suggested predefines buffer.
442 SuggestedPredefines += Extra;
443 SuggestedPredefines += '\n';
444 }
445
446 // If we get here, it's because the predefines buffer had compatible
447 // contents. Accept the PCH file.
448 return false;
449}
450
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000451void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
452 unsigned ID) {
453 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
454 ++NumHeaderInfos;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000455}
456
457void PCHValidator::ReadCounter(unsigned Value) {
458 PP.setCounterValue(Value);
459}
460
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000461//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +0000462// AST reader implementation
Douglas Gregora868bbd2009-04-21 22:25:48 +0000463//===----------------------------------------------------------------------===//
464
Sebastian Redl07a89a82010-07-30 00:29:29 +0000465void
Sebastian Redl3e31c722010-08-18 23:56:56 +0000466ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redl07a89a82010-07-30 00:29:29 +0000467 DeserializationListener = Listener;
Sebastian Redl07a89a82010-07-30 00:29:29 +0000468}
469
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000470
Douglas Gregora868bbd2009-04-21 22:25:48 +0000471namespace {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000472class ASTSelectorLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000473 ASTReader &Reader;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000474
475public:
Sebastian Redl834bb972010-08-04 17:20:04 +0000476 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +0000477 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +0000478 ObjCMethodList Instance, Factory;
479 };
Douglas Gregorc78d3462009-04-24 21:10:55 +0000480
481 typedef Selector external_key_type;
482 typedef external_key_type internal_key_type;
483
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000484 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump11289f42009-09-09 15:08:12 +0000485
Douglas Gregorc78d3462009-04-24 21:10:55 +0000486 static bool EqualKey(const internal_key_type& a,
487 const internal_key_type& b) {
488 return a == b;
489 }
Mike Stump11289f42009-09-09 15:08:12 +0000490
Douglas Gregorc78d3462009-04-24 21:10:55 +0000491 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +0000492 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000493 }
Mike Stump11289f42009-09-09 15:08:12 +0000494
Douglas Gregorc78d3462009-04-24 21:10:55 +0000495 // This hopefully will just get inlined and removed by the optimizer.
496 static const internal_key_type&
497 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000498
Douglas Gregorc78d3462009-04-24 21:10:55 +0000499 static std::pair<unsigned, unsigned>
500 ReadKeyDataLength(const unsigned char*& d) {
501 using namespace clang::io;
502 unsigned KeyLen = ReadUnalignedLE16(d);
503 unsigned DataLen = ReadUnalignedLE16(d);
504 return std::make_pair(KeyLen, DataLen);
505 }
Mike Stump11289f42009-09-09 15:08:12 +0000506
Douglas Gregor95c13f52009-04-25 17:48:32 +0000507 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000508 using namespace clang::io;
Chris Lattner8575daa2009-04-27 21:45:14 +0000509 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000510 unsigned N = ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000511 IdentifierInfo *FirstII
Douglas Gregorc78d3462009-04-24 21:10:55 +0000512 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
513 if (N == 0)
514 return SelTable.getNullarySelector(FirstII);
515 else if (N == 1)
516 return SelTable.getUnarySelector(FirstII);
517
518 llvm::SmallVector<IdentifierInfo *, 16> Args;
519 Args.push_back(FirstII);
520 for (unsigned I = 1; I != N; ++I)
521 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
522
Douglas Gregor038c3382009-05-22 22:45:36 +0000523 return SelTable.getSelector(N, Args.data());
Douglas Gregorc78d3462009-04-24 21:10:55 +0000524 }
Mike Stump11289f42009-09-09 15:08:12 +0000525
Douglas Gregorc78d3462009-04-24 21:10:55 +0000526 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
527 using namespace clang::io;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000528
529 data_type Result;
530
Sebastian Redl834bb972010-08-04 17:20:04 +0000531 Result.ID = ReadUnalignedLE32(d);
532 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
533 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
534
Douglas Gregorc78d3462009-04-24 21:10:55 +0000535 // Load instance methods
536 ObjCMethodList *Prev = 0;
537 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000538 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000539 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000540 if (!Result.Instance.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000541 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000542 Result.Instance.Method = Method;
543 Prev = &Result.Instance;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000544 continue;
545 }
546
Ted Kremenekda4abf12010-02-11 00:53:01 +0000547 ObjCMethodList *Mem =
548 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
549 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000550 Prev = Prev->Next;
551 }
552
553 // Load factory methods
554 Prev = 0;
555 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000556 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000557 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000558 if (!Result.Factory.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000559 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000560 Result.Factory.Method = Method;
561 Prev = &Result.Factory;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000562 continue;
563 }
564
Ted Kremenekda4abf12010-02-11 00:53:01 +0000565 ObjCMethodList *Mem =
566 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
567 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000568 Prev = Prev->Next;
569 }
570
571 return Result;
572 }
573};
Mike Stump11289f42009-09-09 15:08:12 +0000574
575} // end anonymous namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +0000576
577/// \brief The on-disk hash table used for the global method pool.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000578typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
579 ASTSelectorLookupTable;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000580
Sebastian Redl2c373b92010-10-05 15:59:54 +0000581namespace clang {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000582class ASTIdentifierLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000583 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +0000584 ASTReader::PerFileData &F;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000585
586 // If we know the IdentifierInfo in advance, it is here and we will
587 // not build a new one. Used when deserializing information about an
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000588 // identifier that was constructed before the AST file was read.
Douglas Gregora868bbd2009-04-21 22:25:48 +0000589 IdentifierInfo *KnownII;
590
591public:
592 typedef IdentifierInfo * data_type;
593
594 typedef const std::pair<const char*, unsigned> external_key_type;
595
596 typedef external_key_type internal_key_type;
597
Sebastian Redl2c373b92010-10-05 15:59:54 +0000598 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000599 IdentifierInfo *II = 0)
Sebastian Redl2c373b92010-10-05 15:59:54 +0000600 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump11289f42009-09-09 15:08:12 +0000601
Douglas Gregora868bbd2009-04-21 22:25:48 +0000602 static bool EqualKey(const internal_key_type& a,
603 const internal_key_type& b) {
604 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
605 : false;
606 }
Mike Stump11289f42009-09-09 15:08:12 +0000607
Douglas Gregora868bbd2009-04-21 22:25:48 +0000608 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000609 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregora868bbd2009-04-21 22:25:48 +0000610 }
Mike Stump11289f42009-09-09 15:08:12 +0000611
Douglas Gregora868bbd2009-04-21 22:25:48 +0000612 // This hopefully will just get inlined and removed by the optimizer.
613 static const internal_key_type&
614 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000615
Douglas Gregor57756ea2010-10-14 22:11:03 +0000616 // This hopefully will just get inlined and removed by the optimizer.
617 static const external_key_type&
618 GetExternalKey(const internal_key_type& x) { return x; }
619
Douglas Gregora868bbd2009-04-21 22:25:48 +0000620 static std::pair<unsigned, unsigned>
621 ReadKeyDataLength(const unsigned char*& d) {
622 using namespace clang::io;
Douglas Gregor6b7bf5a2009-04-25 20:26:24 +0000623 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor5287b4e2009-04-25 21:04:17 +0000624 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000625 return std::make_pair(KeyLen, DataLen);
626 }
Mike Stump11289f42009-09-09 15:08:12 +0000627
Douglas Gregora868bbd2009-04-21 22:25:48 +0000628 static std::pair<const char*, unsigned>
629 ReadKey(const unsigned char* d, unsigned n) {
630 assert(n >= 2 && d[n-1] == '\0');
631 return std::make_pair((const char*) d, n-1);
632 }
Mike Stump11289f42009-09-09 15:08:12 +0000633
634 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregora868bbd2009-04-21 22:25:48 +0000635 const unsigned char* d,
636 unsigned DataLen) {
637 using namespace clang::io;
Sebastian Redl539c5062010-08-18 23:57:32 +0000638 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000639 bool IsInteresting = ID & 0x01;
640
641 // Wipe out the "is interesting" bit.
642 ID = ID >> 1;
643
644 if (!IsInteresting) {
Sebastian Redl98912122010-07-27 23:01:28 +0000645 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregor1d583f22009-04-28 21:18:29 +0000646 // and associate it with the persistent ID.
647 IdentifierInfo *II = KnownII;
648 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000649 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000650 Reader.SetIdentifierInfo(ID, II);
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000651 II->setIsFromAST();
Douglas Gregor1d583f22009-04-28 21:18:29 +0000652 return II;
653 }
654
Douglas Gregorb9256522009-04-28 21:32:13 +0000655 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000656 bool CPlusPlusOperatorKeyword = Bits & 0x01;
657 Bits >>= 1;
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000658 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
659 Bits >>= 1;
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000660 bool Poisoned = Bits & 0x01;
661 Bits >>= 1;
662 bool ExtensionToken = Bits & 0x01;
663 Bits >>= 1;
664 bool hasMacroDefinition = Bits & 0x01;
665 Bits >>= 1;
666 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
667 Bits >>= 10;
Mike Stump11289f42009-09-09 15:08:12 +0000668
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000669 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorb9256522009-04-28 21:32:13 +0000670 DataLen -= 6;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000671
672 // Build the IdentifierInfo itself and link the identifier ID with
673 // the new IdentifierInfo.
674 IdentifierInfo *II = KnownII;
675 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000676 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000677 Reader.SetIdentifierInfo(ID, II);
678
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000679 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000680 // Token IDs are read-only.
681 if (HasRevertedTokenIDToIdentifier)
682 II->RevertTokenIDToIdentifier();
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000683 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump11289f42009-09-09 15:08:12 +0000684 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000685 "Incorrect extension token flag");
686 (void)ExtensionToken;
687 II->setIsPoisoned(Poisoned);
688 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
689 "Incorrect C++ operator keyword flag");
690 (void)CPlusPlusOperatorKeyword;
691
Douglas Gregorc3366a52009-04-21 23:56:24 +0000692 // If this identifier is a macro, deserialize the macro
693 // definition.
694 if (hasMacroDefinition) {
Douglas Gregorb9256522009-04-28 21:32:13 +0000695 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor5ef9e332010-10-30 00:23:06 +0000696 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregorb9256522009-04-28 21:32:13 +0000697 DataLen -= 4;
Douglas Gregorc3366a52009-04-21 23:56:24 +0000698 }
Douglas Gregora868bbd2009-04-21 22:25:48 +0000699
700 // Read all of the declarations visible at global scope with this
701 // name.
Chris Lattner1d728882009-04-27 22:17:41 +0000702 if (Reader.getContext() == 0) return II;
Douglas Gregor1342e842009-07-06 18:54:52 +0000703 if (DataLen > 0) {
704 llvm::SmallVector<uint32_t, 4> DeclIDs;
705 for (; DataLen > 0; DataLen -= 4)
706 DeclIDs.push_back(ReadUnalignedLE32(d));
707 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000708 }
Mike Stump11289f42009-09-09 15:08:12 +0000709
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000710 II->setIsFromAST();
Douglas Gregora868bbd2009-04-21 22:25:48 +0000711 return II;
712 }
713};
Mike Stump11289f42009-09-09 15:08:12 +0000714
715} // end anonymous namespace
Douglas Gregora868bbd2009-04-21 22:25:48 +0000716
717/// \brief The on-disk hash table used to contain information about
718/// all of the identifiers in the program.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000719typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
720 ASTIdentifierLookupTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000721
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000722namespace {
723class ASTDeclContextNameLookupTrait {
724 ASTReader &Reader;
725
726public:
727 /// \brief Pair of begin/end iterators for DeclIDs.
728 typedef std::pair<DeclID *, DeclID *> data_type;
729
730 /// \brief Special internal key for declaration names.
731 /// The hash table creates keys for comparison; we do not create
732 /// a DeclarationName for the internal key to avoid deserializing types.
733 struct DeclNameKey {
734 DeclarationName::NameKind Kind;
735 uint64_t Data;
736 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
737 };
738
739 typedef DeclarationName external_key_type;
740 typedef DeclNameKey internal_key_type;
741
742 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
743
744 static bool EqualKey(const internal_key_type& a,
745 const internal_key_type& b) {
746 return a.Kind == b.Kind && a.Data == b.Data;
747 }
748
749 unsigned ComputeHash(const DeclNameKey &Key) const {
750 llvm::FoldingSetNodeID ID;
751 ID.AddInteger(Key.Kind);
752
753 switch (Key.Kind) {
754 case DeclarationName::Identifier:
755 case DeclarationName::CXXLiteralOperatorName:
756 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
757 break;
758 case DeclarationName::ObjCZeroArgSelector:
759 case DeclarationName::ObjCOneArgSelector:
760 case DeclarationName::ObjCMultiArgSelector:
761 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
762 break;
763 case DeclarationName::CXXConstructorName:
764 case DeclarationName::CXXDestructorName:
765 case DeclarationName::CXXConversionFunctionName:
766 ID.AddInteger((TypeID)Key.Data);
767 break;
768 case DeclarationName::CXXOperatorName:
769 ID.AddInteger((OverloadedOperatorKind)Key.Data);
770 break;
771 case DeclarationName::CXXUsingDirective:
772 break;
773 }
774
775 return ID.ComputeHash();
776 }
777
778 internal_key_type GetInternalKey(const external_key_type& Name) const {
779 DeclNameKey Key;
780 Key.Kind = Name.getNameKind();
781 switch (Name.getNameKind()) {
782 case DeclarationName::Identifier:
783 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
784 break;
785 case DeclarationName::ObjCZeroArgSelector:
786 case DeclarationName::ObjCOneArgSelector:
787 case DeclarationName::ObjCMultiArgSelector:
788 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
789 break;
790 case DeclarationName::CXXConstructorName:
791 case DeclarationName::CXXDestructorName:
792 case DeclarationName::CXXConversionFunctionName:
793 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
794 break;
795 case DeclarationName::CXXOperatorName:
796 Key.Data = Name.getCXXOverloadedOperator();
797 break;
798 case DeclarationName::CXXLiteralOperatorName:
799 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
800 break;
801 case DeclarationName::CXXUsingDirective:
802 break;
803 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000804
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000805 return Key;
806 }
807
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000808 external_key_type GetExternalKey(const internal_key_type& Key) const {
809 ASTContext *Context = Reader.getContext();
810 switch (Key.Kind) {
811 case DeclarationName::Identifier:
812 return DeclarationName((IdentifierInfo*)Key.Data);
813
814 case DeclarationName::ObjCZeroArgSelector:
815 case DeclarationName::ObjCOneArgSelector:
816 case DeclarationName::ObjCMultiArgSelector:
817 return DeclarationName(Selector(Key.Data));
818
819 case DeclarationName::CXXConstructorName:
820 return Context->DeclarationNames.getCXXConstructorName(
821 Context->getCanonicalType(Reader.GetType(Key.Data)));
822
823 case DeclarationName::CXXDestructorName:
824 return Context->DeclarationNames.getCXXDestructorName(
825 Context->getCanonicalType(Reader.GetType(Key.Data)));
826
827 case DeclarationName::CXXConversionFunctionName:
828 return Context->DeclarationNames.getCXXConversionFunctionName(
829 Context->getCanonicalType(Reader.GetType(Key.Data)));
830
831 case DeclarationName::CXXOperatorName:
832 return Context->DeclarationNames.getCXXOperatorName(
833 (OverloadedOperatorKind)Key.Data);
834
835 case DeclarationName::CXXLiteralOperatorName:
836 return Context->DeclarationNames.getCXXLiteralOperatorName(
837 (IdentifierInfo*)Key.Data);
838
839 case DeclarationName::CXXUsingDirective:
840 return DeclarationName::getUsingDirectiveName();
841 }
842
843 llvm_unreachable("Invalid Name Kind ?");
844 }
845
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000846 static std::pair<unsigned, unsigned>
847 ReadKeyDataLength(const unsigned char*& d) {
848 using namespace clang::io;
849 unsigned KeyLen = ReadUnalignedLE16(d);
850 unsigned DataLen = ReadUnalignedLE16(d);
851 return std::make_pair(KeyLen, DataLen);
852 }
853
854 internal_key_type ReadKey(const unsigned char* d, unsigned) {
855 using namespace clang::io;
856
857 DeclNameKey Key;
858 Key.Kind = (DeclarationName::NameKind)*d++;
859 switch (Key.Kind) {
860 case DeclarationName::Identifier:
861 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
862 break;
863 case DeclarationName::ObjCZeroArgSelector:
864 case DeclarationName::ObjCOneArgSelector:
865 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000866 Key.Data =
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000867 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
868 break;
869 case DeclarationName::CXXConstructorName:
870 case DeclarationName::CXXDestructorName:
871 case DeclarationName::CXXConversionFunctionName:
872 Key.Data = ReadUnalignedLE32(d); // TypeID
873 break;
874 case DeclarationName::CXXOperatorName:
875 Key.Data = *d++; // OverloadedOperatorKind
876 break;
877 case DeclarationName::CXXLiteralOperatorName:
878 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
879 break;
880 case DeclarationName::CXXUsingDirective:
881 break;
882 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000883
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000884 return Key;
885 }
886
887 data_type ReadData(internal_key_type, const unsigned char* d,
888 unsigned DataLen) {
889 using namespace clang::io;
890 unsigned NumDecls = ReadUnalignedLE16(d);
891 DeclID *Start = (DeclID *)d;
892 return std::make_pair(Start, Start + NumDecls);
893 }
894};
895
896} // end anonymous namespace
897
898/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
899typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
900 ASTDeclContextNameLookupTable;
901
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000902bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
903 const std::pair<uint64_t, uint64_t> &Offsets,
904 DeclContextInfo &Info) {
905 SavedStreamPosition SavedPosition(Cursor);
906 // First the lexical decls.
907 if (Offsets.first != 0) {
908 Cursor.JumpToBit(Offsets.first);
909
910 RecordData Record;
911 const char *Blob;
912 unsigned BlobLen;
913 unsigned Code = Cursor.ReadCode();
914 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
915 if (RecCode != DECL_CONTEXT_LEXICAL) {
916 Error("Expected lexical block");
917 return true;
918 }
919
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000920 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
921 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000922 } else {
923 Info.LexicalDecls = 0;
924 Info.NumLexicalDecls = 0;
925 }
926
927 // Now the lookup table.
928 if (Offsets.second != 0) {
929 Cursor.JumpToBit(Offsets.second);
930
931 RecordData Record;
932 const char *Blob;
933 unsigned BlobLen;
934 unsigned Code = Cursor.ReadCode();
935 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
936 if (RecCode != DECL_CONTEXT_VISIBLE) {
937 Error("Expected visible lookup table block");
938 return true;
939 }
940 Info.NameLookupTableData
941 = ASTDeclContextNameLookupTable::Create(
942 (const unsigned char *)Blob + Record[0],
943 (const unsigned char *)Blob,
944 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl9d8f58b2010-08-24 00:50:00 +0000945 } else {
946 Info.NameLookupTableData = 0;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000947 }
948
949 return false;
950}
951
Sebastian Redl2c499f62010-08-18 23:56:43 +0000952void ASTReader::Error(const char *Msg) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000953 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000954}
955
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000956/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000957bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000958 if (Listener)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000959 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000960 ActualOriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000961 SuggestedPredefines);
Douglas Gregorc379c072009-04-28 18:58:38 +0000962 return false;
Douglas Gregor92863e42009-04-10 23:10:45 +0000963}
964
Douglas Gregorc5046832009-04-27 18:38:38 +0000965//===----------------------------------------------------------------------===//
966// Source Manager Deserialization
967//===----------------------------------------------------------------------===//
968
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000969/// \brief Read the line table in the source manager block.
Sebastian Redl2c373b92010-10-05 15:59:54 +0000970/// \returns true if there was an error.
971bool ASTReader::ParseLineTable(PerFileData &F,
972 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000973 unsigned Idx = 0;
974 LineTableInfo &LineTable = SourceMgr.getLineTable();
975
976 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +0000977 std::map<int, int> FileIDs;
978 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000979 // Extract the file name
980 unsigned FilenameLen = Record[Idx++];
981 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
982 Idx += FilenameLen;
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000983 MaybeAddSystemRootToFilename(Filename);
Mike Stump11289f42009-09-09 15:08:12 +0000984 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregora8854652009-04-13 17:12:42 +0000985 Filename.size());
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000986 }
987
988 // Parse the line entries
989 std::vector<LineEntry> Entries;
990 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000991 int FID = Record[Idx++];
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000992
993 // Extract the line entries
994 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000995 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000996 Entries.clear();
997 Entries.reserve(NumEntries);
998 for (unsigned I = 0; I != NumEntries; ++I) {
999 unsigned FileOffset = Record[Idx++];
1000 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +00001001 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +00001002 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001003 = (SrcMgr::CharacteristicKind)Record[Idx++];
1004 unsigned IncludeOffset = Record[Idx++];
1005 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1006 FileKind, IncludeOffset));
1007 }
1008 LineTable.AddEntry(FID, Entries);
1009 }
1010
1011 return false;
1012}
1013
Douglas Gregorc5046832009-04-27 18:38:38 +00001014namespace {
1015
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001016class ASTStatData {
Douglas Gregorc5046832009-04-27 18:38:38 +00001017public:
Douglas Gregorc5046832009-04-27 18:38:38 +00001018 const ino_t ino;
1019 const dev_t dev;
1020 const mode_t mode;
1021 const time_t mtime;
1022 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +00001023
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001024 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner2a6fa472010-11-23 19:28:12 +00001025 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregorc5046832009-04-27 18:38:38 +00001026};
1027
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001028class ASTStatLookupTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001029 public:
1030 typedef const char *external_key_type;
1031 typedef const char *internal_key_type;
1032
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001033 typedef ASTStatData data_type;
Douglas Gregorc5046832009-04-27 18:38:38 +00001034
1035 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001036 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001037 }
1038
1039 static internal_key_type GetInternalKey(const char *path) { return path; }
1040
1041 static bool EqualKey(internal_key_type a, internal_key_type b) {
1042 return strcmp(a, b) == 0;
1043 }
1044
1045 static std::pair<unsigned, unsigned>
1046 ReadKeyDataLength(const unsigned char*& d) {
1047 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1048 unsigned DataLen = (unsigned) *d++;
1049 return std::make_pair(KeyLen + 1, DataLen);
1050 }
1051
1052 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1053 return (const char *)d;
1054 }
1055
1056 static data_type ReadData(const internal_key_type, const unsigned char *d,
1057 unsigned /*DataLen*/) {
1058 using namespace clang::io;
1059
Douglas Gregorc5046832009-04-27 18:38:38 +00001060 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1061 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1062 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +00001063 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +00001064 off_t size = (off_t) ReadUnalignedLE64(d);
1065 return data_type(ino, dev, mode, mtime, size);
1066 }
1067};
1068
1069/// \brief stat() cache for precompiled headers.
1070///
1071/// This cache is very similar to the stat cache used by pretokenized
1072/// headers.
Chris Lattner226efd32010-11-23 19:19:34 +00001073class ASTStatCache : public FileSystemStatCache {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001074 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregorc5046832009-04-27 18:38:38 +00001075 CacheTy *Cache;
1076
1077 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +00001078public:
Chris Lattner2a6fa472010-11-23 19:28:12 +00001079 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1080 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +00001081 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1082 Cache = CacheTy::Create(Buckets, Base);
1083 }
1084
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001085 ~ASTStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +00001086
Chris Lattnerdd278432010-11-23 21:17:56 +00001087 LookupResult getStat(const char *Path, struct stat &StatBuf,
1088 int *FileDescriptor) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001089 // Do the lookup for the file's data in the AST file.
Chris Lattner226efd32010-11-23 19:19:34 +00001090 CacheTy::iterator I = Cache->find(Path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001091
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001092 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregorc5046832009-04-27 18:38:38 +00001093 if (I == Cache->end()) {
1094 ++NumStatMisses;
Chris Lattnerdd278432010-11-23 21:17:56 +00001095 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregorc5046832009-04-27 18:38:38 +00001096 }
Mike Stump11289f42009-09-09 15:08:12 +00001097
Douglas Gregorc5046832009-04-27 18:38:38 +00001098 ++NumStatHits;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001099 ASTStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001100
Chris Lattner226efd32010-11-23 19:19:34 +00001101 StatBuf.st_ino = Data.ino;
1102 StatBuf.st_dev = Data.dev;
1103 StatBuf.st_mtime = Data.mtime;
1104 StatBuf.st_mode = Data.mode;
1105 StatBuf.st_size = Data.size;
Chris Lattner8f0583d2010-11-23 20:05:15 +00001106 return CacheExists;
Douglas Gregorc5046832009-04-27 18:38:38 +00001107 }
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
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001184/// \brief If a header file is not found at the path that we expect it to be
1185/// and the PCH file was moved from its original location, try to resolve the
1186/// file by assuming that header+PCH were moved together and the header is in
1187/// the same place relative to the PCH.
1188static std::string
1189resolveFileRelativeToOriginalDir(const std::string &Filename,
1190 const std::string &OriginalDir,
1191 const std::string &CurrDir) {
1192 assert(OriginalDir != CurrDir &&
1193 "No point trying to resolve the file if the PCH dir didn't change");
1194 using namespace llvm::sys;
1195 llvm::SmallString<128> filePath(Filename);
1196 fs::make_absolute(filePath);
1197 assert(path::is_absolute(OriginalDir));
1198 llvm::SmallString<128> currPCHPath(CurrDir);
1199
1200 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1201 fileDirE = path::end(path::parent_path(filePath));
1202 path::const_iterator origDirI = path::begin(OriginalDir),
1203 origDirE = path::end(OriginalDir);
1204 // Skip the common path components from filePath and OriginalDir.
1205 while (fileDirI != fileDirE && origDirI != origDirE &&
1206 *fileDirI == *origDirI) {
1207 ++fileDirI;
1208 ++origDirI;
1209 }
1210 for (; origDirI != origDirE; ++origDirI)
1211 path::append(currPCHPath, "..");
1212 path::append(currPCHPath, fileDirI, fileDirE);
1213 path::append(currPCHPath, path::filename(Filename));
1214 return currPCHPath.str();
1215}
1216
Sebastian Redl06750302010-07-20 21:50:20 +00001217/// \brief Get a cursor that's correctly positioned for reading the source
1218/// location entry with the given ID.
Sebastian Redl2c373b92010-10-05 15:59:54 +00001219ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl06750302010-07-20 21:50:20 +00001220 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1221 "SLocCursorForID should only be called for real IDs.");
1222
1223 ID -= 1;
1224 PerFileData *F = 0;
1225 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1226 F = Chain[N - I - 1];
1227 if (ID < F->LocalNumSLocEntries)
1228 break;
1229 ID -= F->LocalNumSLocEntries;
1230 }
1231 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1232
1233 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00001234 return F;
Sebastian Redl06750302010-07-20 21:50:20 +00001235}
1236
Douglas Gregor258ae542009-04-27 06:38:32 +00001237/// \brief Read in the source location entry with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001238ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001239 if (ID == 0)
1240 return Success;
1241
1242 if (ID > TotalNumSLocEntries) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001243 Error("source location entry ID out-of-range for AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001244 return Failure;
1245 }
1246
Sebastian Redl2c373b92010-10-05 15:59:54 +00001247 PerFileData *F = SLocCursorForID(ID);
1248 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001249
Douglas Gregor258ae542009-04-27 06:38:32 +00001250 ++NumSLocEntriesRead;
Douglas Gregor258ae542009-04-27 06:38:32 +00001251 unsigned Code = SLocEntryCursor.ReadCode();
1252 if (Code == llvm::bitc::END_BLOCK ||
1253 Code == llvm::bitc::ENTER_SUBBLOCK ||
1254 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001255 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001256 return Failure;
1257 }
1258
Douglas Gregor258ae542009-04-27 06:38:32 +00001259 RecordData Record;
1260 const char *BlobStart;
1261 unsigned BlobLen;
1262 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1263 default:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001264 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001265 return Failure;
1266
Sebastian Redl539c5062010-08-18 23:57:32 +00001267 case SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001268 std::string Filename(BlobStart, BlobStart + BlobLen);
1269 MaybeAddSystemRootToFilename(Filename);
Chris Lattner5159f612010-11-23 08:35:12 +00001270 const FileEntry *File = FileMgr.getFile(Filename);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001271 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1272 OriginalDir != CurrentDir) {
1273 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1274 OriginalDir,
1275 CurrentDir);
1276 if (!resolved.empty())
1277 File = FileMgr.getFile(resolved);
1278 }
Axel Naumann63fbaed2011-01-27 10:55:51 +00001279 if (File == 0)
1280 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1281 (time_t)Record[5]);
Chris Lattnerd20dc872009-06-15 04:35:16 +00001282 if (File == 0) {
1283 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001284 ErrorStr += Filename;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001285 ErrorStr += "' referenced by AST file";
Chris Lattnerd20dc872009-06-15 04:35:16 +00001286 Error(ErrorStr.c_str());
1287 return Failure;
1288 }
Mike Stump11289f42009-09-09 15:08:12 +00001289
Douglas Gregor09b69892011-02-10 17:09:37 +00001290 if (Record.size() < 6) {
Ted Kremenekabb1ddd2010-03-18 21:23:05 +00001291 Error("source location entry is incorrect");
1292 return Failure;
1293 }
1294
Douglas Gregorce3a8292010-07-27 00:27:13 +00001295 if (!DisableValidation &&
1296 ((off_t)Record[4] != File->getSize()
Douglas Gregor08288f22010-04-09 15:54:22 +00001297#if !defined(LLVM_ON_WIN32)
1298 // In our regression testing, the Windows file system seems to
1299 // have inconsistent modification times that sometimes
1300 // erroneously trigger this error-handling path.
Douglas Gregorce3a8292010-07-27 00:27:13 +00001301 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor08288f22010-04-09 15:54:22 +00001302#endif
Douglas Gregorce3a8292010-07-27 00:27:13 +00001303 )) {
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001304 Diag(diag::err_fe_pch_file_modified)
1305 << Filename;
1306 return Failure;
1307 }
1308
Chris Lattner26b5c192010-11-23 09:19:42 +00001309 FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]),
1310 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor258ae542009-04-27 06:38:32 +00001311 ID, Record[0]);
1312 if (Record[3])
1313 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1314 .setHasLineDirectives();
Douglas Gregor09b69892011-02-10 17:09:37 +00001315
Douglas Gregor258ae542009-04-27 06:38:32 +00001316 break;
1317 }
1318
Sebastian Redl539c5062010-08-18 23:57:32 +00001319 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor258ae542009-04-27 06:38:32 +00001320 const char *Name = BlobStart;
1321 unsigned Offset = Record[0];
1322 unsigned Code = SLocEntryCursor.ReadCode();
1323 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001324 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +00001325 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001326
Sebastian Redl539c5062010-08-18 23:57:32 +00001327 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001328 Error("AST record has invalid code");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001329 return Failure;
1330 }
1331
Douglas Gregor258ae542009-04-27 06:38:32 +00001332 llvm::MemoryBuffer *Buffer
Chris Lattner58c79342010-04-05 22:42:27 +00001333 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1334 Name);
Douglas Gregor258ae542009-04-27 06:38:32 +00001335 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump11289f42009-09-09 15:08:12 +00001336
Douglas Gregore6648fb2009-04-28 20:33:11 +00001337 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001338 PCHPredefinesBlock Block = {
1339 BufferID,
1340 llvm::StringRef(BlobStart, BlobLen - 1)
1341 };
1342 PCHPredefinesBuffers.push_back(Block);
Douglas Gregore6648fb2009-04-28 20:33:11 +00001343 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001344
1345 break;
1346 }
1347
Sebastian Redl539c5062010-08-18 23:57:32 +00001348 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001349 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001350 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001351 ReadSourceLocation(*F, Record[2]),
1352 ReadSourceLocation(*F, Record[3]),
Douglas Gregor258ae542009-04-27 06:38:32 +00001353 Record[4],
1354 ID,
1355 Record[0]);
1356 break;
Mike Stump11289f42009-09-09 15:08:12 +00001357 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001358 }
1359
1360 return Success;
1361}
1362
Chris Lattnere78a6be2009-04-27 01:05:14 +00001363/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1364/// specified cursor. Read the abbreviations that are at the top of the block
1365/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001366bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001367 unsigned BlockID) {
1368 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001369 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001370 return Failure;
1371 }
Mike Stump11289f42009-09-09 15:08:12 +00001372
Chris Lattnere78a6be2009-04-27 01:05:14 +00001373 while (true) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001374 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattnere78a6be2009-04-27 01:05:14 +00001375 unsigned Code = Cursor.ReadCode();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001376
Chris Lattnere78a6be2009-04-27 01:05:14 +00001377 // We expect all abbrevs to be at the start of the block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001378 if (Code != llvm::bitc::DEFINE_ABBREV) {
1379 Cursor.JumpToBit(Offset);
Chris Lattnere78a6be2009-04-27 01:05:14 +00001380 return false;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001381 }
Chris Lattnere78a6be2009-04-27 01:05:14 +00001382 Cursor.ReadAbbrevRecord();
1383 }
1384}
1385
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001386PreprocessedEntity *ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001387 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregor796d76a2010-10-20 22:00:55 +00001388 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump11289f42009-09-09 15:08:12 +00001389
Douglas Gregorc3366a52009-04-21 23:56:24 +00001390 // Keep track of where we are in the stream, then jump back there
1391 // after reading this macro.
1392 SavedStreamPosition SavedPosition(Stream);
1393
1394 Stream.JumpToBit(Offset);
1395 RecordData Record;
1396 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1397 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001398
Douglas Gregorc3366a52009-04-21 23:56:24 +00001399 while (true) {
1400 unsigned Code = Stream.ReadCode();
1401 switch (Code) {
1402 case llvm::bitc::END_BLOCK:
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001403 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001404
1405 case llvm::bitc::ENTER_SUBBLOCK:
1406 // No known subblocks, always skip them.
1407 Stream.ReadSubBlockID();
1408 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001409 Error("malformed block record in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001410 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001411 }
1412 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001413
Douglas Gregorc3366a52009-04-21 23:56:24 +00001414 case llvm::bitc::DEFINE_ABBREV:
1415 Stream.ReadAbbrevRecord();
1416 continue;
1417 default: break;
1418 }
1419
1420 // Read a record.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001421 const char *BlobStart = 0;
1422 unsigned BlobLen = 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001423 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001424 PreprocessorRecordTypes RecType =
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001425 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001426 BlobLen);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001427 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001428 case PP_MACRO_OBJECT_LIKE:
1429 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001430 // If we already have a macro, that means that we've hit the end
1431 // of the definition of the macro we were looking for. We're
1432 // done.
1433 if (Macro)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001434 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001435
1436 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1437 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001438 Error("macro must have a name in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001439 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001440 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00001441 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001442 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001443
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001444 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001445 MI->setIsUsed(isUsed);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001446 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001447
Douglas Gregoraae92242010-03-19 21:51:54 +00001448 unsigned NextIndex = 3;
Sebastian Redl539c5062010-08-18 23:57:32 +00001449 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001450 // Decode function-like macro info.
1451 bool isC99VarArgs = Record[3];
1452 bool isGNUVarArgs = Record[4];
1453 MacroArgs.clear();
1454 unsigned NumArgs = Record[5];
Douglas Gregoraae92242010-03-19 21:51:54 +00001455 NextIndex = 6 + NumArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001456 for (unsigned i = 0; i != NumArgs; ++i)
1457 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1458
1459 // Install function-like macro info.
1460 MI->setIsFunctionLike();
1461 if (isC99VarArgs) MI->setIsC99Varargs();
1462 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001463 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001464 PP->getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001465 }
1466
1467 // Finally, install the macro.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001468 PP->setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001469
1470 // Remember that we saw this macro last so that we add the tokens that
1471 // form its body to it.
1472 Macro = MI;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001473
Douglas Gregoraae92242010-03-19 21:51:54 +00001474 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1475 // We have a macro definition. Load it now.
1476 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1477 getMacroDefinition(Record[NextIndex]));
1478 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001479
Douglas Gregorc3366a52009-04-21 23:56:24 +00001480 ++NumMacrosRead;
1481 break;
1482 }
Mike Stump11289f42009-09-09 15:08:12 +00001483
Sebastian Redl539c5062010-08-18 23:57:32 +00001484 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001485 // If we see a TOKEN before a PP_MACRO_*, then the file is
1486 // erroneous, just pretend we didn't see this.
1487 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001488
Douglas Gregorc3366a52009-04-21 23:56:24 +00001489 Token Tok;
1490 Tok.startToken();
Sebastian Redl2c373b92010-10-05 15:59:54 +00001491 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001492 Tok.setLength(Record[1]);
1493 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1494 Tok.setIdentifierInfo(II);
1495 Tok.setKind((tok::TokenKind)Record[3]);
1496 Tok.setFlag((Token::TokenFlags)Record[4]);
1497 Macro->AddTokenToBody(Tok);
1498 break;
1499 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001500 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001501 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001502
1503 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001504}
1505
Douglas Gregor92a96f52011-02-08 21:58:10 +00001506PreprocessedEntity *ASTReader::LoadPreprocessedEntity(PerFileData &F) {
1507 assert(PP && "Forgot to set Preprocessor ?");
1508 unsigned Code = F.PreprocessorDetailCursor.ReadCode();
1509 switch (Code) {
1510 case llvm::bitc::END_BLOCK:
1511 return 0;
1512
1513 case llvm::bitc::ENTER_SUBBLOCK:
1514 Error("unexpected subblock record in preprocessor detail block");
1515 return 0;
1516
1517 case llvm::bitc::DEFINE_ABBREV:
1518 Error("unexpected abbrevation record in preprocessor detail block");
1519 return 0;
1520
1521 default:
1522 break;
1523 }
1524
1525 if (!PP->getPreprocessingRecord()) {
1526 Error("no preprocessing record");
1527 return 0;
1528 }
1529
1530 // Read the record.
1531 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1532 const char *BlobStart = 0;
1533 unsigned BlobLen = 0;
1534 RecordData Record;
1535 PreprocessorDetailRecordTypes RecType =
1536 (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord(
1537 Code, Record, BlobStart, BlobLen);
1538 switch (RecType) {
1539 case PPD_MACRO_INSTANTIATION: {
1540 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1541 return PE;
1542
1543 MacroInstantiation *MI
1544 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1545 SourceRange(ReadSourceLocation(F, Record[1]),
1546 ReadSourceLocation(F, Record[2])),
1547 getMacroDefinition(Record[4]));
1548 PPRec.SetPreallocatedEntity(Record[0], MI);
1549 return MI;
1550 }
1551
1552 case PPD_MACRO_DEFINITION: {
1553 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1554 return PE;
1555
1556 if (Record[1] > MacroDefinitionsLoaded.size()) {
1557 Error("out-of-bounds macro definition record");
1558 return 0;
1559 }
1560
1561 // Decode the identifier info and then check again; if the macro is
1562 // still defined and associated with the identifier,
1563 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1564 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1565 MacroDefinition *MD
1566 = new (PPRec) MacroDefinition(II,
1567 ReadSourceLocation(F, Record[5]),
1568 SourceRange(
1569 ReadSourceLocation(F, Record[2]),
1570 ReadSourceLocation(F, Record[3])));
1571
1572 PPRec.SetPreallocatedEntity(Record[0], MD);
1573 MacroDefinitionsLoaded[Record[1] - 1] = MD;
1574
1575 if (DeserializationListener)
1576 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1577 }
1578
1579 return MacroDefinitionsLoaded[Record[1] - 1];
1580 }
1581
1582 case PPD_INCLUSION_DIRECTIVE: {
1583 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1584 return PE;
1585
1586 const char *FullFileNameStart = BlobStart + Record[3];
1587 const FileEntry *File
1588 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
1589 BlobLen - Record[3]));
1590
1591 // FIXME: Stable encoding
1592 InclusionDirective::InclusionKind Kind
1593 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1594 InclusionDirective *ID
1595 = new (PPRec) InclusionDirective(PPRec, Kind,
1596 llvm::StringRef(BlobStart, Record[3]),
1597 Record[4],
1598 File,
1599 SourceRange(ReadSourceLocation(F, Record[1]),
1600 ReadSourceLocation(F, Record[2])));
1601 PPRec.SetPreallocatedEntity(Record[0], ID);
1602 return ID;
1603 }
1604 }
1605
1606 Error("invalid offset in preprocessor detail block");
1607 return 0;
1608}
1609
Douglas Gregor09b69892011-02-10 17:09:37 +00001610namespace {
1611 /// \brief Trait class used to search the on-disk hash table containing all of
1612 /// the header search information.
1613 ///
1614 /// The on-disk hash table contains a mapping from each header path to
1615 /// information about that header (how many times it has been included, its
1616 /// controlling macro, etc.). Note that we actually hash based on the
1617 /// filename, and support "deep" comparisons of file names based on current
1618 /// inode numbers, so that the search can cope with non-normalized path names
1619 /// and symlinks.
1620 class HeaderFileInfoTrait {
1621 const char *SearchPath;
1622 struct stat SearchPathStatBuf;
1623 llvm::Optional<int> SearchPathStatResult;
1624
1625 int StatSimpleCache(const char *Path, struct stat *StatBuf) {
1626 if (Path == SearchPath) {
1627 if (!SearchPathStatResult)
1628 SearchPathStatResult = stat(Path, &SearchPathStatBuf);
1629
1630 *StatBuf = SearchPathStatBuf;
1631 return *SearchPathStatResult;
1632 }
1633
1634 return stat(Path, StatBuf);
1635 }
1636
1637 public:
1638 typedef const char *external_key_type;
1639 typedef const char *internal_key_type;
1640
1641 typedef HeaderFileInfo data_type;
1642
1643 HeaderFileInfoTrait(const char *SearchPath = 0) : SearchPath(SearchPath) { }
1644
1645 static unsigned ComputeHash(const char *path) {
1646 return llvm::HashString(llvm::sys::path::filename(path));
1647 }
1648
1649 static internal_key_type GetInternalKey(const char *path) { return path; }
1650
1651 bool EqualKey(internal_key_type a, internal_key_type b) {
1652 if (strcmp(a, b) == 0)
1653 return true;
1654
1655 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1656 return false;
1657
1658 // The file names match, but the path names don't. stat() the files to
1659 // see if they are the same.
1660 struct stat StatBufA, StatBufB;
1661 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1662 return false;
1663
1664 return StatBufA.st_ino == StatBufB.st_ino;
1665 }
1666
1667 static std::pair<unsigned, unsigned>
1668 ReadKeyDataLength(const unsigned char*& d) {
1669 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1670 unsigned DataLen = (unsigned) *d++;
1671 return std::make_pair(KeyLen + 1, DataLen);
1672 }
1673
1674 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1675 return (const char *)d;
1676 }
1677
1678 static data_type ReadData(const internal_key_type, const unsigned char *d,
1679 unsigned DataLen) {
1680 const unsigned char *End = d + DataLen;
1681 using namespace clang::io;
1682 HeaderFileInfo HFI;
1683 unsigned Flags = *d++;
1684 HFI.isImport = (Flags >> 3) & 0x01;
1685 HFI.DirInfo = (Flags >> 1) & 0x03;
1686 HFI.Resolved = Flags & 0x01;
1687 HFI.NumIncludes = ReadUnalignedLE16(d);
1688 HFI.ControllingMacroID = ReadUnalignedLE32(d);
1689 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1690 (void)End;
1691
1692 // This HeaderFileInfo was externally loaded.
1693 HFI.External = true;
1694 return HFI;
1695 }
1696 };
1697}
1698
1699/// \brief The on-disk hash table used for the global method pool.
1700typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
1701 HeaderFileInfoLookupTable;
1702
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001703void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1704 uint64_t Offset) {
1705 // Note that this identifier has a macro definition.
1706 II->setHasMacroDefinition(true);
1707
1708 // Adjust the offset based on our position in the chain.
1709 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1710 if (Chain[I] == &F)
1711 break;
1712
1713 Offset += Chain[I]->SizeInBits;
1714 }
1715
1716 UnreadMacroRecordOffsets[II] = Offset;
1717}
1718
Sebastian Redl2c499f62010-08-18 23:56:43 +00001719void ASTReader::ReadDefinedMacros() {
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001720 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001721 PerFileData &F = *Chain[N - I - 1];
1722 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001723
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001724 // If there was no preprocessor block, skip this file.
1725 if (!MacroCursor.getBitStreamReader())
1726 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001727
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001728 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001729 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001730
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001731 RecordData Record;
1732 while (true) {
1733 unsigned Code = Cursor.ReadCode();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001734 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001735 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001736
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001737 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1738 // No known subblocks, always skip them.
1739 Cursor.ReadSubBlockID();
1740 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001741 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001742 return;
1743 }
1744 continue;
1745 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001746
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001747 if (Code == llvm::bitc::DEFINE_ABBREV) {
1748 Cursor.ReadAbbrevRecord();
1749 continue;
1750 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001751
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001752 // Read a record.
1753 const char *BlobStart;
1754 unsigned BlobLen;
1755 Record.clear();
1756 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1757 default: // Default behavior: ignore.
1758 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001759
Sebastian Redl539c5062010-08-18 23:57:32 +00001760 case PP_MACRO_OBJECT_LIKE:
1761 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001762 DecodeIdentifierInfo(Record[0]);
1763 break;
1764
Sebastian Redl539c5062010-08-18 23:57:32 +00001765 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001766 // Ignore tokens.
1767 break;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001768 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001769 }
1770 }
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001771
1772 // Drain the unread macro-record offsets map.
1773 while (!UnreadMacroRecordOffsets.empty())
1774 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1775}
1776
1777void ASTReader::LoadMacroDefinition(
1778 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1779 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1780 PerFileData *F = 0;
1781 uint64_t Offset = Pos->second;
1782 UnreadMacroRecordOffsets.erase(Pos);
1783
1784 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1785 if (Offset < Chain[I]->SizeInBits) {
1786 F = Chain[I];
1787 break;
1788 }
1789
1790 Offset -= Chain[I]->SizeInBits;
1791 }
1792 if (!F) {
1793 Error("Malformed macro record offset");
1794 return;
1795 }
1796
1797 ReadMacroRecord(*F, Offset);
1798}
1799
1800void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1801 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1802 = UnreadMacroRecordOffsets.find(II);
1803 LoadMacroDefinition(Pos);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001804}
1805
Sebastian Redl50e26582010-09-15 19:54:06 +00001806MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor91096292010-10-02 19:29:26 +00001807 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregoraae92242010-03-19 21:51:54 +00001808 return 0;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001809
Douglas Gregor91096292010-10-02 19:29:26 +00001810 if (!MacroDefinitionsLoaded[ID - 1]) {
1811 unsigned Index = ID - 1;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001812 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1813 PerFileData &F = *Chain[N - I - 1];
1814 if (Index < F.LocalNumMacroDefinitions) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001815 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
1816 F.PreprocessorDetailCursor.JumpToBit(F.MacroDefinitionOffsets[Index]);
1817 LoadPreprocessedEntity(F);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001818 break;
1819 }
1820 Index -= F.LocalNumMacroDefinitions;
1821 }
Douglas Gregor91096292010-10-02 19:29:26 +00001822 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001823 }
1824
Douglas Gregor91096292010-10-02 19:29:26 +00001825 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001826}
1827
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001828/// \brief If we are loading a relocatable PCH file, and the filename is
1829/// not an absolute path, add the system root to the beginning of the file
1830/// name.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001831void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001832 // If this is not a relocatable PCH file, there's nothing to do.
1833 if (!RelocatablePCH)
1834 return;
Mike Stump11289f42009-09-09 15:08:12 +00001835
Michael J. Spencerf28df4c2010-12-17 21:22:22 +00001836 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001837 return;
1838
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001839 if (isysroot == 0) {
1840 // If no system root was given, default to '/'
1841 Filename.insert(Filename.begin(), '/');
1842 return;
1843 }
Mike Stump11289f42009-09-09 15:08:12 +00001844
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001845 unsigned Length = strlen(isysroot);
1846 if (isysroot[Length - 1] != '/')
1847 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001848
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001849 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1850}
1851
Sebastian Redl2c499f62010-08-18 23:56:43 +00001852ASTReader::ASTReadResult
Sebastian Redl3e31c722010-08-18 23:56:56 +00001853ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001854 llvm::BitstreamCursor &Stream = F.Stream;
1855
Sebastian Redl539c5062010-08-18 23:57:32 +00001856 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001857 Error("malformed block record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001858 return Failure;
1859 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001860
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001861 // Read all of the records and blocks for the ASt file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001862 RecordData Record;
Sebastian Redl393f8b72010-07-19 20:52:06 +00001863 bool First = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001864 while (!Stream.AtEndOfStream()) {
1865 unsigned Code = Stream.ReadCode();
1866 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001867 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001868 Error("error at end of module block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001869 return Failure;
1870 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001871
Douglas Gregor55abb232009-04-10 20:39:37 +00001872 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001873 }
1874
1875 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1876 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001877 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001878 // We lazily load the decls block, but we want to set up the
1879 // DeclsCursor cursor to point into it. Clone our current bitcode
1880 // cursor to it, enter the block and read the abbrevs in that block.
1881 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001882 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001883 if (Stream.SkipBlock() || // Skip with the main cursor.
1884 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001885 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001886 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001887 return Failure;
1888 }
1889 break;
Mike Stump11289f42009-09-09 15:08:12 +00001890
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00001891 case DECL_UPDATES_BLOCK_ID:
1892 if (Stream.SkipBlock()) {
1893 Error("malformed block record in AST file");
1894 return Failure;
1895 }
1896 break;
1897
Sebastian Redl539c5062010-08-18 23:57:32 +00001898 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001899 F.MacroCursor = Stream;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001900 if (PP)
1901 PP->setExternalSource(this);
1902
Douglas Gregor796d76a2010-10-20 22:00:55 +00001903 if (Stream.SkipBlock() ||
1904 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001905 Error("malformed block record in AST file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001906 return Failure;
1907 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001908 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001909 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001910
Douglas Gregor92a96f52011-02-08 21:58:10 +00001911 case PREPROCESSOR_DETAIL_BLOCK_ID:
1912 F.PreprocessorDetailCursor = Stream;
1913 if (Stream.SkipBlock() ||
1914 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1915 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1916 Error("malformed preprocessor detail record in AST file");
1917 return Failure;
1918 }
1919 F.PreprocessorDetailStartOffset
1920 = F.PreprocessorDetailCursor.GetCurrentBitNo();
1921 break;
1922
Sebastian Redl539c5062010-08-18 23:57:32 +00001923 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001924 switch (ReadSourceManagerBlock(F)) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001925 case Success:
1926 break;
1927
1928 case Failure:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001929 Error("malformed source manager block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001930 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001931
1932 case IgnorePCH:
1933 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001934 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001935 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001936 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00001937 First = false;
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001938 continue;
1939 }
1940
1941 if (Code == llvm::bitc::DEFINE_ABBREV) {
1942 Stream.ReadAbbrevRecord();
1943 continue;
1944 }
1945
1946 // Read and process a record.
1947 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001948 const char *BlobStart = 0;
1949 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001950 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001951 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001952 default: // Default behavior: ignore.
1953 break;
1954
Sebastian Redl539c5062010-08-18 23:57:32 +00001955 case METADATA: {
1956 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1957 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001958 : diag::warn_pch_version_too_new);
1959 return IgnorePCH;
1960 }
1961
1962 RelocatablePCH = Record[4];
1963 if (Listener) {
1964 std::string TargetTriple(BlobStart, BlobLen);
1965 if (Listener->ReadTargetTriple(TargetTriple))
1966 return IgnorePCH;
1967 }
1968 break;
1969 }
1970
Sebastian Redl539c5062010-08-18 23:57:32 +00001971 case CHAINED_METADATA: {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001972 if (!First) {
1973 Error("CHAINED_METADATA is not first record in block");
1974 return Failure;
1975 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001976 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1977 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001978 : diag::warn_pch_version_too_new);
1979 return IgnorePCH;
1980 }
1981
Sebastian Redl009e7f22010-10-05 16:15:19 +00001982 // Load the chained file, which is always a PCH file.
1983 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001984 case Failure: return Failure;
1985 // If we have to ignore the dependency, we'll have to ignore this too.
1986 case IgnorePCH: return IgnorePCH;
1987 case Success: break;
1988 }
1989 break;
1990 }
1991
Sebastian Redl539c5062010-08-18 23:57:32 +00001992 case TYPE_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001993 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001994 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001995 return Failure;
1996 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001997 F.TypeOffsets = (const uint32_t *)BlobStart;
1998 F.LocalNumTypes = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001999 break;
2000
Sebastian Redl539c5062010-08-18 23:57:32 +00002001 case DECL_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00002002 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002003 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002004 return Failure;
2005 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002006 F.DeclOffsets = (const uint32_t *)BlobStart;
2007 F.LocalNumDecls = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002008 break;
Douglas Gregor55abb232009-04-10 20:39:37 +00002009
Sebastian Redl539c5062010-08-18 23:57:32 +00002010 case TU_UPDATE_LEXICAL: {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002011 DeclContextInfo Info = {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002012 /* No visible information */ 0,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002013 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
2014 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002015 };
Douglas Gregoraa433012010-10-01 01:18:02 +00002016 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
2017 .push_back(Info);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002018 break;
2019 }
2020
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002021 case UPDATE_VISIBLE: {
2022 serialization::DeclID ID = Record[0];
2023 void *Table = ASTDeclContextNameLookupTable::Create(
2024 (const unsigned char *)BlobStart + Record[1],
2025 (const unsigned char *)BlobStart,
2026 ASTDeclContextNameLookupTrait(*this));
Douglas Gregoraa433012010-10-01 01:18:02 +00002027 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002028 DeclContextInfo Info = {
2029 Table, /* No lexical inforamtion */ 0, 0
2030 };
2031 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
2032 } else
2033 PendingVisibleUpdates[ID].push_back(Table);
2034 break;
2035 }
2036
Sebastian Redl539c5062010-08-18 23:57:32 +00002037 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002038 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
2039 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002040 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002041 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
2042 Latest > FirstLatestDeclIDs[First]) &&
2043 "The new latest is supposed to come after the previous latest");
2044 FirstLatestDeclIDs[First] = Latest;
2045 }
2046 break;
2047 }
2048
Sebastian Redl539c5062010-08-18 23:57:32 +00002049 case LANGUAGE_OPTIONS:
Douglas Gregorce3a8292010-07-27 00:27:13 +00002050 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor55abb232009-04-10 20:39:37 +00002051 return IgnorePCH;
2052 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00002053
Sebastian Redl539c5062010-08-18 23:57:32 +00002054 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00002055 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00002056 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00002057 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002058 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00002059 (const unsigned char *)F.IdentifierTableData + Record[0],
2060 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl2c373b92010-10-05 15:59:54 +00002061 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002062 if (PP)
2063 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00002064 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002065 break;
2066
Sebastian Redl539c5062010-08-18 23:57:32 +00002067 case IDENTIFIER_OFFSET:
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002068 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002069 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002070 return Failure;
2071 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002072 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2073 F.LocalNumIdentifiers = Record[0];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002074 break;
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002075
Sebastian Redl539c5062010-08-18 23:57:32 +00002076 case EXTERNAL_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002077 // Optimization for the first block.
2078 if (ExternalDefinitions.empty())
2079 ExternalDefinitions.swap(Record);
2080 else
2081 ExternalDefinitions.insert(ExternalDefinitions.end(),
2082 Record.begin(), Record.end());
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002083 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00002084
Sebastian Redl539c5062010-08-18 23:57:32 +00002085 case SPECIAL_TYPES:
Sebastian Redlb293a452010-07-20 21:20:32 +00002086 // Optimization for the first block
2087 if (SpecialTypes.empty())
2088 SpecialTypes.swap(Record);
2089 else
2090 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002091 break;
2092
Sebastian Redl539c5062010-08-18 23:57:32 +00002093 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002094 TotalNumStatements += Record[0];
2095 TotalNumMacros += Record[1];
2096 TotalLexicalDeclContexts += Record[2];
2097 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00002098 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002099
Sebastian Redl539c5062010-08-18 23:57:32 +00002100 case TENTATIVE_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002101 // Optimization for the first block.
2102 if (TentativeDefinitions.empty())
2103 TentativeDefinitions.swap(Record);
2104 else
2105 TentativeDefinitions.insert(TentativeDefinitions.end(),
2106 Record.begin(), Record.end());
Douglas Gregord4df8652009-04-22 22:02:47 +00002107 break;
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002108
Sebastian Redl539c5062010-08-18 23:57:32 +00002109 case UNUSED_FILESCOPED_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002110 // Optimization for the first block.
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002111 if (UnusedFileScopedDecls.empty())
2112 UnusedFileScopedDecls.swap(Record);
Sebastian Redlb293a452010-07-20 21:20:32 +00002113 else
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002114 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
2115 Record.begin(), Record.end());
Tanya Lattner90073802010-02-12 00:07:30 +00002116 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002117
Sebastian Redl539c5062010-08-18 23:57:32 +00002118 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002119 // Later blocks overwrite earlier ones.
2120 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002121 break;
2122
Sebastian Redl539c5062010-08-18 23:57:32 +00002123 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002124 // Optimization for the first block.
2125 if (LocallyScopedExternalDecls.empty())
2126 LocallyScopedExternalDecls.swap(Record);
2127 else
2128 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
2129 Record.begin(), Record.end());
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002130 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002131
Sebastian Redl539c5062010-08-18 23:57:32 +00002132 case SELECTOR_OFFSETS:
Sebastian Redla19a67f2010-08-03 21:58:15 +00002133 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00002134 F.LocalNumSelectors = Record[0];
Douglas Gregor95c13f52009-04-25 17:48:32 +00002135 break;
2136
Sebastian Redl539c5062010-08-18 23:57:32 +00002137 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00002138 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002139 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00002140 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002141 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00002142 F.SelectorLookupTableData + Record[0],
2143 F.SelectorLookupTableData,
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002144 ASTSelectorLookupTrait(*this));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002145 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002146 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00002147
Sebastian Redl96371b42010-09-22 00:42:30 +00002148 case REFERENCED_SELECTOR_POOL:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002149 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002150 break;
2151
Sebastian Redl539c5062010-08-18 23:57:32 +00002152 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002153 if (!Record.empty() && Listener)
2154 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002155 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002156
Sebastian Redl539c5062010-08-18 23:57:32 +00002157 case SOURCE_LOCATION_OFFSETS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002158 F.SLocOffsets = (const uint32_t *)BlobStart;
2159 F.LocalNumSLocEntries = Record[0];
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002160 F.LocalSLocSize = Record[1];
Douglas Gregor258ae542009-04-27 06:38:32 +00002161 break;
2162
Sebastian Redl539c5062010-08-18 23:57:32 +00002163 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl96371b42010-09-22 00:42:30 +00002164 if (PreloadSLocEntries.empty())
2165 PreloadSLocEntries.swap(Record);
2166 else
2167 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2168 Record.begin(), Record.end());
Douglas Gregor258ae542009-04-27 06:38:32 +00002169 break;
Douglas Gregorc5046832009-04-27 18:38:38 +00002170
Sebastian Redl539c5062010-08-18 23:57:32 +00002171 case STAT_CACHE: {
Douglas Gregor606c4ac2011-02-05 19:42:43 +00002172 if (!DisableStatCache) {
2173 ASTStatCache *MyStatCache =
2174 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2175 (const unsigned char *)BlobStart,
2176 NumStatHits, NumStatMisses);
2177 FileMgr.addStatCache(MyStatCache);
2178 F.StatCache = MyStatCache;
2179 }
Douglas Gregorc5046832009-04-27 18:38:38 +00002180 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00002181 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002182
Sebastian Redl539c5062010-08-18 23:57:32 +00002183 case EXT_VECTOR_DECLS:
Sebastian Redl04f5c312010-07-28 21:38:49 +00002184 // Optimization for the first block.
2185 if (ExtVectorDecls.empty())
2186 ExtVectorDecls.swap(Record);
2187 else
2188 ExtVectorDecls.insert(ExtVectorDecls.end(),
2189 Record.begin(), Record.end());
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002190 break;
2191
Sebastian Redl539c5062010-08-18 23:57:32 +00002192 case VTABLE_USES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002193 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002194 VTableUses.swap(Record);
2195 break;
2196
Sebastian Redl539c5062010-08-18 23:57:32 +00002197 case DYNAMIC_CLASSES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002198 // Optimization for the first block.
2199 if (DynamicClasses.empty())
2200 DynamicClasses.swap(Record);
2201 else
2202 DynamicClasses.insert(DynamicClasses.end(),
2203 Record.begin(), Record.end());
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002204 break;
2205
Sebastian Redl539c5062010-08-18 23:57:32 +00002206 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002207 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002208 break;
2209
Sebastian Redl539c5062010-08-18 23:57:32 +00002210 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002211 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002212 SemaDeclRefs.swap(Record);
2213 break;
2214
Sebastian Redl539c5062010-08-18 23:57:32 +00002215 case ORIGINAL_FILE_NAME:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002216 // The primary AST will be the last to get here, so it will be the one
Sebastian Redlb293a452010-07-20 21:20:32 +00002217 // that's used.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00002218 ActualOriginalFileName.assign(BlobStart, BlobLen);
2219 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002220 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00002221 break;
Mike Stump11289f42009-09-09 15:08:12 +00002222
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002223 case ORIGINAL_PCH_DIR:
2224 // The primary AST will be the last to get here, so it will be the one
2225 // that's used.
2226 OriginalDir.assign(BlobStart, BlobLen);
2227 break;
2228
Sebastian Redl539c5062010-08-18 23:57:32 +00002229 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00002230 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002231 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2232 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2233 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregord54f3a12009-10-05 21:07:28 +00002234 return IgnorePCH;
2235 }
2236 break;
2237 }
Sebastian Redlfa061442010-07-21 20:07:32 +00002238
Sebastian Redl539c5062010-08-18 23:57:32 +00002239 case MACRO_DEFINITION_OFFSETS:
Sebastian Redlfa061442010-07-21 20:07:32 +00002240 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2241 F.NumPreallocatedPreprocessingEntities = Record[0];
2242 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregoraae92242010-03-19 21:51:54 +00002243 break;
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002244
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002245 case DECL_UPDATE_OFFSETS: {
2246 if (Record.size() % 2 != 0) {
2247 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2248 return Failure;
2249 }
2250 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2251 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2252 .push_back(std::make_pair(&F, Record[I+1]));
2253 break;
2254 }
2255
Sebastian Redl539c5062010-08-18 23:57:32 +00002256 case DECL_REPLACEMENTS: {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002257 if (Record.size() % 2 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002258 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002259 return Failure;
2260 }
2261 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl539c5062010-08-18 23:57:32 +00002262 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002263 std::make_pair(&F, Record[I+1]);
2264 break;
2265 }
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002266
2267 case CXX_BASE_SPECIFIER_OFFSETS: {
2268 if (F.LocalNumCXXBaseSpecifiers != 0) {
2269 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2270 return Failure;
2271 }
2272
2273 F.LocalNumCXXBaseSpecifiers = Record[0];
2274 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2275 break;
2276 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002277
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002278 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002279 if (Record.size() % 2 != 0) {
2280 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2281 return Failure;
2282 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002283 if (PragmaDiagMappings.empty())
2284 PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002285 else
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002286 PragmaDiagMappings.insert(PragmaDiagMappings.end(),
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002287 Record.begin(), Record.end());
2288 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002289
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002290 case CUDA_SPECIAL_DECL_REFS:
2291 // Later tables overwrite earlier ones.
2292 CUDASpecialDeclRefs.swap(Record);
2293 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002294
2295 case HEADER_SEARCH_TABLE:
2296 F.HeaderFileInfoTableData = BlobStart;
2297 F.LocalNumHeaderFileInfos = Record[1];
2298 if (Record[0]) {
2299 F.HeaderFileInfoTable
2300 = HeaderFileInfoLookupTable::Create(
2301 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2302 (const unsigned char *)F.HeaderFileInfoTableData);
2303 if (PP)
2304 PP->getHeaderSearchInfo().SetExternalSource(this);
2305 }
2306 break;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002307 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00002308 First = false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002309 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002310 Error("premature end of bitstream in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002311 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002312}
2313
Sebastian Redl009e7f22010-10-05 16:15:19 +00002314ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2315 ASTFileType Type) {
2316 switch(ReadASTCore(FileName, Type)) {
Sebastian Redl2abc0382010-07-16 20:41:52 +00002317 case Failure: return Failure;
2318 case IgnorePCH: return IgnorePCH;
2319 case Success: break;
2320 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002321
2322 // Here comes stuff that we only do once the entire chain is loaded.
2323
Sebastian Redl96371b42010-09-22 00:42:30 +00002324 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redlfa061442010-07-21 20:07:32 +00002325 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redlada023c2010-08-04 20:40:17 +00002326 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2327 TotalNumSelectors = 0;
Sebastian Redl9e687992010-07-19 22:06:55 +00002328 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl96371b42010-09-22 00:42:30 +00002329 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002330 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002331 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl9e687992010-07-19 22:06:55 +00002332 TotalNumTypes += Chain[I]->LocalNumTypes;
2333 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redlfa061442010-07-21 20:07:32 +00002334 TotalNumPreallocatedPreprocessingEntities +=
2335 Chain[I]->NumPreallocatedPreprocessingEntities;
2336 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redlada023c2010-08-04 20:40:17 +00002337 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl9e687992010-07-19 22:06:55 +00002338 }
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002339 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002340 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl9e687992010-07-19 22:06:55 +00002341 TypesLoaded.resize(TotalNumTypes);
2342 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redlfa061442010-07-21 20:07:32 +00002343 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2344 if (PP) {
2345 if (TotalNumIdentifiers > 0)
2346 PP->getHeaderSearchInfo().SetExternalLookup(this);
2347 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2348 if (!PP->getPreprocessingRecord())
2349 PP->createPreprocessingRecord();
2350 PP->getPreprocessingRecord()->SetExternalSource(*this,
2351 TotalNumPreallocatedPreprocessingEntities);
2352 }
2353 }
Sebastian Redlada023c2010-08-04 20:40:17 +00002354 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl96371b42010-09-22 00:42:30 +00002355 // Preload SLocEntries.
2356 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2357 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2358 if (Result != Success)
2359 return Result;
2360 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002361
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002362 // Check the predefines buffers.
Douglas Gregorce3a8292010-07-27 00:27:13 +00002363 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002364 return IgnorePCH;
2365
2366 if (PP) {
2367 // Initialization of keywords and pragmas occurs before the
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002368 // AST file is read, so there may be some identifiers that were
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002369 // loaded into the IdentifierTable before we intercepted the
2370 // creation of identifiers. Iterate through the list of known
2371 // identifiers and determine whether we have to establish
2372 // preprocessor definitions or top-level identifier declaration
2373 // chains for those identifiers.
2374 //
2375 // We copy the IdentifierInfo pointers to a small vector first,
2376 // since de-serializing declarations or macro definitions can add
2377 // new entries into the identifier table, invalidating the
2378 // iterators.
2379 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2380 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2381 IdEnd = PP->getIdentifierTable().end();
2382 Id != IdEnd; ++Id)
2383 Identifiers.push_back(Id->second);
Sebastian Redlfa061442010-07-21 20:07:32 +00002384 // We need to search the tables in all files.
Sebastian Redlfa061442010-07-21 20:07:32 +00002385 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002386 ASTIdentifierLookupTable *IdTable
2387 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2388 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl5c415f32010-07-22 17:01:13 +00002389 // ones.
2390 if (!IdTable)
2391 continue;
Sebastian Redlfa061442010-07-21 20:07:32 +00002392 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2393 IdentifierInfo *II = Identifiers[I];
2394 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00002395 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redlfa061442010-07-21 20:07:32 +00002396 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002397 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redlb293a452010-07-20 21:20:32 +00002398 if (Pos == IdTable->end())
2399 continue;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002400
Sebastian Redlb293a452010-07-20 21:20:32 +00002401 // Dereferencing the iterator has the effect of populating the
2402 // IdentifierInfo node with the various declarations it needs.
2403 (void)*Pos;
2404 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002405 }
2406 }
2407
2408 if (Context)
2409 InitializeContext(*Context);
2410
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002411 if (DeserializationListener)
2412 DeserializationListener->ReaderInitialized(this);
2413
Douglas Gregor936a5b42010-11-30 05:23:00 +00002414 // If this AST file is a precompiled preamble, then set the main file ID of
2415 // the source manager to the file source file from which the preamble was
2416 // built. This is the only valid way to use a precompiled preamble.
2417 if (Type == Preamble) {
2418 SourceLocation Loc
2419 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2420 if (Loc.isValid()) {
2421 std::pair<FileID, unsigned> Decomposed = SourceMgr.getDecomposedLoc(Loc);
2422 SourceMgr.SetPreambleFileID(Decomposed.first);
2423 }
2424 }
2425
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002426 return Success;
2427}
2428
Sebastian Redl009e7f22010-10-05 16:15:19 +00002429ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2430 ASTFileType Type) {
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002431 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl009e7f22010-10-05 16:15:19 +00002432 Chain.push_back(new PerFileData(Type));
Sebastian Redl34522812010-07-16 17:50:48 +00002433 PerFileData &F = *Chain.back();
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002434 if (Prev)
2435 Prev->NextInSource = &F;
2436 else
2437 FirstInSource = &F;
2438 F.Loaders.push_back(Prev);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002439
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002440 // Set the AST file name.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002441 F.FileName = FileName;
2442
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002443 if (FileName != "-") {
2444 CurrentDir = llvm::sys::path::parent_path(FileName);
2445 if (CurrentDir.empty()) CurrentDir = ".";
2446 }
2447
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002448 // Open the AST file.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002449 //
2450 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2451 std::string ErrStr;
Michael J. Spencerf25faaa2010-12-09 17:36:38 +00002452 llvm::error_code ec;
2453 if (FileName == "-") {
Michael J. Spencerd9da7a12010-12-16 03:28:14 +00002454 ec = llvm::MemoryBuffer::getSTDIN(F.Buffer);
Michael J. Spencerf25faaa2010-12-09 17:36:38 +00002455 if (ec)
2456 ErrStr = ec.message();
2457 } else
Chris Lattner5159f612010-11-23 08:35:12 +00002458 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002459 if (!F.Buffer) {
2460 Error(ErrStr.c_str());
2461 return IgnorePCH;
2462 }
2463
2464 // Initialize the stream
2465 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2466 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl34522812010-07-16 17:50:48 +00002467 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002468 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002469 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002470
2471 // Sniff for the signature.
2472 if (Stream.Read(8) != 'C' ||
2473 Stream.Read(8) != 'P' ||
2474 Stream.Read(8) != 'C' ||
2475 Stream.Read(8) != 'H') {
2476 Diag(diag::err_not_a_pch_file) << FileName;
2477 return Failure;
2478 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002479
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002480 while (!Stream.AtEndOfStream()) {
2481 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002482
Douglas Gregor92863e42009-04-10 23:10:45 +00002483 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002484 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002485 return Failure;
2486 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002487
2488 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002489
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002490 // We only know the AST subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002491 switch (BlockID) {
2492 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002493 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002494 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002495 return Failure;
2496 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002497 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00002498 case AST_BLOCK_ID:
Sebastian Redl3e31c722010-08-18 23:56:56 +00002499 switch (ReadASTBlock(F)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002500 case Success:
2501 break;
2502
2503 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00002504 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00002505
2506 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00002507 // FIXME: We could consider reading through to the end of this
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002508 // AST block, skipping subblocks, to see if there are other
2509 // AST blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00002510
2511 // Clear out any preallocated source location entries, so that
2512 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002513 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00002514
2515 // Remove the stat cache.
Sebastian Redl34522812010-07-16 17:50:48 +00002516 if (F.StatCache)
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002517 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00002518
Douglas Gregor92863e42009-04-10 23:10:45 +00002519 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002520 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002521 break;
2522 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002523 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002524 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002525 return Failure;
2526 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002527 break;
2528 }
Mike Stump11289f42009-09-09 15:08:12 +00002529 }
2530
Sebastian Redl2abc0382010-07-16 20:41:52 +00002531 return Success;
2532}
2533
Sebastian Redl2c499f62010-08-18 23:56:43 +00002534void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002535 PP = &pp;
Sebastian Redlfa061442010-07-21 20:07:32 +00002536
2537 unsigned TotalNum = 0;
2538 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2539 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2540 if (TotalNum) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002541 if (!PP->getPreprocessingRecord())
2542 PP->createPreprocessingRecord();
Sebastian Redlfa061442010-07-21 20:07:32 +00002543 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregoraae92242010-03-19 21:51:54 +00002544 }
2545}
2546
Sebastian Redl2c499f62010-08-18 23:56:43 +00002547void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002548 Context = &Ctx;
2549 assert(Context && "Passed null context!");
2550
2551 assert(PP && "Forgot to set Preprocessor ?");
2552 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2553 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00002554 PP->setExternalSource(this);
Douglas Gregor09b69892011-02-10 17:09:37 +00002555 PP->getHeaderSearchInfo().SetExternalSource(this);
2556
Douglas Gregoraa433012010-10-01 01:18:02 +00002557 // If we have an update block for the TU waiting, we have to add it before
2558 // deserializing the decl.
2559 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2560 if (DCU != DeclContextOffsets.end()) {
2561 // Insertion could invalidate map, so grab vector.
2562 DeclContextInfos T;
2563 T.swap(DCU->second);
2564 DeclContextOffsets.erase(DCU);
2565 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2566 }
2567
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002568 // Load the translation unit declaration
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00002569 GetTranslationUnitDecl();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002570
2571 // Load the special types.
2572 Context->setBuiltinVaListType(
Sebastian Redl539c5062010-08-18 23:57:32 +00002573 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2574 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002575 Context->setObjCIdType(GetType(Id));
Sebastian Redl539c5062010-08-18 23:57:32 +00002576 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002577 Context->setObjCSelType(GetType(Sel));
Sebastian Redl539c5062010-08-18 23:57:32 +00002578 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002579 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl539c5062010-08-18 23:57:32 +00002580 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002581 Context->setObjCClassType(GetType(Class));
Steve Naroff7cae42b2009-07-10 23:34:53 +00002582
Sebastian Redl539c5062010-08-18 23:57:32 +00002583 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002584 Context->setCFConstantStringType(GetType(String));
Mike Stump11289f42009-09-09 15:08:12 +00002585 if (unsigned FastEnum
Sebastian Redl539c5062010-08-18 23:57:32 +00002586 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002587 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl539c5062010-08-18 23:57:32 +00002588 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregor27821ce2009-07-07 16:35:42 +00002589 QualType FileType = GetType(File);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002590 if (FileType.isNull()) {
2591 Error("FILE type is NULL");
2592 return;
2593 }
John McCall9dd450b2009-09-21 23:43:11 +00002594 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregor27821ce2009-07-07 16:35:42 +00002595 Context->setFILEDecl(Typedef->getDecl());
2596 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002597 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002598 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002599 Error("Invalid FILE type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002600 return;
2601 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002602 Context->setFILEDecl(Tag->getDecl());
2603 }
2604 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002605 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002606 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002607 if (Jmp_bufType.isNull()) {
2608 Error("jmp_bug type is NULL");
2609 return;
2610 }
John McCall9dd450b2009-09-21 23:43:11 +00002611 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002612 Context->setjmp_bufDecl(Typedef->getDecl());
2613 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002614 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002615 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002616 Error("Invalid jmp_buf type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002617 return;
2618 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002619 Context->setjmp_bufDecl(Tag->getDecl());
2620 }
2621 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002622 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002623 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002624 if (Sigjmp_bufType.isNull()) {
2625 Error("sigjmp_buf type is NULL");
2626 return;
2627 }
John McCall9dd450b2009-09-21 23:43:11 +00002628 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002629 Context->setsigjmp_bufDecl(Typedef->getDecl());
2630 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002631 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002632 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stumpa4de80b2009-07-28 02:25:19 +00002633 Context->setsigjmp_bufDecl(Tag->getDecl());
2634 }
2635 }
Mike Stump11289f42009-09-09 15:08:12 +00002636 if (unsigned ObjCIdRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002637 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002638 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump11289f42009-09-09 15:08:12 +00002639 if (unsigned ObjCClassRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002640 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002641 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002642 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpd0153282009-10-20 02:12:22 +00002643 Context->setBlockDescriptorType(GetType(String));
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002644 if (unsigned String
Sebastian Redl539c5062010-08-18 23:57:32 +00002645 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002646 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002647 if (unsigned ObjCSelRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002648 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002649 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002650 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002651 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002652
Sebastian Redl539c5062010-08-18 23:57:32 +00002653 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002654 Context->setInt128Installed();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002655
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002656 ReadPragmaDiagnosticMappings(Context->getDiagnostics());
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002657
2658 // If there were any CUDA special declarations, deserialize them.
2659 if (!CUDASpecialDeclRefs.empty()) {
2660 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
2661 Context->setcudaConfigureCallDecl(
2662 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2663 }
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002664}
2665
Douglas Gregor45fe0362009-05-12 01:31:05 +00002666/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002667/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00002668/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002669std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00002670 FileManager &FileMgr,
Daniel Dunbar3b951482009-12-03 09:13:06 +00002671 Diagnostic &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002672 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002673 std::string ErrStr;
2674 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner5159f612010-11-23 08:35:12 +00002675 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00002676 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00002677 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002678 return std::string();
2679 }
2680
2681 // Initialize the stream
2682 llvm::BitstreamReader StreamFile;
2683 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00002684 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00002685 (const unsigned char *)Buffer->getBufferEnd());
2686 Stream.init(StreamFile);
2687
2688 // Sniff for the signature.
2689 if (Stream.Read(8) != 'C' ||
2690 Stream.Read(8) != 'P' ||
2691 Stream.Read(8) != 'C' ||
2692 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002693 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002694 return std::string();
2695 }
2696
2697 RecordData Record;
2698 while (!Stream.AtEndOfStream()) {
2699 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002700
Douglas Gregor45fe0362009-05-12 01:31:05 +00002701 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2702 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00002703
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002704 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002705 switch (BlockID) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002706 case AST_BLOCK_ID:
2707 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002708 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002709 return std::string();
2710 }
2711 break;
Mike Stump11289f42009-09-09 15:08:12 +00002712
Douglas Gregor45fe0362009-05-12 01:31:05 +00002713 default:
2714 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002715 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002716 return std::string();
2717 }
2718 break;
2719 }
2720 continue;
2721 }
2722
2723 if (Code == llvm::bitc::END_BLOCK) {
2724 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002725 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002726 return std::string();
2727 }
2728 continue;
2729 }
2730
2731 if (Code == llvm::bitc::DEFINE_ABBREV) {
2732 Stream.ReadAbbrevRecord();
2733 continue;
2734 }
2735
2736 Record.clear();
2737 const char *BlobStart = 0;
2738 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002739 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl539c5062010-08-18 23:57:32 +00002740 == ORIGINAL_FILE_NAME)
Douglas Gregor45fe0362009-05-12 01:31:05 +00002741 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00002742 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00002743
2744 return std::string();
2745}
2746
Douglas Gregor55abb232009-04-10 20:39:37 +00002747/// \brief Parse the record that corresponds to a LangOptions data
2748/// structure.
2749///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002750/// This routine parses the language options from the AST file and then gives
2751/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00002752///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002753/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002754bool ASTReader::ParseLanguageOptions(
Douglas Gregor55abb232009-04-10 20:39:37 +00002755 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002756 if (Listener) {
2757 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00002758
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002759 #define PARSE_LANGOPT(Option) \
2760 LangOpts.Option = Record[Idx]; \
2761 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00002762
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002763 unsigned Idx = 0;
2764 PARSE_LANGOPT(Trigraphs);
2765 PARSE_LANGOPT(BCPLComment);
2766 PARSE_LANGOPT(DollarIdents);
2767 PARSE_LANGOPT(AsmPreprocessor);
2768 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00002769 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002770 PARSE_LANGOPT(ImplicitInt);
2771 PARSE_LANGOPT(Digraphs);
2772 PARSE_LANGOPT(HexFloats);
2773 PARSE_LANGOPT(C99);
2774 PARSE_LANGOPT(Microsoft);
2775 PARSE_LANGOPT(CPlusPlus);
2776 PARSE_LANGOPT(CPlusPlus0x);
2777 PARSE_LANGOPT(CXXOperatorNames);
2778 PARSE_LANGOPT(ObjC1);
2779 PARSE_LANGOPT(ObjC2);
2780 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00002781 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00002782 PARSE_LANGOPT(AppleKext);
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00002783 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00002784 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002785 PARSE_LANGOPT(PascalStrings);
2786 PARSE_LANGOPT(WritableStrings);
2787 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00002788 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002789 PARSE_LANGOPT(Exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +00002790 PARSE_LANGOPT(SjLjExceptions);
Douglas Gregordbe39272011-02-01 15:15:22 +00002791 PARSE_LANGOPT(MSBitfields);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002792 PARSE_LANGOPT(NeXTRuntime);
2793 PARSE_LANGOPT(Freestanding);
2794 PARSE_LANGOPT(NoBuiltin);
2795 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00002796 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002797 PARSE_LANGOPT(Blocks);
2798 PARSE_LANGOPT(EmitAllDecls);
2799 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00002800 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2801 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002802 PARSE_LANGOPT(HeinousExtensions);
2803 PARSE_LANGOPT(Optimize);
2804 PARSE_LANGOPT(OptimizeSize);
2805 PARSE_LANGOPT(Static);
2806 PARSE_LANGOPT(PICLevel);
2807 PARSE_LANGOPT(GNUInline);
2808 PARSE_LANGOPT(NoInline);
2809 PARSE_LANGOPT(AccessControl);
2810 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00002811 PARSE_LANGOPT(ShortWChar);
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00002812 PARSE_LANGOPT(ShortEnums);
Chris Lattner51924e512010-06-26 21:25:03 +00002813 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall457a04e2010-10-22 21:05:15 +00002814 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00002815 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00002816 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002817 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00002818 PARSE_LANGOPT(OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00002819 PARSE_LANGOPT(CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00002820 PARSE_LANGOPT(CatchUndefined);
2821 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002822 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00002823
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002824 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00002825 }
Douglas Gregor55abb232009-04-10 20:39:37 +00002826
2827 return false;
2828}
2829
Sebastian Redl2c499f62010-08-18 23:56:43 +00002830void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002831 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2832 PerFileData &F = *Chain[I];
2833 if (!F.PreprocessorDetailCursor.getBitStreamReader())
2834 continue;
2835
2836 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
2837 F.PreprocessorDetailCursor.JumpToBit(F.PreprocessorDetailStartOffset);
2838 while (LoadPreprocessedEntity(F)) { }
2839 }
Douglas Gregoraae92242010-03-19 21:51:54 +00002840}
2841
Douglas Gregor46c50012011-02-11 19:46:30 +00002842PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002843 PerFileData *F = 0;
2844 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2845 if (Offset < Chain[I]->SizeInBits) {
2846 F = Chain[I];
2847 break;
2848 }
2849
2850 Offset -= Chain[I]->SizeInBits;
2851 }
2852
2853 if (!F) {
2854 Error("Malformed preprocessed entity offset");
2855 return 0;
2856 }
2857
Douglas Gregor92a96f52011-02-08 21:58:10 +00002858 // Keep track of where we are in the stream, then jump back there
2859 // after reading this entity.
2860 SavedStreamPosition SavedPosition(F->PreprocessorDetailCursor);
2861 F->PreprocessorDetailCursor.JumpToBit(Offset);
2862 return LoadPreprocessedEntity(*F);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002863}
2864
Douglas Gregor09b69892011-02-10 17:09:37 +00002865HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
2866 HeaderFileInfoTrait Trait(FE->getName());
2867 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2868 PerFileData &F = *Chain[I];
2869 HeaderFileInfoLookupTable *Table
2870 = static_cast<HeaderFileInfoLookupTable *>(F.HeaderFileInfoTable);
2871 if (!Table)
2872 continue;
2873
2874 // Look in the on-disk hash table for an entry for this file name.
2875 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE->getName(),
2876 &Trait);
2877 if (Pos == Table->end())
2878 continue;
2879
2880 HeaderFileInfo HFI = *Pos;
2881 if (Listener)
2882 Listener->ReadHeaderFileInfo(HFI, FE->getUID());
2883
2884 return HFI;
2885 }
2886
2887 return HeaderFileInfo();
2888}
2889
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002890void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002891 unsigned Idx = 0;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002892 while (Idx < PragmaDiagMappings.size()) {
2893 SourceLocation
2894 Loc = SourceLocation::getFromRawEncoding(PragmaDiagMappings[Idx++]);
2895 while (1) {
2896 assert(Idx < PragmaDiagMappings.size() &&
2897 "Invalid data, didn't find '-1' marking end of diag/map pairs");
2898 if (Idx >= PragmaDiagMappings.size())
2899 break; // Something is messed up but at least avoid infinite loop in
2900 // release build.
2901 unsigned DiagID = PragmaDiagMappings[Idx++];
2902 if (DiagID == (unsigned)-1)
2903 break; // no more diag/map pairs for this location.
2904 diag::Mapping Map = (diag::Mapping)PragmaDiagMappings[Idx++];
2905 Diag.setDiagnosticMapping(DiagID, Map, Loc);
2906 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002907 }
2908}
2909
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002910/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002911ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002912 PerFileData *F = 0;
2913 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2914 F = Chain[N - I - 1];
2915 if (Index < F->LocalNumTypes)
2916 break;
2917 Index -= F->LocalNumTypes;
2918 }
2919 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redl2c373b92010-10-05 15:59:54 +00002920 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002921}
2922
2923/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002924///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002925/// The index is the type ID, shifted and minus the number of predefs. This
2926/// routine actually reads the record corresponding to the type at the given
2927/// location. It is a helper routine for GetType, which deals with reading type
2928/// IDs.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002929QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002930 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00002931 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00002932
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002933 // Keep track of where we are in the stream, then jump back there
2934 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002935 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002936
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002937 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00002938
Douglas Gregor1342e842009-07-06 18:54:52 +00002939 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00002940 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00002941
Sebastian Redl2c373b92010-10-05 15:59:54 +00002942 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002943 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002944 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00002945 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2946 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002947 if (Record.size() != 2) {
2948 Error("Incorrect encoding of extended qualifier type");
2949 return QualType();
2950 }
Douglas Gregor455b8f42009-04-15 22:00:08 +00002951 QualType Base = GetType(Record[0]);
John McCall8ccfcb52009-09-24 19:53:00 +00002952 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2953 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00002954 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002955
Sebastian Redl539c5062010-08-18 23:57:32 +00002956 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002957 if (Record.size() != 1) {
2958 Error("Incorrect encoding of complex type");
2959 return QualType();
2960 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002961 QualType ElemType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002962 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002963 }
2964
Sebastian Redl539c5062010-08-18 23:57:32 +00002965 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002966 if (Record.size() != 1) {
2967 Error("Incorrect encoding of pointer type");
2968 return QualType();
2969 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002970 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002971 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002972 }
2973
Sebastian Redl539c5062010-08-18 23:57:32 +00002974 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002975 if (Record.size() != 1) {
2976 Error("Incorrect encoding of block pointer type");
2977 return QualType();
2978 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002979 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002980 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002981 }
2982
Sebastian Redl539c5062010-08-18 23:57:32 +00002983 case TYPE_LVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002984 if (Record.size() != 1) {
2985 Error("Incorrect encoding of lvalue reference type");
2986 return QualType();
2987 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002988 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002989 return Context->getLValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002990 }
2991
Sebastian Redl539c5062010-08-18 23:57:32 +00002992 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002993 if (Record.size() != 1) {
2994 Error("Incorrect encoding of rvalue reference type");
2995 return QualType();
2996 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002997 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002998 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002999 }
3000
Sebastian Redl539c5062010-08-18 23:57:32 +00003001 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00003002 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003003 Error("Incorrect encoding of member pointer type");
3004 return QualType();
3005 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003006 QualType PointeeType = GetType(Record[0]);
3007 QualType ClassType = GetType(Record[1]);
Douglas Gregor0cdc8322010-12-10 17:03:06 +00003008 if (PointeeType.isNull() || ClassType.isNull())
3009 return QualType();
3010
Chris Lattner8575daa2009-04-27 21:45:14 +00003011 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003012 }
3013
Sebastian Redl539c5062010-08-18 23:57:32 +00003014 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003015 QualType ElementType = GetType(Record[0]);
3016 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3017 unsigned IndexTypeQuals = Record[2];
3018 unsigned Idx = 3;
3019 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00003020 return Context->getConstantArrayType(ElementType, Size,
3021 ASM, IndexTypeQuals);
3022 }
3023
Sebastian Redl539c5062010-08-18 23:57:32 +00003024 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003025 QualType ElementType = GetType(Record[0]);
3026 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3027 unsigned IndexTypeQuals = Record[2];
Chris Lattner8575daa2009-04-27 21:45:14 +00003028 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003029 }
3030
Sebastian Redl539c5062010-08-18 23:57:32 +00003031 case TYPE_VARIABLE_ARRAY: {
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003032 QualType ElementType = GetType(Record[0]);
3033 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3034 unsigned IndexTypeQuals = Record[2];
Sebastian Redl2c373b92010-10-05 15:59:54 +00003035 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3036 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
3037 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor04318252009-07-06 15:59:29 +00003038 ASM, IndexTypeQuals,
3039 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003040 }
3041
Sebastian Redl539c5062010-08-18 23:57:32 +00003042 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00003043 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003044 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003045 return QualType();
3046 }
3047
3048 QualType ElementType = GetType(Record[0]);
3049 unsigned NumElements = Record[1];
Bob Wilsonaeb56442010-11-10 21:56:12 +00003050 unsigned VecKind = Record[2];
Chris Lattner37141f42010-06-23 06:00:24 +00003051 return Context->getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00003052 (VectorType::VectorKind)VecKind);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003053 }
3054
Sebastian Redl539c5062010-08-18 23:57:32 +00003055 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00003056 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003057 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003058 return QualType();
3059 }
3060
3061 QualType ElementType = GetType(Record[0]);
3062 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00003063 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003064 }
3065
Sebastian Redl539c5062010-08-18 23:57:32 +00003066 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00003067 if (Record.size() != 4) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00003068 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003069 return QualType();
3070 }
3071 QualType ResultType = GetType(Record[0]);
Rafael Espindola49b85ab2010-03-30 22:15:11 +00003072 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00003073 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003074 }
3075
Sebastian Redl539c5062010-08-18 23:57:32 +00003076 case TYPE_FUNCTION_PROTO: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003077 QualType ResultType = GetType(Record[0]);
John McCalldb40c7f2010-12-14 08:05:40 +00003078
3079 FunctionProtoType::ExtProtoInfo EPI;
3080 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
3081 /*regparm*/ Record[2],
3082 static_cast<CallingConv>(Record[3]));
3083
Rafael Espindola49b85ab2010-03-30 22:15:11 +00003084 unsigned Idx = 4;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003085 unsigned NumParams = Record[Idx++];
3086 llvm::SmallVector<QualType, 16> ParamTypes;
3087 for (unsigned I = 0; I != NumParams; ++I)
3088 ParamTypes.push_back(GetType(Record[Idx++]));
John McCalldb40c7f2010-12-14 08:05:40 +00003089
3090 EPI.Variadic = Record[Idx++];
3091 EPI.TypeQuals = Record[Idx++];
Douglas Gregordb9d6642011-01-26 05:01:58 +00003092 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
John McCalldb40c7f2010-12-14 08:05:40 +00003093 EPI.HasExceptionSpec = Record[Idx++];
3094 EPI.HasAnyExceptionSpec = Record[Idx++];
3095 EPI.NumExceptions = Record[Idx++];
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00003096 llvm::SmallVector<QualType, 2> Exceptions;
John McCalldb40c7f2010-12-14 08:05:40 +00003097 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00003098 Exceptions.push_back(GetType(Record[Idx++]));
John McCalldb40c7f2010-12-14 08:05:40 +00003099 EPI.Exceptions = Exceptions.data();
Jay Foad7d0479f2009-05-21 09:52:38 +00003100 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalldb40c7f2010-12-14 08:05:40 +00003101 EPI);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003102 }
3103
Sebastian Redl539c5062010-08-18 23:57:32 +00003104 case TYPE_UNRESOLVED_USING:
John McCallb96ec562009-12-04 22:46:56 +00003105 return Context->getTypeDeclType(
3106 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
3107
Sebastian Redl539c5062010-08-18 23:57:32 +00003108 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003109 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003110 Error("incorrect encoding of typedef type");
3111 return QualType();
3112 }
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003113 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
3114 QualType Canonical = GetType(Record[1]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003115 if (!Canonical.isNull())
3116 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003117 return Context->getTypedefType(Decl, Canonical);
3118 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003119
Sebastian Redl539c5062010-08-18 23:57:32 +00003120 case TYPE_TYPEOF_EXPR:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003121 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003122
Sebastian Redl539c5062010-08-18 23:57:32 +00003123 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003124 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003125 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003126 return QualType();
3127 }
3128 QualType UnderlyingType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003129 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003130 }
Mike Stump11289f42009-09-09 15:08:12 +00003131
Sebastian Redl539c5062010-08-18 23:57:32 +00003132 case TYPE_DECLTYPE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003133 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson81df7b82009-06-24 19:06:50 +00003134
Sebastian Redl539c5062010-08-18 23:57:32 +00003135 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003136 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003137 Error("incorrect encoding of record type");
3138 return QualType();
3139 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003140 bool IsDependent = Record[0];
3141 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCall424cec92011-01-19 06:33:43 +00003142 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003143 return T;
3144 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003145
Sebastian Redl539c5062010-08-18 23:57:32 +00003146 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003147 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003148 Error("incorrect encoding of enum type");
3149 return QualType();
3150 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003151 bool IsDependent = Record[0];
3152 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCall424cec92011-01-19 06:33:43 +00003153 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003154 return T;
3155 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00003156
John McCall81904512011-01-06 01:58:22 +00003157 case TYPE_ATTRIBUTED: {
3158 if (Record.size() != 3) {
3159 Error("incorrect encoding of attributed type");
3160 return QualType();
3161 }
3162 QualType modifiedType = GetType(Record[0]);
3163 QualType equivalentType = GetType(Record[1]);
3164 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3165 return Context->getAttributedType(kind, modifiedType, equivalentType);
3166 }
3167
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003168 case TYPE_PAREN: {
3169 if (Record.size() != 1) {
3170 Error("incorrect encoding of paren type");
3171 return QualType();
3172 }
3173 QualType InnerType = GetType(Record[0]);
3174 return Context->getParenType(InnerType);
3175 }
3176
Douglas Gregord2fa7662010-12-20 02:24:11 +00003177 case TYPE_PACK_EXPANSION: {
Douglas Gregor17328502011-02-01 15:24:58 +00003178 if (Record.size() != 2) {
Douglas Gregord2fa7662010-12-20 02:24:11 +00003179 Error("incorrect encoding of pack expansion type");
3180 return QualType();
3181 }
3182 QualType Pattern = GetType(Record[0]);
3183 if (Pattern.isNull())
3184 return QualType();
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00003185 llvm::Optional<unsigned> NumExpansions;
3186 if (Record[1])
3187 NumExpansions = Record[1] - 1;
3188 return Context->getPackExpansionType(Pattern, NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00003189 }
3190
Sebastian Redl539c5062010-08-18 23:57:32 +00003191 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003192 unsigned Idx = 0;
3193 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3194 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3195 QualType NamedType = GetType(Record[Idx++]);
3196 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00003197 }
3198
Sebastian Redl539c5062010-08-18 23:57:32 +00003199 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00003200 unsigned Idx = 0;
3201 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00003202 return Context->getObjCInterfaceType(ItfD);
3203 }
3204
Sebastian Redl539c5062010-08-18 23:57:32 +00003205 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00003206 unsigned Idx = 0;
3207 QualType Base = GetType(Record[Idx++]);
Chris Lattner587cbe12009-04-22 06:45:28 +00003208 unsigned NumProtos = Record[Idx++];
3209 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
3210 for (unsigned I = 0; I != NumProtos; ++I)
3211 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003212 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00003213 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003214
Sebastian Redl539c5062010-08-18 23:57:32 +00003215 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00003216 unsigned Idx = 0;
John McCall8b07ec22010-05-15 11:32:37 +00003217 QualType Pointee = GetType(Record[Idx++]);
3218 return Context->getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00003219 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00003220
Sebastian Redl539c5062010-08-18 23:57:32 +00003221 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00003222 unsigned Idx = 0;
3223 QualType Parm = GetType(Record[Idx++]);
3224 QualType Replacement = GetType(Record[Idx++]);
3225 return
3226 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3227 Replacement);
3228 }
John McCalle78aac42010-03-10 03:28:59 +00003229
Douglas Gregorada4b792011-01-14 02:55:32 +00003230 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3231 unsigned Idx = 0;
3232 QualType Parm = GetType(Record[Idx++]);
3233 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3234 return Context->getSubstTemplateTypeParmPackType(
3235 cast<TemplateTypeParmType>(Parm),
3236 ArgPack);
3237 }
3238
Sebastian Redl539c5062010-08-18 23:57:32 +00003239 case TYPE_INJECTED_CLASS_NAME: {
John McCalle78aac42010-03-10 03:28:59 +00003240 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
3241 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003242 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003243 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003244 return
3245 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00003246 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003247
Sebastian Redl539c5062010-08-18 23:57:32 +00003248 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003249 unsigned Idx = 0;
3250 unsigned Depth = Record[Idx++];
3251 unsigned Index = Record[Idx++];
3252 bool Pack = Record[Idx++];
3253 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
3254 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
3255 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003256
Sebastian Redl539c5062010-08-18 23:57:32 +00003257 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003258 unsigned Idx = 0;
3259 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3260 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3261 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00003262 QualType Canon = GetType(Record[Idx++]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003263 if (!Canon.isNull())
3264 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00003265 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003266 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003267
Sebastian Redl539c5062010-08-18 23:57:32 +00003268 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003269 unsigned Idx = 0;
3270 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3271 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3272 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3273 unsigned NumArgs = Record[Idx++];
3274 llvm::SmallVector<TemplateArgument, 8> Args;
3275 Args.reserve(NumArgs);
3276 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003277 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003278 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3279 Args.size(), Args.data());
3280 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003281
Sebastian Redl539c5062010-08-18 23:57:32 +00003282 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003283 unsigned Idx = 0;
3284
3285 // ArrayType
3286 QualType ElementType = GetType(Record[Idx++]);
3287 ArrayType::ArraySizeModifier ASM
3288 = (ArrayType::ArraySizeModifier)Record[Idx++];
3289 unsigned IndexTypeQuals = Record[Idx++];
3290
3291 // DependentSizedArrayType
Sebastian Redl2c373b92010-10-05 15:59:54 +00003292 Expr *NumElts = ReadExpr(*Loc.F);
3293 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003294
3295 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3296 IndexTypeQuals, Brackets);
3297 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003298
Sebastian Redl539c5062010-08-18 23:57:32 +00003299 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003300 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003301 bool IsDependent = Record[Idx++];
Douglas Gregor5590be02011-01-15 06:45:20 +00003302 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003303 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003304 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003305 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003306 QualType T;
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003307 if (Canon.isNull())
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003308 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3309 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003310 else
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003311 T = Context->getTemplateSpecializationType(Name, Args.data(),
3312 Args.size(), Canon);
John McCall424cec92011-01-19 06:33:43 +00003313 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003314 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003315 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003316 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003317 // Suppress a GCC warning
3318 return QualType();
3319}
3320
Sebastian Redl2c373b92010-10-05 15:59:54 +00003321class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00003322 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003323 ASTReader::PerFileData &F;
Sebastian Redlc67764e2010-07-22 22:43:28 +00003324 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redl2c499f62010-08-18 23:56:43 +00003325 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00003326 unsigned &Idx;
3327
Sebastian Redl2c373b92010-10-05 15:59:54 +00003328 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3329 unsigned &I) {
3330 return Reader.ReadSourceLocation(F, R, I);
3331 }
3332
John McCall8f115c62009-10-16 21:56:05 +00003333public:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003334 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl2c499f62010-08-18 23:56:43 +00003335 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003336 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3337 { }
John McCall8f115c62009-10-16 21:56:05 +00003338
John McCall17001972009-10-18 01:05:36 +00003339 // We want compile-time assurance that we've enumerated all of
3340 // these, so unfortunately we have to declare them first, then
3341 // define them out-of-line.
3342#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00003343#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00003344 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00003345#include "clang/AST/TypeLocNodes.def"
3346
John McCall17001972009-10-18 01:05:36 +00003347 void VisitFunctionTypeLoc(FunctionTypeLoc);
3348 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00003349};
3350
John McCall17001972009-10-18 01:05:36 +00003351void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00003352 // nothing to do
3353}
John McCall17001972009-10-18 01:05:36 +00003354void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003355 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorc9b7a592010-01-18 18:04:31 +00003356 if (TL.needsExtraLocalData()) {
3357 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3358 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3359 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3360 TL.setModeAttr(Record[Idx++]);
3361 }
John McCall8f115c62009-10-16 21:56:05 +00003362}
John McCall17001972009-10-18 01:05:36 +00003363void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003364 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003365}
John McCall17001972009-10-18 01:05:36 +00003366void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003367 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003368}
John McCall17001972009-10-18 01:05:36 +00003369void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003370 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003371}
John McCall17001972009-10-18 01:05:36 +00003372void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003373 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003374}
John McCall17001972009-10-18 01:05:36 +00003375void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003376 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003377}
John McCall17001972009-10-18 01:05:36 +00003378void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003379 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003380}
John McCall17001972009-10-18 01:05:36 +00003381void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003382 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3383 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003384 if (Record[Idx++])
Sebastian Redl2c373b92010-10-05 15:59:54 +00003385 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003386 else
John McCall17001972009-10-18 01:05:36 +00003387 TL.setSizeExpr(0);
3388}
3389void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3390 VisitArrayTypeLoc(TL);
3391}
3392void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3393 VisitArrayTypeLoc(TL);
3394}
3395void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3396 VisitArrayTypeLoc(TL);
3397}
3398void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3399 DependentSizedArrayTypeLoc TL) {
3400 VisitArrayTypeLoc(TL);
3401}
3402void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3403 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003404 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003405}
3406void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003407 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003408}
3409void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003410 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003411}
3412void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003413 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3414 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor7fb25412010-10-01 18:44:50 +00003415 TL.setTrailingReturn(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00003416 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCalle6347002009-10-23 01:28:53 +00003417 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall17001972009-10-18 01:05:36 +00003418 }
3419}
3420void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3421 VisitFunctionTypeLoc(TL);
3422}
3423void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3424 VisitFunctionTypeLoc(TL);
3425}
John McCallb96ec562009-12-04 22:46:56 +00003426void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003427 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallb96ec562009-12-04 22:46:56 +00003428}
John McCall17001972009-10-18 01:05:36 +00003429void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003430 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003431}
3432void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003433 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3434 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3435 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003436}
3437void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003438 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3439 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3440 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3441 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003442}
3443void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003444 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003445}
3446void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003447 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003448}
3449void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003450 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003451}
John McCall81904512011-01-06 01:58:22 +00003452void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3453 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3454 if (TL.hasAttrOperand()) {
3455 SourceRange range;
3456 range.setBegin(ReadSourceLocation(Record, Idx));
3457 range.setEnd(ReadSourceLocation(Record, Idx));
3458 TL.setAttrOperandParensRange(range);
3459 }
3460 if (TL.hasAttrExprOperand()) {
3461 if (Record[Idx++])
3462 TL.setAttrExprOperand(Reader.ReadExpr(F));
3463 else
3464 TL.setAttrExprOperand(0);
3465 } else if (TL.hasAttrEnumOperand())
3466 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3467}
John McCall17001972009-10-18 01:05:36 +00003468void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003469 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003470}
John McCallcebee162009-10-18 09:09:24 +00003471void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3472 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003473 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallcebee162009-10-18 09:09:24 +00003474}
Douglas Gregorada4b792011-01-14 02:55:32 +00003475void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3476 SubstTemplateTypeParmPackTypeLoc TL) {
3477 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3478}
John McCall17001972009-10-18 01:05:36 +00003479void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3480 TemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003481 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3482 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3483 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall0ad16662009-10-29 08:12:44 +00003484 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3485 TL.setArgLocInfo(i,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003486 Reader.GetTemplateArgumentLocInfo(F,
3487 TL.getTypePtr()->getArg(i).getKind(),
3488 Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003489}
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003490void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3491 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3492 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3493}
Abramo Bagnara6150c882010-05-11 21:36:43 +00003494void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003495 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3496 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003497}
John McCalle78aac42010-03-10 03:28:59 +00003498void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003499 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalle78aac42010-03-10 03:28:59 +00003500}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00003501void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003502 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3503 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3504 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003505}
John McCallc392f372010-06-11 00:33:02 +00003506void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3507 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003508 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3509 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3510 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3511 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3512 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003513 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3514 TL.setArgLocInfo(I,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003515 Reader.GetTemplateArgumentLocInfo(F,
3516 TL.getTypePtr()->getArg(I).getKind(),
3517 Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003518}
Douglas Gregord2fa7662010-12-20 02:24:11 +00003519void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3520 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3521}
John McCall17001972009-10-18 01:05:36 +00003522void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003523 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8b07ec22010-05-15 11:32:37 +00003524}
3525void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3526 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003527 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3528 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003529 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003530 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003531}
John McCallfc93cf92009-10-22 22:37:11 +00003532void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003533 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCallfc93cf92009-10-22 22:37:11 +00003534}
John McCall8f115c62009-10-16 21:56:05 +00003535
Sebastian Redl2c373b92010-10-05 15:59:54 +00003536TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003537 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00003538 unsigned &Idx) {
3539 QualType InfoTy = GetType(Record[Idx++]);
3540 if (InfoTy.isNull())
3541 return 0;
3542
John McCallbcd03502009-12-07 02:54:59 +00003543 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003544 TypeLocReader TLR(*this, F, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00003545 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00003546 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00003547 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00003548}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003549
Sebastian Redl539c5062010-08-18 23:57:32 +00003550QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00003551 unsigned FastQuals = ID & Qualifiers::FastMask;
3552 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003553
Sebastian Redl539c5062010-08-18 23:57:32 +00003554 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003555 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00003556 switch ((PredefinedTypeIDs)Index) {
3557 case PREDEF_TYPE_NULL_ID: return QualType();
3558 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3559 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003560
Sebastian Redl539c5062010-08-18 23:57:32 +00003561 case PREDEF_TYPE_CHAR_U_ID:
3562 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003563 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00003564 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003565 break;
3566
Sebastian Redl539c5062010-08-18 23:57:32 +00003567 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3568 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3569 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3570 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3571 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3572 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3573 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3574 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3575 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3576 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3577 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3578 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3579 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3580 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3581 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3582 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3583 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3584 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3585 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3586 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3587 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3588 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3589 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3590 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003591 }
3592
3593 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00003594 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003595 }
3596
Sebastian Redl539c5062010-08-18 23:57:32 +00003597 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003598 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00003599 if (TypesLoaded[Index].isNull()) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003600 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003601 if (TypesLoaded[Index].isNull())
3602 return QualType();
3603
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003604 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003605 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003606 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003607 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003608 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00003609 }
Mike Stump11289f42009-09-09 15:08:12 +00003610
John McCall8ccfcb52009-09-24 19:53:00 +00003611 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003612}
3613
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003614TypeID ASTReader::GetTypeID(QualType T) const {
3615 return MakeTypeID(T,
3616 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3617}
3618
3619TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3620 if (T.isNull())
3621 return TypeIdx();
3622 assert(!T.getLocalFastQualifiers());
3623
3624 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3625 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3626 // comparing keys of ASTDeclContextNameLookupTable.
3627 // If the type didn't come from the AST file use a specially marked index
3628 // so that any hash/key comparison fail since no such index is stored
3629 // in a AST file.
3630 if (I == TypeIdxs.end())
3631 return TypeIdx(-1);
3632 return I->second;
3633}
3634
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003635unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3636 unsigned Result = 0;
3637 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3638 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3639
3640 return Result;
3641}
3642
John McCall0ad16662009-10-29 08:12:44 +00003643TemplateArgumentLocInfo
Sebastian Redl2c373b92010-10-05 15:59:54 +00003644ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3645 TemplateArgument::ArgKind Kind,
John McCall0ad16662009-10-29 08:12:44 +00003646 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003647 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00003648 switch (Kind) {
3649 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003650 return ReadExpr(F);
John McCall0ad16662009-10-29 08:12:44 +00003651 case TemplateArgument::Type:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003652 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003653 case TemplateArgument::Template: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003654 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3655 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003656 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc,
3657 SourceLocation());
3658 }
3659 case TemplateArgument::TemplateExpansion: {
3660 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3661 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003662 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
3663 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc,
3664 EllipsisLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003665 }
John McCall0ad16662009-10-29 08:12:44 +00003666 case TemplateArgument::Null:
3667 case TemplateArgument::Integral:
3668 case TemplateArgument::Declaration:
3669 case TemplateArgument::Pack:
3670 return TemplateArgumentLocInfo();
3671 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00003672 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00003673 return TemplateArgumentLocInfo();
3674}
3675
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003676TemplateArgumentLoc
Sebastian Redl2c373b92010-10-05 15:59:54 +00003677ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003678 const RecordData &Record, unsigned &Index) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003679 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003680
3681 if (Arg.getKind() == TemplateArgument::Expression) {
3682 if (Record[Index++]) // bool InfoHasSameExpr.
3683 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3684 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00003685 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003686 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003687}
3688
Sebastian Redl2c499f62010-08-18 23:56:43 +00003689Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00003690 return GetDecl(ID);
3691}
3692
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003693uint64_t
3694ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3695 if (ID == 0)
3696 return 0;
3697
3698 --ID;
3699 uint64_t Offset = 0;
3700 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3701 if (ID < Chain[I]->LocalNumCXXBaseSpecifiers)
3702 return Offset + Chain[I]->CXXBaseSpecifiersOffsets[ID];
3703
3704 ID -= Chain[I]->LocalNumCXXBaseSpecifiers;
3705 Offset += Chain[I]->SizeInBits;
3706 }
3707
3708 assert(false && "CXXBaseSpecifiers not found");
3709 return 0;
3710}
3711
3712CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3713 // Figure out which AST file contains this offset.
3714 PerFileData *F = 0;
3715 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3716 if (Offset < Chain[I]->SizeInBits) {
3717 F = Chain[I];
3718 break;
3719 }
3720
3721 Offset -= Chain[I]->SizeInBits;
3722 }
3723
3724 if (!F) {
3725 Error("Malformed AST file: C++ base specifiers at impossible offset");
3726 return 0;
3727 }
3728
3729 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3730 SavedStreamPosition SavedPosition(Cursor);
3731 Cursor.JumpToBit(Offset);
3732 ReadingKindTracker ReadingKind(Read_Decl, *this);
3733 RecordData Record;
3734 unsigned Code = Cursor.ReadCode();
3735 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3736 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3737 Error("Malformed AST file: missing C++ base specifiers");
3738 return 0;
3739 }
3740
3741 unsigned Idx = 0;
3742 unsigned NumBases = Record[Idx++];
3743 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3744 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3745 for (unsigned I = 0; I != NumBases; ++I)
3746 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3747 return Bases;
3748}
3749
Sebastian Redl2c499f62010-08-18 23:56:43 +00003750TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003751 if (!DeclsLoaded[0]) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00003752 ReadDeclRecord(0, 1);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003753 if (DeserializationListener)
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003754 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003755 }
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00003756
3757 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3758}
3759
Sebastian Redl539c5062010-08-18 23:57:32 +00003760Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003761 if (ID == 0)
3762 return 0;
3763
Douglas Gregor745ed142009-04-25 18:35:21 +00003764 if (ID > DeclsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003765 Error("declaration ID out-of-range for AST file");
Douglas Gregor745ed142009-04-25 18:35:21 +00003766 return 0;
3767 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003768
Douglas Gregor745ed142009-04-25 18:35:21 +00003769 unsigned Index = ID - 1;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003770 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003771 ReadDeclRecord(Index, ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003772 if (DeserializationListener)
3773 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3774 }
Douglas Gregor745ed142009-04-25 18:35:21 +00003775
3776 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003777}
3778
Chris Lattner9c28af02009-04-27 05:46:25 +00003779/// \brief Resolve the offset of a statement into a statement.
3780///
3781/// This operation will read a new statement from the external
3782/// source each time it is called, and is meant to be used via a
3783/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00003784Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00003785 // Switch case IDs are per Decl.
3786 ClearSwitchCaseIDs();
3787
Sebastian Redl5c415f32010-07-22 17:01:13 +00003788 // Offset here is a global offset across the entire chain.
3789 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3790 PerFileData &F = *Chain[N - I - 1];
3791 if (Offset < F.SizeInBits) {
3792 // Since we know that this statement is part of a decl, make sure to use
3793 // the decl cursor to read it.
3794 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003795 return ReadStmtFromStream(F);
Sebastian Redl5c415f32010-07-22 17:01:13 +00003796 }
3797 Offset -= F.SizeInBits;
3798 }
3799 llvm_unreachable("Broken chain");
Douglas Gregor3c3aa612009-04-18 00:07:54 +00003800}
3801
Sebastian Redl2c499f62010-08-18 23:56:43 +00003802bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003803 bool (*isKindWeWant)(Decl::Kind),
John McCall75b960e2010-06-01 09:23:16 +00003804 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump11289f42009-09-09 15:08:12 +00003805 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003806 "DeclContext has no lexical decls in storage");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003807
Sebastian Redl5c415f32010-07-22 17:01:13 +00003808 // There might be lexical decls in multiple parts of the chain, for the TU
3809 // at least.
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003810 // DeclContextOffsets might reallocate as we load additional decls below,
3811 // so make a copy of the vector.
3812 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl5c415f32010-07-22 17:01:13 +00003813 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3814 I != E; ++I) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003815 // IDs can be 0 if this context doesn't contain declarations.
3816 if (!I->LexicalDecls)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003817 continue;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003818
3819 // Load all of the declaration IDs
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003820 for (const KindDeclIDPair *ID = I->LexicalDecls,
3821 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3822 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3823 continue;
3824
3825 Decl *D = GetDecl(ID->second);
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003826 assert(D && "Null decl in lexical decls");
3827 Decls.push_back(D);
3828 }
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003829 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003830
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003831 ++NumLexicalDeclContextsRead;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003832 return false;
3833}
3834
John McCall75b960e2010-06-01 09:23:16 +00003835DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00003836ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00003837 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003838 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003839 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003840 if (!Name)
3841 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3842 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003843
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003844 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl471ac2f2010-08-24 00:49:55 +00003845 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003846 // and namespaces. For any given name, the last available results replace
3847 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl5c415f32010-07-22 17:01:13 +00003848 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003849 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl5c415f32010-07-22 17:01:13 +00003850 I != E; ++I) {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003851 if (!I->NameLookupTableData)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003852 continue;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003853
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003854 ASTDeclContextNameLookupTable *LookupTable =
3855 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3856 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3857 if (Pos == LookupTable->end())
Sebastian Redl5c415f32010-07-22 17:01:13 +00003858 continue;
3859
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003860 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3861 for (; Data.first != Data.second; ++Data.first)
3862 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003863 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003864 }
3865
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003866 ++NumVisibleDeclContextsRead;
John McCall75b960e2010-06-01 09:23:16 +00003867
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003868 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00003869 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003870}
3871
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00003872void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3873 assert(DC->hasExternalVisibleStorage() &&
3874 "DeclContext has no visible decls in storage");
3875
3876 llvm::SmallVector<NamedDecl *, 64> Decls;
3877 // There might be visible decls in multiple parts of the chain, for the TU
3878 // and namespaces.
3879 DeclContextInfos &Infos = DeclContextOffsets[DC];
3880 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3881 I != E; ++I) {
3882 if (!I->NameLookupTableData)
3883 continue;
3884
3885 ASTDeclContextNameLookupTable *LookupTable =
3886 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3887 for (ASTDeclContextNameLookupTable::item_iterator
3888 ItemI = LookupTable->item_begin(),
3889 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3890 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3891 = *ItemI;
3892 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3893 Decls.clear();
3894 for (; Data.first != Data.second; ++Data.first)
3895 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3896 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3897 }
3898 }
3899}
3900
Sebastian Redl2c499f62010-08-18 23:56:43 +00003901void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003902 assert(Consumer);
3903 while (!InterestingDecls.empty()) {
3904 DeclGroupRef DG(InterestingDecls.front());
3905 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00003906 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003907 }
3908}
3909
Sebastian Redl2c499f62010-08-18 23:56:43 +00003910void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00003911 this->Consumer = Consumer;
3912
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003913 if (!Consumer)
3914 return;
3915
3916 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003917 // Force deserialization of this decl, which will cause it to be queued for
3918 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00003919 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003920 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00003921
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003922 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003923}
3924
Sebastian Redl2c499f62010-08-18 23:56:43 +00003925void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003926 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003927
Mike Stump11289f42009-09-09 15:08:12 +00003928 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003929 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00003930 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00003931 unsigned NumDeclsLoaded
3932 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3933 (Decl *)0);
3934 unsigned NumIdentifiersLoaded
3935 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3936 IdentifiersLoaded.end(),
3937 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00003938 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003939 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3940 SelectorsLoaded.end(),
3941 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00003942
Douglas Gregorc5046832009-04-27 18:38:38 +00003943 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3944 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor258ae542009-04-27 06:38:32 +00003945 if (TotalNumSLocEntries)
3946 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3947 NumSLocEntriesRead, TotalNumSLocEntries,
3948 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00003949 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003950 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003951 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3952 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3953 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003954 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003955 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3956 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00003957 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003958 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00003959 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3960 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00003961 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003962 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00003963 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3964 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003965 if (TotalNumStatements)
3966 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3967 NumStatementsRead, TotalNumStatements,
3968 ((float)NumStatementsRead/TotalNumStatements * 100));
3969 if (TotalNumMacros)
3970 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3971 NumMacrosRead, TotalNumMacros,
3972 ((float)NumMacrosRead/TotalNumMacros * 100));
3973 if (TotalLexicalDeclContexts)
3974 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3975 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3976 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3977 * 100));
3978 if (TotalVisibleDeclContexts)
3979 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3980 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3981 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3982 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003983 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003984 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003985 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3986 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00003987 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003988 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003989 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003990 std::fprintf(stderr, "\n");
3991}
3992
Sebastian Redl2c499f62010-08-18 23:56:43 +00003993void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00003994 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003995 S.ExternalSource = this;
3996
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003997 // Makes sure any declarations that were deserialized "too early"
3998 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003999 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4000 if (SemaObj->TUScope)
John McCall48871652010-08-21 09:40:31 +00004001 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004002
4003 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004004 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00004005 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00004006
4007 // If there were any tentative definitions, deserialize them and add
Sebastian Redl35351a92010-01-31 22:27:38 +00004008 // them to Sema's list of tentative definitions.
Douglas Gregord4df8652009-04-22 22:02:47 +00004009 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4010 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redl35351a92010-01-31 22:27:38 +00004011 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregord4df8652009-04-22 22:02:47 +00004012 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00004013
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004014 // If there were any unused file scoped decls, deserialize them and add to
4015 // Sema's list of unused file scoped decls.
4016 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4017 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4018 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattner90073802010-02-12 00:07:30 +00004019 }
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004020
4021 // If there were any locally-scoped external declarations,
4022 // deserialize them and add them to Sema's table of locally-scoped
4023 // external declarations.
4024 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4025 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4026 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
4027 }
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004028
4029 // If there were any ext_vector type declarations, deserialize them
4030 // and add them to Sema's vector of such declarations.
4031 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
4032 SemaObj->ExtVectorDecls.push_back(
4033 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004034
4035 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
4036 // Can we cut them down before writing them ?
4037
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004038 // If there were any dynamic classes declarations, deserialize them
4039 // and add them to Sema's vector of such declarations.
4040 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
4041 SemaObj->DynamicClasses.push_back(
4042 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004043
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004044 // Load the offsets of the declarations that Sema references.
4045 // They will be lazily deserialized when needed.
4046 if (!SemaDeclRefs.empty()) {
4047 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
4048 SemaObj->StdNamespace = SemaDeclRefs[0];
4049 SemaObj->StdBadAlloc = SemaDeclRefs[1];
4050 }
4051
Sebastian Redl2c373b92010-10-05 15:59:54 +00004052 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
4053
4054 // If there are @selector references added them to its pool. This is for
4055 // implementation of -Wselector.
4056 if (!F->ReferencedSelectorsData.empty()) {
4057 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
4058 unsigned I = 0;
4059 while (I < DataSize) {
4060 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
4061 SourceLocation SelLoc = ReadSourceLocation(
4062 *F, F->ReferencedSelectorsData, I);
4063 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
4064 }
4065 }
4066
4067 // If there were any pending implicit instantiations, deserialize them
4068 // and add them to Sema's queue of such instantiations.
4069 assert(F->PendingInstantiations.size() % 2 == 0 &&
4070 "Expected pairs of entries");
4071 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
4072 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
4073 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
4074 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
4075 }
4076 }
4077
4078 // The two special data sets below always come from the most recent PCH,
4079 // which is at the front of the chain.
4080 PerFileData &F = *Chain.front();
4081
4082 // If there were any weak undeclared identifiers, deserialize them and add to
4083 // Sema's list of weak undeclared identifiers.
4084 if (!WeakUndeclaredIdentifiers.empty()) {
4085 unsigned Idx = 0;
4086 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
4087 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4088 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4089 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
4090 bool Used = WeakUndeclaredIdentifiers[Idx++];
4091 Sema::WeakInfo WI(AliasId, Loc);
4092 WI.setUsed(Used);
4093 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
4094 }
4095 }
4096
4097 // If there were any VTable uses, deserialize the information and add it
4098 // to Sema's vector and map of VTable uses.
4099 if (!VTableUses.empty()) {
4100 unsigned Idx = 0;
4101 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
4102 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4103 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
4104 bool DefinitionRequired = VTableUses[Idx++];
4105 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
4106 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004107 }
4108 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00004109}
4110
Sebastian Redl2c499f62010-08-18 23:56:43 +00004111IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redl78f51772010-08-02 18:30:12 +00004112 // Try to find this name within our on-disk hash tables. We start with the
4113 // most recent one, since that one contains the most up-to-date info.
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004114 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004115 ASTIdentifierLookupTable *IdTable
4116 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl5c415f32010-07-22 17:01:13 +00004117 if (!IdTable)
4118 continue;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004119 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004120 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004121 if (Pos == IdTable->end())
4122 continue;
Douglas Gregora868bbd2009-04-21 22:25:48 +00004123
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004124 // Dereferencing the iterator has the effect of building the
4125 // IdentifierInfo node and populating it with the various
4126 // declarations it needs.
Sebastian Redl78f51772010-08-02 18:30:12 +00004127 return *Pos;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004128 }
Sebastian Redl78f51772010-08-02 18:30:12 +00004129 return 0;
Douglas Gregora868bbd2009-04-21 22:25:48 +00004130}
4131
Douglas Gregor57756ea2010-10-14 22:11:03 +00004132namespace clang {
4133 /// \brief An identifier-lookup iterator that enumerates all of the
4134 /// identifiers stored within a set of AST files.
4135 class ASTIdentifierIterator : public IdentifierIterator {
4136 /// \brief The AST reader whose identifiers are being enumerated.
4137 const ASTReader &Reader;
4138
4139 /// \brief The current index into the chain of AST files stored in
4140 /// the AST reader.
4141 unsigned Index;
4142
4143 /// \brief The current position within the identifier lookup table
4144 /// of the current AST file.
4145 ASTIdentifierLookupTable::key_iterator Current;
4146
4147 /// \brief The end position within the identifier lookup table of
4148 /// the current AST file.
4149 ASTIdentifierLookupTable::key_iterator End;
4150
4151 public:
4152 explicit ASTIdentifierIterator(const ASTReader &Reader);
4153
4154 virtual llvm::StringRef Next();
4155 };
4156}
4157
4158ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
4159 : Reader(Reader), Index(Reader.Chain.size() - 1) {
4160 ASTIdentifierLookupTable *IdTable
4161 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4162 Current = IdTable->key_begin();
4163 End = IdTable->key_end();
4164}
4165
4166llvm::StringRef ASTIdentifierIterator::Next() {
4167 while (Current == End) {
4168 // If we have exhausted all of our AST files, we're done.
4169 if (Index == 0)
4170 return llvm::StringRef();
4171
4172 --Index;
4173 ASTIdentifierLookupTable *IdTable
4174 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4175 Current = IdTable->key_begin();
4176 End = IdTable->key_end();
4177 }
4178
4179 // We have any identifiers remaining in the current AST file; return
4180 // the next one.
4181 std::pair<const char*, unsigned> Key = *Current;
4182 ++Current;
4183 return llvm::StringRef(Key.first, Key.second);
4184}
4185
4186IdentifierIterator *ASTReader::getIdentifiers() const {
4187 return new ASTIdentifierIterator(*this);
4188}
4189
Mike Stump11289f42009-09-09 15:08:12 +00004190std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redl2c499f62010-08-18 23:56:43 +00004191ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redlada023c2010-08-04 20:40:17 +00004192 // Find this selector in a hash table. We want to find the most recent entry.
4193 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4194 PerFileData &F = *Chain[I];
4195 if (!F.SelectorLookupTable)
4196 continue;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004197
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004198 ASTSelectorLookupTable *PoolTable
4199 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
4200 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redlada023c2010-08-04 20:40:17 +00004201 if (Pos != PoolTable->end()) {
4202 ++NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004203 // FIXME: Not quite happy with the statistics here. We probably should
4204 // disable this tracking when called via LoadSelector.
4205 // Also, should entries without methods count as misses?
4206 ++NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004207 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redlada023c2010-08-04 20:40:17 +00004208 if (DeserializationListener)
4209 DeserializationListener->SelectorRead(Data.ID, Sel);
4210 return std::make_pair(Data.Instance, Data.Factory);
4211 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004212 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00004213
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004214 ++NumMethodPoolMisses;
Sebastian Redlada023c2010-08-04 20:40:17 +00004215 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorc78d3462009-04-24 21:10:55 +00004216}
4217
Sebastian Redl2c499f62010-08-18 23:56:43 +00004218void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004219 // It would be complicated to avoid reading the methods anyway. So don't.
4220 ReadMethodPool(Sel);
4221}
4222
Sebastian Redl2c499f62010-08-18 23:56:43 +00004223void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004224 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00004225 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00004226 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00004227 if (DeserializationListener)
4228 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004229}
4230
Douglas Gregor1342e842009-07-06 18:54:52 +00004231/// \brief Set the globally-visible declarations associated with the given
4232/// identifier.
4233///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004234/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00004235/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00004236/// them.
4237///
4238/// \param II an IdentifierInfo that refers to one or more globally-visible
4239/// declarations.
4240///
4241/// \param DeclIDs the set of declaration IDs with the name @p II that are
4242/// visible at global scope.
4243///
4244/// \param Nonrecursive should be true to indicate that the caller knows that
4245/// this call is non-recursive, and therefore the globally-visible declarations
4246/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00004247void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004248ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregor1342e842009-07-06 18:54:52 +00004249 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
4250 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004251 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004252 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4253 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4254 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004255 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00004256 return;
4257 }
Mike Stump11289f42009-09-09 15:08:12 +00004258
Douglas Gregor1342e842009-07-06 18:54:52 +00004259 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4260 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4261 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004262 if (SemaObj->TUScope) {
4263 // Introduce this declaration into the translation-unit scope
4264 // and add it to the declaration chain for this identifier, so
4265 // that (unqualified) name lookup will find it.
John McCall48871652010-08-21 09:40:31 +00004266 SemaObj->TUScope->AddDecl(D);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004267 }
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004268 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregor1342e842009-07-06 18:54:52 +00004269 } else {
4270 // Queue this declaration so that it will be added to the
4271 // translation unit scope and identifier's declaration chain
4272 // once a Sema object is known.
4273 PreloadedDecls.push_back(D);
4274 }
4275 }
4276}
4277
Sebastian Redl2c499f62010-08-18 23:56:43 +00004278IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004279 if (ID == 0)
4280 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00004281
Sebastian Redlc713b962010-07-21 00:46:22 +00004282 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004283 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004284 return 0;
4285 }
Mike Stump11289f42009-09-09 15:08:12 +00004286
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004287 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc713b962010-07-21 00:46:22 +00004288 ID -= 1;
4289 if (!IdentifiersLoaded[ID]) {
4290 unsigned Index = ID;
4291 const char *Str = 0;
4292 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4293 PerFileData *F = Chain[N - I - 1];
4294 if (Index < F->LocalNumIdentifiers) {
4295 uint32_t Offset = F->IdentifierOffsets[Index];
4296 Str = F->IdentifierTableData + Offset;
4297 break;
4298 }
4299 Index -= F->LocalNumIdentifiers;
4300 }
4301 assert(Str && "Broken Chain");
Douglas Gregor5287b4e2009-04-25 21:04:17 +00004302
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004303 // All of the strings in the AST file are preceded by a 16-bit length.
4304 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00004305 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4306 // unsigned integers. This is important to avoid integer overflow when
4307 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00004308 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00004309 unsigned StrLen = (((unsigned) StrLenPtr[0])
4310 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00004311 IdentifiersLoaded[ID]
Kovarththanan Rajaratnama3b09592010-03-12 10:32:27 +00004312 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlff4a2952010-07-23 23:49:55 +00004313 if (DeserializationListener)
4314 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004315 }
Mike Stump11289f42009-09-09 15:08:12 +00004316
Sebastian Redlc713b962010-07-21 00:46:22 +00004317 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004318}
4319
Sebastian Redl2c499f62010-08-18 23:56:43 +00004320void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00004321 ReadSLocEntryRecord(ID);
4322}
4323
Sebastian Redl2c499f62010-08-18 23:56:43 +00004324Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00004325 if (ID == 0)
4326 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00004327
Sebastian Redlada023c2010-08-04 20:40:17 +00004328 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004329 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00004330 return Selector();
4331 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004332
Sebastian Redlada023c2010-08-04 20:40:17 +00004333 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004334 // Load this selector from the selector table.
Sebastian Redlada023c2010-08-04 20:40:17 +00004335 unsigned Idx = ID - 1;
4336 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4337 PerFileData &F = *Chain[N - I - 1];
4338 if (Idx < F.LocalNumSelectors) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004339 ASTSelectorLookupTrait Trait(*this);
Sebastian Redlada023c2010-08-04 20:40:17 +00004340 SelectorsLoaded[ID - 1] =
4341 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4342 if (DeserializationListener)
4343 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4344 break;
4345 }
4346 Idx -= F.LocalNumSelectors;
4347 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004348 }
4349
Sebastian Redlada023c2010-08-04 20:40:17 +00004350 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00004351}
4352
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004353Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00004354 return DecodeSelector(ID);
4355}
4356
Sebastian Redl2c499f62010-08-18 23:56:43 +00004357uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00004358 // ID 0 (the null selector) is considered an external selector.
4359 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00004360}
4361
Mike Stump11289f42009-09-09 15:08:12 +00004362DeclarationName
Sebastian Redl2c499f62010-08-18 23:56:43 +00004363ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004364 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4365 switch (Kind) {
4366 case DeclarationName::Identifier:
4367 return DeclarationName(GetIdentifierInfo(Record, Idx));
4368
4369 case DeclarationName::ObjCZeroArgSelector:
4370 case DeclarationName::ObjCOneArgSelector:
4371 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff3c301dc2009-04-23 15:15:40 +00004372 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004373
4374 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004375 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004376 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004377
4378 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004379 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004380 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004381
4382 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004383 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004384 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004385
4386 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004387 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004388 (OverloadedOperatorKind)Record[Idx++]);
4389
Alexis Hunt3d221f22009-11-29 07:34:05 +00004390 case DeclarationName::CXXLiteralOperatorName:
4391 return Context->DeclarationNames.getCXXLiteralOperatorName(
4392 GetIdentifierInfo(Record, Idx));
4393
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004394 case DeclarationName::CXXUsingDirective:
4395 return DeclarationName::getUsingDirectiveName();
4396 }
4397
4398 // Required to silence GCC warning
4399 return DeclarationName();
4400}
Douglas Gregor55abb232009-04-10 20:39:37 +00004401
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004402void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4403 DeclarationNameLoc &DNLoc,
4404 DeclarationName Name,
4405 const RecordData &Record, unsigned &Idx) {
4406 switch (Name.getNameKind()) {
4407 case DeclarationName::CXXConstructorName:
4408 case DeclarationName::CXXDestructorName:
4409 case DeclarationName::CXXConversionFunctionName:
4410 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4411 break;
4412
4413 case DeclarationName::CXXOperatorName:
4414 DNLoc.CXXOperatorName.BeginOpNameLoc
4415 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4416 DNLoc.CXXOperatorName.EndOpNameLoc
4417 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4418 break;
4419
4420 case DeclarationName::CXXLiteralOperatorName:
4421 DNLoc.CXXLiteralOperatorName.OpNameLoc
4422 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4423 break;
4424
4425 case DeclarationName::Identifier:
4426 case DeclarationName::ObjCZeroArgSelector:
4427 case DeclarationName::ObjCOneArgSelector:
4428 case DeclarationName::ObjCMultiArgSelector:
4429 case DeclarationName::CXXUsingDirective:
4430 break;
4431 }
4432}
4433
4434void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4435 DeclarationNameInfo &NameInfo,
4436 const RecordData &Record, unsigned &Idx) {
4437 NameInfo.setName(ReadDeclarationName(Record, Idx));
4438 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4439 DeclarationNameLoc DNLoc;
4440 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4441 NameInfo.setInfo(DNLoc);
4442}
4443
4444void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4445 const RecordData &Record, unsigned &Idx) {
4446 Info.NNS = ReadNestedNameSpecifier(Record, Idx);
4447 Info.NNSRange = ReadSourceRange(F, Record, Idx);
4448 unsigned NumTPLists = Record[Idx++];
4449 Info.NumTemplParamLists = NumTPLists;
4450 if (NumTPLists) {
4451 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4452 for (unsigned i=0; i != NumTPLists; ++i)
4453 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4454 }
4455}
4456
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004457TemplateName
Douglas Gregor5590be02011-01-15 06:45:20 +00004458ASTReader::ReadTemplateName(PerFileData &F, const RecordData &Record,
4459 unsigned &Idx) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004460 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004461 switch (Kind) {
4462 case TemplateName::Template:
4463 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4464
4465 case TemplateName::OverloadedTemplate: {
4466 unsigned size = Record[Idx++];
4467 UnresolvedSet<8> Decls;
4468 while (size--)
4469 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4470
4471 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4472 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004473
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004474 case TemplateName::QualifiedTemplate: {
4475 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4476 bool hasTemplKeyword = Record[Idx++];
4477 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4478 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4479 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004480
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004481 case TemplateName::DependentTemplate: {
4482 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4483 if (Record[Idx++]) // isIdentifier
4484 return Context->getDependentTemplateName(NNS,
4485 GetIdentifierInfo(Record, Idx));
4486 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004487 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004488 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004489
4490 case TemplateName::SubstTemplateTemplateParmPack: {
4491 TemplateTemplateParmDecl *Param
4492 = cast_or_null<TemplateTemplateParmDecl>(GetDecl(Record[Idx++]));
4493 if (!Param)
4494 return TemplateName();
4495
4496 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
4497 if (ArgPack.getKind() != TemplateArgument::Pack)
4498 return TemplateName();
4499
4500 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
4501 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004502 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004503
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004504 assert(0 && "Unhandled template name kind!");
4505 return TemplateName();
4506}
4507
4508TemplateArgument
Sebastian Redl2c373b92010-10-05 15:59:54 +00004509ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00004510 const RecordData &Record, unsigned &Idx) {
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004511 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
4512 switch (Kind) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004513 case TemplateArgument::Null:
4514 return TemplateArgument();
4515 case TemplateArgument::Type:
4516 return TemplateArgument(GetType(Record[Idx++]));
4517 case TemplateArgument::Declaration:
4518 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00004519 case TemplateArgument::Integral: {
4520 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4521 QualType T = GetType(Record[Idx++]);
4522 return TemplateArgument(Value, T);
4523 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004524 case TemplateArgument::Template:
Douglas Gregor5590be02011-01-15 06:45:20 +00004525 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004526 case TemplateArgument::TemplateExpansion: {
Douglas Gregor5590be02011-01-15 06:45:20 +00004527 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregore1d60df2011-01-14 23:41:42 +00004528 llvm::Optional<unsigned> NumTemplateExpansions;
4529 if (unsigned NumExpansions = Record[Idx++])
4530 NumTemplateExpansions = NumExpansions - 1;
4531 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004532 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004533 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004534 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004535 case TemplateArgument::Pack: {
4536 unsigned NumArgs = Record[Idx++];
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004537 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4538 for (unsigned I = 0; I != NumArgs; ++I)
4539 Args[I] = ReadTemplateArgument(F, Record, Idx);
4540 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004541 }
4542 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004543
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004544 assert(0 && "Unhandled template argument kind!");
4545 return TemplateArgument();
4546}
4547
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004548TemplateParameterList *
Sebastian Redl2c373b92010-10-05 15:59:54 +00004549ASTReader::ReadTemplateParameterList(PerFileData &F,
4550 const RecordData &Record, unsigned &Idx) {
4551 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4552 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4553 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004554
4555 unsigned NumParams = Record[Idx++];
4556 llvm::SmallVector<NamedDecl *, 16> Params;
4557 Params.reserve(NumParams);
4558 while (NumParams--)
4559 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004560
4561 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004562 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4563 Params.data(), Params.size(), RAngleLoc);
4564 return TemplateParams;
4565}
4566
4567void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004568ASTReader::
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004569ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004570 PerFileData &F, const RecordData &Record,
4571 unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004572 unsigned NumTemplateArgs = Record[Idx++];
4573 TemplArgs.reserve(NumTemplateArgs);
4574 while (NumTemplateArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004575 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004576}
4577
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004578/// \brief Read a UnresolvedSet structure.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004579void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004580 const RecordData &Record, unsigned &Idx) {
4581 unsigned NumDecls = Record[Idx++];
4582 while (NumDecls--) {
4583 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4584 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4585 Set.addDecl(D, AS);
4586 }
4587}
4588
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004589CXXBaseSpecifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00004590ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky19b9f952010-07-26 16:56:01 +00004591 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004592 bool isVirtual = static_cast<bool>(Record[Idx++]);
4593 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4594 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redl08905022011-02-05 19:23:19 +00004595 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004596 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4597 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor752a5952011-01-03 22:36:02 +00004598 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl08905022011-02-05 19:23:19 +00004599 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregor752a5952011-01-03 22:36:02 +00004600 EllipsisLoc);
Sebastian Redl08905022011-02-05 19:23:19 +00004601 Result.setInheritConstructors(inheritConstructors);
4602 return Result;
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004603}
4604
Alexis Hunt1d792652011-01-08 20:30:50 +00004605std::pair<CXXCtorInitializer **, unsigned>
4606ASTReader::ReadCXXCtorInitializers(PerFileData &F, const RecordData &Record,
4607 unsigned &Idx) {
4608 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004609 unsigned NumInitializers = Record[Idx++];
4610 if (NumInitializers) {
4611 ASTContext &C = *getContext();
4612
Alexis Hunt1d792652011-01-08 20:30:50 +00004613 CtorInitializers
4614 = new (C) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004615 for (unsigned i=0; i != NumInitializers; ++i) {
4616 TypeSourceInfo *BaseClassInfo = 0;
4617 bool IsBaseVirtual = false;
4618 FieldDecl *Member = 0;
Francois Pichetd583da02010-12-04 09:14:42 +00004619 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004620
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004621 bool IsBaseInitializer = Record[Idx++];
4622 if (IsBaseInitializer) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004623 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004624 IsBaseVirtual = Record[Idx++];
4625 } else {
Francois Pichetd583da02010-12-04 09:14:42 +00004626 bool IsIndirectMemberInitializer = Record[Idx++];
4627 if (IsIndirectMemberInitializer)
4628 IndirectMember = cast<IndirectFieldDecl>(GetDecl(Record[Idx++]));
4629 else
4630 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004631 }
Douglas Gregor44e7df62011-01-04 00:32:56 +00004632 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004633 Expr *Init = ReadExpr(F);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004634 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4635 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004636 bool IsWritten = Record[Idx++];
4637 unsigned SourceOrderOrNumArrayIndices;
4638 llvm::SmallVector<VarDecl *, 8> Indices;
4639 if (IsWritten) {
4640 SourceOrderOrNumArrayIndices = Record[Idx++];
4641 } else {
4642 SourceOrderOrNumArrayIndices = Record[Idx++];
4643 Indices.reserve(SourceOrderOrNumArrayIndices);
4644 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4645 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4646 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004647
Alexis Hunt1d792652011-01-08 20:30:50 +00004648 CXXCtorInitializer *BOMInit;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004649 if (IsBaseInitializer) {
Alexis Hunt1d792652011-01-08 20:30:50 +00004650 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
4651 LParenLoc, Init, RParenLoc,
4652 MemberOrEllipsisLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004653 } else if (IsWritten) {
Francois Pichetd583da02010-12-04 09:14:42 +00004654 if (Member)
Alexis Hunt1d792652011-01-08 20:30:50 +00004655 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
4656 LParenLoc, Init, RParenLoc);
Francois Pichetd583da02010-12-04 09:14:42 +00004657 else
Alexis Hunt1d792652011-01-08 20:30:50 +00004658 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
4659 MemberOrEllipsisLoc, LParenLoc,
4660 Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004661 } else {
Alexis Hunt1d792652011-01-08 20:30:50 +00004662 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
4663 LParenLoc, Init, RParenLoc,
4664 Indices.data(), Indices.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004665 }
4666
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00004667 if (IsWritten)
4668 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Alexis Hunt1d792652011-01-08 20:30:50 +00004669 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004670 }
4671 }
4672
Alexis Hunt1d792652011-01-08 20:30:50 +00004673 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004674}
4675
Chris Lattnerca025db2010-05-07 21:43:38 +00004676NestedNameSpecifier *
Sebastian Redl2c499f62010-08-18 23:56:43 +00004677ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004678 unsigned N = Record[Idx++];
4679 NestedNameSpecifier *NNS = 0, *Prev = 0;
4680 for (unsigned I = 0; I != N; ++I) {
4681 NestedNameSpecifier::SpecifierKind Kind
4682 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4683 switch (Kind) {
4684 case NestedNameSpecifier::Identifier: {
4685 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4686 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4687 break;
4688 }
4689
4690 case NestedNameSpecifier::Namespace: {
4691 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4692 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4693 break;
4694 }
4695
4696 case NestedNameSpecifier::TypeSpec:
4697 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall424cec92011-01-19 06:33:43 +00004698 const Type *T = GetType(Record[Idx++]).getTypePtrOrNull();
Douglas Gregor0cdc8322010-12-10 17:03:06 +00004699 if (!T)
4700 return 0;
4701
Chris Lattnerca025db2010-05-07 21:43:38 +00004702 bool Template = Record[Idx++];
4703 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4704 break;
4705 }
4706
4707 case NestedNameSpecifier::Global: {
4708 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4709 // No associated value, and there can't be a prefix.
4710 break;
4711 }
Chris Lattnerca025db2010-05-07 21:43:38 +00004712 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00004713 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00004714 }
4715 return NNS;
4716}
4717
4718SourceRange
Sebastian Redl2c373b92010-10-05 15:59:54 +00004719ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4720 unsigned &Idx) {
4721 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4722 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00004723 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00004724}
4725
Douglas Gregor1daeb692009-04-13 18:14:40 +00004726/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004727llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004728 unsigned BitWidth = Record[Idx++];
4729 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4730 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4731 Idx += NumWords;
4732 return Result;
4733}
4734
4735/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004736llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004737 bool isUnsigned = Record[Idx++];
4738 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4739}
4740
Douglas Gregore0a3a512009-04-14 21:55:33 +00004741/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004742llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004743 return llvm::APFloat(ReadAPInt(Record, Idx));
4744}
4745
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004746// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00004747std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004748 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00004749 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004750 Idx += Len;
4751 return Result;
4752}
4753
Sebastian Redl2c499f62010-08-18 23:56:43 +00004754CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00004755 unsigned &Idx) {
4756 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4757 return CXXTemporary::Create(*Context, Decl);
4758}
4759
Sebastian Redl2c499f62010-08-18 23:56:43 +00004760DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00004761 return Diag(SourceLocation(), DiagID);
4762}
4763
Sebastian Redl2c499f62010-08-18 23:56:43 +00004764DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004765 return Diags.Report(Loc, DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00004766}
Douglas Gregora9af1d12009-04-17 00:04:06 +00004767
Douglas Gregora868bbd2009-04-21 22:25:48 +00004768/// \brief Retrieve the identifier table associated with the
4769/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004770IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004771 assert(PP && "Forgot to set Preprocessor ?");
4772 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00004773}
4774
Douglas Gregora9af1d12009-04-17 00:04:06 +00004775/// \brief Record that the given ID maps to the given switch-case
4776/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004777void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004778 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4779 SwitchCaseStmts[ID] = SC;
4780}
4781
4782/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004783SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004784 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4785 return SwitchCaseStmts[ID];
4786}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004787
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00004788void ASTReader::ClearSwitchCaseIDs() {
4789 SwitchCaseStmts.clear();
4790}
4791
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004792/// \brief Record that the given label statement has been
4793/// deserialized and has the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004794void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump11289f42009-09-09 15:08:12 +00004795 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004796 "Deserialized label twice");
4797 LabelStmts[ID] = S;
4798
4799 // If we've already seen any goto statements that point to this
4800 // label, resolve them now.
4801 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
4802 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
4803 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
4804 Goto->second->setLabel(S);
4805 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor779d8652009-04-17 18:58:21 +00004806
4807 // If we've already seen any address-label statements that point to
4808 // this label, resolve them now.
4809 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump11289f42009-09-09 15:08:12 +00004810 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor779d8652009-04-17 18:58:21 +00004811 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump11289f42009-09-09 15:08:12 +00004812 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor779d8652009-04-17 18:58:21 +00004813 AddrLabel != AddrLabels.second; ++AddrLabel)
4814 AddrLabel->second->setLabel(S);
4815 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004816}
4817
4818/// \brief Set the label of the given statement to the label
4819/// identified by ID.
4820///
4821/// Depending on the order in which the label and other statements
4822/// referencing that label occur, this operation may complete
4823/// immediately (updating the statement) or it may queue the
4824/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004825void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004826 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4827 if (Label != LabelStmts.end()) {
4828 // We've already seen this label, so set the label of the goto and
4829 // we're done.
4830 S->setLabel(Label->second);
4831 } else {
4832 // We haven't seen this label yet, so add this goto to the set of
4833 // unresolved goto statements.
4834 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
4835 }
4836}
Douglas Gregor779d8652009-04-17 18:58:21 +00004837
4838/// \brief Set the label of the given expression to the label
4839/// identified by ID.
4840///
4841/// Depending on the order in which the label and other statements
4842/// referencing that label occur, this operation may complete
4843/// immediately (updating the statement) or it may queue the
4844/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004845void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor779d8652009-04-17 18:58:21 +00004846 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4847 if (Label != LabelStmts.end()) {
4848 // We've already seen this label, so set the label of the
4849 // label-address expression and we're done.
4850 S->setLabel(Label->second);
4851 } else {
4852 // We haven't seen this label yet, so add this label-address
4853 // expression to the set of unresolved label-address expressions.
4854 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
4855 }
4856}
Douglas Gregor1342e842009-07-06 18:54:52 +00004857
Sebastian Redl2c499f62010-08-18 23:56:43 +00004858void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004859 assert(NumCurrentElementsDeserializing &&
4860 "FinishedDeserializing not paired with StartedDeserializing");
4861 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004862 // If any identifiers with corresponding top-level declarations have
4863 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004864 while (!PendingIdentifierInfos.empty()) {
4865 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4866 PendingIdentifierInfos.front().DeclIDs, true);
4867 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00004868 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004869
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00004870 // Ready to load previous declarations of Decls that were delayed.
4871 while (!PendingPreviousDecls.empty()) {
4872 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
4873 PendingPreviousDecls.front().second);
4874 PendingPreviousDecls.pop_front();
4875 }
4876
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004877 // We are not in recursive loading, so it's safe to pass the "interesting"
4878 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004879 if (Consumer)
4880 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidisad5f95c2010-10-24 17:26:31 +00004881
4882 assert(PendingForwardRefs.size() == 0 &&
4883 "Some forward refs did not get linked to the definition!");
Douglas Gregor1342e842009-07-06 18:54:52 +00004884 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004885 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00004886}
Douglas Gregorb473b072010-08-19 00:28:17 +00004887
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004888ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00004889 const char *isysroot, bool DisableValidation,
4890 bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004891 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4892 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4893 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4894 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00004895 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
4896 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NextSLocOffset(0),
4897 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
4898 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
4899 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
4900 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
4901 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
4902 NumCurrentElementsDeserializing(0)
4903{
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004904 RelocatablePCH = false;
4905}
4906
4907ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4908 Diagnostic &Diags, const char *isysroot,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00004909 bool DisableValidation, bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004910 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4911 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00004912 isysroot(isysroot), DisableValidation(DisableValidation),
4913 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
4914 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00004915 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4916 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4917 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4918 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4919 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4920 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004921 RelocatablePCH = false;
4922}
4923
4924ASTReader::~ASTReader() {
4925 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4926 delete Chain[e - i - 1];
4927 // Delete all visible decl lookup tables
4928 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4929 E = DeclContextOffsets.end();
4930 I != E; ++I) {
4931 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4932 J != F; ++J) {
4933 if (J->NameLookupTableData)
4934 delete static_cast<ASTDeclContextNameLookupTable*>(
4935 J->NameLookupTableData);
4936 }
4937 }
4938 for (DeclContextVisibleUpdatesPending::iterator
4939 I = PendingVisibleUpdates.begin(),
4940 E = PendingVisibleUpdates.end();
4941 I != E; ++I) {
4942 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4943 F = I->second.end();
4944 J != F; ++J)
4945 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4946 }
4947}
4948
Sebastian Redl009e7f22010-10-05 16:15:19 +00004949ASTReader::PerFileData::PerFileData(ASTFileType Ty)
4950 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00004951 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
4952 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
Douglas Gregor09b69892011-02-10 17:09:37 +00004953 MacroDefinitionOffsets(0),
4954 LocalNumHeaderFileInfos(0), HeaderFileInfoTableData(0),
4955 HeaderFileInfoTable(0),
4956 LocalNumSelectors(0), SelectorOffsets(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00004957 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004958 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
4959 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redl3f6b7532010-10-01 19:59:12 +00004960 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregorb473b072010-08-19 00:28:17 +00004961{}
4962
4963ASTReader::PerFileData::~PerFileData() {
4964 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
Douglas Gregor09b69892011-02-10 17:09:37 +00004965 delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable);
Douglas Gregorb473b072010-08-19 00:28:17 +00004966 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4967}
4968