blob: 7d031019acb654035bd59d6520f11519b165e38a [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 Gregor9b272512011-02-28 23:58:31 +000026#include "clang/AST/NestedNameSpecifier.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000027#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000028#include "clang/AST/TypeLocVisitor.h"
Chris Lattner34321bc2009-04-10 21:41:48 +000029#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000030#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000031#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000032#include "clang/Lex/HeaderSearch.h"
Douglas Gregora868bbd2009-04-21 22:25:48 +000033#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000034#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000035#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000036#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000037#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000038#include "clang/Basic/TargetInfo.h"
Douglas Gregord54f3a12009-10-05 21:07:28 +000039#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000040#include "clang/Basic/VersionTuple.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000041#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000042#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000043#include "llvm/Support/MemoryBuffer.h"
John McCall0ad16662009-10-29 08:12:44 +000044#include "llvm/Support/ErrorHandling.h"
Douglas Gregor09b69892011-02-10 17:09:37 +000045#include "llvm/Support/FileSystem.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Michael J. Spencerf25faaa2010-12-09 17:36:38 +000047#include "llvm/Support/system_error.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000048#include <algorithm>
Douglas Gregorc379c072009-04-28 18:58:38 +000049#include <iterator>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000050#include <cstdio>
Douglas Gregorc5046832009-04-27 18:38:38 +000051#include <sys/stat.h>
Douglas Gregor925296b2011-07-19 16:10:42 +000052#include <iostream>
Douglas Gregor09b69892011-02-10 17:09:37 +000053
Douglas Gregoref84c4b2009-04-09 22:27:44 +000054using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000055using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000056
57//===----------------------------------------------------------------------===//
Sebastian Redld44cd6a2010-08-18 23:57:06 +000058// PCH validator implementation
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000059//===----------------------------------------------------------------------===//
60
Sebastian Redl3e31c722010-08-18 23:56:56 +000061ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000062
63bool
64PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
65 const LangOptions &PPLangOpts = PP.getLangOptions();
66#define PARSE_LANGOPT_BENIGN(Option)
67#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
68 if (PPLangOpts.Option != LangOpts.Option) { \
69 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
70 return true; \
71 }
72
73 PARSE_LANGOPT_BENIGN(Trigraphs);
74 PARSE_LANGOPT_BENIGN(BCPLComment);
75 PARSE_LANGOPT_BENIGN(DollarIdents);
76 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
77 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carruthe03aa552010-04-17 20:17:31 +000078 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000079 PARSE_LANGOPT_BENIGN(ImplicitInt);
80 PARSE_LANGOPT_BENIGN(Digraphs);
81 PARSE_LANGOPT_BENIGN(HexFloats);
82 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
Peter Collingbournea686b5f2011-04-15 00:35:23 +000083 PARSE_LANGOPT_IMPORTANT(C1X, diag::warn_pch_c1x);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000084 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +000085 PARSE_LANGOPT_BENIGN(MSCVersion);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000086 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
87 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
88 PARSE_LANGOPT_BENIGN(CXXOperatorName);
89 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
90 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
91 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian45878032010-02-09 19:31:38 +000092 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +000093 PARSE_LANGOPT_IMPORTANT(AppleKext, diag::warn_pch_apple_kext);
Ted Kremenek1d56c9e2010-12-23 21:35:43 +000094 PARSE_LANGOPT_IMPORTANT(ObjCDefaultSynthProperties,
95 diag::warn_pch_objc_auto_properties);
Douglas Gregora860e6a2011-06-14 23:20:43 +000096 PARSE_LANGOPT_BENIGN(ObjCInferRelatedResultType)
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +000097 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian62c56022010-04-22 21:01:59 +000098 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000099 PARSE_LANGOPT_BENIGN(PascalStrings);
100 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump11289f42009-09-09 15:08:12 +0000101 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000102 diag::warn_pch_lax_vector_conversions);
Nate Begeman9d905792009-06-25 22:57:40 +0000103 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000104 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Anders Carlssonce8dd3a2011-02-19 23:53:54 +0000105 PARSE_LANGOPT_IMPORTANT(ObjCExceptions, diag::warn_pch_objc_exceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +0000106 PARSE_LANGOPT_IMPORTANT(CXXExceptions, diag::warn_pch_cxx_exceptions);
107 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Douglas Gregordbe39272011-02-01 15:15:22 +0000108 PARSE_LANGOPT_IMPORTANT(MSBitfields, diag::warn_pch_ms_bitfields);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000109 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
110 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
111 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump11289f42009-09-09 15:08:12 +0000112 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000113 diag::warn_pch_thread_safe_statics);
Daniel Dunbara77eaeb2009-09-03 04:54:28 +0000114 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000115 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
116 PARSE_LANGOPT_BENIGN(EmitAllDecls);
117 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattner51924e512010-06-26 21:25:03 +0000118 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump11289f42009-09-09 15:08:12 +0000119 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000120 diag::warn_pch_heinous_extensions);
121 // FIXME: Most of the options below are benign if the macro wasn't
122 // used. Unfortunately, this means that a PCH compiled without
123 // optimization can't be used with optimization turned on, even
124 // though the only thing that changes is whether __OPTIMIZE__ was
125 // defined... but if __OPTIMIZE__ never showed up in the header, it
126 // doesn't matter. We could consider making this some special kind
127 // of check.
128 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
129 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
130 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
131 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
132 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
133 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
Chandler Carruth7ffce732011-04-23 20:05:38 +0000134 PARSE_LANGOPT_IMPORTANT(Deprecated, diag::warn_pch_deprecated);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000135 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
136 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsoned4e2952009-11-05 20:14:16 +0000137 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +0000138 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000139 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump11289f42009-09-09 15:08:12 +0000140 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000141 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
142 return true;
143 }
144 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000145 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
146 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000147 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman9d905792009-06-25 22:57:40 +0000148 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Peter Collingbourne546d0792010-12-01 19:14:57 +0000149 PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda);
Mike Stumpd9546382009-12-12 01:27:46 +0000150 PARSE_LANGOPT_BENIGN(CatchUndefined);
John McCall31168b02011-06-15 23:02:42 +0000151 PARSE_LANGOPT_BENIGN(DefaultFPContract);
Daniel Dunbar143021e2009-09-21 04:16:19 +0000152 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000153 PARSE_LANGOPT_BENIGN(SpellChecking);
John McCall31168b02011-06-15 23:02:42 +0000154 PARSE_LANGOPT_IMPORTANT(ObjCAutoRefCount, diag::warn_pch_auto_ref_count);
155 PARSE_LANGOPT_BENIGN(ObjCInferRelatedReturnType);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +0000156#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000157#undef PARSE_LANGOPT_BENIGN
158
159 return false;
160}
161
Daniel Dunbar20a682d2009-11-11 00:52:11 +0000162bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
163 if (Triple == PP.getTargetInfo().getTriple().str())
164 return false;
165
166 Reader.Diag(diag::warn_pch_target_triple)
167 << Triple << PP.getTargetInfo().getTriple().str();
168 return true;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000169}
170
Benjamin Kramer90b5b682010-11-25 18:29:30 +0000171namespace {
172 struct EmptyStringRef {
173 bool operator ()(llvm::StringRef r) const { return r.empty(); }
174 };
175 struct EmptyBlock {
176 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
177 };
178}
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000179
180static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
181 PCHPredefinesBlocks R) {
182 // First, sum up the lengths.
183 unsigned LL = 0, RL = 0;
184 for (unsigned I = 0, N = L.size(); I != N; ++I) {
185 LL += L[I].size();
186 }
187 for (unsigned I = 0, N = R.size(); I != N; ++I) {
188 RL += R[I].Data.size();
189 }
190 if (LL != RL)
191 return false;
192 if (LL == 0 && RL == 0)
193 return true;
194
195 // Kick out empty parts, they confuse the algorithm below.
196 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
197 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
198
199 // Do it the hard way. At this point, both vectors must be non-empty.
200 llvm::StringRef LR = L[0], RR = R[0].Data;
201 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbar01ad0a72010-07-16 00:00:11 +0000202 (void) RN;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000203 for (;;) {
204 // Compare the current pieces.
205 if (LR.size() == RR.size()) {
206 // If they're the same length, it's pretty easy.
207 if (LR != RR)
208 return false;
209 // Both pieces are done, advance.
210 ++LI;
211 ++RI;
212 // If either string is done, they're both done, since they're the same
213 // length.
214 if (LI == LN) {
215 assert(RI == RN && "Strings not the same length after all?");
216 return true;
217 }
218 LR = L[LI];
219 RR = R[RI].Data;
220 } else if (LR.size() < RR.size()) {
221 // Right piece is longer.
222 if (!RR.startswith(LR))
223 return false;
224 ++LI;
225 assert(LI != LN && "Strings not the same length after all?");
226 RR = RR.substr(LR.size());
227 LR = L[LI];
228 } else {
229 // Left piece is longer.
230 if (!LR.startswith(RR))
231 return false;
232 ++RI;
233 assert(RI != RN && "Strings not the same length after all?");
234 LR = LR.substr(RR.size());
235 RR = R[RI].Data;
236 }
237 }
238}
239
240static std::pair<FileID, llvm::StringRef::size_type>
241FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
242 std::pair<FileID, llvm::StringRef::size_type> Res;
243 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
244 Res.second = Buffers[I].Data.find(MacroDef);
245 if (Res.second != llvm::StringRef::npos) {
246 Res.first = Buffers[I].BufferID;
247 break;
248 }
249 }
250 return Res;
251}
252
253bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000254 llvm::StringRef OriginalFileName,
Nick Lewycky36079892011-02-23 21:16:44 +0000255 std::string &SuggestedPredefines,
256 FileManager &FileMgr) {
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000257 // We are in the context of an implicit include, so the predefines buffer will
258 // have a #include entry for the PCH file itself (as normalized by the
259 // preprocessor initialization). Find it and skip over it in the checking
260 // below.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000261 llvm::SmallString<256> PCHInclude;
262 PCHInclude += "#include \"";
Nick Lewycky36079892011-02-23 21:16:44 +0000263 PCHInclude += NormalizeDashIncludePath(OriginalFileName, FileMgr);
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000264 PCHInclude += "\"\n";
265 std::pair<llvm::StringRef,llvm::StringRef> Split =
266 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
267 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000268 if (Left == PP.getPredefines()) {
269 Error("Missing PCH include entry!");
270 return true;
271 }
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000272
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000273 // If the concatenation of all the PCH buffers is equal to the adjusted
274 // command line, we're done.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000275 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
276 CommandLine.push_back(Left);
277 CommandLine.push_back(Right);
278 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000279 return false;
280
281 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000282
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000283 // The predefines buffers are different. Determine what the differences are,
284 // and whether they require us to reject the PCH file.
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000285 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000286 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
287 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000288
289 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
290 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000291
292 // Pick out implicit #includes after the PCH and don't consider them for
293 // validation; we will insert them into SuggestedPredefines so that the
294 // preprocessor includes them.
295 std::string IncludesAfterPCH;
296 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
297 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
298 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
299 if (AfterPCHLines[i].startswith("#include ")) {
300 IncludesAfterPCH += AfterPCHLines[i];
301 IncludesAfterPCH += '\n';
302 } else {
303 CmdLineLines.push_back(AfterPCHLines[i]);
304 }
305 }
306
307 // Make sure we add the includes last into SuggestedPredefines before we
308 // exit this function.
309 struct AddIncludesRAII {
310 std::string &SuggestedPredefines;
311 std::string &IncludesAfterPCH;
312
313 AddIncludesRAII(std::string &SuggestedPredefines,
314 std::string &IncludesAfterPCH)
315 : SuggestedPredefines(SuggestedPredefines),
316 IncludesAfterPCH(IncludesAfterPCH) { }
317 ~AddIncludesRAII() {
318 SuggestedPredefines += IncludesAfterPCH;
319 }
320 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000321
Daniel Dunbar499baed2009-11-11 05:26:28 +0000322 // Sort both sets of predefined buffer lines, since we allow some extra
323 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000324 std::sort(CmdLineLines.begin(), CmdLineLines.end());
325 std::sort(PCHLines.begin(), PCHLines.end());
326
Daniel Dunbar499baed2009-11-11 05:26:28 +0000327 // Determine which predefines that were used to build the PCH file are missing
328 // from the command line.
329 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000330 std::set_difference(PCHLines.begin(), PCHLines.end(),
331 CmdLineLines.begin(), CmdLineLines.end(),
332 std::back_inserter(MissingPredefines));
333
334 bool MissingDefines = false;
335 bool ConflictingDefines = false;
336 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000337 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000338 if (Missing.startswith("#include ")) {
339 // An -include was specified when generating the PCH; it is included in
340 // the PCH, just ignore it.
341 continue;
342 }
Daniel Dunbar499baed2009-11-11 05:26:28 +0000343 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000344 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
345 return true;
346 }
Mike Stump11289f42009-09-09 15:08:12 +0000347
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000348 // This is a macro definition. Determine the name of the macro we're
349 // defining.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000350 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000351 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000352 = Missing.find_first_of("( \n\r", StartOfMacroName);
353 assert(EndOfMacroName != std::string::npos &&
354 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000355 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000356
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000357 // Determine whether this macro was given a different definition on the
358 // command line.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000359 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000360 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000361 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000362 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
363 MacroDefStart);
364 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000365 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000366 // Different macro; we're done.
367 ConflictPos = CmdLineLines.end();
Mike Stump11289f42009-09-09 15:08:12 +0000368 break;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000369 }
Mike Stump11289f42009-09-09 15:08:12 +0000370
371 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000372 "Invalid #define in predefines buffer?");
Mike Stump11289f42009-09-09 15:08:12 +0000373 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000374 (*ConflictPos)[MacroDefLen] != '(')
375 continue; // Longer macro name; keep trying.
Mike Stump11289f42009-09-09 15:08:12 +0000376
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000377 // We found a conflicting macro definition.
378 break;
379 }
Mike Stump11289f42009-09-09 15:08:12 +0000380
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000381 if (ConflictPos != CmdLineLines.end()) {
382 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
383 << MacroName;
384
385 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000386 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
387 FindMacro(Buffers, Missing);
388 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
389 SourceLocation PCHMissingLoc =
390 SourceMgr.getLocForStartOfFile(MacroLoc.first)
391 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar499baed2009-11-11 05:26:28 +0000392 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000393
394 ConflictingDefines = true;
395 continue;
396 }
Mike Stump11289f42009-09-09 15:08:12 +0000397
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000398 // If the macro doesn't conflict, then we'll just pick up the macro
399 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000400 if (ConflictingDefines)
401 continue; // Don't complain if there are already conflicting defs
Mike Stump11289f42009-09-09 15:08:12 +0000402
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000403 if (!MissingDefines) {
404 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
405 MissingDefines = true;
406 }
407
408 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000409 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
410 FindMacro(Buffers, Missing);
411 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
412 SourceLocation PCHMissingLoc =
413 SourceMgr.getLocForStartOfFile(MacroLoc.first)
414 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000415 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
416 }
Mike Stump11289f42009-09-09 15:08:12 +0000417
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000418 if (ConflictingDefines)
419 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000420
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000421 // Determine what predefines were introduced based on command-line
422 // parameters that were not present when building the PCH
423 // file. Extra #defines are okay, so long as the identifiers being
424 // defined were not used within the precompiled header.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000425 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000426 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
427 PCHLines.begin(), PCHLines.end(),
Mike Stump11289f42009-09-09 15:08:12 +0000428 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000429 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000430 llvm::StringRef &Extra = ExtraPredefines[I];
431 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000432 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
433 return true;
434 }
435
436 // This is an extra macro definition. Determine the name of the
437 // macro we're defining.
438 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000439 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000440 = Extra.find_first_of("( \n\r", StartOfMacroName);
441 assert(EndOfMacroName != std::string::npos &&
442 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000443 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000444
445 // Check whether this name was used somewhere in the PCH file. If
446 // so, defining it as a macro could change behavior, so we reject
447 // the PCH file.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000448 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar045c92f2009-11-11 00:52:00 +0000449 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000450 return true;
451 }
452
453 // Add this definition to the suggested predefines buffer.
454 SuggestedPredefines += Extra;
455 SuggestedPredefines += '\n';
456 }
457
458 // If we get here, it's because the predefines buffer had compatible
459 // contents. Accept the PCH file.
460 return false;
461}
462
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000463void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
464 unsigned ID) {
465 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
466 ++NumHeaderInfos;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000467}
468
469void PCHValidator::ReadCounter(unsigned Value) {
470 PP.setCounterValue(Value);
471}
472
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000473//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +0000474// AST reader implementation
Douglas Gregora868bbd2009-04-21 22:25:48 +0000475//===----------------------------------------------------------------------===//
476
Sebastian Redl07a89a82010-07-30 00:29:29 +0000477void
Sebastian Redl3e31c722010-08-18 23:56:56 +0000478ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redl07a89a82010-07-30 00:29:29 +0000479 DeserializationListener = Listener;
Sebastian Redl07a89a82010-07-30 00:29:29 +0000480}
481
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000482
Douglas Gregora868bbd2009-04-21 22:25:48 +0000483namespace {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000484class ASTSelectorLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000485 ASTReader &Reader;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000486
487public:
Sebastian Redl834bb972010-08-04 17:20:04 +0000488 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +0000489 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +0000490 ObjCMethodList Instance, Factory;
491 };
Douglas Gregorc78d3462009-04-24 21:10:55 +0000492
493 typedef Selector external_key_type;
494 typedef external_key_type internal_key_type;
495
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000496 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump11289f42009-09-09 15:08:12 +0000497
Douglas Gregorc78d3462009-04-24 21:10:55 +0000498 static bool EqualKey(const internal_key_type& a,
499 const internal_key_type& b) {
500 return a == b;
501 }
Mike Stump11289f42009-09-09 15:08:12 +0000502
Douglas Gregorc78d3462009-04-24 21:10:55 +0000503 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +0000504 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000505 }
Mike Stump11289f42009-09-09 15:08:12 +0000506
Douglas Gregorc78d3462009-04-24 21:10:55 +0000507 // This hopefully will just get inlined and removed by the optimizer.
508 static const internal_key_type&
509 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000510
Douglas Gregorc78d3462009-04-24 21:10:55 +0000511 static std::pair<unsigned, unsigned>
512 ReadKeyDataLength(const unsigned char*& d) {
513 using namespace clang::io;
514 unsigned KeyLen = ReadUnalignedLE16(d);
515 unsigned DataLen = ReadUnalignedLE16(d);
516 return std::make_pair(KeyLen, DataLen);
517 }
Mike Stump11289f42009-09-09 15:08:12 +0000518
Douglas Gregor95c13f52009-04-25 17:48:32 +0000519 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000520 using namespace clang::io;
Chris Lattner8575daa2009-04-27 21:45:14 +0000521 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000522 unsigned N = ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000523 IdentifierInfo *FirstII
Douglas Gregorc78d3462009-04-24 21:10:55 +0000524 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
525 if (N == 0)
526 return SelTable.getNullarySelector(FirstII);
527 else if (N == 1)
528 return SelTable.getUnarySelector(FirstII);
529
530 llvm::SmallVector<IdentifierInfo *, 16> Args;
531 Args.push_back(FirstII);
532 for (unsigned I = 1; I != N; ++I)
533 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
534
Douglas Gregor038c3382009-05-22 22:45:36 +0000535 return SelTable.getSelector(N, Args.data());
Douglas Gregorc78d3462009-04-24 21:10:55 +0000536 }
Mike Stump11289f42009-09-09 15:08:12 +0000537
Douglas Gregorc78d3462009-04-24 21:10:55 +0000538 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
539 using namespace clang::io;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000540
541 data_type Result;
542
Sebastian Redl834bb972010-08-04 17:20:04 +0000543 Result.ID = ReadUnalignedLE32(d);
544 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
545 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
546
Douglas Gregorc78d3462009-04-24 21:10:55 +0000547 // Load instance methods
548 ObjCMethodList *Prev = 0;
549 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000550 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000551 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000552 if (!Result.Instance.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000553 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000554 Result.Instance.Method = Method;
555 Prev = &Result.Instance;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000556 continue;
557 }
558
Ted Kremenekda4abf12010-02-11 00:53:01 +0000559 ObjCMethodList *Mem =
560 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
561 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000562 Prev = Prev->Next;
563 }
564
565 // Load factory methods
566 Prev = 0;
567 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000568 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000569 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000570 if (!Result.Factory.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000571 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000572 Result.Factory.Method = Method;
573 Prev = &Result.Factory;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000574 continue;
575 }
576
Ted Kremenekda4abf12010-02-11 00:53:01 +0000577 ObjCMethodList *Mem =
578 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
579 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000580 Prev = Prev->Next;
581 }
582
583 return Result;
584 }
585};
Mike Stump11289f42009-09-09 15:08:12 +0000586
587} // end anonymous namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +0000588
589/// \brief The on-disk hash table used for the global method pool.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000590typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
591 ASTSelectorLookupTable;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000592
Sebastian Redl2c373b92010-10-05 15:59:54 +0000593namespace clang {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000594class ASTIdentifierLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000595 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +0000596 ASTReader::PerFileData &F;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000597
598 // If we know the IdentifierInfo in advance, it is here and we will
599 // not build a new one. Used when deserializing information about an
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000600 // identifier that was constructed before the AST file was read.
Douglas Gregora868bbd2009-04-21 22:25:48 +0000601 IdentifierInfo *KnownII;
602
603public:
604 typedef IdentifierInfo * data_type;
605
606 typedef const std::pair<const char*, unsigned> external_key_type;
607
608 typedef external_key_type internal_key_type;
609
Sebastian Redl2c373b92010-10-05 15:59:54 +0000610 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000611 IdentifierInfo *II = 0)
Sebastian Redl2c373b92010-10-05 15:59:54 +0000612 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump11289f42009-09-09 15:08:12 +0000613
Douglas Gregora868bbd2009-04-21 22:25:48 +0000614 static bool EqualKey(const internal_key_type& a,
615 const internal_key_type& b) {
616 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
617 : false;
618 }
Mike Stump11289f42009-09-09 15:08:12 +0000619
Douglas Gregora868bbd2009-04-21 22:25:48 +0000620 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000621 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregora868bbd2009-04-21 22:25:48 +0000622 }
Mike Stump11289f42009-09-09 15:08:12 +0000623
Douglas Gregora868bbd2009-04-21 22:25:48 +0000624 // This hopefully will just get inlined and removed by the optimizer.
625 static const internal_key_type&
626 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000627
Douglas Gregor57756ea2010-10-14 22:11:03 +0000628 // This hopefully will just get inlined and removed by the optimizer.
629 static const external_key_type&
630 GetExternalKey(const internal_key_type& x) { return x; }
631
Douglas Gregora868bbd2009-04-21 22:25:48 +0000632 static std::pair<unsigned, unsigned>
633 ReadKeyDataLength(const unsigned char*& d) {
634 using namespace clang::io;
Douglas Gregor6b7bf5a2009-04-25 20:26:24 +0000635 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor5287b4e2009-04-25 21:04:17 +0000636 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000637 return std::make_pair(KeyLen, DataLen);
638 }
Mike Stump11289f42009-09-09 15:08:12 +0000639
Douglas Gregora868bbd2009-04-21 22:25:48 +0000640 static std::pair<const char*, unsigned>
641 ReadKey(const unsigned char* d, unsigned n) {
642 assert(n >= 2 && d[n-1] == '\0');
643 return std::make_pair((const char*) d, n-1);
644 }
Mike Stump11289f42009-09-09 15:08:12 +0000645
646 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregora868bbd2009-04-21 22:25:48 +0000647 const unsigned char* d,
648 unsigned DataLen) {
649 using namespace clang::io;
Sebastian Redl539c5062010-08-18 23:57:32 +0000650 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000651 bool IsInteresting = ID & 0x01;
652
653 // Wipe out the "is interesting" bit.
654 ID = ID >> 1;
655
656 if (!IsInteresting) {
Sebastian Redl98912122010-07-27 23:01:28 +0000657 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregor1d583f22009-04-28 21:18:29 +0000658 // and associate it with the persistent ID.
659 IdentifierInfo *II = KnownII;
660 if (!II)
Jay Foad9a6b0982011-06-21 15:13:30 +0000661 II = &Reader.getIdentifierTable().getOwn(llvm::StringRef(k.first,
662 k.second));
Douglas Gregor1d583f22009-04-28 21:18:29 +0000663 Reader.SetIdentifierInfo(ID, II);
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000664 II->setIsFromAST();
Douglas Gregor1d583f22009-04-28 21:18:29 +0000665 return II;
666 }
667
Douglas Gregorb9256522009-04-28 21:32:13 +0000668 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000669 bool CPlusPlusOperatorKeyword = Bits & 0x01;
670 Bits >>= 1;
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000671 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
672 Bits >>= 1;
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000673 bool Poisoned = Bits & 0x01;
674 Bits >>= 1;
675 bool ExtensionToken = Bits & 0x01;
676 Bits >>= 1;
677 bool hasMacroDefinition = Bits & 0x01;
678 Bits >>= 1;
679 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
680 Bits >>= 10;
Mike Stump11289f42009-09-09 15:08:12 +0000681
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000682 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorb9256522009-04-28 21:32:13 +0000683 DataLen -= 6;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000684
685 // Build the IdentifierInfo itself and link the identifier ID with
686 // the new IdentifierInfo.
687 IdentifierInfo *II = KnownII;
688 if (!II)
Jay Foad9a6b0982011-06-21 15:13:30 +0000689 II = &Reader.getIdentifierTable().getOwn(llvm::StringRef(k.first,
690 k.second));
Douglas Gregora868bbd2009-04-21 22:25:48 +0000691 Reader.SetIdentifierInfo(ID, II);
692
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000693 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000694 // Token IDs are read-only.
695 if (HasRevertedTokenIDToIdentifier)
696 II->RevertTokenIDToIdentifier();
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000697 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump11289f42009-09-09 15:08:12 +0000698 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000699 "Incorrect extension token flag");
700 (void)ExtensionToken;
701 II->setIsPoisoned(Poisoned);
702 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
703 "Incorrect C++ operator keyword flag");
704 (void)CPlusPlusOperatorKeyword;
705
Douglas Gregorc3366a52009-04-21 23:56:24 +0000706 // If this identifier is a macro, deserialize the macro
707 // definition.
708 if (hasMacroDefinition) {
Douglas Gregorb9256522009-04-28 21:32:13 +0000709 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor5ef9e332010-10-30 00:23:06 +0000710 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregorb9256522009-04-28 21:32:13 +0000711 DataLen -= 4;
Douglas Gregorc3366a52009-04-21 23:56:24 +0000712 }
Douglas Gregora868bbd2009-04-21 22:25:48 +0000713
714 // Read all of the declarations visible at global scope with this
715 // name.
Chris Lattner1d728882009-04-27 22:17:41 +0000716 if (Reader.getContext() == 0) return II;
Douglas Gregor1342e842009-07-06 18:54:52 +0000717 if (DataLen > 0) {
718 llvm::SmallVector<uint32_t, 4> DeclIDs;
719 for (; DataLen > 0; DataLen -= 4)
720 DeclIDs.push_back(ReadUnalignedLE32(d));
721 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000722 }
Mike Stump11289f42009-09-09 15:08:12 +0000723
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000724 II->setIsFromAST();
Douglas Gregora868bbd2009-04-21 22:25:48 +0000725 return II;
726 }
727};
Mike Stump11289f42009-09-09 15:08:12 +0000728
729} // end anonymous namespace
Douglas Gregora868bbd2009-04-21 22:25:48 +0000730
731/// \brief The on-disk hash table used to contain information about
732/// all of the identifiers in the program.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000733typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
734 ASTIdentifierLookupTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000735
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000736namespace {
737class ASTDeclContextNameLookupTrait {
738 ASTReader &Reader;
739
740public:
741 /// \brief Pair of begin/end iterators for DeclIDs.
742 typedef std::pair<DeclID *, DeclID *> data_type;
743
744 /// \brief Special internal key for declaration names.
745 /// The hash table creates keys for comparison; we do not create
746 /// a DeclarationName for the internal key to avoid deserializing types.
747 struct DeclNameKey {
748 DeclarationName::NameKind Kind;
749 uint64_t Data;
750 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
751 };
752
753 typedef DeclarationName external_key_type;
754 typedef DeclNameKey internal_key_type;
755
756 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
757
758 static bool EqualKey(const internal_key_type& a,
759 const internal_key_type& b) {
760 return a.Kind == b.Kind && a.Data == b.Data;
761 }
762
763 unsigned ComputeHash(const DeclNameKey &Key) const {
764 llvm::FoldingSetNodeID ID;
765 ID.AddInteger(Key.Kind);
766
767 switch (Key.Kind) {
768 case DeclarationName::Identifier:
769 case DeclarationName::CXXLiteralOperatorName:
770 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
771 break;
772 case DeclarationName::ObjCZeroArgSelector:
773 case DeclarationName::ObjCOneArgSelector:
774 case DeclarationName::ObjCMultiArgSelector:
775 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
776 break;
777 case DeclarationName::CXXConstructorName:
778 case DeclarationName::CXXDestructorName:
779 case DeclarationName::CXXConversionFunctionName:
780 ID.AddInteger((TypeID)Key.Data);
781 break;
782 case DeclarationName::CXXOperatorName:
783 ID.AddInteger((OverloadedOperatorKind)Key.Data);
784 break;
785 case DeclarationName::CXXUsingDirective:
786 break;
787 }
788
789 return ID.ComputeHash();
790 }
791
792 internal_key_type GetInternalKey(const external_key_type& Name) const {
793 DeclNameKey Key;
794 Key.Kind = Name.getNameKind();
795 switch (Name.getNameKind()) {
796 case DeclarationName::Identifier:
797 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
798 break;
799 case DeclarationName::ObjCZeroArgSelector:
800 case DeclarationName::ObjCOneArgSelector:
801 case DeclarationName::ObjCMultiArgSelector:
802 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
803 break;
804 case DeclarationName::CXXConstructorName:
805 case DeclarationName::CXXDestructorName:
806 case DeclarationName::CXXConversionFunctionName:
807 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
808 break;
809 case DeclarationName::CXXOperatorName:
810 Key.Data = Name.getCXXOverloadedOperator();
811 break;
812 case DeclarationName::CXXLiteralOperatorName:
813 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
814 break;
815 case DeclarationName::CXXUsingDirective:
816 break;
817 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000818
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000819 return Key;
820 }
821
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000822 external_key_type GetExternalKey(const internal_key_type& Key) const {
823 ASTContext *Context = Reader.getContext();
824 switch (Key.Kind) {
825 case DeclarationName::Identifier:
826 return DeclarationName((IdentifierInfo*)Key.Data);
827
828 case DeclarationName::ObjCZeroArgSelector:
829 case DeclarationName::ObjCOneArgSelector:
830 case DeclarationName::ObjCMultiArgSelector:
831 return DeclarationName(Selector(Key.Data));
832
833 case DeclarationName::CXXConstructorName:
834 return Context->DeclarationNames.getCXXConstructorName(
835 Context->getCanonicalType(Reader.GetType(Key.Data)));
836
837 case DeclarationName::CXXDestructorName:
838 return Context->DeclarationNames.getCXXDestructorName(
839 Context->getCanonicalType(Reader.GetType(Key.Data)));
840
841 case DeclarationName::CXXConversionFunctionName:
842 return Context->DeclarationNames.getCXXConversionFunctionName(
843 Context->getCanonicalType(Reader.GetType(Key.Data)));
844
845 case DeclarationName::CXXOperatorName:
846 return Context->DeclarationNames.getCXXOperatorName(
847 (OverloadedOperatorKind)Key.Data);
848
849 case DeclarationName::CXXLiteralOperatorName:
850 return Context->DeclarationNames.getCXXLiteralOperatorName(
851 (IdentifierInfo*)Key.Data);
852
853 case DeclarationName::CXXUsingDirective:
854 return DeclarationName::getUsingDirectiveName();
855 }
856
857 llvm_unreachable("Invalid Name Kind ?");
858 }
859
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000860 static std::pair<unsigned, unsigned>
861 ReadKeyDataLength(const unsigned char*& d) {
862 using namespace clang::io;
863 unsigned KeyLen = ReadUnalignedLE16(d);
864 unsigned DataLen = ReadUnalignedLE16(d);
865 return std::make_pair(KeyLen, DataLen);
866 }
867
868 internal_key_type ReadKey(const unsigned char* d, unsigned) {
869 using namespace clang::io;
870
871 DeclNameKey Key;
872 Key.Kind = (DeclarationName::NameKind)*d++;
873 switch (Key.Kind) {
874 case DeclarationName::Identifier:
875 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
876 break;
877 case DeclarationName::ObjCZeroArgSelector:
878 case DeclarationName::ObjCOneArgSelector:
879 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000880 Key.Data =
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000881 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
882 break;
883 case DeclarationName::CXXConstructorName:
884 case DeclarationName::CXXDestructorName:
885 case DeclarationName::CXXConversionFunctionName:
886 Key.Data = ReadUnalignedLE32(d); // TypeID
887 break;
888 case DeclarationName::CXXOperatorName:
889 Key.Data = *d++; // OverloadedOperatorKind
890 break;
891 case DeclarationName::CXXLiteralOperatorName:
892 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
893 break;
894 case DeclarationName::CXXUsingDirective:
895 break;
896 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000897
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000898 return Key;
899 }
900
901 data_type ReadData(internal_key_type, const unsigned char* d,
902 unsigned DataLen) {
903 using namespace clang::io;
904 unsigned NumDecls = ReadUnalignedLE16(d);
905 DeclID *Start = (DeclID *)d;
906 return std::make_pair(Start, Start + NumDecls);
907 }
908};
909
910} // end anonymous namespace
911
912/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
913typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
914 ASTDeclContextNameLookupTable;
915
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000916bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
917 const std::pair<uint64_t, uint64_t> &Offsets,
918 DeclContextInfo &Info) {
919 SavedStreamPosition SavedPosition(Cursor);
920 // First the lexical decls.
921 if (Offsets.first != 0) {
922 Cursor.JumpToBit(Offsets.first);
923
924 RecordData Record;
925 const char *Blob;
926 unsigned BlobLen;
927 unsigned Code = Cursor.ReadCode();
928 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
929 if (RecCode != DECL_CONTEXT_LEXICAL) {
930 Error("Expected lexical block");
931 return true;
932 }
933
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000934 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
935 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000936 } else {
937 Info.LexicalDecls = 0;
938 Info.NumLexicalDecls = 0;
939 }
940
941 // Now the lookup table.
942 if (Offsets.second != 0) {
943 Cursor.JumpToBit(Offsets.second);
944
945 RecordData Record;
946 const char *Blob;
947 unsigned BlobLen;
948 unsigned Code = Cursor.ReadCode();
949 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
950 if (RecCode != DECL_CONTEXT_VISIBLE) {
951 Error("Expected visible lookup table block");
952 return true;
953 }
954 Info.NameLookupTableData
955 = ASTDeclContextNameLookupTable::Create(
956 (const unsigned char *)Blob + Record[0],
957 (const unsigned char *)Blob,
958 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl9d8f58b2010-08-24 00:50:00 +0000959 } else {
960 Info.NameLookupTableData = 0;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000961 }
962
963 return false;
964}
965
Argyrios Kyrtzidisdaa41f52011-04-25 22:23:56 +0000966void ASTReader::Error(llvm::StringRef Msg) {
967 Error(diag::err_fe_pch_malformed, Msg);
968}
969
970void ASTReader::Error(unsigned DiagID,
971 llvm::StringRef Arg1, llvm::StringRef Arg2) {
972 if (Diags.isDiagnosticInFlight())
973 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
974 else
975 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000976}
977
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000978/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000979bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000980 if (Listener)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000981 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000982 ActualOriginalFileName,
Nick Lewycky36079892011-02-23 21:16:44 +0000983 SuggestedPredefines,
984 FileMgr);
Douglas Gregorc379c072009-04-28 18:58:38 +0000985 return false;
Douglas Gregor92863e42009-04-10 23:10:45 +0000986}
987
Douglas Gregorc5046832009-04-27 18:38:38 +0000988//===----------------------------------------------------------------------===//
989// Source Manager Deserialization
990//===----------------------------------------------------------------------===//
991
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000992/// \brief Read the line table in the source manager block.
Sebastian Redl2c373b92010-10-05 15:59:54 +0000993/// \returns true if there was an error.
994bool ASTReader::ParseLineTable(PerFileData &F,
995 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000996 unsigned Idx = 0;
997 LineTableInfo &LineTable = SourceMgr.getLineTable();
998
999 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +00001000 std::map<int, int> FileIDs;
1001 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001002 // Extract the file name
1003 unsigned FilenameLen = Record[Idx++];
1004 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1005 Idx += FilenameLen;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001006 MaybeAddSystemRootToFilename(Filename);
Jay Foad9a6b0982011-06-21 15:13:30 +00001007 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001008 }
1009
1010 // Parse the line entries
1011 std::vector<LineEntry> Entries;
1012 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +00001013 int FID = Record[Idx++];
Douglas Gregor925296b2011-07-19 16:10:42 +00001014 assert(FID >= 0 && "Serialized line entries for non-local file.");
1015 // Remap FileID from 1-based old view.
1016 FID += F.SLocEntryBaseID - 1;
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001017
1018 // Extract the line entries
1019 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +00001020 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001021 Entries.clear();
1022 Entries.reserve(NumEntries);
1023 for (unsigned I = 0; I != NumEntries; ++I) {
1024 unsigned FileOffset = Record[Idx++];
1025 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +00001026 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +00001027 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001028 = (SrcMgr::CharacteristicKind)Record[Idx++];
1029 unsigned IncludeOffset = Record[Idx++];
1030 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1031 FileKind, IncludeOffset));
1032 }
1033 LineTable.AddEntry(FID, Entries);
1034 }
1035
1036 return false;
1037}
1038
Douglas Gregorc5046832009-04-27 18:38:38 +00001039namespace {
1040
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001041class ASTStatData {
Douglas Gregorc5046832009-04-27 18:38:38 +00001042public:
Douglas Gregorc5046832009-04-27 18:38:38 +00001043 const ino_t ino;
1044 const dev_t dev;
1045 const mode_t mode;
1046 const time_t mtime;
1047 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +00001048
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001049 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner2a6fa472010-11-23 19:28:12 +00001050 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregorc5046832009-04-27 18:38:38 +00001051};
1052
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001053class ASTStatLookupTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001054 public:
1055 typedef const char *external_key_type;
1056 typedef const char *internal_key_type;
1057
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001058 typedef ASTStatData data_type;
Douglas Gregorc5046832009-04-27 18:38:38 +00001059
1060 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001061 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001062 }
1063
1064 static internal_key_type GetInternalKey(const char *path) { return path; }
1065
1066 static bool EqualKey(internal_key_type a, internal_key_type b) {
1067 return strcmp(a, b) == 0;
1068 }
1069
1070 static std::pair<unsigned, unsigned>
1071 ReadKeyDataLength(const unsigned char*& d) {
1072 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1073 unsigned DataLen = (unsigned) *d++;
1074 return std::make_pair(KeyLen + 1, DataLen);
1075 }
1076
1077 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1078 return (const char *)d;
1079 }
1080
1081 static data_type ReadData(const internal_key_type, const unsigned char *d,
1082 unsigned /*DataLen*/) {
1083 using namespace clang::io;
1084
Douglas Gregorc5046832009-04-27 18:38:38 +00001085 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1086 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1087 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +00001088 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +00001089 off_t size = (off_t) ReadUnalignedLE64(d);
1090 return data_type(ino, dev, mode, mtime, size);
1091 }
1092};
1093
1094/// \brief stat() cache for precompiled headers.
1095///
1096/// This cache is very similar to the stat cache used by pretokenized
1097/// headers.
Chris Lattner226efd32010-11-23 19:19:34 +00001098class ASTStatCache : public FileSystemStatCache {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001099 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregorc5046832009-04-27 18:38:38 +00001100 CacheTy *Cache;
1101
1102 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +00001103public:
Chris Lattner2a6fa472010-11-23 19:28:12 +00001104 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1105 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +00001106 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1107 Cache = CacheTy::Create(Buckets, Base);
1108 }
1109
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001110 ~ASTStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +00001111
Chris Lattnerdd278432010-11-23 21:17:56 +00001112 LookupResult getStat(const char *Path, struct stat &StatBuf,
1113 int *FileDescriptor) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001114 // Do the lookup for the file's data in the AST file.
Chris Lattner226efd32010-11-23 19:19:34 +00001115 CacheTy::iterator I = Cache->find(Path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001116
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001117 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregorc5046832009-04-27 18:38:38 +00001118 if (I == Cache->end()) {
1119 ++NumStatMisses;
Chris Lattnerdd278432010-11-23 21:17:56 +00001120 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregorc5046832009-04-27 18:38:38 +00001121 }
Mike Stump11289f42009-09-09 15:08:12 +00001122
Douglas Gregorc5046832009-04-27 18:38:38 +00001123 ++NumStatHits;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001124 ASTStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001125
Chris Lattner226efd32010-11-23 19:19:34 +00001126 StatBuf.st_ino = Data.ino;
1127 StatBuf.st_dev = Data.dev;
1128 StatBuf.st_mtime = Data.mtime;
1129 StatBuf.st_mode = Data.mode;
1130 StatBuf.st_size = Data.size;
Chris Lattner8f0583d2010-11-23 20:05:15 +00001131 return CacheExists;
Douglas Gregorc5046832009-04-27 18:38:38 +00001132 }
1133};
1134} // end anonymous namespace
1135
1136
Sebastian Redl393f8b72010-07-19 20:52:06 +00001137/// \brief Read a source manager block
Sebastian Redl2c499f62010-08-18 23:56:43 +00001138ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001139 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +00001140
Sebastian Redl393f8b72010-07-19 20:52:06 +00001141 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001142
Douglas Gregor258ae542009-04-27 06:38:32 +00001143 // Set the source-location entry cursor to the current position in
1144 // the stream. This cursor will be used to read the contents of the
1145 // source manager block initially, and then lazily read
1146 // source-location entries as needed.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001147 SLocEntryCursor = F.Stream;
Douglas Gregor258ae542009-04-27 06:38:32 +00001148
1149 // The stream itself is going to skip over the source manager block.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001150 if (F.Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001151 Error("malformed block record in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001152 return Failure;
1153 }
1154
1155 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001156 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001157 Error("malformed source manager block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001158 return Failure;
1159 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001160
Douglas Gregora7f71a92009-04-10 03:52:48 +00001161 RecordData Record;
1162 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001163 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001164 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001165 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001166 Error("error at end of Source Manager block in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001167 return Failure;
1168 }
Douglas Gregor92863e42009-04-10 23:10:45 +00001169 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001170 }
Mike Stump11289f42009-09-09 15:08:12 +00001171
Douglas Gregora7f71a92009-04-10 03:52:48 +00001172 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1173 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +00001174 SLocEntryCursor.ReadSubBlockID();
1175 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001176 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001177 return Failure;
1178 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001179 continue;
1180 }
Mike Stump11289f42009-09-09 15:08:12 +00001181
Douglas Gregora7f71a92009-04-10 03:52:48 +00001182 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001183 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001184 continue;
1185 }
Mike Stump11289f42009-09-09 15:08:12 +00001186
Douglas Gregora7f71a92009-04-10 03:52:48 +00001187 // Read a record.
1188 const char *BlobStart;
1189 unsigned BlobLen;
1190 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +00001191 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001192 default: // Default behavior: ignore.
1193 break;
1194
Sebastian Redl539c5062010-08-18 23:57:32 +00001195 case SM_SLOC_FILE_ENTRY:
1196 case SM_SLOC_BUFFER_ENTRY:
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001197 case SM_SLOC_EXPANSION_ENTRY:
Douglas Gregor258ae542009-04-27 06:38:32 +00001198 // Once we hit one of the source location entries, we're done.
1199 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001200 }
1201 }
1202}
1203
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001204/// \brief If a header file is not found at the path that we expect it to be
1205/// and the PCH file was moved from its original location, try to resolve the
1206/// file by assuming that header+PCH were moved together and the header is in
1207/// the same place relative to the PCH.
1208static std::string
1209resolveFileRelativeToOriginalDir(const std::string &Filename,
1210 const std::string &OriginalDir,
1211 const std::string &CurrDir) {
1212 assert(OriginalDir != CurrDir &&
1213 "No point trying to resolve the file if the PCH dir didn't change");
1214 using namespace llvm::sys;
1215 llvm::SmallString<128> filePath(Filename);
1216 fs::make_absolute(filePath);
1217 assert(path::is_absolute(OriginalDir));
1218 llvm::SmallString<128> currPCHPath(CurrDir);
1219
1220 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1221 fileDirE = path::end(path::parent_path(filePath));
1222 path::const_iterator origDirI = path::begin(OriginalDir),
1223 origDirE = path::end(OriginalDir);
1224 // Skip the common path components from filePath and OriginalDir.
1225 while (fileDirI != fileDirE && origDirI != origDirE &&
1226 *fileDirI == *origDirI) {
1227 ++fileDirI;
1228 ++origDirI;
1229 }
1230 for (; origDirI != origDirE; ++origDirI)
1231 path::append(currPCHPath, "..");
1232 path::append(currPCHPath, fileDirI, fileDirE);
1233 path::append(currPCHPath, path::filename(Filename));
1234 return currPCHPath.str();
1235}
1236
Douglas Gregor258ae542009-04-27 06:38:32 +00001237/// \brief Read in the source location entry with the given ID.
Douglas Gregor925296b2011-07-19 16:10:42 +00001238ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(int ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001239 if (ID == 0)
1240 return Success;
1241
Douglas Gregor925296b2011-07-19 16:10:42 +00001242 if (unsigned(-ID) - 2 >= TotalNumSLocEntries || ID > 0) {
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
Douglas Gregor925296b2011-07-19 16:10:42 +00001247 PerFileData *F = GlobalSLocEntryMap.find(-ID)->second;
1248 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00001249 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Douglas Gregor925296b2011-07-19 16:10:42 +00001250 unsigned BaseOffset = F->SLocEntryBaseOffset;
Sebastian Redl34522812010-07-16 17:50:48 +00001251
Douglas Gregor258ae542009-04-27 06:38:32 +00001252 ++NumSLocEntriesRead;
Douglas Gregor258ae542009-04-27 06:38:32 +00001253 unsigned Code = SLocEntryCursor.ReadCode();
1254 if (Code == llvm::bitc::END_BLOCK ||
1255 Code == llvm::bitc::ENTER_SUBBLOCK ||
1256 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001257 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001258 return Failure;
1259 }
1260
Douglas Gregor258ae542009-04-27 06:38:32 +00001261 RecordData Record;
1262 const char *BlobStart;
1263 unsigned BlobLen;
1264 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1265 default:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001266 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001267 return Failure;
1268
Sebastian Redl539c5062010-08-18 23:57:32 +00001269 case SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001270 std::string Filename(BlobStart, BlobStart + BlobLen);
1271 MaybeAddSystemRootToFilename(Filename);
Chris Lattner5159f612010-11-23 08:35:12 +00001272 const FileEntry *File = FileMgr.getFile(Filename);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001273 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1274 OriginalDir != CurrentDir) {
1275 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1276 OriginalDir,
1277 CurrentDir);
1278 if (!resolved.empty())
1279 File = FileMgr.getFile(resolved);
1280 }
Axel Naumann63fbaed2011-01-27 10:55:51 +00001281 if (File == 0)
1282 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1283 (time_t)Record[5]);
Chris Lattnerd20dc872009-06-15 04:35:16 +00001284 if (File == 0) {
1285 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001286 ErrorStr += Filename;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001287 ErrorStr += "' referenced by AST file";
Chris Lattnerd20dc872009-06-15 04:35:16 +00001288 Error(ErrorStr.c_str());
1289 return Failure;
1290 }
Mike Stump11289f42009-09-09 15:08:12 +00001291
Douglas Gregor09b69892011-02-10 17:09:37 +00001292 if (Record.size() < 6) {
Ted Kremenekabb1ddd2010-03-18 21:23:05 +00001293 Error("source location entry is incorrect");
1294 return Failure;
1295 }
1296
Douglas Gregorce3a8292010-07-27 00:27:13 +00001297 if (!DisableValidation &&
1298 ((off_t)Record[4] != File->getSize()
Douglas Gregor08288f22010-04-09 15:54:22 +00001299#if !defined(LLVM_ON_WIN32)
1300 // In our regression testing, the Windows file system seems to
1301 // have inconsistent modification times that sometimes
1302 // erroneously trigger this error-handling path.
Douglas Gregorce3a8292010-07-27 00:27:13 +00001303 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor08288f22010-04-09 15:54:22 +00001304#endif
Douglas Gregorce3a8292010-07-27 00:27:13 +00001305 )) {
Argyrios Kyrtzidisdaa41f52011-04-25 22:23:56 +00001306 Error(diag::err_fe_pch_file_modified, Filename);
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001307 return Failure;
1308 }
1309
Douglas Gregor925296b2011-07-19 16:10:42 +00001310 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1311 if (IncludeLoc.isInvalid() && F->Type != MainFile) {
1312 // This is the module's main file.
1313 IncludeLoc = getImportLocation(F);
1314 }
1315 FileID FID = SourceMgr.createFileID(File, IncludeLoc,
Chris Lattner26b5c192010-11-23 09:19:42 +00001316 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor925296b2011-07-19 16:10:42 +00001317 ID, BaseOffset + Record[0]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001318 if (Record[3])
1319 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1320 .setHasLineDirectives();
Douglas Gregor09b69892011-02-10 17:09:37 +00001321
Douglas Gregor258ae542009-04-27 06:38:32 +00001322 break;
1323 }
1324
Sebastian Redl539c5062010-08-18 23:57:32 +00001325 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor258ae542009-04-27 06:38:32 +00001326 const char *Name = BlobStart;
1327 unsigned Offset = Record[0];
1328 unsigned Code = SLocEntryCursor.ReadCode();
1329 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001330 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +00001331 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001332
Sebastian Redl539c5062010-08-18 23:57:32 +00001333 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001334 Error("AST record has invalid code");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001335 return Failure;
1336 }
1337
Douglas Gregor258ae542009-04-27 06:38:32 +00001338 llvm::MemoryBuffer *Buffer
Chris Lattner58c79342010-04-05 22:42:27 +00001339 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1340 Name);
Douglas Gregor925296b2011-07-19 16:10:42 +00001341 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID,
1342 BaseOffset + Offset);
Mike Stump11289f42009-09-09 15:08:12 +00001343
Douglas Gregore6648fb2009-04-28 20:33:11 +00001344 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001345 PCHPredefinesBlock Block = {
1346 BufferID,
1347 llvm::StringRef(BlobStart, BlobLen - 1)
1348 };
1349 PCHPredefinesBuffers.push_back(Block);
Douglas Gregore6648fb2009-04-28 20:33:11 +00001350 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001351
1352 break;
1353 }
1354
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001355 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001356 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001357 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001358 ReadSourceLocation(*F, Record[2]),
1359 ReadSourceLocation(*F, Record[3]),
Douglas Gregor258ae542009-04-27 06:38:32 +00001360 Record[4],
1361 ID,
Douglas Gregor925296b2011-07-19 16:10:42 +00001362 BaseOffset + Record[0]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001363 break;
Mike Stump11289f42009-09-09 15:08:12 +00001364 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001365 }
1366
1367 return Success;
1368}
1369
Douglas Gregor925296b2011-07-19 16:10:42 +00001370/// \brief Find the location where the module F is imported.
1371SourceLocation ASTReader::getImportLocation(PerFileData *F) {
1372 if (F->ImportLoc.isValid())
1373 return F->ImportLoc;
1374 // Otherwise we have a PCH. It's considered to be "imported" at the first
1375 // location of its includer.
1376 if (F->Loaders.empty() || !F->Loaders[0]) {
1377 // Main file is the importer. We assume that it is the first entry in the
1378 // entry table. We can't ask the manager, because at the time of PCH loading
1379 // the main file entry doesn't exist yet.
1380 // The very first entry is the invalid instantiation loc, which takes up
1381 // offsets 0 and 1.
1382 return SourceLocation::getFromRawEncoding(2U);
1383 }
1384 return F->Loaders[0]->FirstLoc;
1385}
1386
Chris Lattnere78a6be2009-04-27 01:05:14 +00001387/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1388/// specified cursor. Read the abbreviations that are at the top of the block
1389/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001390bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001391 unsigned BlockID) {
1392 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001393 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001394 return Failure;
1395 }
Mike Stump11289f42009-09-09 15:08:12 +00001396
Chris Lattnere78a6be2009-04-27 01:05:14 +00001397 while (true) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001398 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattnere78a6be2009-04-27 01:05:14 +00001399 unsigned Code = Cursor.ReadCode();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001400
Chris Lattnere78a6be2009-04-27 01:05:14 +00001401 // We expect all abbrevs to be at the start of the block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001402 if (Code != llvm::bitc::DEFINE_ABBREV) {
1403 Cursor.JumpToBit(Offset);
Chris Lattnere78a6be2009-04-27 01:05:14 +00001404 return false;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001405 }
Chris Lattnere78a6be2009-04-27 01:05:14 +00001406 Cursor.ReadAbbrevRecord();
1407 }
1408}
1409
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001410PreprocessedEntity *ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001411 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregor796d76a2010-10-20 22:00:55 +00001412 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump11289f42009-09-09 15:08:12 +00001413
Douglas Gregorc3366a52009-04-21 23:56:24 +00001414 // Keep track of where we are in the stream, then jump back there
1415 // after reading this macro.
1416 SavedStreamPosition SavedPosition(Stream);
1417
1418 Stream.JumpToBit(Offset);
1419 RecordData Record;
1420 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1421 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001422
Douglas Gregorc3366a52009-04-21 23:56:24 +00001423 while (true) {
1424 unsigned Code = Stream.ReadCode();
1425 switch (Code) {
1426 case llvm::bitc::END_BLOCK:
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001427 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001428
1429 case llvm::bitc::ENTER_SUBBLOCK:
1430 // No known subblocks, always skip them.
1431 Stream.ReadSubBlockID();
1432 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001433 Error("malformed block record in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001434 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001435 }
1436 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001437
Douglas Gregorc3366a52009-04-21 23:56:24 +00001438 case llvm::bitc::DEFINE_ABBREV:
1439 Stream.ReadAbbrevRecord();
1440 continue;
1441 default: break;
1442 }
1443
1444 // Read a record.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001445 const char *BlobStart = 0;
1446 unsigned BlobLen = 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001447 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001448 PreprocessorRecordTypes RecType =
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001449 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001450 BlobLen);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001451 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001452 case PP_MACRO_OBJECT_LIKE:
1453 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001454 // If we already have a macro, that means that we've hit the end
1455 // of the definition of the macro we were looking for. We're
1456 // done.
1457 if (Macro)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001458 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001459
1460 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1461 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001462 Error("macro must have a name in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001463 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001464 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00001465 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001466 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001467
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001468 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001469 MI->setIsUsed(isUsed);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001470 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001471
Douglas Gregoraae92242010-03-19 21:51:54 +00001472 unsigned NextIndex = 3;
Sebastian Redl539c5062010-08-18 23:57:32 +00001473 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001474 // Decode function-like macro info.
1475 bool isC99VarArgs = Record[3];
1476 bool isGNUVarArgs = Record[4];
1477 MacroArgs.clear();
1478 unsigned NumArgs = Record[5];
Douglas Gregoraae92242010-03-19 21:51:54 +00001479 NextIndex = 6 + NumArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001480 for (unsigned i = 0; i != NumArgs; ++i)
1481 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1482
1483 // Install function-like macro info.
1484 MI->setIsFunctionLike();
1485 if (isC99VarArgs) MI->setIsC99Varargs();
1486 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001487 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001488 PP->getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001489 }
1490
1491 // Finally, install the macro.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001492 PP->setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001493
1494 // Remember that we saw this macro last so that we add the tokens that
1495 // form its body to it.
1496 Macro = MI;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001497
Douglas Gregoraae92242010-03-19 21:51:54 +00001498 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1499 // We have a macro definition. Load it now.
1500 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1501 getMacroDefinition(Record[NextIndex]));
1502 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001503
Douglas Gregorc3366a52009-04-21 23:56:24 +00001504 ++NumMacrosRead;
1505 break;
1506 }
Mike Stump11289f42009-09-09 15:08:12 +00001507
Sebastian Redl539c5062010-08-18 23:57:32 +00001508 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001509 // If we see a TOKEN before a PP_MACRO_*, then the file is
1510 // erroneous, just pretend we didn't see this.
1511 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001512
Douglas Gregorc3366a52009-04-21 23:56:24 +00001513 Token Tok;
1514 Tok.startToken();
Sebastian Redl2c373b92010-10-05 15:59:54 +00001515 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001516 Tok.setLength(Record[1]);
1517 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1518 Tok.setIdentifierInfo(II);
1519 Tok.setKind((tok::TokenKind)Record[3]);
1520 Tok.setFlag((Token::TokenFlags)Record[4]);
1521 Macro->AddTokenToBody(Tok);
1522 break;
1523 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001524 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001525 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001526
1527 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001528}
1529
Douglas Gregor92a96f52011-02-08 21:58:10 +00001530PreprocessedEntity *ASTReader::LoadPreprocessedEntity(PerFileData &F) {
1531 assert(PP && "Forgot to set Preprocessor ?");
1532 unsigned Code = F.PreprocessorDetailCursor.ReadCode();
1533 switch (Code) {
1534 case llvm::bitc::END_BLOCK:
1535 return 0;
1536
1537 case llvm::bitc::ENTER_SUBBLOCK:
1538 Error("unexpected subblock record in preprocessor detail block");
1539 return 0;
1540
1541 case llvm::bitc::DEFINE_ABBREV:
1542 Error("unexpected abbrevation record in preprocessor detail block");
1543 return 0;
1544
1545 default:
1546 break;
1547 }
1548
1549 if (!PP->getPreprocessingRecord()) {
1550 Error("no preprocessing record");
1551 return 0;
1552 }
1553
1554 // Read the record.
1555 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1556 const char *BlobStart = 0;
1557 unsigned BlobLen = 0;
1558 RecordData Record;
1559 PreprocessorDetailRecordTypes RecType =
1560 (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord(
1561 Code, Record, BlobStart, BlobLen);
1562 switch (RecType) {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001563 case PPD_MACRO_EXPANSION: {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001564 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1565 return PE;
1566
Chandler Carrutha88a22182011-07-14 08:20:46 +00001567 MacroExpansion *ME =
1568 new (PPRec) MacroExpansion(DecodeIdentifierInfo(Record[3]),
Douglas Gregor92a96f52011-02-08 21:58:10 +00001569 SourceRange(ReadSourceLocation(F, Record[1]),
1570 ReadSourceLocation(F, Record[2])),
Chandler Carrutha88a22182011-07-14 08:20:46 +00001571 getMacroDefinition(Record[4]));
1572 PPRec.SetPreallocatedEntity(Record[0], ME);
1573 return ME;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001574 }
1575
1576 case PPD_MACRO_DEFINITION: {
1577 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1578 return PE;
1579
1580 if (Record[1] > MacroDefinitionsLoaded.size()) {
1581 Error("out-of-bounds macro definition record");
1582 return 0;
1583 }
1584
1585 // Decode the identifier info and then check again; if the macro is
1586 // still defined and associated with the identifier,
1587 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1588 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1589 MacroDefinition *MD
1590 = new (PPRec) MacroDefinition(II,
1591 ReadSourceLocation(F, Record[5]),
1592 SourceRange(
1593 ReadSourceLocation(F, Record[2]),
1594 ReadSourceLocation(F, Record[3])));
1595
1596 PPRec.SetPreallocatedEntity(Record[0], MD);
1597 MacroDefinitionsLoaded[Record[1] - 1] = MD;
1598
1599 if (DeserializationListener)
1600 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1601 }
1602
1603 return MacroDefinitionsLoaded[Record[1] - 1];
1604 }
1605
1606 case PPD_INCLUSION_DIRECTIVE: {
1607 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1608 return PE;
1609
1610 const char *FullFileNameStart = BlobStart + Record[3];
1611 const FileEntry *File
1612 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
1613 BlobLen - Record[3]));
1614
1615 // FIXME: Stable encoding
1616 InclusionDirective::InclusionKind Kind
1617 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1618 InclusionDirective *ID
1619 = new (PPRec) InclusionDirective(PPRec, Kind,
1620 llvm::StringRef(BlobStart, Record[3]),
1621 Record[4],
1622 File,
1623 SourceRange(ReadSourceLocation(F, Record[1]),
1624 ReadSourceLocation(F, Record[2])));
1625 PPRec.SetPreallocatedEntity(Record[0], ID);
1626 return ID;
1627 }
1628 }
1629
1630 Error("invalid offset in preprocessor detail block");
1631 return 0;
1632}
1633
Douglas Gregor09b69892011-02-10 17:09:37 +00001634namespace {
1635 /// \brief Trait class used to search the on-disk hash table containing all of
1636 /// the header search information.
1637 ///
1638 /// The on-disk hash table contains a mapping from each header path to
1639 /// information about that header (how many times it has been included, its
1640 /// controlling macro, etc.). Note that we actually hash based on the
1641 /// filename, and support "deep" comparisons of file names based on current
1642 /// inode numbers, so that the search can cope with non-normalized path names
1643 /// and symlinks.
1644 class HeaderFileInfoTrait {
1645 const char *SearchPath;
1646 struct stat SearchPathStatBuf;
1647 llvm::Optional<int> SearchPathStatResult;
1648
1649 int StatSimpleCache(const char *Path, struct stat *StatBuf) {
1650 if (Path == SearchPath) {
1651 if (!SearchPathStatResult)
1652 SearchPathStatResult = stat(Path, &SearchPathStatBuf);
1653
1654 *StatBuf = SearchPathStatBuf;
1655 return *SearchPathStatResult;
1656 }
1657
1658 return stat(Path, StatBuf);
1659 }
1660
1661 public:
1662 typedef const char *external_key_type;
1663 typedef const char *internal_key_type;
1664
1665 typedef HeaderFileInfo data_type;
1666
1667 HeaderFileInfoTrait(const char *SearchPath = 0) : SearchPath(SearchPath) { }
1668
1669 static unsigned ComputeHash(const char *path) {
1670 return llvm::HashString(llvm::sys::path::filename(path));
1671 }
1672
1673 static internal_key_type GetInternalKey(const char *path) { return path; }
1674
1675 bool EqualKey(internal_key_type a, internal_key_type b) {
1676 if (strcmp(a, b) == 0)
1677 return true;
1678
1679 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1680 return false;
1681
1682 // The file names match, but the path names don't. stat() the files to
1683 // see if they are the same.
1684 struct stat StatBufA, StatBufB;
1685 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1686 return false;
1687
1688 return StatBufA.st_ino == StatBufB.st_ino;
1689 }
1690
1691 static std::pair<unsigned, unsigned>
1692 ReadKeyDataLength(const unsigned char*& d) {
1693 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1694 unsigned DataLen = (unsigned) *d++;
1695 return std::make_pair(KeyLen + 1, DataLen);
1696 }
1697
1698 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1699 return (const char *)d;
1700 }
1701
1702 static data_type ReadData(const internal_key_type, const unsigned char *d,
1703 unsigned DataLen) {
1704 const unsigned char *End = d + DataLen;
1705 using namespace clang::io;
1706 HeaderFileInfo HFI;
1707 unsigned Flags = *d++;
Douglas Gregor37aa4932011-05-04 00:14:37 +00001708 HFI.isImport = (Flags >> 4) & 0x01;
1709 HFI.isPragmaOnce = (Flags >> 3) & 0x01;
Douglas Gregor09b69892011-02-10 17:09:37 +00001710 HFI.DirInfo = (Flags >> 1) & 0x03;
1711 HFI.Resolved = Flags & 0x01;
1712 HFI.NumIncludes = ReadUnalignedLE16(d);
1713 HFI.ControllingMacroID = ReadUnalignedLE32(d);
1714 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1715 (void)End;
1716
1717 // This HeaderFileInfo was externally loaded.
1718 HFI.External = true;
1719 return HFI;
1720 }
1721 };
1722}
1723
1724/// \brief The on-disk hash table used for the global method pool.
1725typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
1726 HeaderFileInfoLookupTable;
1727
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001728void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1729 uint64_t Offset) {
1730 // Note that this identifier has a macro definition.
1731 II->setHasMacroDefinition(true);
1732
1733 // Adjust the offset based on our position in the chain.
1734 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1735 if (Chain[I] == &F)
1736 break;
1737
1738 Offset += Chain[I]->SizeInBits;
1739 }
1740
1741 UnreadMacroRecordOffsets[II] = Offset;
1742}
1743
Sebastian Redl2c499f62010-08-18 23:56:43 +00001744void ASTReader::ReadDefinedMacros() {
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001745 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001746 PerFileData &F = *Chain[N - I - 1];
1747 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001748
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001749 // If there was no preprocessor block, skip this file.
1750 if (!MacroCursor.getBitStreamReader())
1751 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001752
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001753 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001754 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001755
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001756 RecordData Record;
1757 while (true) {
1758 unsigned Code = Cursor.ReadCode();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001759 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001760 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001761
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001762 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1763 // No known subblocks, always skip them.
1764 Cursor.ReadSubBlockID();
1765 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001766 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001767 return;
1768 }
1769 continue;
1770 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001771
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001772 if (Code == llvm::bitc::DEFINE_ABBREV) {
1773 Cursor.ReadAbbrevRecord();
1774 continue;
1775 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001776
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001777 // Read a record.
1778 const char *BlobStart;
1779 unsigned BlobLen;
1780 Record.clear();
1781 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1782 default: // Default behavior: ignore.
1783 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001784
Sebastian Redl539c5062010-08-18 23:57:32 +00001785 case PP_MACRO_OBJECT_LIKE:
1786 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001787 DecodeIdentifierInfo(Record[0]);
1788 break;
1789
Sebastian Redl539c5062010-08-18 23:57:32 +00001790 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001791 // Ignore tokens.
1792 break;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001793 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001794 }
1795 }
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001796
1797 // Drain the unread macro-record offsets map.
1798 while (!UnreadMacroRecordOffsets.empty())
1799 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1800}
1801
1802void ASTReader::LoadMacroDefinition(
1803 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1804 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1805 PerFileData *F = 0;
1806 uint64_t Offset = Pos->second;
1807 UnreadMacroRecordOffsets.erase(Pos);
1808
1809 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1810 if (Offset < Chain[I]->SizeInBits) {
1811 F = Chain[I];
1812 break;
1813 }
1814
1815 Offset -= Chain[I]->SizeInBits;
1816 }
1817 if (!F) {
1818 Error("Malformed macro record offset");
1819 return;
1820 }
1821
1822 ReadMacroRecord(*F, Offset);
1823}
1824
1825void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1826 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1827 = UnreadMacroRecordOffsets.find(II);
1828 LoadMacroDefinition(Pos);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001829}
1830
Sebastian Redl50e26582010-09-15 19:54:06 +00001831MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor91096292010-10-02 19:29:26 +00001832 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregoraae92242010-03-19 21:51:54 +00001833 return 0;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001834
Douglas Gregor91096292010-10-02 19:29:26 +00001835 if (!MacroDefinitionsLoaded[ID - 1]) {
1836 unsigned Index = ID - 1;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001837 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1838 PerFileData &F = *Chain[N - I - 1];
1839 if (Index < F.LocalNumMacroDefinitions) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001840 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
1841 F.PreprocessorDetailCursor.JumpToBit(F.MacroDefinitionOffsets[Index]);
1842 LoadPreprocessedEntity(F);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001843 break;
1844 }
1845 Index -= F.LocalNumMacroDefinitions;
1846 }
Douglas Gregor91096292010-10-02 19:29:26 +00001847 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001848 }
1849
Douglas Gregor91096292010-10-02 19:29:26 +00001850 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001851}
1852
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00001853const FileEntry *ASTReader::getFileEntry(llvm::StringRef filenameStrRef) {
1854 std::string Filename = filenameStrRef;
1855 MaybeAddSystemRootToFilename(Filename);
1856 const FileEntry *File = FileMgr.getFile(Filename);
1857 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1858 OriginalDir != CurrentDir) {
1859 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1860 OriginalDir,
1861 CurrentDir);
1862 if (!resolved.empty())
1863 File = FileMgr.getFile(resolved);
1864 }
1865
1866 return File;
1867}
1868
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001869/// \brief If we are loading a relocatable PCH file, and the filename is
1870/// not an absolute path, add the system root to the beginning of the file
1871/// name.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001872void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001873 // If this is not a relocatable PCH file, there's nothing to do.
1874 if (!RelocatablePCH)
1875 return;
Mike Stump11289f42009-09-09 15:08:12 +00001876
Michael J. Spencerf28df4c2010-12-17 21:22:22 +00001877 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001878 return;
1879
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001880 if (isysroot == 0) {
1881 // If no system root was given, default to '/'
1882 Filename.insert(Filename.begin(), '/');
1883 return;
1884 }
Mike Stump11289f42009-09-09 15:08:12 +00001885
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001886 unsigned Length = strlen(isysroot);
1887 if (isysroot[Length - 1] != '/')
1888 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001889
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001890 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1891}
1892
Sebastian Redl2c499f62010-08-18 23:56:43 +00001893ASTReader::ASTReadResult
Sebastian Redl3e31c722010-08-18 23:56:56 +00001894ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001895 llvm::BitstreamCursor &Stream = F.Stream;
1896
Sebastian Redl539c5062010-08-18 23:57:32 +00001897 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001898 Error("malformed block record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001899 return Failure;
1900 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001901
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001902 // Read all of the records and blocks for the ASt file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001903 RecordData Record;
Sebastian Redl393f8b72010-07-19 20:52:06 +00001904 bool First = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001905 while (!Stream.AtEndOfStream()) {
1906 unsigned Code = Stream.ReadCode();
1907 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001908 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001909 Error("error at end of module block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001910 return Failure;
1911 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001912
Douglas Gregor55abb232009-04-10 20:39:37 +00001913 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001914 }
1915
1916 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1917 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001918 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001919 // We lazily load the decls block, but we want to set up the
1920 // DeclsCursor cursor to point into it. Clone our current bitcode
1921 // cursor to it, enter the block and read the abbrevs in that block.
1922 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001923 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001924 if (Stream.SkipBlock() || // Skip with the main cursor.
1925 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001926 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001927 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001928 return Failure;
1929 }
1930 break;
Mike Stump11289f42009-09-09 15:08:12 +00001931
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00001932 case DECL_UPDATES_BLOCK_ID:
1933 if (Stream.SkipBlock()) {
1934 Error("malformed block record in AST file");
1935 return Failure;
1936 }
1937 break;
1938
Sebastian Redl539c5062010-08-18 23:57:32 +00001939 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001940 F.MacroCursor = Stream;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001941 if (PP)
1942 PP->setExternalSource(this);
1943
Douglas Gregor796d76a2010-10-20 22:00:55 +00001944 if (Stream.SkipBlock() ||
1945 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001946 Error("malformed block record in AST file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001947 return Failure;
1948 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001949 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001950 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001951
Douglas Gregor92a96f52011-02-08 21:58:10 +00001952 case PREPROCESSOR_DETAIL_BLOCK_ID:
1953 F.PreprocessorDetailCursor = Stream;
1954 if (Stream.SkipBlock() ||
1955 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1956 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1957 Error("malformed preprocessor detail record in AST file");
1958 return Failure;
1959 }
1960 F.PreprocessorDetailStartOffset
1961 = F.PreprocessorDetailCursor.GetCurrentBitNo();
1962 break;
1963
Sebastian Redl539c5062010-08-18 23:57:32 +00001964 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001965 switch (ReadSourceManagerBlock(F)) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001966 case Success:
1967 break;
1968
1969 case Failure:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001970 Error("malformed source manager block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001971 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001972
1973 case IgnorePCH:
1974 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001975 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001976 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001977 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00001978 First = false;
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001979 continue;
1980 }
1981
1982 if (Code == llvm::bitc::DEFINE_ABBREV) {
1983 Stream.ReadAbbrevRecord();
1984 continue;
1985 }
1986
1987 // Read and process a record.
1988 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001989 const char *BlobStart = 0;
1990 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001991 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001992 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001993 default: // Default behavior: ignore.
1994 break;
1995
Sebastian Redl539c5062010-08-18 23:57:32 +00001996 case METADATA: {
1997 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1998 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001999 : diag::warn_pch_version_too_new);
2000 return IgnorePCH;
2001 }
2002
2003 RelocatablePCH = Record[4];
2004 if (Listener) {
2005 std::string TargetTriple(BlobStart, BlobLen);
2006 if (Listener->ReadTargetTriple(TargetTriple))
2007 return IgnorePCH;
2008 }
2009 break;
2010 }
2011
Sebastian Redl539c5062010-08-18 23:57:32 +00002012 case CHAINED_METADATA: {
Sebastian Redl393f8b72010-07-19 20:52:06 +00002013 if (!First) {
2014 Error("CHAINED_METADATA is not first record in block");
2015 return Failure;
2016 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002017 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2018 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002019 : diag::warn_pch_version_too_new);
2020 return IgnorePCH;
2021 }
2022
Sebastian Redl009e7f22010-10-05 16:15:19 +00002023 // Load the chained file, which is always a PCH file.
2024 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002025 case Failure: return Failure;
2026 // If we have to ignore the dependency, we'll have to ignore this too.
2027 case IgnorePCH: return IgnorePCH;
2028 case Success: break;
2029 }
2030 break;
2031 }
2032
Sebastian Redl539c5062010-08-18 23:57:32 +00002033 case TYPE_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00002034 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002035 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002036 return Failure;
2037 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002038 F.TypeOffsets = (const uint32_t *)BlobStart;
2039 F.LocalNumTypes = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002040 break;
2041
Sebastian Redl539c5062010-08-18 23:57:32 +00002042 case DECL_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00002043 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002044 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002045 return Failure;
2046 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002047 F.DeclOffsets = (const uint32_t *)BlobStart;
2048 F.LocalNumDecls = Record[0];
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002049
2050 // Introduce the global -> local mapping for declarations within this
Douglas Gregor19d26352011-07-20 00:59:32 +00002051 // AST file.
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002052 GlobalDeclMap.insert(std::make_pair(getTotalNumDecls() + 1,
2053 std::make_pair(&F,
2054 -getTotalNumDecls())));
Douglas Gregor19d26352011-07-20 00:59:32 +00002055 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002056 break;
Douglas Gregor55abb232009-04-10 20:39:37 +00002057
Sebastian Redl539c5062010-08-18 23:57:32 +00002058 case TU_UPDATE_LEXICAL: {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002059 DeclContextInfo Info = {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002060 /* No visible information */ 0,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002061 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
2062 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002063 };
Douglas Gregoraa433012010-10-01 01:18:02 +00002064 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
2065 .push_back(Info);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002066 break;
2067 }
2068
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002069 case UPDATE_VISIBLE: {
2070 serialization::DeclID ID = Record[0];
2071 void *Table = ASTDeclContextNameLookupTable::Create(
2072 (const unsigned char *)BlobStart + Record[1],
2073 (const unsigned char *)BlobStart,
2074 ASTDeclContextNameLookupTrait(*this));
Douglas Gregoraa433012010-10-01 01:18:02 +00002075 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002076 DeclContextInfo Info = {
2077 Table, /* No lexical inforamtion */ 0, 0
2078 };
2079 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
2080 } else
2081 PendingVisibleUpdates[ID].push_back(Table);
2082 break;
2083 }
2084
Sebastian Redl539c5062010-08-18 23:57:32 +00002085 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002086 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
2087 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002088 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002089 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
2090 Latest > FirstLatestDeclIDs[First]) &&
2091 "The new latest is supposed to come after the previous latest");
2092 FirstLatestDeclIDs[First] = Latest;
2093 }
2094 break;
2095 }
2096
Sebastian Redl539c5062010-08-18 23:57:32 +00002097 case LANGUAGE_OPTIONS:
Douglas Gregorce3a8292010-07-27 00:27:13 +00002098 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor55abb232009-04-10 20:39:37 +00002099 return IgnorePCH;
2100 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00002101
Sebastian Redl539c5062010-08-18 23:57:32 +00002102 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00002103 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00002104 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00002105 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002106 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00002107 (const unsigned char *)F.IdentifierTableData + Record[0],
2108 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl2c373b92010-10-05 15:59:54 +00002109 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002110 if (PP)
2111 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00002112 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002113 break;
2114
Sebastian Redl539c5062010-08-18 23:57:32 +00002115 case IDENTIFIER_OFFSET:
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002116 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002117 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002118 return Failure;
2119 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002120 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2121 F.LocalNumIdentifiers = Record[0];
Douglas Gregor19d26352011-07-20 00:59:32 +00002122
2123 // Introduce the global -> local mapping for identifiers within this AST
2124 // file
2125 GlobalIdentifierMap.insert(
2126 std::make_pair(getTotalNumIdentifiers() + 1,
2127 std::make_pair(&F,
2128 -getTotalNumIdentifiers())));
2129 IdentifiersLoaded.resize(IdentifiersLoaded.size() +F.LocalNumIdentifiers);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002130 break;
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002131
Sebastian Redl539c5062010-08-18 23:57:32 +00002132 case EXTERNAL_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002133 // Optimization for the first block.
2134 if (ExternalDefinitions.empty())
2135 ExternalDefinitions.swap(Record);
2136 else
2137 ExternalDefinitions.insert(ExternalDefinitions.end(),
2138 Record.begin(), Record.end());
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002139 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00002140
Sebastian Redl539c5062010-08-18 23:57:32 +00002141 case SPECIAL_TYPES:
Sebastian Redlb293a452010-07-20 21:20:32 +00002142 // Optimization for the first block
2143 if (SpecialTypes.empty())
2144 SpecialTypes.swap(Record);
2145 else
2146 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002147 break;
2148
Sebastian Redl539c5062010-08-18 23:57:32 +00002149 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002150 TotalNumStatements += Record[0];
2151 TotalNumMacros += Record[1];
2152 TotalLexicalDeclContexts += Record[2];
2153 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00002154 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002155
Sebastian Redl539c5062010-08-18 23:57:32 +00002156 case UNUSED_FILESCOPED_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002157 // Optimization for the first block.
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002158 if (UnusedFileScopedDecls.empty())
2159 UnusedFileScopedDecls.swap(Record);
Sebastian Redlb293a452010-07-20 21:20:32 +00002160 else
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002161 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
2162 Record.begin(), Record.end());
Tanya Lattner90073802010-02-12 00:07:30 +00002163 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002164
Alexis Hunt27a761d2011-05-04 23:29:54 +00002165 case DELEGATING_CTORS:
2166 // Optimization for the first block.
2167 if (DelegatingCtorDecls.empty())
2168 DelegatingCtorDecls.swap(Record);
2169 else
2170 DelegatingCtorDecls.insert(DelegatingCtorDecls.end(),
2171 Record.begin(), Record.end());
2172 break;
2173
Sebastian Redl539c5062010-08-18 23:57:32 +00002174 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002175 // Later blocks overwrite earlier ones.
2176 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002177 break;
2178
Sebastian Redl539c5062010-08-18 23:57:32 +00002179 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002180 // Optimization for the first block.
2181 if (LocallyScopedExternalDecls.empty())
2182 LocallyScopedExternalDecls.swap(Record);
2183 else
2184 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
2185 Record.begin(), Record.end());
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002186 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002187
Sebastian Redl539c5062010-08-18 23:57:32 +00002188 case SELECTOR_OFFSETS:
Sebastian Redla19a67f2010-08-03 21:58:15 +00002189 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00002190 F.LocalNumSelectors = Record[0];
Douglas Gregor95c13f52009-04-25 17:48:32 +00002191 break;
2192
Sebastian Redl539c5062010-08-18 23:57:32 +00002193 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00002194 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002195 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00002196 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002197 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00002198 F.SelectorLookupTableData + Record[0],
2199 F.SelectorLookupTableData,
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002200 ASTSelectorLookupTrait(*this));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002201 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002202 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00002203
Sebastian Redl96371b42010-09-22 00:42:30 +00002204 case REFERENCED_SELECTOR_POOL:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002205 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002206 break;
2207
Sebastian Redl539c5062010-08-18 23:57:32 +00002208 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002209 if (!Record.empty() && Listener)
2210 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002211 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002212
Douglas Gregor925296b2011-07-19 16:10:42 +00002213 case SOURCE_LOCATION_OFFSETS: {
2214 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redlb293a452010-07-20 21:20:32 +00002215 F.LocalNumSLocEntries = Record[0];
Douglas Gregor925296b2011-07-19 16:10:42 +00002216 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2217 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, Record[1]);
2218 // Make our entry in the range map. BaseID is negative and growing, so
2219 // we invert it. Because we invert it, though, we need the other end of
2220 // the range.
2221 unsigned RangeStart =
2222 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2223 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2224 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2225
2226 // Initialize the remapping table.
2227 // Invalid stays invalid.
2228 F.SLocRemap.insert(std::make_pair(0U, 0));
2229 // This module. Base was 2 when being compiled.
2230 F.SLocRemap.insert(std::make_pair(2U,
2231 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2232 break;
2233 }
2234
2235 case SOURCE_LOCATION_MAP: {
2236 // Additional remapping information.
2237 const unsigned char *Data = (const unsigned char*)BlobStart;
2238 const unsigned char *DataEnd = Data + BlobLen;
2239 while(Data < DataEnd) {
2240 uint32_t Offset = io::ReadUnalignedLE32(Data);
2241 uint16_t Len = io::ReadUnalignedLE16(Data);
2242 llvm::StringRef Name = llvm::StringRef((const char*)Data, Len);
2243 PerFileData *OM = Modules.lookup(Name);
2244 if (!OM) {
2245 Error("SourceLocation remap refers to unknown module");
2246 return Failure;
2247 }
2248 // My Offset is mapped to OM->SLocEntryBaseOffset.
2249 F.SLocRemap.insert(std::make_pair(Offset,
2250 static_cast<int>(OM->SLocEntryBaseOffset - Offset)));
2251 Data += Len;
2252 }
2253 break;
2254 }
2255
2256
2257 case SOURCE_MANAGER_LINE_TABLE:
2258 if (ParseLineTable(F, Record))
2259 return Failure;
Douglas Gregor258ae542009-04-27 06:38:32 +00002260 break;
2261
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002262 case FILE_SOURCE_LOCATION_OFFSETS:
2263 F.SLocFileOffsets = (const uint32_t *)BlobStart;
2264 F.LocalNumSLocFileEntries = Record[0];
2265 break;
2266
Douglas Gregor925296b2011-07-19 16:10:42 +00002267 case SOURCE_LOCATION_PRELOADS: {
2268 // Need to transform from the local view (1-based IDs) to the global view,
2269 // which is based off F.SLocEntryBaseID.
2270 PreloadSLocEntries.reserve(PreloadSLocEntries.size() + Record.size());
2271 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2272 PreloadSLocEntries.push_back(int(Record[I] - 1) + F.SLocEntryBaseID);
Douglas Gregor258ae542009-04-27 06:38:32 +00002273 break;
Douglas Gregor925296b2011-07-19 16:10:42 +00002274 }
Douglas Gregorc5046832009-04-27 18:38:38 +00002275
Sebastian Redl539c5062010-08-18 23:57:32 +00002276 case STAT_CACHE: {
Douglas Gregor606c4ac2011-02-05 19:42:43 +00002277 if (!DisableStatCache) {
2278 ASTStatCache *MyStatCache =
2279 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2280 (const unsigned char *)BlobStart,
2281 NumStatHits, NumStatMisses);
2282 FileMgr.addStatCache(MyStatCache);
2283 F.StatCache = MyStatCache;
2284 }
Douglas Gregorc5046832009-04-27 18:38:38 +00002285 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00002286 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002287
Sebastian Redl539c5062010-08-18 23:57:32 +00002288 case EXT_VECTOR_DECLS:
Sebastian Redl04f5c312010-07-28 21:38:49 +00002289 // Optimization for the first block.
2290 if (ExtVectorDecls.empty())
2291 ExtVectorDecls.swap(Record);
2292 else
2293 ExtVectorDecls.insert(ExtVectorDecls.end(),
2294 Record.begin(), Record.end());
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002295 break;
2296
Sebastian Redl539c5062010-08-18 23:57:32 +00002297 case VTABLE_USES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002298 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002299 VTableUses.swap(Record);
2300 break;
2301
Sebastian Redl539c5062010-08-18 23:57:32 +00002302 case DYNAMIC_CLASSES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002303 // Optimization for the first block.
2304 if (DynamicClasses.empty())
2305 DynamicClasses.swap(Record);
2306 else
2307 DynamicClasses.insert(DynamicClasses.end(),
2308 Record.begin(), Record.end());
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002309 break;
2310
Sebastian Redl539c5062010-08-18 23:57:32 +00002311 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002312 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002313 break;
2314
Sebastian Redl539c5062010-08-18 23:57:32 +00002315 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002316 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002317 SemaDeclRefs.swap(Record);
2318 break;
2319
Sebastian Redl539c5062010-08-18 23:57:32 +00002320 case ORIGINAL_FILE_NAME:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002321 // The primary AST will be the last to get here, so it will be the one
Sebastian Redlb293a452010-07-20 21:20:32 +00002322 // that's used.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00002323 ActualOriginalFileName.assign(BlobStart, BlobLen);
2324 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002325 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00002326 break;
Mike Stump11289f42009-09-09 15:08:12 +00002327
Douglas Gregora3b20262011-05-06 21:43:30 +00002328 case ORIGINAL_FILE_ID:
2329 OriginalFileID = FileID::get(Record[0]);
2330 break;
2331
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002332 case ORIGINAL_PCH_DIR:
2333 // The primary AST will be the last to get here, so it will be the one
2334 // that's used.
2335 OriginalDir.assign(BlobStart, BlobLen);
2336 break;
2337
Sebastian Redl539c5062010-08-18 23:57:32 +00002338 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00002339 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002340 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2341 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2342 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregord54f3a12009-10-05 21:07:28 +00002343 return IgnorePCH;
2344 }
2345 break;
2346 }
Sebastian Redlfa061442010-07-21 20:07:32 +00002347
Sebastian Redl539c5062010-08-18 23:57:32 +00002348 case MACRO_DEFINITION_OFFSETS:
Sebastian Redlfa061442010-07-21 20:07:32 +00002349 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2350 F.NumPreallocatedPreprocessingEntities = Record[0];
2351 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregoraae92242010-03-19 21:51:54 +00002352 break;
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002353
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002354 case DECL_UPDATE_OFFSETS: {
2355 if (Record.size() % 2 != 0) {
2356 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2357 return Failure;
2358 }
2359 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2360 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2361 .push_back(std::make_pair(&F, Record[I+1]));
2362 break;
2363 }
2364
Sebastian Redl539c5062010-08-18 23:57:32 +00002365 case DECL_REPLACEMENTS: {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002366 if (Record.size() % 2 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002367 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002368 return Failure;
2369 }
2370 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl539c5062010-08-18 23:57:32 +00002371 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002372 std::make_pair(&F, Record[I+1]);
2373 break;
2374 }
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002375
2376 case CXX_BASE_SPECIFIER_OFFSETS: {
2377 if (F.LocalNumCXXBaseSpecifiers != 0) {
2378 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2379 return Failure;
2380 }
2381
2382 F.LocalNumCXXBaseSpecifiers = Record[0];
2383 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2384 break;
2385 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002386
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002387 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002388 if (Record.size() % 2 != 0) {
2389 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2390 return Failure;
2391 }
Douglas Gregor925296b2011-07-19 16:10:42 +00002392
2393 if (F.PragmaDiagMappings.empty())
2394 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002395 else
Douglas Gregor925296b2011-07-19 16:10:42 +00002396 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2397 Record.begin(), Record.end());
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002398 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002399
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002400 case CUDA_SPECIAL_DECL_REFS:
2401 // Later tables overwrite earlier ones.
2402 CUDASpecialDeclRefs.swap(Record);
2403 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002404
2405 case HEADER_SEARCH_TABLE:
2406 F.HeaderFileInfoTableData = BlobStart;
2407 F.LocalNumHeaderFileInfos = Record[1];
2408 if (Record[0]) {
2409 F.HeaderFileInfoTable
2410 = HeaderFileInfoLookupTable::Create(
2411 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2412 (const unsigned char *)F.HeaderFileInfoTableData);
2413 if (PP)
2414 PP->getHeaderSearchInfo().SetExternalSource(this);
2415 }
2416 break;
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002417
2418 case FP_PRAGMA_OPTIONS:
2419 // Later tables overwrite earlier ones.
2420 FPPragmaOptions.swap(Record);
2421 break;
2422
2423 case OPENCL_EXTENSIONS:
2424 // Later tables overwrite earlier ones.
2425 OpenCLExtensions.swap(Record);
2426 break;
Alexis Hunt27a761d2011-05-04 23:29:54 +00002427
2428 case TENTATIVE_DEFINITIONS:
2429 // Optimization for the first block.
2430 if (TentativeDefinitions.empty())
2431 TentativeDefinitions.swap(Record);
2432 else
2433 TentativeDefinitions.insert(TentativeDefinitions.end(),
2434 Record.begin(), Record.end());
2435 break;
Douglas Gregorc2fa1692011-06-28 16:20:02 +00002436
2437 case KNOWN_NAMESPACES:
2438 // Optimization for the first block.
2439 if (KnownNamespaces.empty())
2440 KnownNamespaces.swap(Record);
2441 else
2442 KnownNamespaces.insert(KnownNamespaces.end(),
2443 Record.begin(), Record.end());
2444 break;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002445 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00002446 First = false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002447 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002448 Error("premature end of bitstream in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002449 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002450}
2451
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002452ASTReader::ASTReadResult ASTReader::validateFileEntries() {
2453 for (unsigned CI = 0, CN = Chain.size(); CI != CN; ++CI) {
2454 PerFileData *F = Chain[CI];
2455 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
2456
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002457 for (unsigned i = 0, e = F->LocalNumSLocFileEntries; i != e; ++i) {
2458 SLocEntryCursor.JumpToBit(F->SLocFileOffsets[i]);
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002459 unsigned Code = SLocEntryCursor.ReadCode();
2460 if (Code == llvm::bitc::END_BLOCK ||
2461 Code == llvm::bitc::ENTER_SUBBLOCK ||
2462 Code == llvm::bitc::DEFINE_ABBREV) {
2463 Error("incorrectly-formatted source location entry in AST file");
2464 return Failure;
2465 }
2466
2467 RecordData Record;
2468 const char *BlobStart;
2469 unsigned BlobLen;
2470 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2471 default:
2472 Error("incorrectly-formatted source location entry in AST file");
2473 return Failure;
2474
2475 case SM_SLOC_FILE_ENTRY: {
2476 llvm::StringRef Filename(BlobStart, BlobLen);
2477 const FileEntry *File = getFileEntry(Filename);
2478
2479 if (File == 0) {
2480 std::string ErrorStr = "could not find file '";
2481 ErrorStr += Filename;
2482 ErrorStr += "' referenced by AST file";
2483 Error(ErrorStr.c_str());
2484 return IgnorePCH;
2485 }
2486
2487 if (Record.size() < 6) {
2488 Error("source location entry is incorrect");
2489 return Failure;
2490 }
2491
2492 // The stat info from the FileEntry came from the cached stat
2493 // info of the PCH, so we cannot trust it.
2494 struct stat StatBuf;
2495 if (::stat(File->getName(), &StatBuf) != 0) {
2496 StatBuf.st_size = File->getSize();
2497 StatBuf.st_mtime = File->getModificationTime();
2498 }
2499
2500 if (((off_t)Record[4] != StatBuf.st_size
2501#if !defined(LLVM_ON_WIN32)
2502 // In our regression testing, the Windows file system seems to
2503 // have inconsistent modification times that sometimes
2504 // erroneously trigger this error-handling path.
2505 || (time_t)Record[5] != StatBuf.st_mtime
2506#endif
2507 )) {
2508 Error(diag::err_fe_pch_file_modified, Filename);
2509 return IgnorePCH;
2510 }
2511
2512 break;
2513 }
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002514 }
2515 }
2516 }
2517
2518 return Success;
2519}
2520
Sebastian Redl009e7f22010-10-05 16:15:19 +00002521ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2522 ASTFileType Type) {
2523 switch(ReadASTCore(FileName, Type)) {
Sebastian Redl2abc0382010-07-16 20:41:52 +00002524 case Failure: return Failure;
2525 case IgnorePCH: return IgnorePCH;
2526 case Success: break;
2527 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002528
2529 // Here comes stuff that we only do once the entire chain is loaded.
2530
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002531 if (!DisableValidation) {
2532 switch(validateFileEntries()) {
2533 case Failure: return Failure;
2534 case IgnorePCH: return IgnorePCH;
2535 case Success: break;
2536 }
2537 }
2538
Sebastian Redl96371b42010-09-22 00:42:30 +00002539 // Allocate space for loaded slocentries, identifiers, decls and types.
Douglas Gregor19d26352011-07-20 00:59:32 +00002540 unsigned TotalNumTypes = 0,
Sebastian Redlada023c2010-08-04 20:40:17 +00002541 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2542 TotalNumSelectors = 0;
Sebastian Redl9e687992010-07-19 22:06:55 +00002543 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl96371b42010-09-22 00:42:30 +00002544 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redl9e687992010-07-19 22:06:55 +00002545 TotalNumTypes += Chain[I]->LocalNumTypes;
Sebastian Redlfa061442010-07-21 20:07:32 +00002546 TotalNumPreallocatedPreprocessingEntities +=
2547 Chain[I]->NumPreallocatedPreprocessingEntities;
2548 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redlada023c2010-08-04 20:40:17 +00002549 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl9e687992010-07-19 22:06:55 +00002550 }
2551 TypesLoaded.resize(TotalNumTypes);
Sebastian Redlfa061442010-07-21 20:07:32 +00002552 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2553 if (PP) {
Douglas Gregor19d26352011-07-20 00:59:32 +00002554 PP->getHeaderSearchInfo().SetExternalLookup(this);
Sebastian Redlfa061442010-07-21 20:07:32 +00002555 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2556 if (!PP->getPreprocessingRecord())
Douglas Gregor998caea2011-05-06 16:33:08 +00002557 PP->createPreprocessingRecord(true);
Sebastian Redlfa061442010-07-21 20:07:32 +00002558 PP->getPreprocessingRecord()->SetExternalSource(*this,
2559 TotalNumPreallocatedPreprocessingEntities);
2560 }
2561 }
Sebastian Redlada023c2010-08-04 20:40:17 +00002562 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl96371b42010-09-22 00:42:30 +00002563 // Preload SLocEntries.
2564 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2565 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2566 if (Result != Success)
Douglas Gregor925296b2011-07-19 16:10:42 +00002567 return Failure;
Sebastian Redl96371b42010-09-22 00:42:30 +00002568 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002569
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002570 // Check the predefines buffers.
Douglas Gregorce3a8292010-07-27 00:27:13 +00002571 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002572 return IgnorePCH;
2573
2574 if (PP) {
2575 // Initialization of keywords and pragmas occurs before the
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002576 // AST file is read, so there may be some identifiers that were
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002577 // loaded into the IdentifierTable before we intercepted the
2578 // creation of identifiers. Iterate through the list of known
2579 // identifiers and determine whether we have to establish
2580 // preprocessor definitions or top-level identifier declaration
2581 // chains for those identifiers.
2582 //
2583 // We copy the IdentifierInfo pointers to a small vector first,
2584 // since de-serializing declarations or macro definitions can add
2585 // new entries into the identifier table, invalidating the
2586 // iterators.
2587 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2588 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2589 IdEnd = PP->getIdentifierTable().end();
2590 Id != IdEnd; ++Id)
2591 Identifiers.push_back(Id->second);
Sebastian Redlfa061442010-07-21 20:07:32 +00002592 // We need to search the tables in all files.
Sebastian Redlfa061442010-07-21 20:07:32 +00002593 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002594 ASTIdentifierLookupTable *IdTable
2595 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2596 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl5c415f32010-07-22 17:01:13 +00002597 // ones.
2598 if (!IdTable)
2599 continue;
Sebastian Redlfa061442010-07-21 20:07:32 +00002600 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2601 IdentifierInfo *II = Identifiers[I];
2602 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00002603 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redlfa061442010-07-21 20:07:32 +00002604 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002605 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redlb293a452010-07-20 21:20:32 +00002606 if (Pos == IdTable->end())
2607 continue;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002608
Sebastian Redlb293a452010-07-20 21:20:32 +00002609 // Dereferencing the iterator has the effect of populating the
2610 // IdentifierInfo node with the various declarations it needs.
2611 (void)*Pos;
2612 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002613 }
2614 }
2615
2616 if (Context)
2617 InitializeContext(*Context);
2618
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002619 if (DeserializationListener)
2620 DeserializationListener->ReaderInitialized(this);
2621
Douglas Gregor936a5b42010-11-30 05:23:00 +00002622 // If this AST file is a precompiled preamble, then set the main file ID of
2623 // the source manager to the file source file from which the preamble was
2624 // built. This is the only valid way to use a precompiled preamble.
2625 if (Type == Preamble) {
Douglas Gregora3b20262011-05-06 21:43:30 +00002626 if (OriginalFileID.isInvalid()) {
2627 SourceLocation Loc
2628 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2629 if (Loc.isValid())
2630 OriginalFileID = SourceMgr.getDecomposedLoc(Loc).first;
Douglas Gregor936a5b42010-11-30 05:23:00 +00002631 }
Douglas Gregor925296b2011-07-19 16:10:42 +00002632 else {
2633 OriginalFileID = FileID::get(Chain[0]->SLocEntryBaseID
2634 + OriginalFileID.getOpaqueValue() - 1);
2635 }
2636
Douglas Gregora3b20262011-05-06 21:43:30 +00002637 if (!OriginalFileID.isInvalid())
2638 SourceMgr.SetPreambleFileID(OriginalFileID);
Douglas Gregor936a5b42010-11-30 05:23:00 +00002639 }
2640
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002641 return Success;
2642}
2643
Sebastian Redl009e7f22010-10-05 16:15:19 +00002644ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2645 ASTFileType Type) {
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002646 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl009e7f22010-10-05 16:15:19 +00002647 Chain.push_back(new PerFileData(Type));
Sebastian Redl34522812010-07-16 17:50:48 +00002648 PerFileData &F = *Chain.back();
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002649 if (Prev)
2650 Prev->NextInSource = &F;
2651 else
2652 FirstInSource = &F;
2653 F.Loaders.push_back(Prev);
Douglas Gregor925296b2011-07-19 16:10:42 +00002654 // A non-module AST file's module name is $filename.
2655 Modules["$" + FileName.str()] = &F;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002656
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002657 // Set the AST file name.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002658 F.FileName = FileName;
2659
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002660 if (FileName != "-") {
2661 CurrentDir = llvm::sys::path::parent_path(FileName);
2662 if (CurrentDir.empty()) CurrentDir = ".";
2663 }
2664
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +00002665 if (!ASTBuffers.empty()) {
Sebastian Redleb931c62011-04-14 14:07:41 +00002666 F.Buffer.reset(ASTBuffers.back());
2667 ASTBuffers.pop_back();
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +00002668 assert(F.Buffer && "Passed null buffer");
2669 } else {
2670 // Open the AST file.
2671 //
2672 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2673 std::string ErrStr;
2674 llvm::error_code ec;
2675 if (FileName == "-") {
2676 ec = llvm::MemoryBuffer::getSTDIN(F.Buffer);
2677 if (ec)
2678 ErrStr = ec.message();
2679 } else
2680 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
2681 if (!F.Buffer) {
2682 Error(ErrStr.c_str());
2683 return IgnorePCH;
2684 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002685 }
2686
2687 // Initialize the stream
2688 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2689 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl34522812010-07-16 17:50:48 +00002690 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002691 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002692 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002693
2694 // Sniff for the signature.
2695 if (Stream.Read(8) != 'C' ||
2696 Stream.Read(8) != 'P' ||
2697 Stream.Read(8) != 'C' ||
2698 Stream.Read(8) != 'H') {
2699 Diag(diag::err_not_a_pch_file) << FileName;
2700 return Failure;
2701 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002702
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002703 while (!Stream.AtEndOfStream()) {
2704 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002705
Douglas Gregor92863e42009-04-10 23:10:45 +00002706 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002707 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002708 return Failure;
2709 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002710
2711 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002712
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002713 // We only know the AST subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002714 switch (BlockID) {
2715 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002716 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002717 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002718 return Failure;
2719 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002720 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00002721 case AST_BLOCK_ID:
Sebastian Redl3e31c722010-08-18 23:56:56 +00002722 switch (ReadASTBlock(F)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002723 case Success:
2724 break;
2725
2726 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00002727 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00002728
2729 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00002730 // FIXME: We could consider reading through to the end of this
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002731 // AST block, skipping subblocks, to see if there are other
2732 // AST blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00002733
Douglas Gregor925296b2011-07-19 16:10:42 +00002734 // FIXME: We can't clear loaded slocentries anymore.
2735 //SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00002736
2737 // Remove the stat cache.
Sebastian Redl34522812010-07-16 17:50:48 +00002738 if (F.StatCache)
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002739 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00002740
Douglas Gregor92863e42009-04-10 23:10:45 +00002741 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002742 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002743 break;
2744 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002745 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002746 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002747 return Failure;
2748 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002749 break;
2750 }
Mike Stump11289f42009-09-09 15:08:12 +00002751 }
2752
Sebastian Redl2abc0382010-07-16 20:41:52 +00002753 return Success;
2754}
2755
Sebastian Redl2c499f62010-08-18 23:56:43 +00002756void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002757 PP = &pp;
Sebastian Redlfa061442010-07-21 20:07:32 +00002758
2759 unsigned TotalNum = 0;
2760 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2761 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2762 if (TotalNum) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002763 if (!PP->getPreprocessingRecord())
Douglas Gregor998caea2011-05-06 16:33:08 +00002764 PP->createPreprocessingRecord(true);
Sebastian Redlfa061442010-07-21 20:07:32 +00002765 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregoraae92242010-03-19 21:51:54 +00002766 }
2767}
2768
Sebastian Redl2c499f62010-08-18 23:56:43 +00002769void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002770 Context = &Ctx;
2771 assert(Context && "Passed null context!");
2772
2773 assert(PP && "Forgot to set Preprocessor ?");
2774 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2775 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00002776 PP->setExternalSource(this);
Douglas Gregor09b69892011-02-10 17:09:37 +00002777 PP->getHeaderSearchInfo().SetExternalSource(this);
2778
Douglas Gregoraa433012010-10-01 01:18:02 +00002779 // If we have an update block for the TU waiting, we have to add it before
2780 // deserializing the decl.
2781 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2782 if (DCU != DeclContextOffsets.end()) {
2783 // Insertion could invalidate map, so grab vector.
2784 DeclContextInfos T;
2785 T.swap(DCU->second);
2786 DeclContextOffsets.erase(DCU);
2787 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2788 }
2789
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002790 // Load the translation unit declaration
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00002791 GetTranslationUnitDecl();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002792
2793 // Load the special types.
2794 Context->setBuiltinVaListType(
Sebastian Redl539c5062010-08-18 23:57:32 +00002795 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2796 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002797 Context->setObjCIdType(GetType(Id));
Sebastian Redl539c5062010-08-18 23:57:32 +00002798 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002799 Context->setObjCSelType(GetType(Sel));
Sebastian Redl539c5062010-08-18 23:57:32 +00002800 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002801 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl539c5062010-08-18 23:57:32 +00002802 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002803 Context->setObjCClassType(GetType(Class));
Steve Naroff7cae42b2009-07-10 23:34:53 +00002804
Sebastian Redl539c5062010-08-18 23:57:32 +00002805 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002806 Context->setCFConstantStringType(GetType(String));
Mike Stump11289f42009-09-09 15:08:12 +00002807 if (unsigned FastEnum
Sebastian Redl539c5062010-08-18 23:57:32 +00002808 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002809 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl539c5062010-08-18 23:57:32 +00002810 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregor27821ce2009-07-07 16:35:42 +00002811 QualType FileType = GetType(File);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002812 if (FileType.isNull()) {
2813 Error("FILE type is NULL");
2814 return;
2815 }
John McCall9dd450b2009-09-21 23:43:11 +00002816 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregor27821ce2009-07-07 16:35:42 +00002817 Context->setFILEDecl(Typedef->getDecl());
2818 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002819 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002820 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002821 Error("Invalid FILE type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002822 return;
2823 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002824 Context->setFILEDecl(Tag->getDecl());
2825 }
2826 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002827 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002828 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002829 if (Jmp_bufType.isNull()) {
2830 Error("jmp_bug type is NULL");
2831 return;
2832 }
John McCall9dd450b2009-09-21 23:43:11 +00002833 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002834 Context->setjmp_bufDecl(Typedef->getDecl());
2835 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002836 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002837 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002838 Error("Invalid jmp_buf type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002839 return;
2840 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002841 Context->setjmp_bufDecl(Tag->getDecl());
2842 }
2843 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002844 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002845 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002846 if (Sigjmp_bufType.isNull()) {
2847 Error("sigjmp_buf type is NULL");
2848 return;
2849 }
John McCall9dd450b2009-09-21 23:43:11 +00002850 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002851 Context->setsigjmp_bufDecl(Typedef->getDecl());
2852 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002853 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002854 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stumpa4de80b2009-07-28 02:25:19 +00002855 Context->setsigjmp_bufDecl(Tag->getDecl());
2856 }
2857 }
Mike Stump11289f42009-09-09 15:08:12 +00002858 if (unsigned ObjCIdRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002859 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002860 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump11289f42009-09-09 15:08:12 +00002861 if (unsigned ObjCClassRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002862 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002863 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002864 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpd0153282009-10-20 02:12:22 +00002865 Context->setBlockDescriptorType(GetType(String));
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002866 if (unsigned String
Sebastian Redl539c5062010-08-18 23:57:32 +00002867 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002868 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002869 if (unsigned ObjCSelRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002870 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002871 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002872 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002873 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002874
Sebastian Redl539c5062010-08-18 23:57:32 +00002875 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002876 Context->setInt128Installed();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002877
Richard Smith02e85f32011-04-14 22:09:26 +00002878 if (unsigned AutoDeduct = SpecialTypes[SPECIAL_TYPE_AUTO_DEDUCT])
2879 Context->AutoDeductTy = GetType(AutoDeduct);
2880 if (unsigned AutoRRefDeduct = SpecialTypes[SPECIAL_TYPE_AUTO_RREF_DEDUCT])
2881 Context->AutoRRefDeductTy = GetType(AutoRRefDeduct);
2882
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002883 ReadPragmaDiagnosticMappings(Context->getDiagnostics());
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002884
2885 // If there were any CUDA special declarations, deserialize them.
2886 if (!CUDASpecialDeclRefs.empty()) {
2887 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
2888 Context->setcudaConfigureCallDecl(
2889 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2890 }
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002891}
2892
Douglas Gregor45fe0362009-05-12 01:31:05 +00002893/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002894/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00002895/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002896std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00002897 FileManager &FileMgr,
Daniel Dunbar3b951482009-12-03 09:13:06 +00002898 Diagnostic &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002899 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002900 std::string ErrStr;
2901 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner5159f612010-11-23 08:35:12 +00002902 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00002903 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00002904 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002905 return std::string();
2906 }
2907
2908 // Initialize the stream
2909 llvm::BitstreamReader StreamFile;
2910 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00002911 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00002912 (const unsigned char *)Buffer->getBufferEnd());
2913 Stream.init(StreamFile);
2914
2915 // Sniff for the signature.
2916 if (Stream.Read(8) != 'C' ||
2917 Stream.Read(8) != 'P' ||
2918 Stream.Read(8) != 'C' ||
2919 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002920 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002921 return std::string();
2922 }
2923
2924 RecordData Record;
2925 while (!Stream.AtEndOfStream()) {
2926 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002927
Douglas Gregor45fe0362009-05-12 01:31:05 +00002928 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2929 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00002930
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002931 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002932 switch (BlockID) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002933 case AST_BLOCK_ID:
2934 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002935 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002936 return std::string();
2937 }
2938 break;
Mike Stump11289f42009-09-09 15:08:12 +00002939
Douglas Gregor45fe0362009-05-12 01:31:05 +00002940 default:
2941 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002942 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002943 return std::string();
2944 }
2945 break;
2946 }
2947 continue;
2948 }
2949
2950 if (Code == llvm::bitc::END_BLOCK) {
2951 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002952 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002953 return std::string();
2954 }
2955 continue;
2956 }
2957
2958 if (Code == llvm::bitc::DEFINE_ABBREV) {
2959 Stream.ReadAbbrevRecord();
2960 continue;
2961 }
2962
2963 Record.clear();
2964 const char *BlobStart = 0;
2965 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002966 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl539c5062010-08-18 23:57:32 +00002967 == ORIGINAL_FILE_NAME)
Douglas Gregor45fe0362009-05-12 01:31:05 +00002968 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00002969 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00002970
2971 return std::string();
2972}
2973
Douglas Gregor55abb232009-04-10 20:39:37 +00002974/// \brief Parse the record that corresponds to a LangOptions data
2975/// structure.
2976///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002977/// This routine parses the language options from the AST file and then gives
2978/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00002979///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002980/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002981bool ASTReader::ParseLanguageOptions(
Douglas Gregor55abb232009-04-10 20:39:37 +00002982 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002983 if (Listener) {
2984 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00002985
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002986 #define PARSE_LANGOPT(Option) \
2987 LangOpts.Option = Record[Idx]; \
2988 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00002989
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002990 unsigned Idx = 0;
2991 PARSE_LANGOPT(Trigraphs);
2992 PARSE_LANGOPT(BCPLComment);
2993 PARSE_LANGOPT(DollarIdents);
2994 PARSE_LANGOPT(AsmPreprocessor);
2995 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00002996 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002997 PARSE_LANGOPT(ImplicitInt);
2998 PARSE_LANGOPT(Digraphs);
2999 PARSE_LANGOPT(HexFloats);
3000 PARSE_LANGOPT(C99);
Peter Collingbournea686b5f2011-04-15 00:35:23 +00003001 PARSE_LANGOPT(C1X);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003002 PARSE_LANGOPT(Microsoft);
3003 PARSE_LANGOPT(CPlusPlus);
3004 PARSE_LANGOPT(CPlusPlus0x);
3005 PARSE_LANGOPT(CXXOperatorNames);
3006 PARSE_LANGOPT(ObjC1);
3007 PARSE_LANGOPT(ObjC2);
3008 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00003009 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00003010 PARSE_LANGOPT(AppleKext);
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00003011 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Douglas Gregora860e6a2011-06-14 23:20:43 +00003012 PARSE_LANGOPT(ObjCInferRelatedResultType);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00003013 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003014 PARSE_LANGOPT(PascalStrings);
3015 PARSE_LANGOPT(WritableStrings);
3016 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00003017 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003018 PARSE_LANGOPT(Exceptions);
Anders Carlssonce8dd3a2011-02-19 23:53:54 +00003019 PARSE_LANGOPT(ObjCExceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +00003020 PARSE_LANGOPT(CXXExceptions);
3021 PARSE_LANGOPT(SjLjExceptions);
Douglas Gregordbe39272011-02-01 15:15:22 +00003022 PARSE_LANGOPT(MSBitfields);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003023 PARSE_LANGOPT(NeXTRuntime);
3024 PARSE_LANGOPT(Freestanding);
3025 PARSE_LANGOPT(NoBuiltin);
3026 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00003027 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003028 PARSE_LANGOPT(Blocks);
3029 PARSE_LANGOPT(EmitAllDecls);
3030 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00003031 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
3032 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003033 PARSE_LANGOPT(HeinousExtensions);
3034 PARSE_LANGOPT(Optimize);
3035 PARSE_LANGOPT(OptimizeSize);
3036 PARSE_LANGOPT(Static);
3037 PARSE_LANGOPT(PICLevel);
3038 PARSE_LANGOPT(GNUInline);
3039 PARSE_LANGOPT(NoInline);
Chandler Carruth7ffce732011-04-23 20:05:38 +00003040 PARSE_LANGOPT(Deprecated);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003041 PARSE_LANGOPT(AccessControl);
3042 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00003043 PARSE_LANGOPT(ShortWChar);
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00003044 PARSE_LANGOPT(ShortEnums);
Chris Lattner51924e512010-06-26 21:25:03 +00003045 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall457a04e2010-10-22 21:05:15 +00003046 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00003047 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00003048 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003049 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00003050 PARSE_LANGOPT(OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00003051 PARSE_LANGOPT(CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00003052 PARSE_LANGOPT(CatchUndefined);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003053 PARSE_LANGOPT(DefaultFPContract);
Roman Divackydc1f68d2011-03-01 17:36:40 +00003054 PARSE_LANGOPT(ElideConstructors);
3055 PARSE_LANGOPT(SpellChecking);
Roman Divacky65b88cd2011-03-01 17:40:53 +00003056 PARSE_LANGOPT(MRTD);
John McCall31168b02011-06-15 23:02:42 +00003057 PARSE_LANGOPT(ObjCAutoRefCount);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003058 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00003059
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003060 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00003061 }
Douglas Gregor55abb232009-04-10 20:39:37 +00003062
3063 return false;
3064}
3065
Sebastian Redl2c499f62010-08-18 23:56:43 +00003066void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor92a96f52011-02-08 21:58:10 +00003067 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3068 PerFileData &F = *Chain[I];
3069 if (!F.PreprocessorDetailCursor.getBitStreamReader())
3070 continue;
3071
3072 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
3073 F.PreprocessorDetailCursor.JumpToBit(F.PreprocessorDetailStartOffset);
3074 while (LoadPreprocessedEntity(F)) { }
3075 }
Douglas Gregoraae92242010-03-19 21:51:54 +00003076}
3077
Douglas Gregor46c50012011-02-11 19:46:30 +00003078PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003079 PerFileData *F = 0;
3080 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3081 if (Offset < Chain[I]->SizeInBits) {
3082 F = Chain[I];
3083 break;
3084 }
3085
3086 Offset -= Chain[I]->SizeInBits;
3087 }
3088
3089 if (!F) {
3090 Error("Malformed preprocessed entity offset");
3091 return 0;
3092 }
3093
Douglas Gregor92a96f52011-02-08 21:58:10 +00003094 // Keep track of where we are in the stream, then jump back there
3095 // after reading this entity.
3096 SavedStreamPosition SavedPosition(F->PreprocessorDetailCursor);
3097 F->PreprocessorDetailCursor.JumpToBit(Offset);
3098 return LoadPreprocessedEntity(*F);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003099}
3100
Douglas Gregor09b69892011-02-10 17:09:37 +00003101HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
3102 HeaderFileInfoTrait Trait(FE->getName());
3103 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3104 PerFileData &F = *Chain[I];
3105 HeaderFileInfoLookupTable *Table
3106 = static_cast<HeaderFileInfoLookupTable *>(F.HeaderFileInfoTable);
3107 if (!Table)
3108 continue;
3109
3110 // Look in the on-disk hash table for an entry for this file name.
3111 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE->getName(),
3112 &Trait);
3113 if (Pos == Table->end())
3114 continue;
3115
3116 HeaderFileInfo HFI = *Pos;
3117 if (Listener)
3118 Listener->ReadHeaderFileInfo(HFI, FE->getUID());
3119
3120 return HFI;
3121 }
3122
3123 return HeaderFileInfo();
3124}
3125
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003126void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
Douglas Gregor925296b2011-07-19 16:10:42 +00003127 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3128 PerFileData &F = *Chain[I];
3129 unsigned Idx = 0;
3130 while (Idx < F.PragmaDiagMappings.size()) {
3131 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
3132 while (1) {
3133 assert(Idx < F.PragmaDiagMappings.size() &&
3134 "Invalid data, didn't find '-1' marking end of diag/map pairs");
3135 if (Idx >= F.PragmaDiagMappings.size()) {
3136 break; // Something is messed up but at least avoid infinite loop in
3137 // release build.
3138 }
3139 unsigned DiagID = F.PragmaDiagMappings[Idx++];
3140 if (DiagID == (unsigned)-1) {
3141 break; // no more diag/map pairs for this location.
3142 }
3143 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
3144 Diag.setDiagnosticMapping(DiagID, Map, Loc);
3145 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003146 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003147 }
3148}
3149
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003150/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003151ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003152 PerFileData *F = 0;
3153 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3154 F = Chain[N - I - 1];
3155 if (Index < F->LocalNumTypes)
3156 break;
3157 Index -= F->LocalNumTypes;
3158 }
3159 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redl2c373b92010-10-05 15:59:54 +00003160 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003161}
3162
3163/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003164///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003165/// The index is the type ID, shifted and minus the number of predefs. This
3166/// routine actually reads the record corresponding to the type at the given
3167/// location. It is a helper routine for GetType, which deals with reading type
3168/// IDs.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003169QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003170 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003171 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00003172
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003173 // Keep track of where we are in the stream, then jump back there
3174 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00003175 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003176
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003177 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00003178
Douglas Gregor1342e842009-07-06 18:54:52 +00003179 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003180 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00003181
Sebastian Redl2c373b92010-10-05 15:59:54 +00003182 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003183 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003184 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00003185 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3186 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003187 if (Record.size() != 2) {
3188 Error("Incorrect encoding of extended qualifier type");
3189 return QualType();
3190 }
Douglas Gregor455b8f42009-04-15 22:00:08 +00003191 QualType Base = GetType(Record[0]);
John McCall8ccfcb52009-09-24 19:53:00 +00003192 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
3193 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00003194 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003195
Sebastian Redl539c5062010-08-18 23:57:32 +00003196 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003197 if (Record.size() != 1) {
3198 Error("Incorrect encoding of complex type");
3199 return QualType();
3200 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003201 QualType ElemType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003202 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003203 }
3204
Sebastian Redl539c5062010-08-18 23:57:32 +00003205 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003206 if (Record.size() != 1) {
3207 Error("Incorrect encoding of pointer type");
3208 return QualType();
3209 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003210 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003211 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003212 }
3213
Sebastian Redl539c5062010-08-18 23:57:32 +00003214 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003215 if (Record.size() != 1) {
3216 Error("Incorrect encoding of block pointer type");
3217 return QualType();
3218 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003219 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003220 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003221 }
3222
Sebastian Redl539c5062010-08-18 23:57:32 +00003223 case TYPE_LVALUE_REFERENCE: {
Richard Smith0f538462011-04-12 10:38:03 +00003224 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003225 Error("Incorrect encoding of lvalue reference type");
3226 return QualType();
3227 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003228 QualType PointeeType = GetType(Record[0]);
Richard Smith0f538462011-04-12 10:38:03 +00003229 return Context->getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003230 }
3231
Sebastian Redl539c5062010-08-18 23:57:32 +00003232 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003233 if (Record.size() != 1) {
3234 Error("Incorrect encoding of rvalue reference type");
3235 return QualType();
3236 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003237 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003238 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003239 }
3240
Sebastian Redl539c5062010-08-18 23:57:32 +00003241 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00003242 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003243 Error("Incorrect encoding of member pointer type");
3244 return QualType();
3245 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003246 QualType PointeeType = GetType(Record[0]);
3247 QualType ClassType = GetType(Record[1]);
Douglas Gregor0cdc8322010-12-10 17:03:06 +00003248 if (PointeeType.isNull() || ClassType.isNull())
3249 return QualType();
3250
Chris Lattner8575daa2009-04-27 21:45:14 +00003251 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003252 }
3253
Sebastian Redl539c5062010-08-18 23:57:32 +00003254 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003255 QualType ElementType = GetType(Record[0]);
3256 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3257 unsigned IndexTypeQuals = Record[2];
3258 unsigned Idx = 3;
3259 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00003260 return Context->getConstantArrayType(ElementType, Size,
3261 ASM, IndexTypeQuals);
3262 }
3263
Sebastian Redl539c5062010-08-18 23:57:32 +00003264 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003265 QualType ElementType = GetType(Record[0]);
3266 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3267 unsigned IndexTypeQuals = Record[2];
Chris Lattner8575daa2009-04-27 21:45:14 +00003268 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003269 }
3270
Sebastian Redl539c5062010-08-18 23:57:32 +00003271 case TYPE_VARIABLE_ARRAY: {
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003272 QualType ElementType = GetType(Record[0]);
3273 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3274 unsigned IndexTypeQuals = Record[2];
Sebastian Redl2c373b92010-10-05 15:59:54 +00003275 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3276 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
3277 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor04318252009-07-06 15:59:29 +00003278 ASM, IndexTypeQuals,
3279 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003280 }
3281
Sebastian Redl539c5062010-08-18 23:57:32 +00003282 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00003283 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003284 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003285 return QualType();
3286 }
3287
3288 QualType ElementType = GetType(Record[0]);
3289 unsigned NumElements = Record[1];
Bob Wilsonaeb56442010-11-10 21:56:12 +00003290 unsigned VecKind = Record[2];
Chris Lattner37141f42010-06-23 06:00:24 +00003291 return Context->getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00003292 (VectorType::VectorKind)VecKind);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003293 }
3294
Sebastian Redl539c5062010-08-18 23:57:32 +00003295 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00003296 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003297 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003298 return QualType();
3299 }
3300
3301 QualType ElementType = GetType(Record[0]);
3302 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00003303 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003304 }
3305
Sebastian Redl539c5062010-08-18 23:57:32 +00003306 case TYPE_FUNCTION_NO_PROTO: {
John McCall31168b02011-06-15 23:02:42 +00003307 if (Record.size() != 6) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00003308 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003309 return QualType();
3310 }
3311 QualType ResultType = GetType(Record[0]);
John McCall31168b02011-06-15 23:02:42 +00003312 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
3313 (CallingConv)Record[4], Record[5]);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00003314 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003315 }
3316
Sebastian Redl539c5062010-08-18 23:57:32 +00003317 case TYPE_FUNCTION_PROTO: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003318 QualType ResultType = GetType(Record[0]);
John McCalldb40c7f2010-12-14 08:05:40 +00003319
3320 FunctionProtoType::ExtProtoInfo EPI;
3321 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003322 /*hasregparm*/ Record[2],
3323 /*regparm*/ Record[3],
John McCall31168b02011-06-15 23:02:42 +00003324 static_cast<CallingConv>(Record[4]),
3325 /*produces*/ Record[5]);
John McCalldb40c7f2010-12-14 08:05:40 +00003326
John McCall31168b02011-06-15 23:02:42 +00003327 unsigned Idx = 6;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003328 unsigned NumParams = Record[Idx++];
3329 llvm::SmallVector<QualType, 16> ParamTypes;
3330 for (unsigned I = 0; I != NumParams; ++I)
3331 ParamTypes.push_back(GetType(Record[Idx++]));
John McCalldb40c7f2010-12-14 08:05:40 +00003332
3333 EPI.Variadic = Record[Idx++];
3334 EPI.TypeQuals = Record[Idx++];
Douglas Gregordb9d6642011-01-26 05:01:58 +00003335 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003336 ExceptionSpecificationType EST =
3337 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3338 EPI.ExceptionSpecType = EST;
3339 if (EST == EST_Dynamic) {
3340 EPI.NumExceptions = Record[Idx++];
3341 llvm::SmallVector<QualType, 2> Exceptions;
3342 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
3343 Exceptions.push_back(GetType(Record[Idx++]));
3344 EPI.Exceptions = Exceptions.data();
3345 } else if (EST == EST_ComputedNoexcept) {
3346 EPI.NoexceptExpr = ReadExpr(*Loc.F);
3347 }
Jay Foad7d0479f2009-05-21 09:52:38 +00003348 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalldb40c7f2010-12-14 08:05:40 +00003349 EPI);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003350 }
3351
Sebastian Redl539c5062010-08-18 23:57:32 +00003352 case TYPE_UNRESOLVED_USING:
John McCallb96ec562009-12-04 22:46:56 +00003353 return Context->getTypeDeclType(
3354 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
3355
Sebastian Redl539c5062010-08-18 23:57:32 +00003356 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003357 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003358 Error("incorrect encoding of typedef type");
3359 return QualType();
3360 }
Richard Smithdda56e42011-04-15 14:24:37 +00003361 TypedefNameDecl *Decl = cast<TypedefNameDecl>(GetDecl(Record[0]));
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003362 QualType Canonical = GetType(Record[1]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003363 if (!Canonical.isNull())
3364 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003365 return Context->getTypedefType(Decl, Canonical);
3366 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003367
Sebastian Redl539c5062010-08-18 23:57:32 +00003368 case TYPE_TYPEOF_EXPR:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003369 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003370
Sebastian Redl539c5062010-08-18 23:57:32 +00003371 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003372 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003373 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003374 return QualType();
3375 }
3376 QualType UnderlyingType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003377 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003378 }
Mike Stump11289f42009-09-09 15:08:12 +00003379
Sebastian Redl539c5062010-08-18 23:57:32 +00003380 case TYPE_DECLTYPE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003381 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson81df7b82009-06-24 19:06:50 +00003382
Alexis Hunte852b102011-05-24 22:41:36 +00003383 case TYPE_UNARY_TRANSFORM: {
3384 QualType BaseType = GetType(Record[0]);
3385 QualType UnderlyingType = GetType(Record[1]);
3386 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
3387 return Context->getUnaryTransformType(BaseType, UnderlyingType, UKind);
3388 }
3389
Richard Smith30482bc2011-02-20 03:19:35 +00003390 case TYPE_AUTO:
3391 return Context->getAutoType(GetType(Record[0]));
3392
Sebastian Redl539c5062010-08-18 23:57:32 +00003393 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003394 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003395 Error("incorrect encoding of record type");
3396 return QualType();
3397 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003398 bool IsDependent = Record[0];
3399 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCall424cec92011-01-19 06:33:43 +00003400 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003401 return T;
3402 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003403
Sebastian Redl539c5062010-08-18 23:57:32 +00003404 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003405 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003406 Error("incorrect encoding of enum type");
3407 return QualType();
3408 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003409 bool IsDependent = Record[0];
3410 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCall424cec92011-01-19 06:33:43 +00003411 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003412 return T;
3413 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00003414
John McCall81904512011-01-06 01:58:22 +00003415 case TYPE_ATTRIBUTED: {
3416 if (Record.size() != 3) {
3417 Error("incorrect encoding of attributed type");
3418 return QualType();
3419 }
3420 QualType modifiedType = GetType(Record[0]);
3421 QualType equivalentType = GetType(Record[1]);
3422 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3423 return Context->getAttributedType(kind, modifiedType, equivalentType);
3424 }
3425
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003426 case TYPE_PAREN: {
3427 if (Record.size() != 1) {
3428 Error("incorrect encoding of paren type");
3429 return QualType();
3430 }
3431 QualType InnerType = GetType(Record[0]);
3432 return Context->getParenType(InnerType);
3433 }
3434
Douglas Gregord2fa7662010-12-20 02:24:11 +00003435 case TYPE_PACK_EXPANSION: {
Douglas Gregor17328502011-02-01 15:24:58 +00003436 if (Record.size() != 2) {
Douglas Gregord2fa7662010-12-20 02:24:11 +00003437 Error("incorrect encoding of pack expansion type");
3438 return QualType();
3439 }
3440 QualType Pattern = GetType(Record[0]);
3441 if (Pattern.isNull())
3442 return QualType();
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00003443 llvm::Optional<unsigned> NumExpansions;
3444 if (Record[1])
3445 NumExpansions = Record[1] - 1;
3446 return Context->getPackExpansionType(Pattern, NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00003447 }
3448
Sebastian Redl539c5062010-08-18 23:57:32 +00003449 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003450 unsigned Idx = 0;
3451 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3452 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3453 QualType NamedType = GetType(Record[Idx++]);
3454 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00003455 }
3456
Sebastian Redl539c5062010-08-18 23:57:32 +00003457 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00003458 unsigned Idx = 0;
3459 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00003460 return Context->getObjCInterfaceType(ItfD);
3461 }
3462
Sebastian Redl539c5062010-08-18 23:57:32 +00003463 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00003464 unsigned Idx = 0;
3465 QualType Base = GetType(Record[Idx++]);
Chris Lattner587cbe12009-04-22 06:45:28 +00003466 unsigned NumProtos = Record[Idx++];
3467 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
3468 for (unsigned I = 0; I != NumProtos; ++I)
3469 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003470 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00003471 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003472
Sebastian Redl539c5062010-08-18 23:57:32 +00003473 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00003474 unsigned Idx = 0;
John McCall8b07ec22010-05-15 11:32:37 +00003475 QualType Pointee = GetType(Record[Idx++]);
3476 return Context->getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00003477 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00003478
Sebastian Redl539c5062010-08-18 23:57:32 +00003479 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00003480 unsigned Idx = 0;
3481 QualType Parm = GetType(Record[Idx++]);
3482 QualType Replacement = GetType(Record[Idx++]);
3483 return
3484 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3485 Replacement);
3486 }
John McCalle78aac42010-03-10 03:28:59 +00003487
Douglas Gregorada4b792011-01-14 02:55:32 +00003488 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3489 unsigned Idx = 0;
3490 QualType Parm = GetType(Record[Idx++]);
3491 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3492 return Context->getSubstTemplateTypeParmPackType(
3493 cast<TemplateTypeParmType>(Parm),
3494 ArgPack);
3495 }
3496
Sebastian Redl539c5062010-08-18 23:57:32 +00003497 case TYPE_INJECTED_CLASS_NAME: {
John McCalle78aac42010-03-10 03:28:59 +00003498 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
3499 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003500 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003501 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003502 return
3503 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00003504 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003505
Sebastian Redl539c5062010-08-18 23:57:32 +00003506 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003507 unsigned Idx = 0;
3508 unsigned Depth = Record[Idx++];
3509 unsigned Index = Record[Idx++];
3510 bool Pack = Record[Idx++];
Chandler Carruth08836322011-05-01 00:51:33 +00003511 TemplateTypeParmDecl *D =
3512 cast_or_null<TemplateTypeParmDecl>(GetDecl(Record[Idx++]));
3513 return Context->getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003514 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003515
Sebastian Redl539c5062010-08-18 23:57:32 +00003516 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003517 unsigned Idx = 0;
3518 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3519 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3520 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00003521 QualType Canon = GetType(Record[Idx++]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003522 if (!Canon.isNull())
3523 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00003524 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003525 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003526
Sebastian Redl539c5062010-08-18 23:57:32 +00003527 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003528 unsigned Idx = 0;
3529 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3530 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3531 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3532 unsigned NumArgs = Record[Idx++];
3533 llvm::SmallVector<TemplateArgument, 8> Args;
3534 Args.reserve(NumArgs);
3535 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003536 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003537 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3538 Args.size(), Args.data());
3539 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003540
Sebastian Redl539c5062010-08-18 23:57:32 +00003541 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003542 unsigned Idx = 0;
3543
3544 // ArrayType
3545 QualType ElementType = GetType(Record[Idx++]);
3546 ArrayType::ArraySizeModifier ASM
3547 = (ArrayType::ArraySizeModifier)Record[Idx++];
3548 unsigned IndexTypeQuals = Record[Idx++];
3549
3550 // DependentSizedArrayType
Sebastian Redl2c373b92010-10-05 15:59:54 +00003551 Expr *NumElts = ReadExpr(*Loc.F);
3552 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003553
3554 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3555 IndexTypeQuals, Brackets);
3556 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003557
Sebastian Redl539c5062010-08-18 23:57:32 +00003558 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003559 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003560 bool IsDependent = Record[Idx++];
Douglas Gregor5590be02011-01-15 06:45:20 +00003561 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003562 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003563 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Richard Smith3f1b5d02011-05-05 21:57:07 +00003564 QualType Underlying = GetType(Record[Idx++]);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003565 QualType T;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003566 if (Underlying.isNull())
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003567 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3568 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003569 else
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003570 T = Context->getTemplateSpecializationType(Name, Args.data(),
Richard Smith3f1b5d02011-05-05 21:57:07 +00003571 Args.size(), Underlying);
John McCall424cec92011-01-19 06:33:43 +00003572 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003573 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003574 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003575 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003576 // Suppress a GCC warning
3577 return QualType();
3578}
3579
Sebastian Redl2c373b92010-10-05 15:59:54 +00003580class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00003581 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003582 ASTReader::PerFileData &F;
Sebastian Redlc67764e2010-07-22 22:43:28 +00003583 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redl2c499f62010-08-18 23:56:43 +00003584 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00003585 unsigned &Idx;
3586
Sebastian Redl2c373b92010-10-05 15:59:54 +00003587 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3588 unsigned &I) {
3589 return Reader.ReadSourceLocation(F, R, I);
3590 }
3591
John McCall8f115c62009-10-16 21:56:05 +00003592public:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003593 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl2c499f62010-08-18 23:56:43 +00003594 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003595 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3596 { }
John McCall8f115c62009-10-16 21:56:05 +00003597
John McCall17001972009-10-18 01:05:36 +00003598 // We want compile-time assurance that we've enumerated all of
3599 // these, so unfortunately we have to declare them first, then
3600 // define them out-of-line.
3601#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00003602#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00003603 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00003604#include "clang/AST/TypeLocNodes.def"
3605
John McCall17001972009-10-18 01:05:36 +00003606 void VisitFunctionTypeLoc(FunctionTypeLoc);
3607 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00003608};
3609
John McCall17001972009-10-18 01:05:36 +00003610void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00003611 // nothing to do
3612}
John McCall17001972009-10-18 01:05:36 +00003613void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003614 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorc9b7a592010-01-18 18:04:31 +00003615 if (TL.needsExtraLocalData()) {
3616 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3617 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3618 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3619 TL.setModeAttr(Record[Idx++]);
3620 }
John McCall8f115c62009-10-16 21:56:05 +00003621}
John McCall17001972009-10-18 01:05:36 +00003622void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003623 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003624}
John McCall17001972009-10-18 01:05:36 +00003625void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003626 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003627}
John McCall17001972009-10-18 01:05:36 +00003628void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003629 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003630}
John McCall17001972009-10-18 01:05:36 +00003631void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003632 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003633}
John McCall17001972009-10-18 01:05:36 +00003634void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003635 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003636}
John McCall17001972009-10-18 01:05:36 +00003637void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003638 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara509357842011-03-05 14:42:21 +00003639 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003640}
John McCall17001972009-10-18 01:05:36 +00003641void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003642 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3643 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003644 if (Record[Idx++])
Sebastian Redl2c373b92010-10-05 15:59:54 +00003645 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003646 else
John McCall17001972009-10-18 01:05:36 +00003647 TL.setSizeExpr(0);
3648}
3649void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3650 VisitArrayTypeLoc(TL);
3651}
3652void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3653 VisitArrayTypeLoc(TL);
3654}
3655void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3656 VisitArrayTypeLoc(TL);
3657}
3658void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3659 DependentSizedArrayTypeLoc TL) {
3660 VisitArrayTypeLoc(TL);
3661}
3662void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3663 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003664 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003665}
3666void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003667 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003668}
3669void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003670 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003671}
3672void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003673 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
3674 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Douglas Gregor7fb25412010-10-01 18:44:50 +00003675 TL.setTrailingReturn(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00003676 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCalle6347002009-10-23 01:28:53 +00003677 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall17001972009-10-18 01:05:36 +00003678 }
3679}
3680void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3681 VisitFunctionTypeLoc(TL);
3682}
3683void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3684 VisitFunctionTypeLoc(TL);
3685}
John McCallb96ec562009-12-04 22:46:56 +00003686void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003687 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallb96ec562009-12-04 22:46:56 +00003688}
John McCall17001972009-10-18 01:05:36 +00003689void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003690 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003691}
3692void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003693 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3694 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3695 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003696}
3697void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003698 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3699 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3700 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3701 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003702}
3703void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003704 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003705}
Alexis Hunte852b102011-05-24 22:41:36 +00003706void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3707 TL.setKWLoc(ReadSourceLocation(Record, Idx));
3708 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3709 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3710 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3711}
Richard Smith30482bc2011-02-20 03:19:35 +00003712void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3713 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3714}
John McCall17001972009-10-18 01:05:36 +00003715void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003716 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003717}
3718void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003719 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003720}
John McCall81904512011-01-06 01:58:22 +00003721void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3722 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3723 if (TL.hasAttrOperand()) {
3724 SourceRange range;
3725 range.setBegin(ReadSourceLocation(Record, Idx));
3726 range.setEnd(ReadSourceLocation(Record, Idx));
3727 TL.setAttrOperandParensRange(range);
3728 }
3729 if (TL.hasAttrExprOperand()) {
3730 if (Record[Idx++])
3731 TL.setAttrExprOperand(Reader.ReadExpr(F));
3732 else
3733 TL.setAttrExprOperand(0);
3734 } else if (TL.hasAttrEnumOperand())
3735 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3736}
John McCall17001972009-10-18 01:05:36 +00003737void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003738 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003739}
John McCallcebee162009-10-18 09:09:24 +00003740void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3741 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003742 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallcebee162009-10-18 09:09:24 +00003743}
Douglas Gregorada4b792011-01-14 02:55:32 +00003744void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3745 SubstTemplateTypeParmPackTypeLoc TL) {
3746 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3747}
John McCall17001972009-10-18 01:05:36 +00003748void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3749 TemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003750 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3751 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3752 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall0ad16662009-10-29 08:12:44 +00003753 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3754 TL.setArgLocInfo(i,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003755 Reader.GetTemplateArgumentLocInfo(F,
3756 TL.getTypePtr()->getArg(i).getKind(),
3757 Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003758}
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003759void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3760 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3761 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3762}
Abramo Bagnara6150c882010-05-11 21:36:43 +00003763void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003764 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor844cb502011-03-01 18:12:44 +00003765 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003766}
John McCalle78aac42010-03-10 03:28:59 +00003767void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003768 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalle78aac42010-03-10 03:28:59 +00003769}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00003770void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003771 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor3d0da5f2011-03-01 01:34:45 +00003772 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00003773 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003774}
John McCallc392f372010-06-11 00:33:02 +00003775void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3776 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003777 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregora7a795b2011-03-01 20:11:18 +00003778 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00003779 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3780 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3781 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003782 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3783 TL.setArgLocInfo(I,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003784 Reader.GetTemplateArgumentLocInfo(F,
3785 TL.getTypePtr()->getArg(I).getKind(),
3786 Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003787}
Douglas Gregord2fa7662010-12-20 02:24:11 +00003788void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3789 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3790}
John McCall17001972009-10-18 01:05:36 +00003791void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003792 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8b07ec22010-05-15 11:32:37 +00003793}
3794void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3795 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003796 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3797 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003798 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003799 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003800}
John McCallfc93cf92009-10-22 22:37:11 +00003801void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003802 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCallfc93cf92009-10-22 22:37:11 +00003803}
John McCall8f115c62009-10-16 21:56:05 +00003804
Sebastian Redl2c373b92010-10-05 15:59:54 +00003805TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003806 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00003807 unsigned &Idx) {
3808 QualType InfoTy = GetType(Record[Idx++]);
3809 if (InfoTy.isNull())
3810 return 0;
3811
John McCallbcd03502009-12-07 02:54:59 +00003812 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003813 TypeLocReader TLR(*this, F, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00003814 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00003815 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00003816 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00003817}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003818
Sebastian Redl539c5062010-08-18 23:57:32 +00003819QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00003820 unsigned FastQuals = ID & Qualifiers::FastMask;
3821 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003822
Sebastian Redl539c5062010-08-18 23:57:32 +00003823 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003824 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00003825 switch ((PredefinedTypeIDs)Index) {
3826 case PREDEF_TYPE_NULL_ID: return QualType();
3827 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3828 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003829
Sebastian Redl539c5062010-08-18 23:57:32 +00003830 case PREDEF_TYPE_CHAR_U_ID:
3831 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003832 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00003833 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003834 break;
3835
Sebastian Redl539c5062010-08-18 23:57:32 +00003836 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3837 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3838 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3839 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3840 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3841 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3842 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3843 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3844 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3845 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3846 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3847 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3848 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3849 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3850 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3851 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3852 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
John McCall0009fcc2011-04-26 20:42:42 +00003853 case PREDEF_TYPE_BOUND_MEMBER: T = Context->BoundMemberTy; break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003854 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
John McCall31996342011-04-07 08:22:57 +00003855 case PREDEF_TYPE_UNKNOWN_ANY: T = Context->UnknownAnyTy; break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003856 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3857 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3858 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3859 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3860 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3861 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003862 }
3863
3864 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00003865 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003866 }
3867
Sebastian Redl539c5062010-08-18 23:57:32 +00003868 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003869 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00003870 if (TypesLoaded[Index].isNull()) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003871 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003872 if (TypesLoaded[Index].isNull())
3873 return QualType();
3874
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003875 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003876 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003877 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003878 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003879 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00003880 }
Mike Stump11289f42009-09-09 15:08:12 +00003881
John McCall8ccfcb52009-09-24 19:53:00 +00003882 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003883}
3884
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003885TypeID ASTReader::GetTypeID(QualType T) const {
3886 return MakeTypeID(T,
3887 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3888}
3889
3890TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3891 if (T.isNull())
3892 return TypeIdx();
3893 assert(!T.getLocalFastQualifiers());
3894
3895 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3896 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3897 // comparing keys of ASTDeclContextNameLookupTable.
3898 // If the type didn't come from the AST file use a specially marked index
3899 // so that any hash/key comparison fail since no such index is stored
3900 // in a AST file.
3901 if (I == TypeIdxs.end())
3902 return TypeIdx(-1);
3903 return I->second;
3904}
3905
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003906unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3907 unsigned Result = 0;
3908 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3909 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3910
3911 return Result;
3912}
3913
John McCall0ad16662009-10-29 08:12:44 +00003914TemplateArgumentLocInfo
Sebastian Redl2c373b92010-10-05 15:59:54 +00003915ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3916 TemplateArgument::ArgKind Kind,
John McCall0ad16662009-10-29 08:12:44 +00003917 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003918 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00003919 switch (Kind) {
3920 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003921 return ReadExpr(F);
John McCall0ad16662009-10-29 08:12:44 +00003922 case TemplateArgument::Type:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003923 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003924 case TemplateArgument::Template: {
Douglas Gregor9d802122011-03-02 17:09:35 +00003925 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3926 Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003927 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00003928 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003929 SourceLocation());
3930 }
3931 case TemplateArgument::TemplateExpansion: {
Douglas Gregor9d802122011-03-02 17:09:35 +00003932 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3933 Index);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003934 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003935 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00003936 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregoreb29d182011-01-05 17:40:24 +00003937 EllipsisLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003938 }
John McCall0ad16662009-10-29 08:12:44 +00003939 case TemplateArgument::Null:
3940 case TemplateArgument::Integral:
3941 case TemplateArgument::Declaration:
3942 case TemplateArgument::Pack:
3943 return TemplateArgumentLocInfo();
3944 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00003945 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00003946 return TemplateArgumentLocInfo();
3947}
3948
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003949TemplateArgumentLoc
Sebastian Redl2c373b92010-10-05 15:59:54 +00003950ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003951 const RecordData &Record, unsigned &Index) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003952 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003953
3954 if (Arg.getKind() == TemplateArgument::Expression) {
3955 if (Record[Index++]) // bool InfoHasSameExpr.
3956 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3957 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00003958 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003959 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003960}
3961
Sebastian Redl2c499f62010-08-18 23:56:43 +00003962Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00003963 return GetDecl(ID);
3964}
3965
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003966uint64_t
3967ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3968 if (ID == 0)
3969 return 0;
3970
3971 --ID;
3972 uint64_t Offset = 0;
3973 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003974 PerFileData &F = *Chain[N - I - 1];
3975
3976 if (ID < F.LocalNumCXXBaseSpecifiers)
3977 return Offset + F.CXXBaseSpecifiersOffsets[ID];
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003978
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003979 ID -= F.LocalNumCXXBaseSpecifiers;
3980 Offset += F.SizeInBits;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003981 }
3982
3983 assert(false && "CXXBaseSpecifiers not found");
3984 return 0;
3985}
3986
3987CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3988 // Figure out which AST file contains this offset.
3989 PerFileData *F = 0;
3990 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003991 if (Offset < Chain[N - I - 1]->SizeInBits) {
3992 F = Chain[N - I - 1];
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003993 break;
3994 }
3995
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003996 Offset -= Chain[N - I - 1]->SizeInBits;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003997 }
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003998
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003999 if (!F) {
4000 Error("Malformed AST file: C++ base specifiers at impossible offset");
4001 return 0;
4002 }
4003
4004 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
4005 SavedStreamPosition SavedPosition(Cursor);
4006 Cursor.JumpToBit(Offset);
4007 ReadingKindTracker ReadingKind(Read_Decl, *this);
4008 RecordData Record;
4009 unsigned Code = Cursor.ReadCode();
4010 unsigned RecCode = Cursor.ReadRecord(Code, Record);
4011 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4012 Error("Malformed AST file: missing C++ base specifiers");
4013 return 0;
4014 }
4015
4016 unsigned Idx = 0;
4017 unsigned NumBases = Record[Idx++];
4018 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
4019 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4020 for (unsigned I = 0; I != NumBases; ++I)
4021 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
4022 return Bases;
4023}
4024
Sebastian Redl2c499f62010-08-18 23:56:43 +00004025TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004026 if (!DeclsLoaded[0]) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004027 ReadDeclRecord(0, 1);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004028 if (DeserializationListener)
Sebastian Redl1ea025b2010-07-16 16:36:56 +00004029 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004030 }
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00004031
4032 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
4033}
4034
Sebastian Redl539c5062010-08-18 23:57:32 +00004035Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004036 if (ID == 0)
4037 return 0;
4038
Douglas Gregor745ed142009-04-25 18:35:21 +00004039 if (ID > DeclsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004040 Error("declaration ID out-of-range for AST file");
Douglas Gregor745ed142009-04-25 18:35:21 +00004041 return 0;
4042 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004043
Douglas Gregor745ed142009-04-25 18:35:21 +00004044 unsigned Index = ID - 1;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004045 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00004046 ReadDeclRecord(Index, ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004047 if (DeserializationListener)
4048 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
4049 }
Douglas Gregor745ed142009-04-25 18:35:21 +00004050
4051 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004052}
4053
Chris Lattner9c28af02009-04-27 05:46:25 +00004054/// \brief Resolve the offset of a statement into a statement.
4055///
4056/// This operation will read a new statement from the external
4057/// source each time it is called, and is meant to be used via a
4058/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00004059Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00004060 // Switch case IDs are per Decl.
4061 ClearSwitchCaseIDs();
4062
Sebastian Redl5c415f32010-07-22 17:01:13 +00004063 // Offset here is a global offset across the entire chain.
4064 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4065 PerFileData &F = *Chain[N - I - 1];
4066 if (Offset < F.SizeInBits) {
4067 // Since we know that this statement is part of a decl, make sure to use
4068 // the decl cursor to read it.
4069 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004070 return ReadStmtFromStream(F);
Sebastian Redl5c415f32010-07-22 17:01:13 +00004071 }
4072 Offset -= F.SizeInBits;
4073 }
4074 llvm_unreachable("Broken chain");
Douglas Gregor3c3aa612009-04-18 00:07:54 +00004075}
4076
Douglas Gregor3d0adb32011-07-15 21:46:17 +00004077ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00004078 bool (*isKindWeWant)(Decl::Kind),
John McCall75b960e2010-06-01 09:23:16 +00004079 llvm::SmallVectorImpl<Decl*> &Decls) {
Sebastian Redl5c415f32010-07-22 17:01:13 +00004080 // There might be lexical decls in multiple parts of the chain, for the TU
4081 // at least.
Sebastian Redlda6a21c2010-09-28 02:24:44 +00004082 // DeclContextOffsets might reallocate as we load additional decls below,
4083 // so make a copy of the vector.
4084 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl5c415f32010-07-22 17:01:13 +00004085 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
4086 I != E; ++I) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004087 // IDs can be 0 if this context doesn't contain declarations.
4088 if (!I->LexicalDecls)
Sebastian Redl5c415f32010-07-22 17:01:13 +00004089 continue;
Sebastian Redl5c415f32010-07-22 17:01:13 +00004090
4091 // Load all of the declaration IDs
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00004092 for (const KindDeclIDPair *ID = I->LexicalDecls,
4093 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
4094 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
4095 continue;
4096
4097 Decl *D = GetDecl(ID->second);
Sebastian Redlda6a21c2010-09-28 02:24:44 +00004098 assert(D && "Null decl in lexical decls");
4099 Decls.push_back(D);
4100 }
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004101 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004102
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004103 ++NumLexicalDeclContextsRead;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00004104 return ELR_Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004105}
4106
John McCall75b960e2010-06-01 09:23:16 +00004107DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00004108ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00004109 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00004110 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004111 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004112 if (!Name)
4113 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
4114 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004115
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004116 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl471ac2f2010-08-24 00:49:55 +00004117 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl9617e7e2010-08-24 00:50:16 +00004118 // and namespaces. For any given name, the last available results replace
4119 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl5c415f32010-07-22 17:01:13 +00004120 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl9617e7e2010-08-24 00:50:16 +00004121 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl5c415f32010-07-22 17:01:13 +00004122 I != E; ++I) {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004123 if (!I->NameLookupTableData)
Sebastian Redl5c415f32010-07-22 17:01:13 +00004124 continue;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004125
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004126 ASTDeclContextNameLookupTable *LookupTable =
4127 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
4128 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
4129 if (Pos == LookupTable->end())
Sebastian Redl5c415f32010-07-22 17:01:13 +00004130 continue;
4131
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004132 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
4133 for (; Data.first != Data.second; ++Data.first)
4134 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl9617e7e2010-08-24 00:50:16 +00004135 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004136 }
4137
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004138 ++NumVisibleDeclContextsRead;
John McCall75b960e2010-06-01 09:23:16 +00004139
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004140 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00004141 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004142}
4143
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00004144void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
4145 assert(DC->hasExternalVisibleStorage() &&
4146 "DeclContext has no visible decls in storage");
4147
4148 llvm::SmallVector<NamedDecl *, 64> Decls;
4149 // There might be visible decls in multiple parts of the chain, for the TU
4150 // and namespaces.
4151 DeclContextInfos &Infos = DeclContextOffsets[DC];
4152 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
4153 I != E; ++I) {
4154 if (!I->NameLookupTableData)
4155 continue;
4156
4157 ASTDeclContextNameLookupTable *LookupTable =
4158 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
4159 for (ASTDeclContextNameLookupTable::item_iterator
4160 ItemI = LookupTable->item_begin(),
4161 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
4162 ASTDeclContextNameLookupTable::item_iterator::value_type Val
4163 = *ItemI;
4164 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
4165 Decls.clear();
4166 for (; Data.first != Data.second; ++Data.first)
4167 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
4168 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
4169 }
4170 }
4171}
4172
Sebastian Redl2c499f62010-08-18 23:56:43 +00004173void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004174 assert(Consumer);
4175 while (!InterestingDecls.empty()) {
4176 DeclGroupRef DG(InterestingDecls.front());
4177 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00004178 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004179 }
4180}
4181
Sebastian Redl2c499f62010-08-18 23:56:43 +00004182void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00004183 this->Consumer = Consumer;
4184
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004185 if (!Consumer)
4186 return;
4187
4188 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004189 // Force deserialization of this decl, which will cause it to be queued for
4190 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00004191 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004192 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00004193
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004194 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004195}
4196
Sebastian Redl2c499f62010-08-18 23:56:43 +00004197void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004198 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004199
Mike Stump11289f42009-09-09 15:08:12 +00004200 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00004201 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00004202 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00004203 unsigned NumDeclsLoaded
4204 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
4205 (Decl *)0);
4206 unsigned NumIdentifiersLoaded
4207 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
4208 IdentifiersLoaded.end(),
4209 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00004210 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00004211 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
4212 SelectorsLoaded.end(),
4213 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00004214
Douglas Gregorc5046832009-04-27 18:38:38 +00004215 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
4216 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor258ae542009-04-27 06:38:32 +00004217 if (TotalNumSLocEntries)
4218 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
4219 NumSLocEntriesRead, TotalNumSLocEntries,
4220 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00004221 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004222 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00004223 NumTypesLoaded, (unsigned)TypesLoaded.size(),
4224 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
4225 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004226 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00004227 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
4228 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00004229 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004230 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00004231 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
4232 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00004233 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004234 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00004235 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
4236 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00004237 if (TotalNumStatements)
4238 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
4239 NumStatementsRead, TotalNumStatements,
4240 ((float)NumStatementsRead/TotalNumStatements * 100));
4241 if (TotalNumMacros)
4242 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
4243 NumMacrosRead, TotalNumMacros,
4244 ((float)NumMacrosRead/TotalNumMacros * 100));
4245 if (TotalLexicalDeclContexts)
4246 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
4247 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4248 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4249 * 100));
4250 if (TotalVisibleDeclContexts)
4251 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
4252 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4253 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4254 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004255 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004256 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004257 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4258 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00004259 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004260 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00004261 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004262 std::fprintf(stderr, "\n");
4263}
4264
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00004265/// Return the amount of memory used by memory buffers, breaking down
4266/// by heap-backed versus mmap'ed memory.
4267void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
4268 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4269 if (llvm::MemoryBuffer *buf = Chain[i]->Buffer.get()) {
4270 size_t bytes = buf->getBufferSize();
4271 switch (buf->getBufferKind()) {
4272 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
4273 sizes.malloc_bytes += bytes;
4274 break;
4275 case llvm::MemoryBuffer::MemoryBuffer_MMap:
4276 sizes.mmap_bytes += bytes;
4277 break;
4278 }
4279 }
4280}
4281
Sebastian Redl2c499f62010-08-18 23:56:43 +00004282void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004283 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004284 S.ExternalSource = this;
4285
Douglas Gregor7cd60f72009-04-22 21:15:06 +00004286 // Makes sure any declarations that were deserialized "too early"
4287 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004288 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4289 if (SemaObj->TUScope)
John McCall48871652010-08-21 09:40:31 +00004290 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004291
4292 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004293 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00004294 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00004295
4296 // If there were any tentative definitions, deserialize them and add
Sebastian Redl35351a92010-01-31 22:27:38 +00004297 // them to Sema's list of tentative definitions.
Douglas Gregord4df8652009-04-22 22:02:47 +00004298 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4299 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redl35351a92010-01-31 22:27:38 +00004300 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregord4df8652009-04-22 22:02:47 +00004301 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00004302
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004303 // If there were any unused file scoped decls, deserialize them and add to
4304 // Sema's list of unused file scoped decls.
4305 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4306 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4307 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattner90073802010-02-12 00:07:30 +00004308 }
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004309
Alexis Hunt27a761d2011-05-04 23:29:54 +00004310 // If there were any delegating constructors, add them to Sema's list
4311 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
4312 CXXConstructorDecl *D
4313 = cast<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
4314 SemaObj->DelegatingCtorDecls.push_back(D);
4315 }
4316
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004317 // If there were any locally-scoped external declarations,
4318 // deserialize them and add them to Sema's table of locally-scoped
4319 // external declarations.
4320 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4321 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4322 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
4323 }
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004324
4325 // If there were any ext_vector type declarations, deserialize them
4326 // and add them to Sema's vector of such declarations.
4327 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
4328 SemaObj->ExtVectorDecls.push_back(
Richard Smithdda56e42011-04-15 14:24:37 +00004329 cast<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004330
4331 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
4332 // Can we cut them down before writing them ?
4333
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004334 // If there were any dynamic classes declarations, deserialize them
4335 // and add them to Sema's vector of such declarations.
4336 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
4337 SemaObj->DynamicClasses.push_back(
4338 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004339
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004340 // Load the offsets of the declarations that Sema references.
4341 // They will be lazily deserialized when needed.
4342 if (!SemaDeclRefs.empty()) {
4343 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
4344 SemaObj->StdNamespace = SemaDeclRefs[0];
4345 SemaObj->StdBadAlloc = SemaDeclRefs[1];
4346 }
4347
Sebastian Redl2c373b92010-10-05 15:59:54 +00004348 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
4349
4350 // If there are @selector references added them to its pool. This is for
4351 // implementation of -Wselector.
4352 if (!F->ReferencedSelectorsData.empty()) {
4353 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
4354 unsigned I = 0;
4355 while (I < DataSize) {
4356 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
4357 SourceLocation SelLoc = ReadSourceLocation(
4358 *F, F->ReferencedSelectorsData, I);
4359 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
4360 }
4361 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00004362 }
4363
Sebastian Redl14afaf02011-04-24 16:27:30 +00004364 // The special data sets below always come from the most recent PCH,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004365 // which is at the front of the chain.
4366 PerFileData &F = *Chain.front();
4367
Sebastian Redl14afaf02011-04-24 16:27:30 +00004368 // If there were any pending implicit instantiations, deserialize them
4369 // and add them to Sema's queue of such instantiations.
4370 assert(F.PendingInstantiations.size() % 2 == 0 &&
4371 "Expected pairs of entries");
4372 for (unsigned Idx = 0, N = F.PendingInstantiations.size(); Idx < N;) {
4373 ValueDecl *D=cast<ValueDecl>(GetDecl(F.PendingInstantiations[Idx++]));
4374 SourceLocation Loc = ReadSourceLocation(F, F.PendingInstantiations,Idx);
4375 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
4376 }
4377
Sebastian Redl2c373b92010-10-05 15:59:54 +00004378 // If there were any weak undeclared identifiers, deserialize them and add to
4379 // Sema's list of weak undeclared identifiers.
4380 if (!WeakUndeclaredIdentifiers.empty()) {
4381 unsigned Idx = 0;
4382 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
4383 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4384 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4385 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
4386 bool Used = WeakUndeclaredIdentifiers[Idx++];
4387 Sema::WeakInfo WI(AliasId, Loc);
4388 WI.setUsed(Used);
4389 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
4390 }
4391 }
4392
4393 // If there were any VTable uses, deserialize the information and add it
4394 // to Sema's vector and map of VTable uses.
4395 if (!VTableUses.empty()) {
4396 unsigned Idx = 0;
4397 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
4398 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4399 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
4400 bool DefinitionRequired = VTableUses[Idx++];
4401 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
4402 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004403 }
4404 }
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004405
4406 if (!FPPragmaOptions.empty()) {
4407 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4408 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4409 }
4410
4411 if (!OpenCLExtensions.empty()) {
4412 unsigned I = 0;
4413#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4414#include "clang/Basic/OpenCLExtensions.def"
4415
4416 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4417 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00004418}
4419
Sebastian Redl2c499f62010-08-18 23:56:43 +00004420IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redl78f51772010-08-02 18:30:12 +00004421 // Try to find this name within our on-disk hash tables. We start with the
4422 // most recent one, since that one contains the most up-to-date info.
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004423 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004424 ASTIdentifierLookupTable *IdTable
4425 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl5c415f32010-07-22 17:01:13 +00004426 if (!IdTable)
4427 continue;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004428 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004429 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004430 if (Pos == IdTable->end())
4431 continue;
Douglas Gregora868bbd2009-04-21 22:25:48 +00004432
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004433 // Dereferencing the iterator has the effect of building the
4434 // IdentifierInfo node and populating it with the various
4435 // declarations it needs.
Sebastian Redl78f51772010-08-02 18:30:12 +00004436 return *Pos;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004437 }
Sebastian Redl78f51772010-08-02 18:30:12 +00004438 return 0;
Douglas Gregora868bbd2009-04-21 22:25:48 +00004439}
4440
Douglas Gregor57756ea2010-10-14 22:11:03 +00004441namespace clang {
4442 /// \brief An identifier-lookup iterator that enumerates all of the
4443 /// identifiers stored within a set of AST files.
4444 class ASTIdentifierIterator : public IdentifierIterator {
4445 /// \brief The AST reader whose identifiers are being enumerated.
4446 const ASTReader &Reader;
4447
4448 /// \brief The current index into the chain of AST files stored in
4449 /// the AST reader.
4450 unsigned Index;
4451
4452 /// \brief The current position within the identifier lookup table
4453 /// of the current AST file.
4454 ASTIdentifierLookupTable::key_iterator Current;
4455
4456 /// \brief The end position within the identifier lookup table of
4457 /// the current AST file.
4458 ASTIdentifierLookupTable::key_iterator End;
4459
4460 public:
4461 explicit ASTIdentifierIterator(const ASTReader &Reader);
4462
4463 virtual llvm::StringRef Next();
4464 };
4465}
4466
4467ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
4468 : Reader(Reader), Index(Reader.Chain.size() - 1) {
4469 ASTIdentifierLookupTable *IdTable
4470 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4471 Current = IdTable->key_begin();
4472 End = IdTable->key_end();
4473}
4474
4475llvm::StringRef ASTIdentifierIterator::Next() {
4476 while (Current == End) {
4477 // If we have exhausted all of our AST files, we're done.
4478 if (Index == 0)
4479 return llvm::StringRef();
4480
4481 --Index;
4482 ASTIdentifierLookupTable *IdTable
4483 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4484 Current = IdTable->key_begin();
4485 End = IdTable->key_end();
4486 }
4487
4488 // We have any identifiers remaining in the current AST file; return
4489 // the next one.
4490 std::pair<const char*, unsigned> Key = *Current;
4491 ++Current;
4492 return llvm::StringRef(Key.first, Key.second);
4493}
4494
4495IdentifierIterator *ASTReader::getIdentifiers() const {
4496 return new ASTIdentifierIterator(*this);
4497}
4498
Mike Stump11289f42009-09-09 15:08:12 +00004499std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redl2c499f62010-08-18 23:56:43 +00004500ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redlada023c2010-08-04 20:40:17 +00004501 // Find this selector in a hash table. We want to find the most recent entry.
4502 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4503 PerFileData &F = *Chain[I];
4504 if (!F.SelectorLookupTable)
4505 continue;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004506
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004507 ASTSelectorLookupTable *PoolTable
4508 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
4509 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redlada023c2010-08-04 20:40:17 +00004510 if (Pos != PoolTable->end()) {
4511 ++NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004512 // FIXME: Not quite happy with the statistics here. We probably should
4513 // disable this tracking when called via LoadSelector.
4514 // Also, should entries without methods count as misses?
4515 ++NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004516 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redlada023c2010-08-04 20:40:17 +00004517 if (DeserializationListener)
4518 DeserializationListener->SelectorRead(Data.ID, Sel);
4519 return std::make_pair(Data.Instance, Data.Factory);
4520 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004521 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00004522
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004523 ++NumMethodPoolMisses;
Sebastian Redlada023c2010-08-04 20:40:17 +00004524 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorc78d3462009-04-24 21:10:55 +00004525}
4526
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004527void ASTReader::ReadKnownNamespaces(
4528 llvm::SmallVectorImpl<NamespaceDecl *> &Namespaces) {
4529 Namespaces.clear();
4530
4531 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
4532 if (NamespaceDecl *Namespace
4533 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
4534 Namespaces.push_back(Namespace);
4535 }
4536}
4537
Sebastian Redl2c499f62010-08-18 23:56:43 +00004538void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004539 // It would be complicated to avoid reading the methods anyway. So don't.
4540 ReadMethodPool(Sel);
4541}
4542
Sebastian Redl2c499f62010-08-18 23:56:43 +00004543void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004544 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00004545 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00004546 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00004547 if (DeserializationListener)
4548 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004549}
4550
Douglas Gregor1342e842009-07-06 18:54:52 +00004551/// \brief Set the globally-visible declarations associated with the given
4552/// identifier.
4553///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004554/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00004555/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00004556/// them.
4557///
4558/// \param II an IdentifierInfo that refers to one or more globally-visible
4559/// declarations.
4560///
4561/// \param DeclIDs the set of declaration IDs with the name @p II that are
4562/// visible at global scope.
4563///
4564/// \param Nonrecursive should be true to indicate that the caller knows that
4565/// this call is non-recursive, and therefore the globally-visible declarations
4566/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00004567void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004568ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregor1342e842009-07-06 18:54:52 +00004569 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
4570 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004571 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004572 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4573 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4574 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004575 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00004576 return;
4577 }
Mike Stump11289f42009-09-09 15:08:12 +00004578
Douglas Gregor1342e842009-07-06 18:54:52 +00004579 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4580 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4581 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004582 if (SemaObj->TUScope) {
4583 // Introduce this declaration into the translation-unit scope
4584 // and add it to the declaration chain for this identifier, so
4585 // that (unqualified) name lookup will find it.
John McCall48871652010-08-21 09:40:31 +00004586 SemaObj->TUScope->AddDecl(D);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004587 }
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004588 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregor1342e842009-07-06 18:54:52 +00004589 } else {
4590 // Queue this declaration so that it will be added to the
4591 // translation unit scope and identifier's declaration chain
4592 // once a Sema object is known.
4593 PreloadedDecls.push_back(D);
4594 }
4595 }
4596}
4597
Sebastian Redl2c499f62010-08-18 23:56:43 +00004598IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004599 if (ID == 0)
4600 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00004601
Sebastian Redlc713b962010-07-21 00:46:22 +00004602 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004603 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004604 return 0;
4605 }
Mike Stump11289f42009-09-09 15:08:12 +00004606
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004607 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc713b962010-07-21 00:46:22 +00004608 ID -= 1;
4609 if (!IdentifiersLoaded[ID]) {
Douglas Gregor19d26352011-07-20 00:59:32 +00004610 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
4611 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
4612 unsigned Index = ID + I->second.second;
4613 const char *Str = I->second.first->IdentifierTableData
4614 + I->second.first->IdentifierOffsets[Index];
Douglas Gregor5287b4e2009-04-25 21:04:17 +00004615
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004616 // All of the strings in the AST file are preceded by a 16-bit length.
4617 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00004618 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4619 // unsigned integers. This is important to avoid integer overflow when
4620 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00004621 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00004622 unsigned StrLen = (((unsigned) StrLenPtr[0])
4623 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00004624 IdentifiersLoaded[ID]
Jay Foad9a6b0982011-06-21 15:13:30 +00004625 = &PP->getIdentifierTable().get(llvm::StringRef(Str, StrLen));
Sebastian Redlff4a2952010-07-23 23:49:55 +00004626 if (DeserializationListener)
4627 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004628 }
Mike Stump11289f42009-09-09 15:08:12 +00004629
Sebastian Redlc713b962010-07-21 00:46:22 +00004630 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004631}
4632
Douglas Gregor925296b2011-07-19 16:10:42 +00004633bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregor49f754f2011-04-20 00:21:03 +00004634 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor258ae542009-04-27 06:38:32 +00004635}
4636
Sebastian Redl2c499f62010-08-18 23:56:43 +00004637Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00004638 if (ID == 0)
4639 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00004640
Sebastian Redlada023c2010-08-04 20:40:17 +00004641 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004642 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00004643 return Selector();
4644 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004645
Sebastian Redlada023c2010-08-04 20:40:17 +00004646 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004647 // Load this selector from the selector table.
Sebastian Redlada023c2010-08-04 20:40:17 +00004648 unsigned Idx = ID - 1;
4649 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4650 PerFileData &F = *Chain[N - I - 1];
4651 if (Idx < F.LocalNumSelectors) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004652 ASTSelectorLookupTrait Trait(*this);
Sebastian Redlada023c2010-08-04 20:40:17 +00004653 SelectorsLoaded[ID - 1] =
4654 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4655 if (DeserializationListener)
4656 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4657 break;
4658 }
4659 Idx -= F.LocalNumSelectors;
4660 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004661 }
4662
Sebastian Redlada023c2010-08-04 20:40:17 +00004663 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00004664}
4665
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004666Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00004667 return DecodeSelector(ID);
4668}
4669
Sebastian Redl2c499f62010-08-18 23:56:43 +00004670uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00004671 // ID 0 (the null selector) is considered an external selector.
4672 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00004673}
4674
Mike Stump11289f42009-09-09 15:08:12 +00004675DeclarationName
Sebastian Redl2c499f62010-08-18 23:56:43 +00004676ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004677 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4678 switch (Kind) {
4679 case DeclarationName::Identifier:
4680 return DeclarationName(GetIdentifierInfo(Record, Idx));
4681
4682 case DeclarationName::ObjCZeroArgSelector:
4683 case DeclarationName::ObjCOneArgSelector:
4684 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff3c301dc2009-04-23 15:15:40 +00004685 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004686
4687 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004688 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004689 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004690
4691 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004692 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004693 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004694
4695 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004696 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004697 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004698
4699 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004700 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004701 (OverloadedOperatorKind)Record[Idx++]);
4702
Alexis Hunt3d221f22009-11-29 07:34:05 +00004703 case DeclarationName::CXXLiteralOperatorName:
4704 return Context->DeclarationNames.getCXXLiteralOperatorName(
4705 GetIdentifierInfo(Record, Idx));
4706
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004707 case DeclarationName::CXXUsingDirective:
4708 return DeclarationName::getUsingDirectiveName();
4709 }
4710
4711 // Required to silence GCC warning
4712 return DeclarationName();
4713}
Douglas Gregor55abb232009-04-10 20:39:37 +00004714
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004715void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4716 DeclarationNameLoc &DNLoc,
4717 DeclarationName Name,
4718 const RecordData &Record, unsigned &Idx) {
4719 switch (Name.getNameKind()) {
4720 case DeclarationName::CXXConstructorName:
4721 case DeclarationName::CXXDestructorName:
4722 case DeclarationName::CXXConversionFunctionName:
4723 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4724 break;
4725
4726 case DeclarationName::CXXOperatorName:
4727 DNLoc.CXXOperatorName.BeginOpNameLoc
4728 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4729 DNLoc.CXXOperatorName.EndOpNameLoc
4730 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4731 break;
4732
4733 case DeclarationName::CXXLiteralOperatorName:
4734 DNLoc.CXXLiteralOperatorName.OpNameLoc
4735 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4736 break;
4737
4738 case DeclarationName::Identifier:
4739 case DeclarationName::ObjCZeroArgSelector:
4740 case DeclarationName::ObjCOneArgSelector:
4741 case DeclarationName::ObjCMultiArgSelector:
4742 case DeclarationName::CXXUsingDirective:
4743 break;
4744 }
4745}
4746
4747void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4748 DeclarationNameInfo &NameInfo,
4749 const RecordData &Record, unsigned &Idx) {
4750 NameInfo.setName(ReadDeclarationName(Record, Idx));
4751 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4752 DeclarationNameLoc DNLoc;
4753 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4754 NameInfo.setInfo(DNLoc);
4755}
4756
4757void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4758 const RecordData &Record, unsigned &Idx) {
Douglas Gregor14454802011-02-25 02:25:35 +00004759 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004760 unsigned NumTPLists = Record[Idx++];
4761 Info.NumTemplParamLists = NumTPLists;
4762 if (NumTPLists) {
4763 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4764 for (unsigned i=0; i != NumTPLists; ++i)
4765 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4766 }
4767}
4768
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004769TemplateName
Douglas Gregor5590be02011-01-15 06:45:20 +00004770ASTReader::ReadTemplateName(PerFileData &F, const RecordData &Record,
4771 unsigned &Idx) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004772 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004773 switch (Kind) {
4774 case TemplateName::Template:
4775 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4776
4777 case TemplateName::OverloadedTemplate: {
4778 unsigned size = Record[Idx++];
4779 UnresolvedSet<8> Decls;
4780 while (size--)
4781 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4782
4783 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4784 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004785
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004786 case TemplateName::QualifiedTemplate: {
4787 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4788 bool hasTemplKeyword = Record[Idx++];
4789 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4790 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4791 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004792
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004793 case TemplateName::DependentTemplate: {
4794 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4795 if (Record[Idx++]) // isIdentifier
4796 return Context->getDependentTemplateName(NNS,
4797 GetIdentifierInfo(Record, Idx));
4798 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004799 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004800 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004801
4802 case TemplateName::SubstTemplateTemplateParm: {
4803 TemplateTemplateParmDecl *param
4804 = cast_or_null<TemplateTemplateParmDecl>(GetDecl(Record[Idx++]));
4805 if (!param) return TemplateName();
4806 TemplateName replacement = ReadTemplateName(F, Record, Idx);
4807 return Context->getSubstTemplateTemplateParm(param, replacement);
4808 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004809
4810 case TemplateName::SubstTemplateTemplateParmPack: {
4811 TemplateTemplateParmDecl *Param
4812 = cast_or_null<TemplateTemplateParmDecl>(GetDecl(Record[Idx++]));
4813 if (!Param)
4814 return TemplateName();
4815
4816 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
4817 if (ArgPack.getKind() != TemplateArgument::Pack)
4818 return TemplateName();
4819
4820 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
4821 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004822 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004823
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004824 assert(0 && "Unhandled template name kind!");
4825 return TemplateName();
4826}
4827
4828TemplateArgument
Sebastian Redl2c373b92010-10-05 15:59:54 +00004829ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00004830 const RecordData &Record, unsigned &Idx) {
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004831 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
4832 switch (Kind) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004833 case TemplateArgument::Null:
4834 return TemplateArgument();
4835 case TemplateArgument::Type:
4836 return TemplateArgument(GetType(Record[Idx++]));
4837 case TemplateArgument::Declaration:
4838 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00004839 case TemplateArgument::Integral: {
4840 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4841 QualType T = GetType(Record[Idx++]);
4842 return TemplateArgument(Value, T);
4843 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004844 case TemplateArgument::Template:
Douglas Gregor5590be02011-01-15 06:45:20 +00004845 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004846 case TemplateArgument::TemplateExpansion: {
Douglas Gregor5590be02011-01-15 06:45:20 +00004847 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregore1d60df2011-01-14 23:41:42 +00004848 llvm::Optional<unsigned> NumTemplateExpansions;
4849 if (unsigned NumExpansions = Record[Idx++])
4850 NumTemplateExpansions = NumExpansions - 1;
4851 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004852 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004853 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004854 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004855 case TemplateArgument::Pack: {
4856 unsigned NumArgs = Record[Idx++];
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004857 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4858 for (unsigned I = 0; I != NumArgs; ++I)
4859 Args[I] = ReadTemplateArgument(F, Record, Idx);
4860 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004861 }
4862 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004863
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004864 assert(0 && "Unhandled template argument kind!");
4865 return TemplateArgument();
4866}
4867
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004868TemplateParameterList *
Sebastian Redl2c373b92010-10-05 15:59:54 +00004869ASTReader::ReadTemplateParameterList(PerFileData &F,
4870 const RecordData &Record, unsigned &Idx) {
4871 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4872 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4873 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004874
4875 unsigned NumParams = Record[Idx++];
4876 llvm::SmallVector<NamedDecl *, 16> Params;
4877 Params.reserve(NumParams);
4878 while (NumParams--)
4879 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004880
4881 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004882 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4883 Params.data(), Params.size(), RAngleLoc);
4884 return TemplateParams;
4885}
4886
4887void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004888ASTReader::
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004889ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004890 PerFileData &F, const RecordData &Record,
4891 unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004892 unsigned NumTemplateArgs = Record[Idx++];
4893 TemplArgs.reserve(NumTemplateArgs);
4894 while (NumTemplateArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004895 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004896}
4897
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004898/// \brief Read a UnresolvedSet structure.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004899void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004900 const RecordData &Record, unsigned &Idx) {
4901 unsigned NumDecls = Record[Idx++];
4902 while (NumDecls--) {
4903 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4904 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4905 Set.addDecl(D, AS);
4906 }
4907}
4908
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004909CXXBaseSpecifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00004910ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky19b9f952010-07-26 16:56:01 +00004911 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004912 bool isVirtual = static_cast<bool>(Record[Idx++]);
4913 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4914 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redl08905022011-02-05 19:23:19 +00004915 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004916 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4917 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor752a5952011-01-03 22:36:02 +00004918 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl08905022011-02-05 19:23:19 +00004919 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregor752a5952011-01-03 22:36:02 +00004920 EllipsisLoc);
Sebastian Redl08905022011-02-05 19:23:19 +00004921 Result.setInheritConstructors(inheritConstructors);
4922 return Result;
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004923}
4924
Alexis Hunt1d792652011-01-08 20:30:50 +00004925std::pair<CXXCtorInitializer **, unsigned>
4926ASTReader::ReadCXXCtorInitializers(PerFileData &F, const RecordData &Record,
4927 unsigned &Idx) {
4928 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004929 unsigned NumInitializers = Record[Idx++];
4930 if (NumInitializers) {
4931 ASTContext &C = *getContext();
4932
Alexis Hunt1d792652011-01-08 20:30:50 +00004933 CtorInitializers
4934 = new (C) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004935 for (unsigned i=0; i != NumInitializers; ++i) {
4936 TypeSourceInfo *BaseClassInfo = 0;
4937 bool IsBaseVirtual = false;
4938 FieldDecl *Member = 0;
Francois Pichetd583da02010-12-04 09:14:42 +00004939 IndirectFieldDecl *IndirectMember = 0;
Alexis Hunt37a477f2011-05-04 01:19:08 +00004940 CXXConstructorDecl *Target = 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004941
Alexis Hunt37a477f2011-05-04 01:19:08 +00004942 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
4943 switch (Type) {
4944 case CTOR_INITIALIZER_BASE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004945 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004946 IsBaseVirtual = Record[Idx++];
Alexis Hunt37a477f2011-05-04 01:19:08 +00004947 break;
4948
4949 case CTOR_INITIALIZER_DELEGATING:
4950 Target = cast<CXXConstructorDecl>(GetDecl(Record[Idx++]));
4951 break;
4952
4953 case CTOR_INITIALIZER_MEMBER:
4954 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
4955 break;
4956
4957 case CTOR_INITIALIZER_INDIRECT_MEMBER:
4958 IndirectMember = cast<IndirectFieldDecl>(GetDecl(Record[Idx++]));
4959 break;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004960 }
Alexis Hunt37a477f2011-05-04 01:19:08 +00004961
Douglas Gregor44e7df62011-01-04 00:32:56 +00004962 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004963 Expr *Init = ReadExpr(F);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004964 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4965 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004966 bool IsWritten = Record[Idx++];
4967 unsigned SourceOrderOrNumArrayIndices;
4968 llvm::SmallVector<VarDecl *, 8> Indices;
4969 if (IsWritten) {
4970 SourceOrderOrNumArrayIndices = Record[Idx++];
4971 } else {
4972 SourceOrderOrNumArrayIndices = Record[Idx++];
4973 Indices.reserve(SourceOrderOrNumArrayIndices);
4974 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4975 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4976 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004977
Alexis Hunt1d792652011-01-08 20:30:50 +00004978 CXXCtorInitializer *BOMInit;
Alexis Hunt37a477f2011-05-04 01:19:08 +00004979 if (Type == CTOR_INITIALIZER_BASE) {
Alexis Hunt1d792652011-01-08 20:30:50 +00004980 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
4981 LParenLoc, Init, RParenLoc,
4982 MemberOrEllipsisLoc);
Alexis Hunt37a477f2011-05-04 01:19:08 +00004983 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
4984 BOMInit = new (C) CXXCtorInitializer(C, MemberOrEllipsisLoc, LParenLoc,
4985 Target, Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004986 } else if (IsWritten) {
Francois Pichetd583da02010-12-04 09:14:42 +00004987 if (Member)
Alexis Hunt1d792652011-01-08 20:30:50 +00004988 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
4989 LParenLoc, Init, RParenLoc);
Francois Pichetd583da02010-12-04 09:14:42 +00004990 else
Alexis Hunt1d792652011-01-08 20:30:50 +00004991 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
4992 MemberOrEllipsisLoc, LParenLoc,
4993 Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004994 } else {
Alexis Hunt1d792652011-01-08 20:30:50 +00004995 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
4996 LParenLoc, Init, RParenLoc,
4997 Indices.data(), Indices.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004998 }
4999
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00005000 if (IsWritten)
5001 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Alexis Hunt1d792652011-01-08 20:30:50 +00005002 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005003 }
5004 }
5005
Alexis Hunt1d792652011-01-08 20:30:50 +00005006 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005007}
5008
Chris Lattnerca025db2010-05-07 21:43:38 +00005009NestedNameSpecifier *
Sebastian Redl2c499f62010-08-18 23:56:43 +00005010ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005011 unsigned N = Record[Idx++];
5012 NestedNameSpecifier *NNS = 0, *Prev = 0;
5013 for (unsigned I = 0; I != N; ++I) {
5014 NestedNameSpecifier::SpecifierKind Kind
5015 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5016 switch (Kind) {
5017 case NestedNameSpecifier::Identifier: {
5018 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
5019 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
5020 break;
5021 }
5022
5023 case NestedNameSpecifier::Namespace: {
5024 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
5025 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
5026 break;
5027 }
5028
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005029 case NestedNameSpecifier::NamespaceAlias: {
5030 NamespaceAliasDecl *Alias
5031 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++]));
5032 NNS = NestedNameSpecifier::Create(*Context, Prev, Alias);
5033 break;
5034 }
5035
Chris Lattnerca025db2010-05-07 21:43:38 +00005036 case NestedNameSpecifier::TypeSpec:
5037 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall424cec92011-01-19 06:33:43 +00005038 const Type *T = GetType(Record[Idx++]).getTypePtrOrNull();
Douglas Gregor0cdc8322010-12-10 17:03:06 +00005039 if (!T)
5040 return 0;
5041
Chris Lattnerca025db2010-05-07 21:43:38 +00005042 bool Template = Record[Idx++];
5043 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
5044 break;
5045 }
5046
5047 case NestedNameSpecifier::Global: {
5048 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
5049 // No associated value, and there can't be a prefix.
5050 break;
5051 }
Chris Lattnerca025db2010-05-07 21:43:38 +00005052 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00005053 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00005054 }
5055 return NNS;
5056}
5057
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005058NestedNameSpecifierLoc
5059ASTReader::ReadNestedNameSpecifierLoc(PerFileData &F, const RecordData &Record,
5060 unsigned &Idx) {
5061 unsigned N = Record[Idx++];
Douglas Gregor9b272512011-02-28 23:58:31 +00005062 NestedNameSpecifierLocBuilder Builder;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005063 for (unsigned I = 0; I != N; ++I) {
5064 NestedNameSpecifier::SpecifierKind Kind
5065 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5066 switch (Kind) {
5067 case NestedNameSpecifier::Identifier: {
Douglas Gregor9b272512011-02-28 23:58:31 +00005068 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005069 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005070 Builder.Extend(*Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005071 break;
5072 }
5073
5074 case NestedNameSpecifier::Namespace: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005075 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005076 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005077 Builder.Extend(*Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005078 break;
5079 }
5080
5081 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005082 NamespaceAliasDecl *Alias
5083 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++]));
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005084 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005085 Builder.Extend(*Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005086 break;
5087 }
5088
5089 case NestedNameSpecifier::TypeSpec:
5090 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005091 bool Template = Record[Idx++];
5092 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
5093 if (!T)
5094 return NestedNameSpecifierLoc();
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005095 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005096
5097 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
5098 Builder.Extend(*Context,
5099 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
5100 T->getTypeLoc(), ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005101 break;
5102 }
5103
5104 case NestedNameSpecifier::Global: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005105 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005106 Builder.MakeGlobal(*Context, ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005107 break;
5108 }
5109 }
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005110 }
5111
Douglas Gregor9b272512011-02-28 23:58:31 +00005112 return Builder.getWithLocInContext(*Context);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005113}
5114
Chris Lattnerca025db2010-05-07 21:43:38 +00005115SourceRange
Sebastian Redl2c373b92010-10-05 15:59:54 +00005116ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
5117 unsigned &Idx) {
5118 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
5119 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00005120 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00005121}
5122
Douglas Gregor1daeb692009-04-13 18:14:40 +00005123/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005124llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00005125 unsigned BitWidth = Record[Idx++];
5126 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
5127 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
5128 Idx += NumWords;
5129 return Result;
5130}
5131
5132/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005133llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00005134 bool isUnsigned = Record[Idx++];
5135 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
5136}
5137
Douglas Gregore0a3a512009-04-14 21:55:33 +00005138/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005139llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00005140 return llvm::APFloat(ReadAPInt(Record, Idx));
5141}
5142
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005143// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00005144std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005145 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00005146 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005147 Idx += Len;
5148 return Result;
5149}
5150
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005151VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
5152 unsigned &Idx) {
5153 unsigned Major = Record[Idx++];
5154 unsigned Minor = Record[Idx++];
5155 unsigned Subminor = Record[Idx++];
5156 if (Minor == 0)
5157 return VersionTuple(Major);
5158 if (Subminor == 0)
5159 return VersionTuple(Major, Minor - 1);
5160 return VersionTuple(Major, Minor - 1, Subminor - 1);
5161}
5162
Sebastian Redl2c499f62010-08-18 23:56:43 +00005163CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00005164 unsigned &Idx) {
5165 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
5166 return CXXTemporary::Create(*Context, Decl);
5167}
5168
Sebastian Redl2c499f62010-08-18 23:56:43 +00005169DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00005170 return Diag(SourceLocation(), DiagID);
5171}
5172
Sebastian Redl2c499f62010-08-18 23:56:43 +00005173DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005174 return Diags.Report(Loc, DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00005175}
Douglas Gregora9af1d12009-04-17 00:04:06 +00005176
Douglas Gregora868bbd2009-04-21 22:25:48 +00005177/// \brief Retrieve the identifier table associated with the
5178/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005179IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00005180 assert(PP && "Forgot to set Preprocessor ?");
5181 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00005182}
5183
Douglas Gregora9af1d12009-04-17 00:04:06 +00005184/// \brief Record that the given ID maps to the given switch-case
5185/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005186void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00005187 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
5188 SwitchCaseStmts[ID] = SC;
5189}
5190
5191/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005192SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00005193 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
5194 return SwitchCaseStmts[ID];
5195}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00005196
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00005197void ASTReader::ClearSwitchCaseIDs() {
5198 SwitchCaseStmts.clear();
5199}
5200
Sebastian Redl2c499f62010-08-18 23:56:43 +00005201void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005202 assert(NumCurrentElementsDeserializing &&
5203 "FinishedDeserializing not paired with StartedDeserializing");
5204 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00005205 // If any identifiers with corresponding top-level declarations have
5206 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005207 while (!PendingIdentifierInfos.empty()) {
5208 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
5209 PendingIdentifierInfos.front().DeclIDs, true);
5210 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00005211 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005212
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00005213 // Ready to load previous declarations of Decls that were delayed.
5214 while (!PendingPreviousDecls.empty()) {
5215 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
5216 PendingPreviousDecls.front().second);
5217 PendingPreviousDecls.pop_front();
5218 }
5219
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005220 // We are not in recursive loading, so it's safe to pass the "interesting"
5221 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005222 if (Consumer)
5223 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidisad5f95c2010-10-24 17:26:31 +00005224
5225 assert(PendingForwardRefs.size() == 0 &&
5226 "Some forward refs did not get linked to the definition!");
Douglas Gregor1342e842009-07-06 18:54:52 +00005227 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005228 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00005229}
Douglas Gregorb473b072010-08-19 00:28:17 +00005230
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005231ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005232 const char *isysroot, bool DisableValidation,
5233 bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005234 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
5235 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
5236 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
Douglas Gregor925296b2011-07-19 16:10:42 +00005237 Consumer(0), FirstInSource(0), RelocatablePCH(false), isysroot(isysroot),
5238 DisableValidation(DisableValidation),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005239 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
Douglas Gregor925296b2011-07-19 16:10:42 +00005240 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005241 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
5242 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
5243 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
5244 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
5245 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
5246 NumCurrentElementsDeserializing(0)
5247{
Douglas Gregor925296b2011-07-19 16:10:42 +00005248 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005249}
5250
5251ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
5252 Diagnostic &Diags, const char *isysroot,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005253 bool DisableValidation, bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005254 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
Douglas Gregor925296b2011-07-19 16:10:42 +00005255 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0), FirstInSource(0),
5256 RelocatablePCH(false), isysroot(isysroot),
5257 DisableValidation(DisableValidation), DisableStatCache(DisableStatCache),
5258 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
5259 TotalNumSLocEntries(0), NumStatementsRead(0),
5260 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
5261 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00005262 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
5263 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
Douglas Gregor925296b2011-07-19 16:10:42 +00005264 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0)
5265{
5266 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005267}
5268
5269ASTReader::~ASTReader() {
5270 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
5271 delete Chain[e - i - 1];
5272 // Delete all visible decl lookup tables
5273 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
5274 E = DeclContextOffsets.end();
5275 I != E; ++I) {
5276 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
5277 J != F; ++J) {
5278 if (J->NameLookupTableData)
5279 delete static_cast<ASTDeclContextNameLookupTable*>(
5280 J->NameLookupTableData);
5281 }
5282 }
5283 for (DeclContextVisibleUpdatesPending::iterator
5284 I = PendingVisibleUpdates.begin(),
5285 E = PendingVisibleUpdates.end();
5286 I != E; ++I) {
5287 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5288 F = I->second.end();
5289 J != F; ++J)
5290 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
5291 }
5292}
5293
Sebastian Redl009e7f22010-10-05 16:15:19 +00005294ASTReader::PerFileData::PerFileData(ASTFileType Ty)
Douglas Gregor925296b2011-07-19 16:10:42 +00005295 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocEntryBaseID(0),
5296 SLocEntryBaseOffset(0), SLocEntryOffsets(0),
5297 SLocFileOffsets(0), LocalNumIdentifiers(0),
5298 IdentifierOffsets(0), IdentifierTableData(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00005299 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
Douglas Gregor925296b2011-07-19 16:10:42 +00005300 MacroDefinitionOffsets(0), LocalNumHeaderFileInfos(0),
5301 HeaderFileInfoTableData(0), HeaderFileInfoTable(0),
Douglas Gregor09b69892011-02-10 17:09:37 +00005302 LocalNumSelectors(0), SelectorOffsets(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00005303 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005304 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
5305 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redl3f6b7532010-10-01 19:59:12 +00005306 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregorb473b072010-08-19 00:28:17 +00005307{}
5308
5309ASTReader::PerFileData::~PerFileData() {
5310 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
Douglas Gregor09b69892011-02-10 17:09:37 +00005311 delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable);
Douglas Gregorb473b072010-08-19 00:28:17 +00005312 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
5313}