blob: 8fb20d22b7defefec142e6cab295e8b1eb03c846 [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 Gregor09b69892011-02-10 17:09:37 +000052
Douglas Gregoref84c4b2009-04-09 22:27:44 +000053using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000054using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000055
56//===----------------------------------------------------------------------===//
Sebastian Redld44cd6a2010-08-18 23:57:06 +000057// PCH validator implementation
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000058//===----------------------------------------------------------------------===//
59
Sebastian Redl3e31c722010-08-18 23:56:56 +000060ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000061
62bool
63PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
64 const LangOptions &PPLangOpts = PP.getLangOptions();
65#define PARSE_LANGOPT_BENIGN(Option)
66#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
67 if (PPLangOpts.Option != LangOpts.Option) { \
68 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
69 return true; \
70 }
71
72 PARSE_LANGOPT_BENIGN(Trigraphs);
73 PARSE_LANGOPT_BENIGN(BCPLComment);
74 PARSE_LANGOPT_BENIGN(DollarIdents);
75 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
76 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carruthe03aa552010-04-17 20:17:31 +000077 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000078 PARSE_LANGOPT_BENIGN(ImplicitInt);
79 PARSE_LANGOPT_BENIGN(Digraphs);
80 PARSE_LANGOPT_BENIGN(HexFloats);
81 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
Peter Collingbournea686b5f2011-04-15 00:35:23 +000082 PARSE_LANGOPT_IMPORTANT(C1X, diag::warn_pch_c1x);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000083 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +000084 PARSE_LANGOPT_BENIGN(MSCVersion);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000085 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
86 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
87 PARSE_LANGOPT_BENIGN(CXXOperatorName);
88 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
89 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
90 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian45878032010-02-09 19:31:38 +000091 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +000092 PARSE_LANGOPT_IMPORTANT(AppleKext, diag::warn_pch_apple_kext);
Ted Kremenek1d56c9e2010-12-23 21:35:43 +000093 PARSE_LANGOPT_IMPORTANT(ObjCDefaultSynthProperties,
94 diag::warn_pch_objc_auto_properties);
Douglas Gregora860e6a2011-06-14 23:20:43 +000095 PARSE_LANGOPT_BENIGN(ObjCInferRelatedResultType)
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +000096 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian62c56022010-04-22 21:01:59 +000097 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000098 PARSE_LANGOPT_BENIGN(PascalStrings);
99 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump11289f42009-09-09 15:08:12 +0000100 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000101 diag::warn_pch_lax_vector_conversions);
Nate Begeman9d905792009-06-25 22:57:40 +0000102 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000103 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Anders Carlssonce8dd3a2011-02-19 23:53:54 +0000104 PARSE_LANGOPT_IMPORTANT(ObjCExceptions, diag::warn_pch_objc_exceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +0000105 PARSE_LANGOPT_IMPORTANT(CXXExceptions, diag::warn_pch_cxx_exceptions);
106 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Douglas Gregordbe39272011-02-01 15:15:22 +0000107 PARSE_LANGOPT_IMPORTANT(MSBitfields, diag::warn_pch_ms_bitfields);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000108 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
109 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
110 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump11289f42009-09-09 15:08:12 +0000111 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000112 diag::warn_pch_thread_safe_statics);
Daniel Dunbara77eaeb2009-09-03 04:54:28 +0000113 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000114 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
115 PARSE_LANGOPT_BENIGN(EmitAllDecls);
116 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattner51924e512010-06-26 21:25:03 +0000117 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump11289f42009-09-09 15:08:12 +0000118 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000119 diag::warn_pch_heinous_extensions);
120 // FIXME: Most of the options below are benign if the macro wasn't
121 // used. Unfortunately, this means that a PCH compiled without
122 // optimization can't be used with optimization turned on, even
123 // though the only thing that changes is whether __OPTIMIZE__ was
124 // defined... but if __OPTIMIZE__ never showed up in the header, it
125 // doesn't matter. We could consider making this some special kind
126 // of check.
127 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
128 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
129 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
130 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
131 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
132 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
Chandler Carruth7ffce732011-04-23 20:05:38 +0000133 PARSE_LANGOPT_IMPORTANT(Deprecated, diag::warn_pch_deprecated);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000134 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
135 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsoned4e2952009-11-05 20:14:16 +0000136 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +0000137 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000138 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump11289f42009-09-09 15:08:12 +0000139 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000140 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
141 return true;
142 }
143 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000144 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
145 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000146 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman9d905792009-06-25 22:57:40 +0000147 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Peter Collingbourne546d0792010-12-01 19:14:57 +0000148 PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda);
Mike Stumpd9546382009-12-12 01:27:46 +0000149 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbar143021e2009-09-21 04:16:19 +0000150 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000151 PARSE_LANGOPT_BENIGN(SpellChecking);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000152 PARSE_LANGOPT_BENIGN(DefaultFPContract);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +0000153#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000154#undef PARSE_LANGOPT_BENIGN
155
156 return false;
157}
158
Daniel Dunbar20a682d2009-11-11 00:52:11 +0000159bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
160 if (Triple == PP.getTargetInfo().getTriple().str())
161 return false;
162
163 Reader.Diag(diag::warn_pch_target_triple)
164 << Triple << PP.getTargetInfo().getTriple().str();
165 return true;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000166}
167
Benjamin Kramer90b5b682010-11-25 18:29:30 +0000168namespace {
169 struct EmptyStringRef {
170 bool operator ()(llvm::StringRef r) const { return r.empty(); }
171 };
172 struct EmptyBlock {
173 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
174 };
175}
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000176
177static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
178 PCHPredefinesBlocks R) {
179 // First, sum up the lengths.
180 unsigned LL = 0, RL = 0;
181 for (unsigned I = 0, N = L.size(); I != N; ++I) {
182 LL += L[I].size();
183 }
184 for (unsigned I = 0, N = R.size(); I != N; ++I) {
185 RL += R[I].Data.size();
186 }
187 if (LL != RL)
188 return false;
189 if (LL == 0 && RL == 0)
190 return true;
191
192 // Kick out empty parts, they confuse the algorithm below.
193 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
194 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
195
196 // Do it the hard way. At this point, both vectors must be non-empty.
197 llvm::StringRef LR = L[0], RR = R[0].Data;
198 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbar01ad0a72010-07-16 00:00:11 +0000199 (void) RN;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000200 for (;;) {
201 // Compare the current pieces.
202 if (LR.size() == RR.size()) {
203 // If they're the same length, it's pretty easy.
204 if (LR != RR)
205 return false;
206 // Both pieces are done, advance.
207 ++LI;
208 ++RI;
209 // If either string is done, they're both done, since they're the same
210 // length.
211 if (LI == LN) {
212 assert(RI == RN && "Strings not the same length after all?");
213 return true;
214 }
215 LR = L[LI];
216 RR = R[RI].Data;
217 } else if (LR.size() < RR.size()) {
218 // Right piece is longer.
219 if (!RR.startswith(LR))
220 return false;
221 ++LI;
222 assert(LI != LN && "Strings not the same length after all?");
223 RR = RR.substr(LR.size());
224 LR = L[LI];
225 } else {
226 // Left piece is longer.
227 if (!LR.startswith(RR))
228 return false;
229 ++RI;
230 assert(RI != RN && "Strings not the same length after all?");
231 LR = LR.substr(RR.size());
232 RR = R[RI].Data;
233 }
234 }
235}
236
237static std::pair<FileID, llvm::StringRef::size_type>
238FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
239 std::pair<FileID, llvm::StringRef::size_type> Res;
240 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
241 Res.second = Buffers[I].Data.find(MacroDef);
242 if (Res.second != llvm::StringRef::npos) {
243 Res.first = Buffers[I].BufferID;
244 break;
245 }
246 }
247 return Res;
248}
249
250bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000251 llvm::StringRef OriginalFileName,
Nick Lewycky36079892011-02-23 21:16:44 +0000252 std::string &SuggestedPredefines,
253 FileManager &FileMgr) {
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000254 // We are in the context of an implicit include, so the predefines buffer will
255 // have a #include entry for the PCH file itself (as normalized by the
256 // preprocessor initialization). Find it and skip over it in the checking
257 // below.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000258 llvm::SmallString<256> PCHInclude;
259 PCHInclude += "#include \"";
Nick Lewycky36079892011-02-23 21:16:44 +0000260 PCHInclude += NormalizeDashIncludePath(OriginalFileName, FileMgr);
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000261 PCHInclude += "\"\n";
262 std::pair<llvm::StringRef,llvm::StringRef> Split =
263 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
264 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000265 if (Left == PP.getPredefines()) {
266 Error("Missing PCH include entry!");
267 return true;
268 }
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000269
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000270 // If the concatenation of all the PCH buffers is equal to the adjusted
271 // command line, we're done.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000272 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
273 CommandLine.push_back(Left);
274 CommandLine.push_back(Right);
275 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000276 return false;
277
278 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000279
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000280 // The predefines buffers are different. Determine what the differences are,
281 // and whether they require us to reject the PCH file.
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000282 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000283 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
284 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000285
286 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
287 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000288
289 // Pick out implicit #includes after the PCH and don't consider them for
290 // validation; we will insert them into SuggestedPredefines so that the
291 // preprocessor includes them.
292 std::string IncludesAfterPCH;
293 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
294 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
295 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
296 if (AfterPCHLines[i].startswith("#include ")) {
297 IncludesAfterPCH += AfterPCHLines[i];
298 IncludesAfterPCH += '\n';
299 } else {
300 CmdLineLines.push_back(AfterPCHLines[i]);
301 }
302 }
303
304 // Make sure we add the includes last into SuggestedPredefines before we
305 // exit this function.
306 struct AddIncludesRAII {
307 std::string &SuggestedPredefines;
308 std::string &IncludesAfterPCH;
309
310 AddIncludesRAII(std::string &SuggestedPredefines,
311 std::string &IncludesAfterPCH)
312 : SuggestedPredefines(SuggestedPredefines),
313 IncludesAfterPCH(IncludesAfterPCH) { }
314 ~AddIncludesRAII() {
315 SuggestedPredefines += IncludesAfterPCH;
316 }
317 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000318
Daniel Dunbar499baed2009-11-11 05:26:28 +0000319 // Sort both sets of predefined buffer lines, since we allow some extra
320 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000321 std::sort(CmdLineLines.begin(), CmdLineLines.end());
322 std::sort(PCHLines.begin(), PCHLines.end());
323
Daniel Dunbar499baed2009-11-11 05:26:28 +0000324 // Determine which predefines that were used to build the PCH file are missing
325 // from the command line.
326 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000327 std::set_difference(PCHLines.begin(), PCHLines.end(),
328 CmdLineLines.begin(), CmdLineLines.end(),
329 std::back_inserter(MissingPredefines));
330
331 bool MissingDefines = false;
332 bool ConflictingDefines = false;
333 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000334 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000335 if (Missing.startswith("#include ")) {
336 // An -include was specified when generating the PCH; it is included in
337 // the PCH, just ignore it.
338 continue;
339 }
Daniel Dunbar499baed2009-11-11 05:26:28 +0000340 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000341 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
342 return true;
343 }
Mike Stump11289f42009-09-09 15:08:12 +0000344
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000345 // This is a macro definition. Determine the name of the macro we're
346 // defining.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000347 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000348 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000349 = Missing.find_first_of("( \n\r", StartOfMacroName);
350 assert(EndOfMacroName != std::string::npos &&
351 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000352 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000353
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000354 // Determine whether this macro was given a different definition on the
355 // command line.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000356 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000357 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000358 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000359 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
360 MacroDefStart);
361 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000362 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000363 // Different macro; we're done.
364 ConflictPos = CmdLineLines.end();
Mike Stump11289f42009-09-09 15:08:12 +0000365 break;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000366 }
Mike Stump11289f42009-09-09 15:08:12 +0000367
368 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000369 "Invalid #define in predefines buffer?");
Mike Stump11289f42009-09-09 15:08:12 +0000370 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000371 (*ConflictPos)[MacroDefLen] != '(')
372 continue; // Longer macro name; keep trying.
Mike Stump11289f42009-09-09 15:08:12 +0000373
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000374 // We found a conflicting macro definition.
375 break;
376 }
Mike Stump11289f42009-09-09 15:08:12 +0000377
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000378 if (ConflictPos != CmdLineLines.end()) {
379 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
380 << MacroName;
381
382 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000383 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
384 FindMacro(Buffers, Missing);
385 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
386 SourceLocation PCHMissingLoc =
387 SourceMgr.getLocForStartOfFile(MacroLoc.first)
388 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar499baed2009-11-11 05:26:28 +0000389 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000390
391 ConflictingDefines = true;
392 continue;
393 }
Mike Stump11289f42009-09-09 15:08:12 +0000394
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000395 // If the macro doesn't conflict, then we'll just pick up the macro
396 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000397 if (ConflictingDefines)
398 continue; // Don't complain if there are already conflicting defs
Mike Stump11289f42009-09-09 15:08:12 +0000399
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000400 if (!MissingDefines) {
401 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
402 MissingDefines = true;
403 }
404
405 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000406 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
407 FindMacro(Buffers, Missing);
408 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
409 SourceLocation PCHMissingLoc =
410 SourceMgr.getLocForStartOfFile(MacroLoc.first)
411 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000412 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
413 }
Mike Stump11289f42009-09-09 15:08:12 +0000414
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000415 if (ConflictingDefines)
416 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000417
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000418 // Determine what predefines were introduced based on command-line
419 // parameters that were not present when building the PCH
420 // file. Extra #defines are okay, so long as the identifiers being
421 // defined were not used within the precompiled header.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000422 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000423 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
424 PCHLines.begin(), PCHLines.end(),
Mike Stump11289f42009-09-09 15:08:12 +0000425 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000426 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000427 llvm::StringRef &Extra = ExtraPredefines[I];
428 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000429 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
430 return true;
431 }
432
433 // This is an extra macro definition. Determine the name of the
434 // macro we're defining.
435 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000436 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000437 = Extra.find_first_of("( \n\r", StartOfMacroName);
438 assert(EndOfMacroName != std::string::npos &&
439 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000440 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000441
442 // Check whether this name was used somewhere in the PCH file. If
443 // so, defining it as a macro could change behavior, so we reject
444 // the PCH file.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000445 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar045c92f2009-11-11 00:52:00 +0000446 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000447 return true;
448 }
449
450 // Add this definition to the suggested predefines buffer.
451 SuggestedPredefines += Extra;
452 SuggestedPredefines += '\n';
453 }
454
455 // If we get here, it's because the predefines buffer had compatible
456 // contents. Accept the PCH file.
457 return false;
458}
459
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000460void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
461 unsigned ID) {
462 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
463 ++NumHeaderInfos;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000464}
465
466void PCHValidator::ReadCounter(unsigned Value) {
467 PP.setCounterValue(Value);
468}
469
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000470//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +0000471// AST reader implementation
Douglas Gregora868bbd2009-04-21 22:25:48 +0000472//===----------------------------------------------------------------------===//
473
Sebastian Redl07a89a82010-07-30 00:29:29 +0000474void
Sebastian Redl3e31c722010-08-18 23:56:56 +0000475ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redl07a89a82010-07-30 00:29:29 +0000476 DeserializationListener = Listener;
Sebastian Redl07a89a82010-07-30 00:29:29 +0000477}
478
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000479
Douglas Gregora868bbd2009-04-21 22:25:48 +0000480namespace {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000481class ASTSelectorLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000482 ASTReader &Reader;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000483
484public:
Sebastian Redl834bb972010-08-04 17:20:04 +0000485 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +0000486 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +0000487 ObjCMethodList Instance, Factory;
488 };
Douglas Gregorc78d3462009-04-24 21:10:55 +0000489
490 typedef Selector external_key_type;
491 typedef external_key_type internal_key_type;
492
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000493 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump11289f42009-09-09 15:08:12 +0000494
Douglas Gregorc78d3462009-04-24 21:10:55 +0000495 static bool EqualKey(const internal_key_type& a,
496 const internal_key_type& b) {
497 return a == b;
498 }
Mike Stump11289f42009-09-09 15:08:12 +0000499
Douglas Gregorc78d3462009-04-24 21:10:55 +0000500 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +0000501 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000502 }
Mike Stump11289f42009-09-09 15:08:12 +0000503
Douglas Gregorc78d3462009-04-24 21:10:55 +0000504 // This hopefully will just get inlined and removed by the optimizer.
505 static const internal_key_type&
506 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000507
Douglas Gregorc78d3462009-04-24 21:10:55 +0000508 static std::pair<unsigned, unsigned>
509 ReadKeyDataLength(const unsigned char*& d) {
510 using namespace clang::io;
511 unsigned KeyLen = ReadUnalignedLE16(d);
512 unsigned DataLen = ReadUnalignedLE16(d);
513 return std::make_pair(KeyLen, DataLen);
514 }
Mike Stump11289f42009-09-09 15:08:12 +0000515
Douglas Gregor95c13f52009-04-25 17:48:32 +0000516 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000517 using namespace clang::io;
Chris Lattner8575daa2009-04-27 21:45:14 +0000518 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000519 unsigned N = ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000520 IdentifierInfo *FirstII
Douglas Gregorc78d3462009-04-24 21:10:55 +0000521 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
522 if (N == 0)
523 return SelTable.getNullarySelector(FirstII);
524 else if (N == 1)
525 return SelTable.getUnarySelector(FirstII);
526
527 llvm::SmallVector<IdentifierInfo *, 16> Args;
528 Args.push_back(FirstII);
529 for (unsigned I = 1; I != N; ++I)
530 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
531
Douglas Gregor038c3382009-05-22 22:45:36 +0000532 return SelTable.getSelector(N, Args.data());
Douglas Gregorc78d3462009-04-24 21:10:55 +0000533 }
Mike Stump11289f42009-09-09 15:08:12 +0000534
Douglas Gregorc78d3462009-04-24 21:10:55 +0000535 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
536 using namespace clang::io;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000537
538 data_type Result;
539
Sebastian Redl834bb972010-08-04 17:20:04 +0000540 Result.ID = ReadUnalignedLE32(d);
541 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
542 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
543
Douglas Gregorc78d3462009-04-24 21:10:55 +0000544 // Load instance methods
545 ObjCMethodList *Prev = 0;
546 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000547 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000548 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000549 if (!Result.Instance.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000550 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000551 Result.Instance.Method = Method;
552 Prev = &Result.Instance;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000553 continue;
554 }
555
Ted Kremenekda4abf12010-02-11 00:53:01 +0000556 ObjCMethodList *Mem =
557 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
558 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000559 Prev = Prev->Next;
560 }
561
562 // Load factory methods
563 Prev = 0;
564 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000565 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000566 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000567 if (!Result.Factory.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000568 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000569 Result.Factory.Method = Method;
570 Prev = &Result.Factory;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000571 continue;
572 }
573
Ted Kremenekda4abf12010-02-11 00:53:01 +0000574 ObjCMethodList *Mem =
575 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
576 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000577 Prev = Prev->Next;
578 }
579
580 return Result;
581 }
582};
Mike Stump11289f42009-09-09 15:08:12 +0000583
584} // end anonymous namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +0000585
586/// \brief The on-disk hash table used for the global method pool.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000587typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
588 ASTSelectorLookupTable;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000589
Sebastian Redl2c373b92010-10-05 15:59:54 +0000590namespace clang {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000591class ASTIdentifierLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000592 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +0000593 ASTReader::PerFileData &F;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000594
595 // If we know the IdentifierInfo in advance, it is here and we will
596 // not build a new one. Used when deserializing information about an
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000597 // identifier that was constructed before the AST file was read.
Douglas Gregora868bbd2009-04-21 22:25:48 +0000598 IdentifierInfo *KnownII;
599
600public:
601 typedef IdentifierInfo * data_type;
602
603 typedef const std::pair<const char*, unsigned> external_key_type;
604
605 typedef external_key_type internal_key_type;
606
Sebastian Redl2c373b92010-10-05 15:59:54 +0000607 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000608 IdentifierInfo *II = 0)
Sebastian Redl2c373b92010-10-05 15:59:54 +0000609 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump11289f42009-09-09 15:08:12 +0000610
Douglas Gregora868bbd2009-04-21 22:25:48 +0000611 static bool EqualKey(const internal_key_type& a,
612 const internal_key_type& b) {
613 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
614 : false;
615 }
Mike Stump11289f42009-09-09 15:08:12 +0000616
Douglas Gregora868bbd2009-04-21 22:25:48 +0000617 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000618 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregora868bbd2009-04-21 22:25:48 +0000619 }
Mike Stump11289f42009-09-09 15:08:12 +0000620
Douglas Gregora868bbd2009-04-21 22:25:48 +0000621 // This hopefully will just get inlined and removed by the optimizer.
622 static const internal_key_type&
623 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000624
Douglas Gregor57756ea2010-10-14 22:11:03 +0000625 // This hopefully will just get inlined and removed by the optimizer.
626 static const external_key_type&
627 GetExternalKey(const internal_key_type& x) { return x; }
628
Douglas Gregora868bbd2009-04-21 22:25:48 +0000629 static std::pair<unsigned, unsigned>
630 ReadKeyDataLength(const unsigned char*& d) {
631 using namespace clang::io;
Douglas Gregor6b7bf5a2009-04-25 20:26:24 +0000632 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor5287b4e2009-04-25 21:04:17 +0000633 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000634 return std::make_pair(KeyLen, DataLen);
635 }
Mike Stump11289f42009-09-09 15:08:12 +0000636
Douglas Gregora868bbd2009-04-21 22:25:48 +0000637 static std::pair<const char*, unsigned>
638 ReadKey(const unsigned char* d, unsigned n) {
639 assert(n >= 2 && d[n-1] == '\0');
640 return std::make_pair((const char*) d, n-1);
641 }
Mike Stump11289f42009-09-09 15:08:12 +0000642
643 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregora868bbd2009-04-21 22:25:48 +0000644 const unsigned char* d,
645 unsigned DataLen) {
646 using namespace clang::io;
Sebastian Redl539c5062010-08-18 23:57:32 +0000647 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000648 bool IsInteresting = ID & 0x01;
649
650 // Wipe out the "is interesting" bit.
651 ID = ID >> 1;
652
653 if (!IsInteresting) {
Sebastian Redl98912122010-07-27 23:01:28 +0000654 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregor1d583f22009-04-28 21:18:29 +0000655 // and associate it with the persistent ID.
656 IdentifierInfo *II = KnownII;
657 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000658 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000659 Reader.SetIdentifierInfo(ID, II);
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000660 II->setIsFromAST();
Douglas Gregor1d583f22009-04-28 21:18:29 +0000661 return II;
662 }
663
Douglas Gregorb9256522009-04-28 21:32:13 +0000664 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000665 bool CPlusPlusOperatorKeyword = Bits & 0x01;
666 Bits >>= 1;
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000667 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
668 Bits >>= 1;
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000669 bool Poisoned = Bits & 0x01;
670 Bits >>= 1;
671 bool ExtensionToken = Bits & 0x01;
672 Bits >>= 1;
673 bool hasMacroDefinition = Bits & 0x01;
674 Bits >>= 1;
675 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
676 Bits >>= 10;
Mike Stump11289f42009-09-09 15:08:12 +0000677
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000678 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorb9256522009-04-28 21:32:13 +0000679 DataLen -= 6;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000680
681 // Build the IdentifierInfo itself and link the identifier ID with
682 // the new IdentifierInfo.
683 IdentifierInfo *II = KnownII;
684 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000685 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000686 Reader.SetIdentifierInfo(ID, II);
687
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000688 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000689 // Token IDs are read-only.
690 if (HasRevertedTokenIDToIdentifier)
691 II->RevertTokenIDToIdentifier();
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000692 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump11289f42009-09-09 15:08:12 +0000693 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000694 "Incorrect extension token flag");
695 (void)ExtensionToken;
696 II->setIsPoisoned(Poisoned);
697 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
698 "Incorrect C++ operator keyword flag");
699 (void)CPlusPlusOperatorKeyword;
700
Douglas Gregorc3366a52009-04-21 23:56:24 +0000701 // If this identifier is a macro, deserialize the macro
702 // definition.
703 if (hasMacroDefinition) {
Douglas Gregorb9256522009-04-28 21:32:13 +0000704 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor5ef9e332010-10-30 00:23:06 +0000705 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregorb9256522009-04-28 21:32:13 +0000706 DataLen -= 4;
Douglas Gregorc3366a52009-04-21 23:56:24 +0000707 }
Douglas Gregora868bbd2009-04-21 22:25:48 +0000708
709 // Read all of the declarations visible at global scope with this
710 // name.
Chris Lattner1d728882009-04-27 22:17:41 +0000711 if (Reader.getContext() == 0) return II;
Douglas Gregor1342e842009-07-06 18:54:52 +0000712 if (DataLen > 0) {
713 llvm::SmallVector<uint32_t, 4> DeclIDs;
714 for (; DataLen > 0; DataLen -= 4)
715 DeclIDs.push_back(ReadUnalignedLE32(d));
716 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000717 }
Mike Stump11289f42009-09-09 15:08:12 +0000718
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000719 II->setIsFromAST();
Douglas Gregora868bbd2009-04-21 22:25:48 +0000720 return II;
721 }
722};
Mike Stump11289f42009-09-09 15:08:12 +0000723
724} // end anonymous namespace
Douglas Gregora868bbd2009-04-21 22:25:48 +0000725
726/// \brief The on-disk hash table used to contain information about
727/// all of the identifiers in the program.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000728typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
729 ASTIdentifierLookupTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000730
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000731namespace {
732class ASTDeclContextNameLookupTrait {
733 ASTReader &Reader;
734
735public:
736 /// \brief Pair of begin/end iterators for DeclIDs.
737 typedef std::pair<DeclID *, DeclID *> data_type;
738
739 /// \brief Special internal key for declaration names.
740 /// The hash table creates keys for comparison; we do not create
741 /// a DeclarationName for the internal key to avoid deserializing types.
742 struct DeclNameKey {
743 DeclarationName::NameKind Kind;
744 uint64_t Data;
745 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
746 };
747
748 typedef DeclarationName external_key_type;
749 typedef DeclNameKey internal_key_type;
750
751 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
752
753 static bool EqualKey(const internal_key_type& a,
754 const internal_key_type& b) {
755 return a.Kind == b.Kind && a.Data == b.Data;
756 }
757
758 unsigned ComputeHash(const DeclNameKey &Key) const {
759 llvm::FoldingSetNodeID ID;
760 ID.AddInteger(Key.Kind);
761
762 switch (Key.Kind) {
763 case DeclarationName::Identifier:
764 case DeclarationName::CXXLiteralOperatorName:
765 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
766 break;
767 case DeclarationName::ObjCZeroArgSelector:
768 case DeclarationName::ObjCOneArgSelector:
769 case DeclarationName::ObjCMultiArgSelector:
770 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
771 break;
772 case DeclarationName::CXXConstructorName:
773 case DeclarationName::CXXDestructorName:
774 case DeclarationName::CXXConversionFunctionName:
775 ID.AddInteger((TypeID)Key.Data);
776 break;
777 case DeclarationName::CXXOperatorName:
778 ID.AddInteger((OverloadedOperatorKind)Key.Data);
779 break;
780 case DeclarationName::CXXUsingDirective:
781 break;
782 }
783
784 return ID.ComputeHash();
785 }
786
787 internal_key_type GetInternalKey(const external_key_type& Name) const {
788 DeclNameKey Key;
789 Key.Kind = Name.getNameKind();
790 switch (Name.getNameKind()) {
791 case DeclarationName::Identifier:
792 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
793 break;
794 case DeclarationName::ObjCZeroArgSelector:
795 case DeclarationName::ObjCOneArgSelector:
796 case DeclarationName::ObjCMultiArgSelector:
797 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
798 break;
799 case DeclarationName::CXXConstructorName:
800 case DeclarationName::CXXDestructorName:
801 case DeclarationName::CXXConversionFunctionName:
802 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
803 break;
804 case DeclarationName::CXXOperatorName:
805 Key.Data = Name.getCXXOverloadedOperator();
806 break;
807 case DeclarationName::CXXLiteralOperatorName:
808 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
809 break;
810 case DeclarationName::CXXUsingDirective:
811 break;
812 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000813
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000814 return Key;
815 }
816
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000817 external_key_type GetExternalKey(const internal_key_type& Key) const {
818 ASTContext *Context = Reader.getContext();
819 switch (Key.Kind) {
820 case DeclarationName::Identifier:
821 return DeclarationName((IdentifierInfo*)Key.Data);
822
823 case DeclarationName::ObjCZeroArgSelector:
824 case DeclarationName::ObjCOneArgSelector:
825 case DeclarationName::ObjCMultiArgSelector:
826 return DeclarationName(Selector(Key.Data));
827
828 case DeclarationName::CXXConstructorName:
829 return Context->DeclarationNames.getCXXConstructorName(
830 Context->getCanonicalType(Reader.GetType(Key.Data)));
831
832 case DeclarationName::CXXDestructorName:
833 return Context->DeclarationNames.getCXXDestructorName(
834 Context->getCanonicalType(Reader.GetType(Key.Data)));
835
836 case DeclarationName::CXXConversionFunctionName:
837 return Context->DeclarationNames.getCXXConversionFunctionName(
838 Context->getCanonicalType(Reader.GetType(Key.Data)));
839
840 case DeclarationName::CXXOperatorName:
841 return Context->DeclarationNames.getCXXOperatorName(
842 (OverloadedOperatorKind)Key.Data);
843
844 case DeclarationName::CXXLiteralOperatorName:
845 return Context->DeclarationNames.getCXXLiteralOperatorName(
846 (IdentifierInfo*)Key.Data);
847
848 case DeclarationName::CXXUsingDirective:
849 return DeclarationName::getUsingDirectiveName();
850 }
851
852 llvm_unreachable("Invalid Name Kind ?");
853 }
854
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000855 static std::pair<unsigned, unsigned>
856 ReadKeyDataLength(const unsigned char*& d) {
857 using namespace clang::io;
858 unsigned KeyLen = ReadUnalignedLE16(d);
859 unsigned DataLen = ReadUnalignedLE16(d);
860 return std::make_pair(KeyLen, DataLen);
861 }
862
863 internal_key_type ReadKey(const unsigned char* d, unsigned) {
864 using namespace clang::io;
865
866 DeclNameKey Key;
867 Key.Kind = (DeclarationName::NameKind)*d++;
868 switch (Key.Kind) {
869 case DeclarationName::Identifier:
870 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
871 break;
872 case DeclarationName::ObjCZeroArgSelector:
873 case DeclarationName::ObjCOneArgSelector:
874 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000875 Key.Data =
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000876 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
877 break;
878 case DeclarationName::CXXConstructorName:
879 case DeclarationName::CXXDestructorName:
880 case DeclarationName::CXXConversionFunctionName:
881 Key.Data = ReadUnalignedLE32(d); // TypeID
882 break;
883 case DeclarationName::CXXOperatorName:
884 Key.Data = *d++; // OverloadedOperatorKind
885 break;
886 case DeclarationName::CXXLiteralOperatorName:
887 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
888 break;
889 case DeclarationName::CXXUsingDirective:
890 break;
891 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000892
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000893 return Key;
894 }
895
896 data_type ReadData(internal_key_type, const unsigned char* d,
897 unsigned DataLen) {
898 using namespace clang::io;
899 unsigned NumDecls = ReadUnalignedLE16(d);
900 DeclID *Start = (DeclID *)d;
901 return std::make_pair(Start, Start + NumDecls);
902 }
903};
904
905} // end anonymous namespace
906
907/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
908typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
909 ASTDeclContextNameLookupTable;
910
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000911bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
912 const std::pair<uint64_t, uint64_t> &Offsets,
913 DeclContextInfo &Info) {
914 SavedStreamPosition SavedPosition(Cursor);
915 // First the lexical decls.
916 if (Offsets.first != 0) {
917 Cursor.JumpToBit(Offsets.first);
918
919 RecordData Record;
920 const char *Blob;
921 unsigned BlobLen;
922 unsigned Code = Cursor.ReadCode();
923 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
924 if (RecCode != DECL_CONTEXT_LEXICAL) {
925 Error("Expected lexical block");
926 return true;
927 }
928
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000929 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
930 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000931 } else {
932 Info.LexicalDecls = 0;
933 Info.NumLexicalDecls = 0;
934 }
935
936 // Now the lookup table.
937 if (Offsets.second != 0) {
938 Cursor.JumpToBit(Offsets.second);
939
940 RecordData Record;
941 const char *Blob;
942 unsigned BlobLen;
943 unsigned Code = Cursor.ReadCode();
944 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
945 if (RecCode != DECL_CONTEXT_VISIBLE) {
946 Error("Expected visible lookup table block");
947 return true;
948 }
949 Info.NameLookupTableData
950 = ASTDeclContextNameLookupTable::Create(
951 (const unsigned char *)Blob + Record[0],
952 (const unsigned char *)Blob,
953 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl9d8f58b2010-08-24 00:50:00 +0000954 } else {
955 Info.NameLookupTableData = 0;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000956 }
957
958 return false;
959}
960
Argyrios Kyrtzidisdaa41f52011-04-25 22:23:56 +0000961void ASTReader::Error(llvm::StringRef Msg) {
962 Error(diag::err_fe_pch_malformed, Msg);
963}
964
965void ASTReader::Error(unsigned DiagID,
966 llvm::StringRef Arg1, llvm::StringRef Arg2) {
967 if (Diags.isDiagnosticInFlight())
968 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
969 else
970 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000971}
972
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000973/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000974bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000975 if (Listener)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000976 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000977 ActualOriginalFileName,
Nick Lewycky36079892011-02-23 21:16:44 +0000978 SuggestedPredefines,
979 FileMgr);
Douglas Gregorc379c072009-04-28 18:58:38 +0000980 return false;
Douglas Gregor92863e42009-04-10 23:10:45 +0000981}
982
Douglas Gregorc5046832009-04-27 18:38:38 +0000983//===----------------------------------------------------------------------===//
984// Source Manager Deserialization
985//===----------------------------------------------------------------------===//
986
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000987/// \brief Read the line table in the source manager block.
Sebastian Redl2c373b92010-10-05 15:59:54 +0000988/// \returns true if there was an error.
989bool ASTReader::ParseLineTable(PerFileData &F,
990 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000991 unsigned Idx = 0;
992 LineTableInfo &LineTable = SourceMgr.getLineTable();
993
994 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +0000995 std::map<int, int> FileIDs;
996 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000997 // Extract the file name
998 unsigned FilenameLen = Record[Idx++];
999 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1000 Idx += FilenameLen;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001001 MaybeAddSystemRootToFilename(Filename);
Mike Stump11289f42009-09-09 15:08:12 +00001002 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregora8854652009-04-13 17:12:42 +00001003 Filename.size());
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001004 }
1005
1006 // Parse the line entries
1007 std::vector<LineEntry> Entries;
1008 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +00001009 int FID = Record[Idx++];
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001010
1011 // Extract the line entries
1012 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +00001013 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001014 Entries.clear();
1015 Entries.reserve(NumEntries);
1016 for (unsigned I = 0; I != NumEntries; ++I) {
1017 unsigned FileOffset = Record[Idx++];
1018 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +00001019 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +00001020 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001021 = (SrcMgr::CharacteristicKind)Record[Idx++];
1022 unsigned IncludeOffset = Record[Idx++];
1023 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1024 FileKind, IncludeOffset));
1025 }
1026 LineTable.AddEntry(FID, Entries);
1027 }
1028
1029 return false;
1030}
1031
Douglas Gregorc5046832009-04-27 18:38:38 +00001032namespace {
1033
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001034class ASTStatData {
Douglas Gregorc5046832009-04-27 18:38:38 +00001035public:
Douglas Gregorc5046832009-04-27 18:38:38 +00001036 const ino_t ino;
1037 const dev_t dev;
1038 const mode_t mode;
1039 const time_t mtime;
1040 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +00001041
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001042 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner2a6fa472010-11-23 19:28:12 +00001043 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregorc5046832009-04-27 18:38:38 +00001044};
1045
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001046class ASTStatLookupTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001047 public:
1048 typedef const char *external_key_type;
1049 typedef const char *internal_key_type;
1050
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001051 typedef ASTStatData data_type;
Douglas Gregorc5046832009-04-27 18:38:38 +00001052
1053 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001054 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001055 }
1056
1057 static internal_key_type GetInternalKey(const char *path) { return path; }
1058
1059 static bool EqualKey(internal_key_type a, internal_key_type b) {
1060 return strcmp(a, b) == 0;
1061 }
1062
1063 static std::pair<unsigned, unsigned>
1064 ReadKeyDataLength(const unsigned char*& d) {
1065 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1066 unsigned DataLen = (unsigned) *d++;
1067 return std::make_pair(KeyLen + 1, DataLen);
1068 }
1069
1070 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1071 return (const char *)d;
1072 }
1073
1074 static data_type ReadData(const internal_key_type, const unsigned char *d,
1075 unsigned /*DataLen*/) {
1076 using namespace clang::io;
1077
Douglas Gregorc5046832009-04-27 18:38:38 +00001078 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1079 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1080 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +00001081 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +00001082 off_t size = (off_t) ReadUnalignedLE64(d);
1083 return data_type(ino, dev, mode, mtime, size);
1084 }
1085};
1086
1087/// \brief stat() cache for precompiled headers.
1088///
1089/// This cache is very similar to the stat cache used by pretokenized
1090/// headers.
Chris Lattner226efd32010-11-23 19:19:34 +00001091class ASTStatCache : public FileSystemStatCache {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001092 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregorc5046832009-04-27 18:38:38 +00001093 CacheTy *Cache;
1094
1095 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +00001096public:
Chris Lattner2a6fa472010-11-23 19:28:12 +00001097 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1098 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +00001099 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1100 Cache = CacheTy::Create(Buckets, Base);
1101 }
1102
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001103 ~ASTStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +00001104
Chris Lattnerdd278432010-11-23 21:17:56 +00001105 LookupResult getStat(const char *Path, struct stat &StatBuf,
1106 int *FileDescriptor) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001107 // Do the lookup for the file's data in the AST file.
Chris Lattner226efd32010-11-23 19:19:34 +00001108 CacheTy::iterator I = Cache->find(Path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001109
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001110 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregorc5046832009-04-27 18:38:38 +00001111 if (I == Cache->end()) {
1112 ++NumStatMisses;
Chris Lattnerdd278432010-11-23 21:17:56 +00001113 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregorc5046832009-04-27 18:38:38 +00001114 }
Mike Stump11289f42009-09-09 15:08:12 +00001115
Douglas Gregorc5046832009-04-27 18:38:38 +00001116 ++NumStatHits;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001117 ASTStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001118
Chris Lattner226efd32010-11-23 19:19:34 +00001119 StatBuf.st_ino = Data.ino;
1120 StatBuf.st_dev = Data.dev;
1121 StatBuf.st_mtime = Data.mtime;
1122 StatBuf.st_mode = Data.mode;
1123 StatBuf.st_size = Data.size;
Chris Lattner8f0583d2010-11-23 20:05:15 +00001124 return CacheExists;
Douglas Gregorc5046832009-04-27 18:38:38 +00001125 }
1126};
1127} // end anonymous namespace
1128
1129
Sebastian Redl393f8b72010-07-19 20:52:06 +00001130/// \brief Read a source manager block
Sebastian Redl2c499f62010-08-18 23:56:43 +00001131ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001132 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +00001133
Sebastian Redl393f8b72010-07-19 20:52:06 +00001134 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001135
Douglas Gregor258ae542009-04-27 06:38:32 +00001136 // Set the source-location entry cursor to the current position in
1137 // the stream. This cursor will be used to read the contents of the
1138 // source manager block initially, and then lazily read
1139 // source-location entries as needed.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001140 SLocEntryCursor = F.Stream;
Douglas Gregor258ae542009-04-27 06:38:32 +00001141
1142 // The stream itself is going to skip over the source manager block.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001143 if (F.Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001144 Error("malformed block record in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001145 return Failure;
1146 }
1147
1148 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001149 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001150 Error("malformed source manager block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001151 return Failure;
1152 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001153
Douglas Gregora7f71a92009-04-10 03:52:48 +00001154 RecordData Record;
1155 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001156 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001157 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001158 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001159 Error("error at end of Source Manager block in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001160 return Failure;
1161 }
Douglas Gregor92863e42009-04-10 23:10:45 +00001162 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001163 }
Mike Stump11289f42009-09-09 15:08:12 +00001164
Douglas Gregora7f71a92009-04-10 03:52:48 +00001165 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1166 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +00001167 SLocEntryCursor.ReadSubBlockID();
1168 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001169 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001170 return Failure;
1171 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001172 continue;
1173 }
Mike Stump11289f42009-09-09 15:08:12 +00001174
Douglas Gregora7f71a92009-04-10 03:52:48 +00001175 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001176 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001177 continue;
1178 }
Mike Stump11289f42009-09-09 15:08:12 +00001179
Douglas Gregora7f71a92009-04-10 03:52:48 +00001180 // Read a record.
1181 const char *BlobStart;
1182 unsigned BlobLen;
1183 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +00001184 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001185 default: // Default behavior: ignore.
1186 break;
1187
Sebastian Redl539c5062010-08-18 23:57:32 +00001188 case SM_LINE_TABLE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00001189 if (ParseLineTable(F, Record))
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001190 return Failure;
Chris Lattner184e65d2009-04-14 23:22:57 +00001191 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00001192
Sebastian Redl539c5062010-08-18 23:57:32 +00001193 case SM_SLOC_FILE_ENTRY:
1194 case SM_SLOC_BUFFER_ENTRY:
1195 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor258ae542009-04-27 06:38:32 +00001196 // Once we hit one of the source location entries, we're done.
1197 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001198 }
1199 }
1200}
1201
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001202/// \brief If a header file is not found at the path that we expect it to be
1203/// and the PCH file was moved from its original location, try to resolve the
1204/// file by assuming that header+PCH were moved together and the header is in
1205/// the same place relative to the PCH.
1206static std::string
1207resolveFileRelativeToOriginalDir(const std::string &Filename,
1208 const std::string &OriginalDir,
1209 const std::string &CurrDir) {
1210 assert(OriginalDir != CurrDir &&
1211 "No point trying to resolve the file if the PCH dir didn't change");
1212 using namespace llvm::sys;
1213 llvm::SmallString<128> filePath(Filename);
1214 fs::make_absolute(filePath);
1215 assert(path::is_absolute(OriginalDir));
1216 llvm::SmallString<128> currPCHPath(CurrDir);
1217
1218 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1219 fileDirE = path::end(path::parent_path(filePath));
1220 path::const_iterator origDirI = path::begin(OriginalDir),
1221 origDirE = path::end(OriginalDir);
1222 // Skip the common path components from filePath and OriginalDir.
1223 while (fileDirI != fileDirE && origDirI != origDirE &&
1224 *fileDirI == *origDirI) {
1225 ++fileDirI;
1226 ++origDirI;
1227 }
1228 for (; origDirI != origDirE; ++origDirI)
1229 path::append(currPCHPath, "..");
1230 path::append(currPCHPath, fileDirI, fileDirE);
1231 path::append(currPCHPath, path::filename(Filename));
1232 return currPCHPath.str();
1233}
1234
Sebastian Redl06750302010-07-20 21:50:20 +00001235/// \brief Get a cursor that's correctly positioned for reading the source
1236/// location entry with the given ID.
Sebastian Redl2c373b92010-10-05 15:59:54 +00001237ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl06750302010-07-20 21:50:20 +00001238 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1239 "SLocCursorForID should only be called for real IDs.");
1240
1241 ID -= 1;
1242 PerFileData *F = 0;
1243 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1244 F = Chain[N - I - 1];
1245 if (ID < F->LocalNumSLocEntries)
1246 break;
1247 ID -= F->LocalNumSLocEntries;
1248 }
1249 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1250
1251 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00001252 return F;
Sebastian Redl06750302010-07-20 21:50:20 +00001253}
1254
Douglas Gregor258ae542009-04-27 06:38:32 +00001255/// \brief Read in the source location entry with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001256ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001257 if (ID == 0)
1258 return Success;
1259
1260 if (ID > TotalNumSLocEntries) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001261 Error("source location entry ID out-of-range for AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001262 return Failure;
1263 }
1264
Sebastian Redl2c373b92010-10-05 15:59:54 +00001265 PerFileData *F = SLocCursorForID(ID);
1266 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001267
Douglas Gregor258ae542009-04-27 06:38:32 +00001268 ++NumSLocEntriesRead;
Douglas Gregor258ae542009-04-27 06:38:32 +00001269 unsigned Code = SLocEntryCursor.ReadCode();
1270 if (Code == llvm::bitc::END_BLOCK ||
1271 Code == llvm::bitc::ENTER_SUBBLOCK ||
1272 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001273 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001274 return Failure;
1275 }
1276
Douglas Gregor258ae542009-04-27 06:38:32 +00001277 RecordData Record;
1278 const char *BlobStart;
1279 unsigned BlobLen;
1280 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1281 default:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001282 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001283 return Failure;
1284
Sebastian Redl539c5062010-08-18 23:57:32 +00001285 case SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001286 std::string Filename(BlobStart, BlobStart + BlobLen);
1287 MaybeAddSystemRootToFilename(Filename);
Chris Lattner5159f612010-11-23 08:35:12 +00001288 const FileEntry *File = FileMgr.getFile(Filename);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001289 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1290 OriginalDir != CurrentDir) {
1291 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1292 OriginalDir,
1293 CurrentDir);
1294 if (!resolved.empty())
1295 File = FileMgr.getFile(resolved);
1296 }
Axel Naumann63fbaed2011-01-27 10:55:51 +00001297 if (File == 0)
1298 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1299 (time_t)Record[5]);
Chris Lattnerd20dc872009-06-15 04:35:16 +00001300 if (File == 0) {
1301 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001302 ErrorStr += Filename;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001303 ErrorStr += "' referenced by AST file";
Chris Lattnerd20dc872009-06-15 04:35:16 +00001304 Error(ErrorStr.c_str());
1305 return Failure;
1306 }
Mike Stump11289f42009-09-09 15:08:12 +00001307
Douglas Gregor09b69892011-02-10 17:09:37 +00001308 if (Record.size() < 6) {
Ted Kremenekabb1ddd2010-03-18 21:23:05 +00001309 Error("source location entry is incorrect");
1310 return Failure;
1311 }
1312
Douglas Gregorce3a8292010-07-27 00:27:13 +00001313 if (!DisableValidation &&
1314 ((off_t)Record[4] != File->getSize()
Douglas Gregor08288f22010-04-09 15:54:22 +00001315#if !defined(LLVM_ON_WIN32)
1316 // In our regression testing, the Windows file system seems to
1317 // have inconsistent modification times that sometimes
1318 // erroneously trigger this error-handling path.
Douglas Gregorce3a8292010-07-27 00:27:13 +00001319 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor08288f22010-04-09 15:54:22 +00001320#endif
Douglas Gregorce3a8292010-07-27 00:27:13 +00001321 )) {
Argyrios Kyrtzidisdaa41f52011-04-25 22:23:56 +00001322 Error(diag::err_fe_pch_file_modified, Filename);
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001323 return Failure;
1324 }
1325
Chris Lattner26b5c192010-11-23 09:19:42 +00001326 FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]),
1327 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor258ae542009-04-27 06:38:32 +00001328 ID, Record[0]);
1329 if (Record[3])
1330 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1331 .setHasLineDirectives();
Douglas Gregor09b69892011-02-10 17:09:37 +00001332
Douglas Gregor258ae542009-04-27 06:38:32 +00001333 break;
1334 }
1335
Sebastian Redl539c5062010-08-18 23:57:32 +00001336 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor258ae542009-04-27 06:38:32 +00001337 const char *Name = BlobStart;
1338 unsigned Offset = Record[0];
1339 unsigned Code = SLocEntryCursor.ReadCode();
1340 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001341 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +00001342 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001343
Sebastian Redl539c5062010-08-18 23:57:32 +00001344 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001345 Error("AST record has invalid code");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001346 return Failure;
1347 }
1348
Douglas Gregor258ae542009-04-27 06:38:32 +00001349 llvm::MemoryBuffer *Buffer
Chris Lattner58c79342010-04-05 22:42:27 +00001350 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1351 Name);
Douglas Gregor258ae542009-04-27 06:38:32 +00001352 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump11289f42009-09-09 15:08:12 +00001353
Douglas Gregore6648fb2009-04-28 20:33:11 +00001354 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001355 PCHPredefinesBlock Block = {
1356 BufferID,
1357 llvm::StringRef(BlobStart, BlobLen - 1)
1358 };
1359 PCHPredefinesBuffers.push_back(Block);
Douglas Gregore6648fb2009-04-28 20:33:11 +00001360 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001361
1362 break;
1363 }
1364
Sebastian Redl539c5062010-08-18 23:57:32 +00001365 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001366 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001367 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001368 ReadSourceLocation(*F, Record[2]),
1369 ReadSourceLocation(*F, Record[3]),
Douglas Gregor258ae542009-04-27 06:38:32 +00001370 Record[4],
1371 ID,
1372 Record[0]);
1373 break;
Mike Stump11289f42009-09-09 15:08:12 +00001374 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001375 }
1376
1377 return Success;
1378}
1379
Chris Lattnere78a6be2009-04-27 01:05:14 +00001380/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1381/// specified cursor. Read the abbreviations that are at the top of the block
1382/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001383bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001384 unsigned BlockID) {
1385 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001386 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001387 return Failure;
1388 }
Mike Stump11289f42009-09-09 15:08:12 +00001389
Chris Lattnere78a6be2009-04-27 01:05:14 +00001390 while (true) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001391 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattnere78a6be2009-04-27 01:05:14 +00001392 unsigned Code = Cursor.ReadCode();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001393
Chris Lattnere78a6be2009-04-27 01:05:14 +00001394 // We expect all abbrevs to be at the start of the block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001395 if (Code != llvm::bitc::DEFINE_ABBREV) {
1396 Cursor.JumpToBit(Offset);
Chris Lattnere78a6be2009-04-27 01:05:14 +00001397 return false;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001398 }
Chris Lattnere78a6be2009-04-27 01:05:14 +00001399 Cursor.ReadAbbrevRecord();
1400 }
1401}
1402
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001403PreprocessedEntity *ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001404 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregor796d76a2010-10-20 22:00:55 +00001405 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump11289f42009-09-09 15:08:12 +00001406
Douglas Gregorc3366a52009-04-21 23:56:24 +00001407 // Keep track of where we are in the stream, then jump back there
1408 // after reading this macro.
1409 SavedStreamPosition SavedPosition(Stream);
1410
1411 Stream.JumpToBit(Offset);
1412 RecordData Record;
1413 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1414 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001415
Douglas Gregorc3366a52009-04-21 23:56:24 +00001416 while (true) {
1417 unsigned Code = Stream.ReadCode();
1418 switch (Code) {
1419 case llvm::bitc::END_BLOCK:
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001420 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001421
1422 case llvm::bitc::ENTER_SUBBLOCK:
1423 // No known subblocks, always skip them.
1424 Stream.ReadSubBlockID();
1425 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001426 Error("malformed block record in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001427 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001428 }
1429 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001430
Douglas Gregorc3366a52009-04-21 23:56:24 +00001431 case llvm::bitc::DEFINE_ABBREV:
1432 Stream.ReadAbbrevRecord();
1433 continue;
1434 default: break;
1435 }
1436
1437 // Read a record.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001438 const char *BlobStart = 0;
1439 unsigned BlobLen = 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001440 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001441 PreprocessorRecordTypes RecType =
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001442 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001443 BlobLen);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001444 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001445 case PP_MACRO_OBJECT_LIKE:
1446 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001447 // If we already have a macro, that means that we've hit the end
1448 // of the definition of the macro we were looking for. We're
1449 // done.
1450 if (Macro)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001451 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001452
1453 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1454 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001455 Error("macro must have a name in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001456 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001457 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00001458 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001459 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001460
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001461 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001462 MI->setIsUsed(isUsed);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001463 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001464
Douglas Gregoraae92242010-03-19 21:51:54 +00001465 unsigned NextIndex = 3;
Sebastian Redl539c5062010-08-18 23:57:32 +00001466 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001467 // Decode function-like macro info.
1468 bool isC99VarArgs = Record[3];
1469 bool isGNUVarArgs = Record[4];
1470 MacroArgs.clear();
1471 unsigned NumArgs = Record[5];
Douglas Gregoraae92242010-03-19 21:51:54 +00001472 NextIndex = 6 + NumArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001473 for (unsigned i = 0; i != NumArgs; ++i)
1474 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1475
1476 // Install function-like macro info.
1477 MI->setIsFunctionLike();
1478 if (isC99VarArgs) MI->setIsC99Varargs();
1479 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001480 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001481 PP->getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001482 }
1483
1484 // Finally, install the macro.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001485 PP->setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001486
1487 // Remember that we saw this macro last so that we add the tokens that
1488 // form its body to it.
1489 Macro = MI;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001490
Douglas Gregoraae92242010-03-19 21:51:54 +00001491 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1492 // We have a macro definition. Load it now.
1493 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1494 getMacroDefinition(Record[NextIndex]));
1495 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001496
Douglas Gregorc3366a52009-04-21 23:56:24 +00001497 ++NumMacrosRead;
1498 break;
1499 }
Mike Stump11289f42009-09-09 15:08:12 +00001500
Sebastian Redl539c5062010-08-18 23:57:32 +00001501 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001502 // If we see a TOKEN before a PP_MACRO_*, then the file is
1503 // erroneous, just pretend we didn't see this.
1504 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001505
Douglas Gregorc3366a52009-04-21 23:56:24 +00001506 Token Tok;
1507 Tok.startToken();
Sebastian Redl2c373b92010-10-05 15:59:54 +00001508 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001509 Tok.setLength(Record[1]);
1510 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1511 Tok.setIdentifierInfo(II);
1512 Tok.setKind((tok::TokenKind)Record[3]);
1513 Tok.setFlag((Token::TokenFlags)Record[4]);
1514 Macro->AddTokenToBody(Tok);
1515 break;
1516 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001517 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001518 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001519
1520 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001521}
1522
Douglas Gregor92a96f52011-02-08 21:58:10 +00001523PreprocessedEntity *ASTReader::LoadPreprocessedEntity(PerFileData &F) {
1524 assert(PP && "Forgot to set Preprocessor ?");
1525 unsigned Code = F.PreprocessorDetailCursor.ReadCode();
1526 switch (Code) {
1527 case llvm::bitc::END_BLOCK:
1528 return 0;
1529
1530 case llvm::bitc::ENTER_SUBBLOCK:
1531 Error("unexpected subblock record in preprocessor detail block");
1532 return 0;
1533
1534 case llvm::bitc::DEFINE_ABBREV:
1535 Error("unexpected abbrevation record in preprocessor detail block");
1536 return 0;
1537
1538 default:
1539 break;
1540 }
1541
1542 if (!PP->getPreprocessingRecord()) {
1543 Error("no preprocessing record");
1544 return 0;
1545 }
1546
1547 // Read the record.
1548 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1549 const char *BlobStart = 0;
1550 unsigned BlobLen = 0;
1551 RecordData Record;
1552 PreprocessorDetailRecordTypes RecType =
1553 (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord(
1554 Code, Record, BlobStart, BlobLen);
1555 switch (RecType) {
1556 case PPD_MACRO_INSTANTIATION: {
1557 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1558 return PE;
1559
1560 MacroInstantiation *MI
1561 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1562 SourceRange(ReadSourceLocation(F, Record[1]),
1563 ReadSourceLocation(F, Record[2])),
1564 getMacroDefinition(Record[4]));
1565 PPRec.SetPreallocatedEntity(Record[0], MI);
1566 return MI;
1567 }
1568
1569 case PPD_MACRO_DEFINITION: {
1570 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1571 return PE;
1572
1573 if (Record[1] > MacroDefinitionsLoaded.size()) {
1574 Error("out-of-bounds macro definition record");
1575 return 0;
1576 }
1577
1578 // Decode the identifier info and then check again; if the macro is
1579 // still defined and associated with the identifier,
1580 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1581 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1582 MacroDefinition *MD
1583 = new (PPRec) MacroDefinition(II,
1584 ReadSourceLocation(F, Record[5]),
1585 SourceRange(
1586 ReadSourceLocation(F, Record[2]),
1587 ReadSourceLocation(F, Record[3])));
1588
1589 PPRec.SetPreallocatedEntity(Record[0], MD);
1590 MacroDefinitionsLoaded[Record[1] - 1] = MD;
1591
1592 if (DeserializationListener)
1593 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1594 }
1595
1596 return MacroDefinitionsLoaded[Record[1] - 1];
1597 }
1598
1599 case PPD_INCLUSION_DIRECTIVE: {
1600 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1601 return PE;
1602
1603 const char *FullFileNameStart = BlobStart + Record[3];
1604 const FileEntry *File
1605 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
1606 BlobLen - Record[3]));
1607
1608 // FIXME: Stable encoding
1609 InclusionDirective::InclusionKind Kind
1610 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1611 InclusionDirective *ID
1612 = new (PPRec) InclusionDirective(PPRec, Kind,
1613 llvm::StringRef(BlobStart, Record[3]),
1614 Record[4],
1615 File,
1616 SourceRange(ReadSourceLocation(F, Record[1]),
1617 ReadSourceLocation(F, Record[2])));
1618 PPRec.SetPreallocatedEntity(Record[0], ID);
1619 return ID;
1620 }
1621 }
1622
1623 Error("invalid offset in preprocessor detail block");
1624 return 0;
1625}
1626
Douglas Gregor09b69892011-02-10 17:09:37 +00001627namespace {
1628 /// \brief Trait class used to search the on-disk hash table containing all of
1629 /// the header search information.
1630 ///
1631 /// The on-disk hash table contains a mapping from each header path to
1632 /// information about that header (how many times it has been included, its
1633 /// controlling macro, etc.). Note that we actually hash based on the
1634 /// filename, and support "deep" comparisons of file names based on current
1635 /// inode numbers, so that the search can cope with non-normalized path names
1636 /// and symlinks.
1637 class HeaderFileInfoTrait {
1638 const char *SearchPath;
1639 struct stat SearchPathStatBuf;
1640 llvm::Optional<int> SearchPathStatResult;
1641
1642 int StatSimpleCache(const char *Path, struct stat *StatBuf) {
1643 if (Path == SearchPath) {
1644 if (!SearchPathStatResult)
1645 SearchPathStatResult = stat(Path, &SearchPathStatBuf);
1646
1647 *StatBuf = SearchPathStatBuf;
1648 return *SearchPathStatResult;
1649 }
1650
1651 return stat(Path, StatBuf);
1652 }
1653
1654 public:
1655 typedef const char *external_key_type;
1656 typedef const char *internal_key_type;
1657
1658 typedef HeaderFileInfo data_type;
1659
1660 HeaderFileInfoTrait(const char *SearchPath = 0) : SearchPath(SearchPath) { }
1661
1662 static unsigned ComputeHash(const char *path) {
1663 return llvm::HashString(llvm::sys::path::filename(path));
1664 }
1665
1666 static internal_key_type GetInternalKey(const char *path) { return path; }
1667
1668 bool EqualKey(internal_key_type a, internal_key_type b) {
1669 if (strcmp(a, b) == 0)
1670 return true;
1671
1672 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1673 return false;
1674
1675 // The file names match, but the path names don't. stat() the files to
1676 // see if they are the same.
1677 struct stat StatBufA, StatBufB;
1678 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1679 return false;
1680
1681 return StatBufA.st_ino == StatBufB.st_ino;
1682 }
1683
1684 static std::pair<unsigned, unsigned>
1685 ReadKeyDataLength(const unsigned char*& d) {
1686 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1687 unsigned DataLen = (unsigned) *d++;
1688 return std::make_pair(KeyLen + 1, DataLen);
1689 }
1690
1691 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1692 return (const char *)d;
1693 }
1694
1695 static data_type ReadData(const internal_key_type, const unsigned char *d,
1696 unsigned DataLen) {
1697 const unsigned char *End = d + DataLen;
1698 using namespace clang::io;
1699 HeaderFileInfo HFI;
1700 unsigned Flags = *d++;
Douglas Gregor37aa4932011-05-04 00:14:37 +00001701 HFI.isImport = (Flags >> 4) & 0x01;
1702 HFI.isPragmaOnce = (Flags >> 3) & 0x01;
Douglas Gregor09b69892011-02-10 17:09:37 +00001703 HFI.DirInfo = (Flags >> 1) & 0x03;
1704 HFI.Resolved = Flags & 0x01;
1705 HFI.NumIncludes = ReadUnalignedLE16(d);
1706 HFI.ControllingMacroID = ReadUnalignedLE32(d);
1707 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1708 (void)End;
1709
1710 // This HeaderFileInfo was externally loaded.
1711 HFI.External = true;
1712 return HFI;
1713 }
1714 };
1715}
1716
1717/// \brief The on-disk hash table used for the global method pool.
1718typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
1719 HeaderFileInfoLookupTable;
1720
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001721void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1722 uint64_t Offset) {
1723 // Note that this identifier has a macro definition.
1724 II->setHasMacroDefinition(true);
1725
1726 // Adjust the offset based on our position in the chain.
1727 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1728 if (Chain[I] == &F)
1729 break;
1730
1731 Offset += Chain[I]->SizeInBits;
1732 }
1733
1734 UnreadMacroRecordOffsets[II] = Offset;
1735}
1736
Sebastian Redl2c499f62010-08-18 23:56:43 +00001737void ASTReader::ReadDefinedMacros() {
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001738 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001739 PerFileData &F = *Chain[N - I - 1];
1740 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001741
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001742 // If there was no preprocessor block, skip this file.
1743 if (!MacroCursor.getBitStreamReader())
1744 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001745
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001746 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001747 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001748
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001749 RecordData Record;
1750 while (true) {
1751 unsigned Code = Cursor.ReadCode();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001752 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001753 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001754
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001755 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1756 // No known subblocks, always skip them.
1757 Cursor.ReadSubBlockID();
1758 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001759 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001760 return;
1761 }
1762 continue;
1763 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001764
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001765 if (Code == llvm::bitc::DEFINE_ABBREV) {
1766 Cursor.ReadAbbrevRecord();
1767 continue;
1768 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001769
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001770 // Read a record.
1771 const char *BlobStart;
1772 unsigned BlobLen;
1773 Record.clear();
1774 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1775 default: // Default behavior: ignore.
1776 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001777
Sebastian Redl539c5062010-08-18 23:57:32 +00001778 case PP_MACRO_OBJECT_LIKE:
1779 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001780 DecodeIdentifierInfo(Record[0]);
1781 break;
1782
Sebastian Redl539c5062010-08-18 23:57:32 +00001783 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001784 // Ignore tokens.
1785 break;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001786 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001787 }
1788 }
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001789
1790 // Drain the unread macro-record offsets map.
1791 while (!UnreadMacroRecordOffsets.empty())
1792 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1793}
1794
1795void ASTReader::LoadMacroDefinition(
1796 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1797 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1798 PerFileData *F = 0;
1799 uint64_t Offset = Pos->second;
1800 UnreadMacroRecordOffsets.erase(Pos);
1801
1802 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1803 if (Offset < Chain[I]->SizeInBits) {
1804 F = Chain[I];
1805 break;
1806 }
1807
1808 Offset -= Chain[I]->SizeInBits;
1809 }
1810 if (!F) {
1811 Error("Malformed macro record offset");
1812 return;
1813 }
1814
1815 ReadMacroRecord(*F, Offset);
1816}
1817
1818void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1819 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1820 = UnreadMacroRecordOffsets.find(II);
1821 LoadMacroDefinition(Pos);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001822}
1823
Sebastian Redl50e26582010-09-15 19:54:06 +00001824MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor91096292010-10-02 19:29:26 +00001825 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregoraae92242010-03-19 21:51:54 +00001826 return 0;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001827
Douglas Gregor91096292010-10-02 19:29:26 +00001828 if (!MacroDefinitionsLoaded[ID - 1]) {
1829 unsigned Index = ID - 1;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001830 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1831 PerFileData &F = *Chain[N - I - 1];
1832 if (Index < F.LocalNumMacroDefinitions) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001833 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
1834 F.PreprocessorDetailCursor.JumpToBit(F.MacroDefinitionOffsets[Index]);
1835 LoadPreprocessedEntity(F);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001836 break;
1837 }
1838 Index -= F.LocalNumMacroDefinitions;
1839 }
Douglas Gregor91096292010-10-02 19:29:26 +00001840 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001841 }
1842
Douglas Gregor91096292010-10-02 19:29:26 +00001843 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001844}
1845
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00001846const FileEntry *ASTReader::getFileEntry(llvm::StringRef filenameStrRef) {
1847 std::string Filename = filenameStrRef;
1848 MaybeAddSystemRootToFilename(Filename);
1849 const FileEntry *File = FileMgr.getFile(Filename);
1850 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1851 OriginalDir != CurrentDir) {
1852 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1853 OriginalDir,
1854 CurrentDir);
1855 if (!resolved.empty())
1856 File = FileMgr.getFile(resolved);
1857 }
1858
1859 return File;
1860}
1861
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001862/// \brief If we are loading a relocatable PCH file, and the filename is
1863/// not an absolute path, add the system root to the beginning of the file
1864/// name.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001865void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001866 // If this is not a relocatable PCH file, there's nothing to do.
1867 if (!RelocatablePCH)
1868 return;
Mike Stump11289f42009-09-09 15:08:12 +00001869
Michael J. Spencerf28df4c2010-12-17 21:22:22 +00001870 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001871 return;
1872
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001873 if (isysroot == 0) {
1874 // If no system root was given, default to '/'
1875 Filename.insert(Filename.begin(), '/');
1876 return;
1877 }
Mike Stump11289f42009-09-09 15:08:12 +00001878
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001879 unsigned Length = strlen(isysroot);
1880 if (isysroot[Length - 1] != '/')
1881 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001882
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001883 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1884}
1885
Sebastian Redl2c499f62010-08-18 23:56:43 +00001886ASTReader::ASTReadResult
Sebastian Redl3e31c722010-08-18 23:56:56 +00001887ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001888 llvm::BitstreamCursor &Stream = F.Stream;
1889
Sebastian Redl539c5062010-08-18 23:57:32 +00001890 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001891 Error("malformed block record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001892 return Failure;
1893 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001894
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001895 // Read all of the records and blocks for the ASt file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001896 RecordData Record;
Sebastian Redl393f8b72010-07-19 20:52:06 +00001897 bool First = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001898 while (!Stream.AtEndOfStream()) {
1899 unsigned Code = Stream.ReadCode();
1900 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001901 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001902 Error("error at end of module block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001903 return Failure;
1904 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001905
Douglas Gregor55abb232009-04-10 20:39:37 +00001906 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001907 }
1908
1909 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1910 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001911 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001912 // We lazily load the decls block, but we want to set up the
1913 // DeclsCursor cursor to point into it. Clone our current bitcode
1914 // cursor to it, enter the block and read the abbrevs in that block.
1915 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001916 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001917 if (Stream.SkipBlock() || // Skip with the main cursor.
1918 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001919 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001920 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001921 return Failure;
1922 }
1923 break;
Mike Stump11289f42009-09-09 15:08:12 +00001924
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00001925 case DECL_UPDATES_BLOCK_ID:
1926 if (Stream.SkipBlock()) {
1927 Error("malformed block record in AST file");
1928 return Failure;
1929 }
1930 break;
1931
Sebastian Redl539c5062010-08-18 23:57:32 +00001932 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001933 F.MacroCursor = Stream;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001934 if (PP)
1935 PP->setExternalSource(this);
1936
Douglas Gregor796d76a2010-10-20 22:00:55 +00001937 if (Stream.SkipBlock() ||
1938 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001939 Error("malformed block record in AST file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001940 return Failure;
1941 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001942 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001943 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001944
Douglas Gregor92a96f52011-02-08 21:58:10 +00001945 case PREPROCESSOR_DETAIL_BLOCK_ID:
1946 F.PreprocessorDetailCursor = Stream;
1947 if (Stream.SkipBlock() ||
1948 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1949 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1950 Error("malformed preprocessor detail record in AST file");
1951 return Failure;
1952 }
1953 F.PreprocessorDetailStartOffset
1954 = F.PreprocessorDetailCursor.GetCurrentBitNo();
1955 break;
1956
Sebastian Redl539c5062010-08-18 23:57:32 +00001957 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001958 switch (ReadSourceManagerBlock(F)) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001959 case Success:
1960 break;
1961
1962 case Failure:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001963 Error("malformed source manager block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001964 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001965
1966 case IgnorePCH:
1967 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001968 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001969 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001970 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00001971 First = false;
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001972 continue;
1973 }
1974
1975 if (Code == llvm::bitc::DEFINE_ABBREV) {
1976 Stream.ReadAbbrevRecord();
1977 continue;
1978 }
1979
1980 // Read and process a record.
1981 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001982 const char *BlobStart = 0;
1983 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001984 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001985 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001986 default: // Default behavior: ignore.
1987 break;
1988
Sebastian Redl539c5062010-08-18 23:57:32 +00001989 case METADATA: {
1990 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1991 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001992 : diag::warn_pch_version_too_new);
1993 return IgnorePCH;
1994 }
1995
1996 RelocatablePCH = Record[4];
1997 if (Listener) {
1998 std::string TargetTriple(BlobStart, BlobLen);
1999 if (Listener->ReadTargetTriple(TargetTriple))
2000 return IgnorePCH;
2001 }
2002 break;
2003 }
2004
Sebastian Redl539c5062010-08-18 23:57:32 +00002005 case CHAINED_METADATA: {
Sebastian Redl393f8b72010-07-19 20:52:06 +00002006 if (!First) {
2007 Error("CHAINED_METADATA is not first record in block");
2008 return Failure;
2009 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002010 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2011 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002012 : diag::warn_pch_version_too_new);
2013 return IgnorePCH;
2014 }
2015
Sebastian Redl009e7f22010-10-05 16:15:19 +00002016 // Load the chained file, which is always a PCH file.
2017 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002018 case Failure: return Failure;
2019 // If we have to ignore the dependency, we'll have to ignore this too.
2020 case IgnorePCH: return IgnorePCH;
2021 case Success: break;
2022 }
2023 break;
2024 }
2025
Sebastian Redl539c5062010-08-18 23:57:32 +00002026 case TYPE_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00002027 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002028 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002029 return Failure;
2030 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002031 F.TypeOffsets = (const uint32_t *)BlobStart;
2032 F.LocalNumTypes = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002033 break;
2034
Sebastian Redl539c5062010-08-18 23:57:32 +00002035 case DECL_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00002036 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002037 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002038 return Failure;
2039 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002040 F.DeclOffsets = (const uint32_t *)BlobStart;
2041 F.LocalNumDecls = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002042 break;
Douglas Gregor55abb232009-04-10 20:39:37 +00002043
Sebastian Redl539c5062010-08-18 23:57:32 +00002044 case TU_UPDATE_LEXICAL: {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002045 DeclContextInfo Info = {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002046 /* No visible information */ 0,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002047 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
2048 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002049 };
Douglas Gregoraa433012010-10-01 01:18:02 +00002050 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
2051 .push_back(Info);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002052 break;
2053 }
2054
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002055 case UPDATE_VISIBLE: {
2056 serialization::DeclID ID = Record[0];
2057 void *Table = ASTDeclContextNameLookupTable::Create(
2058 (const unsigned char *)BlobStart + Record[1],
2059 (const unsigned char *)BlobStart,
2060 ASTDeclContextNameLookupTrait(*this));
Douglas Gregoraa433012010-10-01 01:18:02 +00002061 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002062 DeclContextInfo Info = {
2063 Table, /* No lexical inforamtion */ 0, 0
2064 };
2065 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
2066 } else
2067 PendingVisibleUpdates[ID].push_back(Table);
2068 break;
2069 }
2070
Sebastian Redl539c5062010-08-18 23:57:32 +00002071 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002072 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
2073 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002074 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002075 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
2076 Latest > FirstLatestDeclIDs[First]) &&
2077 "The new latest is supposed to come after the previous latest");
2078 FirstLatestDeclIDs[First] = Latest;
2079 }
2080 break;
2081 }
2082
Sebastian Redl539c5062010-08-18 23:57:32 +00002083 case LANGUAGE_OPTIONS:
Douglas Gregorce3a8292010-07-27 00:27:13 +00002084 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor55abb232009-04-10 20:39:37 +00002085 return IgnorePCH;
2086 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00002087
Sebastian Redl539c5062010-08-18 23:57:32 +00002088 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00002089 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00002090 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00002091 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002092 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00002093 (const unsigned char *)F.IdentifierTableData + Record[0],
2094 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl2c373b92010-10-05 15:59:54 +00002095 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002096 if (PP)
2097 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00002098 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002099 break;
2100
Sebastian Redl539c5062010-08-18 23:57:32 +00002101 case IDENTIFIER_OFFSET:
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002102 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002103 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002104 return Failure;
2105 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002106 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2107 F.LocalNumIdentifiers = Record[0];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002108 break;
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002109
Sebastian Redl539c5062010-08-18 23:57:32 +00002110 case EXTERNAL_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002111 // Optimization for the first block.
2112 if (ExternalDefinitions.empty())
2113 ExternalDefinitions.swap(Record);
2114 else
2115 ExternalDefinitions.insert(ExternalDefinitions.end(),
2116 Record.begin(), Record.end());
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002117 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00002118
Sebastian Redl539c5062010-08-18 23:57:32 +00002119 case SPECIAL_TYPES:
Sebastian Redlb293a452010-07-20 21:20:32 +00002120 // Optimization for the first block
2121 if (SpecialTypes.empty())
2122 SpecialTypes.swap(Record);
2123 else
2124 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002125 break;
2126
Sebastian Redl539c5062010-08-18 23:57:32 +00002127 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002128 TotalNumStatements += Record[0];
2129 TotalNumMacros += Record[1];
2130 TotalLexicalDeclContexts += Record[2];
2131 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00002132 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002133
Sebastian Redl539c5062010-08-18 23:57:32 +00002134 case UNUSED_FILESCOPED_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002135 // Optimization for the first block.
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002136 if (UnusedFileScopedDecls.empty())
2137 UnusedFileScopedDecls.swap(Record);
Sebastian Redlb293a452010-07-20 21:20:32 +00002138 else
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002139 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
2140 Record.begin(), Record.end());
Tanya Lattner90073802010-02-12 00:07:30 +00002141 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002142
Alexis Hunt27a761d2011-05-04 23:29:54 +00002143 case DELEGATING_CTORS:
2144 // Optimization for the first block.
2145 if (DelegatingCtorDecls.empty())
2146 DelegatingCtorDecls.swap(Record);
2147 else
2148 DelegatingCtorDecls.insert(DelegatingCtorDecls.end(),
2149 Record.begin(), Record.end());
2150 break;
2151
Sebastian Redl539c5062010-08-18 23:57:32 +00002152 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002153 // Later blocks overwrite earlier ones.
2154 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002155 break;
2156
Sebastian Redl539c5062010-08-18 23:57:32 +00002157 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002158 // Optimization for the first block.
2159 if (LocallyScopedExternalDecls.empty())
2160 LocallyScopedExternalDecls.swap(Record);
2161 else
2162 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
2163 Record.begin(), Record.end());
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002164 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002165
Sebastian Redl539c5062010-08-18 23:57:32 +00002166 case SELECTOR_OFFSETS:
Sebastian Redla19a67f2010-08-03 21:58:15 +00002167 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00002168 F.LocalNumSelectors = Record[0];
Douglas Gregor95c13f52009-04-25 17:48:32 +00002169 break;
2170
Sebastian Redl539c5062010-08-18 23:57:32 +00002171 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00002172 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002173 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00002174 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002175 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00002176 F.SelectorLookupTableData + Record[0],
2177 F.SelectorLookupTableData,
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002178 ASTSelectorLookupTrait(*this));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002179 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002180 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00002181
Sebastian Redl96371b42010-09-22 00:42:30 +00002182 case REFERENCED_SELECTOR_POOL:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002183 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002184 break;
2185
Sebastian Redl539c5062010-08-18 23:57:32 +00002186 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002187 if (!Record.empty() && Listener)
2188 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002189 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002190
Sebastian Redl539c5062010-08-18 23:57:32 +00002191 case SOURCE_LOCATION_OFFSETS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002192 F.SLocOffsets = (const uint32_t *)BlobStart;
2193 F.LocalNumSLocEntries = Record[0];
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002194 F.LocalSLocSize = Record[1];
Douglas Gregor258ae542009-04-27 06:38:32 +00002195 break;
2196
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002197 case FILE_SOURCE_LOCATION_OFFSETS:
2198 F.SLocFileOffsets = (const uint32_t *)BlobStart;
2199 F.LocalNumSLocFileEntries = Record[0];
2200 break;
2201
Sebastian Redl539c5062010-08-18 23:57:32 +00002202 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl96371b42010-09-22 00:42:30 +00002203 if (PreloadSLocEntries.empty())
2204 PreloadSLocEntries.swap(Record);
2205 else
2206 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2207 Record.begin(), Record.end());
Douglas Gregor258ae542009-04-27 06:38:32 +00002208 break;
Douglas Gregorc5046832009-04-27 18:38:38 +00002209
Sebastian Redl539c5062010-08-18 23:57:32 +00002210 case STAT_CACHE: {
Douglas Gregor606c4ac2011-02-05 19:42:43 +00002211 if (!DisableStatCache) {
2212 ASTStatCache *MyStatCache =
2213 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2214 (const unsigned char *)BlobStart,
2215 NumStatHits, NumStatMisses);
2216 FileMgr.addStatCache(MyStatCache);
2217 F.StatCache = MyStatCache;
2218 }
Douglas Gregorc5046832009-04-27 18:38:38 +00002219 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00002220 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002221
Sebastian Redl539c5062010-08-18 23:57:32 +00002222 case EXT_VECTOR_DECLS:
Sebastian Redl04f5c312010-07-28 21:38:49 +00002223 // Optimization for the first block.
2224 if (ExtVectorDecls.empty())
2225 ExtVectorDecls.swap(Record);
2226 else
2227 ExtVectorDecls.insert(ExtVectorDecls.end(),
2228 Record.begin(), Record.end());
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002229 break;
2230
Sebastian Redl539c5062010-08-18 23:57:32 +00002231 case VTABLE_USES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002232 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002233 VTableUses.swap(Record);
2234 break;
2235
Sebastian Redl539c5062010-08-18 23:57:32 +00002236 case DYNAMIC_CLASSES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002237 // Optimization for the first block.
2238 if (DynamicClasses.empty())
2239 DynamicClasses.swap(Record);
2240 else
2241 DynamicClasses.insert(DynamicClasses.end(),
2242 Record.begin(), Record.end());
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002243 break;
2244
Sebastian Redl539c5062010-08-18 23:57:32 +00002245 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002246 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002247 break;
2248
Sebastian Redl539c5062010-08-18 23:57:32 +00002249 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002250 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002251 SemaDeclRefs.swap(Record);
2252 break;
2253
Sebastian Redl539c5062010-08-18 23:57:32 +00002254 case ORIGINAL_FILE_NAME:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002255 // The primary AST will be the last to get here, so it will be the one
Sebastian Redlb293a452010-07-20 21:20:32 +00002256 // that's used.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00002257 ActualOriginalFileName.assign(BlobStart, BlobLen);
2258 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002259 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00002260 break;
Mike Stump11289f42009-09-09 15:08:12 +00002261
Douglas Gregora3b20262011-05-06 21:43:30 +00002262 case ORIGINAL_FILE_ID:
2263 OriginalFileID = FileID::get(Record[0]);
2264 break;
2265
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002266 case ORIGINAL_PCH_DIR:
2267 // The primary AST will be the last to get here, so it will be the one
2268 // that's used.
2269 OriginalDir.assign(BlobStart, BlobLen);
2270 break;
2271
Sebastian Redl539c5062010-08-18 23:57:32 +00002272 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00002273 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002274 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2275 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2276 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregord54f3a12009-10-05 21:07:28 +00002277 return IgnorePCH;
2278 }
2279 break;
2280 }
Sebastian Redlfa061442010-07-21 20:07:32 +00002281
Sebastian Redl539c5062010-08-18 23:57:32 +00002282 case MACRO_DEFINITION_OFFSETS:
Sebastian Redlfa061442010-07-21 20:07:32 +00002283 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2284 F.NumPreallocatedPreprocessingEntities = Record[0];
2285 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregoraae92242010-03-19 21:51:54 +00002286 break;
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002287
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002288 case DECL_UPDATE_OFFSETS: {
2289 if (Record.size() % 2 != 0) {
2290 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2291 return Failure;
2292 }
2293 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2294 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2295 .push_back(std::make_pair(&F, Record[I+1]));
2296 break;
2297 }
2298
Sebastian Redl539c5062010-08-18 23:57:32 +00002299 case DECL_REPLACEMENTS: {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002300 if (Record.size() % 2 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002301 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002302 return Failure;
2303 }
2304 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl539c5062010-08-18 23:57:32 +00002305 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002306 std::make_pair(&F, Record[I+1]);
2307 break;
2308 }
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002309
2310 case CXX_BASE_SPECIFIER_OFFSETS: {
2311 if (F.LocalNumCXXBaseSpecifiers != 0) {
2312 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2313 return Failure;
2314 }
2315
2316 F.LocalNumCXXBaseSpecifiers = Record[0];
2317 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2318 break;
2319 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002320
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002321 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002322 if (Record.size() % 2 != 0) {
2323 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2324 return Failure;
2325 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002326 if (PragmaDiagMappings.empty())
2327 PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002328 else
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002329 PragmaDiagMappings.insert(PragmaDiagMappings.end(),
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002330 Record.begin(), Record.end());
2331 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002332
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002333 case CUDA_SPECIAL_DECL_REFS:
2334 // Later tables overwrite earlier ones.
2335 CUDASpecialDeclRefs.swap(Record);
2336 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002337
2338 case HEADER_SEARCH_TABLE:
2339 F.HeaderFileInfoTableData = BlobStart;
2340 F.LocalNumHeaderFileInfos = Record[1];
2341 if (Record[0]) {
2342 F.HeaderFileInfoTable
2343 = HeaderFileInfoLookupTable::Create(
2344 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2345 (const unsigned char *)F.HeaderFileInfoTableData);
2346 if (PP)
2347 PP->getHeaderSearchInfo().SetExternalSource(this);
2348 }
2349 break;
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002350
2351 case FP_PRAGMA_OPTIONS:
2352 // Later tables overwrite earlier ones.
2353 FPPragmaOptions.swap(Record);
2354 break;
2355
2356 case OPENCL_EXTENSIONS:
2357 // Later tables overwrite earlier ones.
2358 OpenCLExtensions.swap(Record);
2359 break;
Alexis Hunt27a761d2011-05-04 23:29:54 +00002360
2361 case TENTATIVE_DEFINITIONS:
2362 // Optimization for the first block.
2363 if (TentativeDefinitions.empty())
2364 TentativeDefinitions.swap(Record);
2365 else
2366 TentativeDefinitions.insert(TentativeDefinitions.end(),
2367 Record.begin(), Record.end());
2368 break;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002369 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00002370 First = false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002371 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002372 Error("premature end of bitstream in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002373 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002374}
2375
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002376ASTReader::ASTReadResult ASTReader::validateFileEntries() {
2377 for (unsigned CI = 0, CN = Chain.size(); CI != CN; ++CI) {
2378 PerFileData *F = Chain[CI];
2379 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
2380
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002381 for (unsigned i = 0, e = F->LocalNumSLocFileEntries; i != e; ++i) {
2382 SLocEntryCursor.JumpToBit(F->SLocFileOffsets[i]);
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002383 unsigned Code = SLocEntryCursor.ReadCode();
2384 if (Code == llvm::bitc::END_BLOCK ||
2385 Code == llvm::bitc::ENTER_SUBBLOCK ||
2386 Code == llvm::bitc::DEFINE_ABBREV) {
2387 Error("incorrectly-formatted source location entry in AST file");
2388 return Failure;
2389 }
2390
2391 RecordData Record;
2392 const char *BlobStart;
2393 unsigned BlobLen;
2394 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2395 default:
2396 Error("incorrectly-formatted source location entry in AST file");
2397 return Failure;
2398
2399 case SM_SLOC_FILE_ENTRY: {
2400 llvm::StringRef Filename(BlobStart, BlobLen);
2401 const FileEntry *File = getFileEntry(Filename);
2402
2403 if (File == 0) {
2404 std::string ErrorStr = "could not find file '";
2405 ErrorStr += Filename;
2406 ErrorStr += "' referenced by AST file";
2407 Error(ErrorStr.c_str());
2408 return IgnorePCH;
2409 }
2410
2411 if (Record.size() < 6) {
2412 Error("source location entry is incorrect");
2413 return Failure;
2414 }
2415
2416 // The stat info from the FileEntry came from the cached stat
2417 // info of the PCH, so we cannot trust it.
2418 struct stat StatBuf;
2419 if (::stat(File->getName(), &StatBuf) != 0) {
2420 StatBuf.st_size = File->getSize();
2421 StatBuf.st_mtime = File->getModificationTime();
2422 }
2423
2424 if (((off_t)Record[4] != StatBuf.st_size
2425#if !defined(LLVM_ON_WIN32)
2426 // In our regression testing, the Windows file system seems to
2427 // have inconsistent modification times that sometimes
2428 // erroneously trigger this error-handling path.
2429 || (time_t)Record[5] != StatBuf.st_mtime
2430#endif
2431 )) {
2432 Error(diag::err_fe_pch_file_modified, Filename);
2433 return IgnorePCH;
2434 }
2435
2436 break;
2437 }
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002438 }
2439 }
2440 }
2441
2442 return Success;
2443}
2444
Sebastian Redl009e7f22010-10-05 16:15:19 +00002445ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2446 ASTFileType Type) {
2447 switch(ReadASTCore(FileName, Type)) {
Sebastian Redl2abc0382010-07-16 20:41:52 +00002448 case Failure: return Failure;
2449 case IgnorePCH: return IgnorePCH;
2450 case Success: break;
2451 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002452
2453 // Here comes stuff that we only do once the entire chain is loaded.
2454
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002455 if (!DisableValidation) {
2456 switch(validateFileEntries()) {
2457 case Failure: return Failure;
2458 case IgnorePCH: return IgnorePCH;
2459 case Success: break;
2460 }
2461 }
2462
Sebastian Redl96371b42010-09-22 00:42:30 +00002463 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redlfa061442010-07-21 20:07:32 +00002464 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redlada023c2010-08-04 20:40:17 +00002465 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2466 TotalNumSelectors = 0;
Sebastian Redl9e687992010-07-19 22:06:55 +00002467 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl96371b42010-09-22 00:42:30 +00002468 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002469 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002470 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl9e687992010-07-19 22:06:55 +00002471 TotalNumTypes += Chain[I]->LocalNumTypes;
2472 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redlfa061442010-07-21 20:07:32 +00002473 TotalNumPreallocatedPreprocessingEntities +=
2474 Chain[I]->NumPreallocatedPreprocessingEntities;
2475 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redlada023c2010-08-04 20:40:17 +00002476 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl9e687992010-07-19 22:06:55 +00002477 }
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002478 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002479 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl9e687992010-07-19 22:06:55 +00002480 TypesLoaded.resize(TotalNumTypes);
2481 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redlfa061442010-07-21 20:07:32 +00002482 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2483 if (PP) {
2484 if (TotalNumIdentifiers > 0)
2485 PP->getHeaderSearchInfo().SetExternalLookup(this);
2486 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2487 if (!PP->getPreprocessingRecord())
Douglas Gregor998caea2011-05-06 16:33:08 +00002488 PP->createPreprocessingRecord(true);
Sebastian Redlfa061442010-07-21 20:07:32 +00002489 PP->getPreprocessingRecord()->SetExternalSource(*this,
2490 TotalNumPreallocatedPreprocessingEntities);
2491 }
2492 }
Sebastian Redlada023c2010-08-04 20:40:17 +00002493 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl96371b42010-09-22 00:42:30 +00002494 // Preload SLocEntries.
2495 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2496 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2497 if (Result != Success)
2498 return Result;
2499 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002500
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002501 // Check the predefines buffers.
Douglas Gregorce3a8292010-07-27 00:27:13 +00002502 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002503 return IgnorePCH;
2504
2505 if (PP) {
2506 // Initialization of keywords and pragmas occurs before the
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002507 // AST file is read, so there may be some identifiers that were
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002508 // loaded into the IdentifierTable before we intercepted the
2509 // creation of identifiers. Iterate through the list of known
2510 // identifiers and determine whether we have to establish
2511 // preprocessor definitions or top-level identifier declaration
2512 // chains for those identifiers.
2513 //
2514 // We copy the IdentifierInfo pointers to a small vector first,
2515 // since de-serializing declarations or macro definitions can add
2516 // new entries into the identifier table, invalidating the
2517 // iterators.
2518 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2519 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2520 IdEnd = PP->getIdentifierTable().end();
2521 Id != IdEnd; ++Id)
2522 Identifiers.push_back(Id->second);
Sebastian Redlfa061442010-07-21 20:07:32 +00002523 // We need to search the tables in all files.
Sebastian Redlfa061442010-07-21 20:07:32 +00002524 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002525 ASTIdentifierLookupTable *IdTable
2526 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2527 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl5c415f32010-07-22 17:01:13 +00002528 // ones.
2529 if (!IdTable)
2530 continue;
Sebastian Redlfa061442010-07-21 20:07:32 +00002531 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2532 IdentifierInfo *II = Identifiers[I];
2533 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00002534 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redlfa061442010-07-21 20:07:32 +00002535 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002536 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redlb293a452010-07-20 21:20:32 +00002537 if (Pos == IdTable->end())
2538 continue;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002539
Sebastian Redlb293a452010-07-20 21:20:32 +00002540 // Dereferencing the iterator has the effect of populating the
2541 // IdentifierInfo node with the various declarations it needs.
2542 (void)*Pos;
2543 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002544 }
2545 }
2546
2547 if (Context)
2548 InitializeContext(*Context);
2549
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002550 if (DeserializationListener)
2551 DeserializationListener->ReaderInitialized(this);
2552
Douglas Gregor936a5b42010-11-30 05:23:00 +00002553 // If this AST file is a precompiled preamble, then set the main file ID of
2554 // the source manager to the file source file from which the preamble was
2555 // built. This is the only valid way to use a precompiled preamble.
2556 if (Type == Preamble) {
Douglas Gregora3b20262011-05-06 21:43:30 +00002557 if (OriginalFileID.isInvalid()) {
2558 SourceLocation Loc
2559 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2560 if (Loc.isValid())
2561 OriginalFileID = SourceMgr.getDecomposedLoc(Loc).first;
Douglas Gregor936a5b42010-11-30 05:23:00 +00002562 }
Douglas Gregora3b20262011-05-06 21:43:30 +00002563
2564 if (!OriginalFileID.isInvalid())
2565 SourceMgr.SetPreambleFileID(OriginalFileID);
Douglas Gregor936a5b42010-11-30 05:23:00 +00002566 }
2567
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002568 return Success;
2569}
2570
Sebastian Redl009e7f22010-10-05 16:15:19 +00002571ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2572 ASTFileType Type) {
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002573 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl009e7f22010-10-05 16:15:19 +00002574 Chain.push_back(new PerFileData(Type));
Sebastian Redl34522812010-07-16 17:50:48 +00002575 PerFileData &F = *Chain.back();
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002576 if (Prev)
2577 Prev->NextInSource = &F;
2578 else
2579 FirstInSource = &F;
2580 F.Loaders.push_back(Prev);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002581
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002582 // Set the AST file name.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002583 F.FileName = FileName;
2584
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002585 if (FileName != "-") {
2586 CurrentDir = llvm::sys::path::parent_path(FileName);
2587 if (CurrentDir.empty()) CurrentDir = ".";
2588 }
2589
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +00002590 if (!ASTBuffers.empty()) {
Sebastian Redleb931c62011-04-14 14:07:41 +00002591 F.Buffer.reset(ASTBuffers.back());
2592 ASTBuffers.pop_back();
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +00002593 assert(F.Buffer && "Passed null buffer");
2594 } else {
2595 // Open the AST file.
2596 //
2597 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2598 std::string ErrStr;
2599 llvm::error_code ec;
2600 if (FileName == "-") {
2601 ec = llvm::MemoryBuffer::getSTDIN(F.Buffer);
2602 if (ec)
2603 ErrStr = ec.message();
2604 } else
2605 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
2606 if (!F.Buffer) {
2607 Error(ErrStr.c_str());
2608 return IgnorePCH;
2609 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002610 }
2611
2612 // Initialize the stream
2613 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2614 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl34522812010-07-16 17:50:48 +00002615 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002616 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002617 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002618
2619 // Sniff for the signature.
2620 if (Stream.Read(8) != 'C' ||
2621 Stream.Read(8) != 'P' ||
2622 Stream.Read(8) != 'C' ||
2623 Stream.Read(8) != 'H') {
2624 Diag(diag::err_not_a_pch_file) << FileName;
2625 return Failure;
2626 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002627
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002628 while (!Stream.AtEndOfStream()) {
2629 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002630
Douglas Gregor92863e42009-04-10 23:10:45 +00002631 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002632 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002633 return Failure;
2634 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002635
2636 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002637
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002638 // We only know the AST subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002639 switch (BlockID) {
2640 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002641 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002642 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002643 return Failure;
2644 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002645 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00002646 case AST_BLOCK_ID:
Sebastian Redl3e31c722010-08-18 23:56:56 +00002647 switch (ReadASTBlock(F)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002648 case Success:
2649 break;
2650
2651 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00002652 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00002653
2654 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00002655 // FIXME: We could consider reading through to the end of this
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002656 // AST block, skipping subblocks, to see if there are other
2657 // AST blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00002658
2659 // Clear out any preallocated source location entries, so that
2660 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002661 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00002662
2663 // Remove the stat cache.
Sebastian Redl34522812010-07-16 17:50:48 +00002664 if (F.StatCache)
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002665 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00002666
Douglas Gregor92863e42009-04-10 23:10:45 +00002667 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002668 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002669 break;
2670 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002671 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002672 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002673 return Failure;
2674 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002675 break;
2676 }
Mike Stump11289f42009-09-09 15:08:12 +00002677 }
2678
Sebastian Redl2abc0382010-07-16 20:41:52 +00002679 return Success;
2680}
2681
Sebastian Redl2c499f62010-08-18 23:56:43 +00002682void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002683 PP = &pp;
Sebastian Redlfa061442010-07-21 20:07:32 +00002684
2685 unsigned TotalNum = 0;
2686 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2687 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2688 if (TotalNum) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002689 if (!PP->getPreprocessingRecord())
Douglas Gregor998caea2011-05-06 16:33:08 +00002690 PP->createPreprocessingRecord(true);
Sebastian Redlfa061442010-07-21 20:07:32 +00002691 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregoraae92242010-03-19 21:51:54 +00002692 }
2693}
2694
Sebastian Redl2c499f62010-08-18 23:56:43 +00002695void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002696 Context = &Ctx;
2697 assert(Context && "Passed null context!");
2698
2699 assert(PP && "Forgot to set Preprocessor ?");
2700 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2701 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00002702 PP->setExternalSource(this);
Douglas Gregor09b69892011-02-10 17:09:37 +00002703 PP->getHeaderSearchInfo().SetExternalSource(this);
2704
Douglas Gregoraa433012010-10-01 01:18:02 +00002705 // If we have an update block for the TU waiting, we have to add it before
2706 // deserializing the decl.
2707 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2708 if (DCU != DeclContextOffsets.end()) {
2709 // Insertion could invalidate map, so grab vector.
2710 DeclContextInfos T;
2711 T.swap(DCU->second);
2712 DeclContextOffsets.erase(DCU);
2713 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2714 }
2715
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002716 // Load the translation unit declaration
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00002717 GetTranslationUnitDecl();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002718
2719 // Load the special types.
2720 Context->setBuiltinVaListType(
Sebastian Redl539c5062010-08-18 23:57:32 +00002721 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2722 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002723 Context->setObjCIdType(GetType(Id));
Sebastian Redl539c5062010-08-18 23:57:32 +00002724 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002725 Context->setObjCSelType(GetType(Sel));
Sebastian Redl539c5062010-08-18 23:57:32 +00002726 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002727 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl539c5062010-08-18 23:57:32 +00002728 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002729 Context->setObjCClassType(GetType(Class));
Steve Naroff7cae42b2009-07-10 23:34:53 +00002730
Sebastian Redl539c5062010-08-18 23:57:32 +00002731 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002732 Context->setCFConstantStringType(GetType(String));
Mike Stump11289f42009-09-09 15:08:12 +00002733 if (unsigned FastEnum
Sebastian Redl539c5062010-08-18 23:57:32 +00002734 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002735 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl539c5062010-08-18 23:57:32 +00002736 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregor27821ce2009-07-07 16:35:42 +00002737 QualType FileType = GetType(File);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002738 if (FileType.isNull()) {
2739 Error("FILE type is NULL");
2740 return;
2741 }
John McCall9dd450b2009-09-21 23:43:11 +00002742 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregor27821ce2009-07-07 16:35:42 +00002743 Context->setFILEDecl(Typedef->getDecl());
2744 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002745 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002746 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002747 Error("Invalid FILE type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002748 return;
2749 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002750 Context->setFILEDecl(Tag->getDecl());
2751 }
2752 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002753 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002754 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002755 if (Jmp_bufType.isNull()) {
2756 Error("jmp_bug type is NULL");
2757 return;
2758 }
John McCall9dd450b2009-09-21 23:43:11 +00002759 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002760 Context->setjmp_bufDecl(Typedef->getDecl());
2761 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002762 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002763 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002764 Error("Invalid jmp_buf type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002765 return;
2766 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002767 Context->setjmp_bufDecl(Tag->getDecl());
2768 }
2769 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002770 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002771 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002772 if (Sigjmp_bufType.isNull()) {
2773 Error("sigjmp_buf type is NULL");
2774 return;
2775 }
John McCall9dd450b2009-09-21 23:43:11 +00002776 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002777 Context->setsigjmp_bufDecl(Typedef->getDecl());
2778 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002779 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002780 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stumpa4de80b2009-07-28 02:25:19 +00002781 Context->setsigjmp_bufDecl(Tag->getDecl());
2782 }
2783 }
Mike Stump11289f42009-09-09 15:08:12 +00002784 if (unsigned ObjCIdRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002785 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002786 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump11289f42009-09-09 15:08:12 +00002787 if (unsigned ObjCClassRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002788 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002789 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002790 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpd0153282009-10-20 02:12:22 +00002791 Context->setBlockDescriptorType(GetType(String));
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002792 if (unsigned String
Sebastian Redl539c5062010-08-18 23:57:32 +00002793 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002794 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002795 if (unsigned ObjCSelRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002796 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002797 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002798 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002799 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002800
Sebastian Redl539c5062010-08-18 23:57:32 +00002801 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002802 Context->setInt128Installed();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002803
Richard Smith02e85f32011-04-14 22:09:26 +00002804 if (unsigned AutoDeduct = SpecialTypes[SPECIAL_TYPE_AUTO_DEDUCT])
2805 Context->AutoDeductTy = GetType(AutoDeduct);
2806 if (unsigned AutoRRefDeduct = SpecialTypes[SPECIAL_TYPE_AUTO_RREF_DEDUCT])
2807 Context->AutoRRefDeductTy = GetType(AutoRRefDeduct);
2808
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002809 ReadPragmaDiagnosticMappings(Context->getDiagnostics());
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002810
2811 // If there were any CUDA special declarations, deserialize them.
2812 if (!CUDASpecialDeclRefs.empty()) {
2813 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
2814 Context->setcudaConfigureCallDecl(
2815 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2816 }
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002817}
2818
Douglas Gregor45fe0362009-05-12 01:31:05 +00002819/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002820/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00002821/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002822std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00002823 FileManager &FileMgr,
Daniel Dunbar3b951482009-12-03 09:13:06 +00002824 Diagnostic &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002825 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002826 std::string ErrStr;
2827 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner5159f612010-11-23 08:35:12 +00002828 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00002829 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00002830 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002831 return std::string();
2832 }
2833
2834 // Initialize the stream
2835 llvm::BitstreamReader StreamFile;
2836 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00002837 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00002838 (const unsigned char *)Buffer->getBufferEnd());
2839 Stream.init(StreamFile);
2840
2841 // Sniff for the signature.
2842 if (Stream.Read(8) != 'C' ||
2843 Stream.Read(8) != 'P' ||
2844 Stream.Read(8) != 'C' ||
2845 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002846 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002847 return std::string();
2848 }
2849
2850 RecordData Record;
2851 while (!Stream.AtEndOfStream()) {
2852 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002853
Douglas Gregor45fe0362009-05-12 01:31:05 +00002854 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2855 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00002856
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002857 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002858 switch (BlockID) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002859 case AST_BLOCK_ID:
2860 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002861 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002862 return std::string();
2863 }
2864 break;
Mike Stump11289f42009-09-09 15:08:12 +00002865
Douglas Gregor45fe0362009-05-12 01:31:05 +00002866 default:
2867 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002868 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002869 return std::string();
2870 }
2871 break;
2872 }
2873 continue;
2874 }
2875
2876 if (Code == llvm::bitc::END_BLOCK) {
2877 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002878 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002879 return std::string();
2880 }
2881 continue;
2882 }
2883
2884 if (Code == llvm::bitc::DEFINE_ABBREV) {
2885 Stream.ReadAbbrevRecord();
2886 continue;
2887 }
2888
2889 Record.clear();
2890 const char *BlobStart = 0;
2891 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002892 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl539c5062010-08-18 23:57:32 +00002893 == ORIGINAL_FILE_NAME)
Douglas Gregor45fe0362009-05-12 01:31:05 +00002894 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00002895 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00002896
2897 return std::string();
2898}
2899
Douglas Gregor55abb232009-04-10 20:39:37 +00002900/// \brief Parse the record that corresponds to a LangOptions data
2901/// structure.
2902///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002903/// This routine parses the language options from the AST file and then gives
2904/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00002905///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002906/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002907bool ASTReader::ParseLanguageOptions(
Douglas Gregor55abb232009-04-10 20:39:37 +00002908 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002909 if (Listener) {
2910 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00002911
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002912 #define PARSE_LANGOPT(Option) \
2913 LangOpts.Option = Record[Idx]; \
2914 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00002915
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002916 unsigned Idx = 0;
2917 PARSE_LANGOPT(Trigraphs);
2918 PARSE_LANGOPT(BCPLComment);
2919 PARSE_LANGOPT(DollarIdents);
2920 PARSE_LANGOPT(AsmPreprocessor);
2921 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00002922 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002923 PARSE_LANGOPT(ImplicitInt);
2924 PARSE_LANGOPT(Digraphs);
2925 PARSE_LANGOPT(HexFloats);
2926 PARSE_LANGOPT(C99);
Peter Collingbournea686b5f2011-04-15 00:35:23 +00002927 PARSE_LANGOPT(C1X);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002928 PARSE_LANGOPT(Microsoft);
2929 PARSE_LANGOPT(CPlusPlus);
2930 PARSE_LANGOPT(CPlusPlus0x);
2931 PARSE_LANGOPT(CXXOperatorNames);
2932 PARSE_LANGOPT(ObjC1);
2933 PARSE_LANGOPT(ObjC2);
2934 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00002935 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00002936 PARSE_LANGOPT(AppleKext);
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00002937 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Douglas Gregora860e6a2011-06-14 23:20:43 +00002938 PARSE_LANGOPT(ObjCInferRelatedResultType);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00002939 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002940 PARSE_LANGOPT(PascalStrings);
2941 PARSE_LANGOPT(WritableStrings);
2942 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00002943 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002944 PARSE_LANGOPT(Exceptions);
Anders Carlssonce8dd3a2011-02-19 23:53:54 +00002945 PARSE_LANGOPT(ObjCExceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +00002946 PARSE_LANGOPT(CXXExceptions);
2947 PARSE_LANGOPT(SjLjExceptions);
Douglas Gregordbe39272011-02-01 15:15:22 +00002948 PARSE_LANGOPT(MSBitfields);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002949 PARSE_LANGOPT(NeXTRuntime);
2950 PARSE_LANGOPT(Freestanding);
2951 PARSE_LANGOPT(NoBuiltin);
2952 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00002953 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002954 PARSE_LANGOPT(Blocks);
2955 PARSE_LANGOPT(EmitAllDecls);
2956 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00002957 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2958 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002959 PARSE_LANGOPT(HeinousExtensions);
2960 PARSE_LANGOPT(Optimize);
2961 PARSE_LANGOPT(OptimizeSize);
2962 PARSE_LANGOPT(Static);
2963 PARSE_LANGOPT(PICLevel);
2964 PARSE_LANGOPT(GNUInline);
2965 PARSE_LANGOPT(NoInline);
Chandler Carruth7ffce732011-04-23 20:05:38 +00002966 PARSE_LANGOPT(Deprecated);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002967 PARSE_LANGOPT(AccessControl);
2968 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00002969 PARSE_LANGOPT(ShortWChar);
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00002970 PARSE_LANGOPT(ShortEnums);
Chris Lattner51924e512010-06-26 21:25:03 +00002971 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall457a04e2010-10-22 21:05:15 +00002972 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00002973 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00002974 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002975 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00002976 PARSE_LANGOPT(OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00002977 PARSE_LANGOPT(CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00002978 PARSE_LANGOPT(CatchUndefined);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002979 PARSE_LANGOPT(DefaultFPContract);
Roman Divackydc1f68d2011-03-01 17:36:40 +00002980 PARSE_LANGOPT(ElideConstructors);
2981 PARSE_LANGOPT(SpellChecking);
Roman Divacky65b88cd2011-03-01 17:40:53 +00002982 PARSE_LANGOPT(MRTD);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002983 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00002984
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002985 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00002986 }
Douglas Gregor55abb232009-04-10 20:39:37 +00002987
2988 return false;
2989}
2990
Sebastian Redl2c499f62010-08-18 23:56:43 +00002991void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002992 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2993 PerFileData &F = *Chain[I];
2994 if (!F.PreprocessorDetailCursor.getBitStreamReader())
2995 continue;
2996
2997 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
2998 F.PreprocessorDetailCursor.JumpToBit(F.PreprocessorDetailStartOffset);
2999 while (LoadPreprocessedEntity(F)) { }
3000 }
Douglas Gregoraae92242010-03-19 21:51:54 +00003001}
3002
Douglas Gregor46c50012011-02-11 19:46:30 +00003003PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003004 PerFileData *F = 0;
3005 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3006 if (Offset < Chain[I]->SizeInBits) {
3007 F = Chain[I];
3008 break;
3009 }
3010
3011 Offset -= Chain[I]->SizeInBits;
3012 }
3013
3014 if (!F) {
3015 Error("Malformed preprocessed entity offset");
3016 return 0;
3017 }
3018
Douglas Gregor92a96f52011-02-08 21:58:10 +00003019 // Keep track of where we are in the stream, then jump back there
3020 // after reading this entity.
3021 SavedStreamPosition SavedPosition(F->PreprocessorDetailCursor);
3022 F->PreprocessorDetailCursor.JumpToBit(Offset);
3023 return LoadPreprocessedEntity(*F);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003024}
3025
Douglas Gregor09b69892011-02-10 17:09:37 +00003026HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
3027 HeaderFileInfoTrait Trait(FE->getName());
3028 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3029 PerFileData &F = *Chain[I];
3030 HeaderFileInfoLookupTable *Table
3031 = static_cast<HeaderFileInfoLookupTable *>(F.HeaderFileInfoTable);
3032 if (!Table)
3033 continue;
3034
3035 // Look in the on-disk hash table for an entry for this file name.
3036 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE->getName(),
3037 &Trait);
3038 if (Pos == Table->end())
3039 continue;
3040
3041 HeaderFileInfo HFI = *Pos;
3042 if (Listener)
3043 Listener->ReadHeaderFileInfo(HFI, FE->getUID());
3044
3045 return HFI;
3046 }
3047
3048 return HeaderFileInfo();
3049}
3050
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003051void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003052 unsigned Idx = 0;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003053 while (Idx < PragmaDiagMappings.size()) {
3054 SourceLocation
3055 Loc = SourceLocation::getFromRawEncoding(PragmaDiagMappings[Idx++]);
3056 while (1) {
3057 assert(Idx < PragmaDiagMappings.size() &&
3058 "Invalid data, didn't find '-1' marking end of diag/map pairs");
3059 if (Idx >= PragmaDiagMappings.size())
3060 break; // Something is messed up but at least avoid infinite loop in
3061 // release build.
3062 unsigned DiagID = PragmaDiagMappings[Idx++];
3063 if (DiagID == (unsigned)-1)
3064 break; // no more diag/map pairs for this location.
3065 diag::Mapping Map = (diag::Mapping)PragmaDiagMappings[Idx++];
3066 Diag.setDiagnosticMapping(DiagID, Map, Loc);
3067 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003068 }
3069}
3070
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003071/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003072ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003073 PerFileData *F = 0;
3074 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3075 F = Chain[N - I - 1];
3076 if (Index < F->LocalNumTypes)
3077 break;
3078 Index -= F->LocalNumTypes;
3079 }
3080 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redl2c373b92010-10-05 15:59:54 +00003081 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003082}
3083
3084/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003085///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003086/// The index is the type ID, shifted and minus the number of predefs. This
3087/// routine actually reads the record corresponding to the type at the given
3088/// location. It is a helper routine for GetType, which deals with reading type
3089/// IDs.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003090QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003091 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003092 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00003093
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003094 // Keep track of where we are in the stream, then jump back there
3095 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00003096 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003097
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003098 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00003099
Douglas Gregor1342e842009-07-06 18:54:52 +00003100 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003101 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00003102
Sebastian Redl2c373b92010-10-05 15:59:54 +00003103 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003104 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003105 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00003106 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3107 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003108 if (Record.size() != 2) {
3109 Error("Incorrect encoding of extended qualifier type");
3110 return QualType();
3111 }
Douglas Gregor455b8f42009-04-15 22:00:08 +00003112 QualType Base = GetType(Record[0]);
John McCall8ccfcb52009-09-24 19:53:00 +00003113 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
3114 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00003115 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003116
Sebastian Redl539c5062010-08-18 23:57:32 +00003117 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003118 if (Record.size() != 1) {
3119 Error("Incorrect encoding of complex type");
3120 return QualType();
3121 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003122 QualType ElemType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003123 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003124 }
3125
Sebastian Redl539c5062010-08-18 23:57:32 +00003126 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003127 if (Record.size() != 1) {
3128 Error("Incorrect encoding of pointer type");
3129 return QualType();
3130 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003131 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003132 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003133 }
3134
Sebastian Redl539c5062010-08-18 23:57:32 +00003135 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003136 if (Record.size() != 1) {
3137 Error("Incorrect encoding of block pointer type");
3138 return QualType();
3139 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003140 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003141 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003142 }
3143
Sebastian Redl539c5062010-08-18 23:57:32 +00003144 case TYPE_LVALUE_REFERENCE: {
Richard Smith0f538462011-04-12 10:38:03 +00003145 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003146 Error("Incorrect encoding of lvalue reference type");
3147 return QualType();
3148 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003149 QualType PointeeType = GetType(Record[0]);
Richard Smith0f538462011-04-12 10:38:03 +00003150 return Context->getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003151 }
3152
Sebastian Redl539c5062010-08-18 23:57:32 +00003153 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003154 if (Record.size() != 1) {
3155 Error("Incorrect encoding of rvalue reference type");
3156 return QualType();
3157 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003158 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003159 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003160 }
3161
Sebastian Redl539c5062010-08-18 23:57:32 +00003162 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00003163 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003164 Error("Incorrect encoding of member pointer type");
3165 return QualType();
3166 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003167 QualType PointeeType = GetType(Record[0]);
3168 QualType ClassType = GetType(Record[1]);
Douglas Gregor0cdc8322010-12-10 17:03:06 +00003169 if (PointeeType.isNull() || ClassType.isNull())
3170 return QualType();
3171
Chris Lattner8575daa2009-04-27 21:45:14 +00003172 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003173 }
3174
Sebastian Redl539c5062010-08-18 23:57:32 +00003175 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003176 QualType ElementType = GetType(Record[0]);
3177 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3178 unsigned IndexTypeQuals = Record[2];
3179 unsigned Idx = 3;
3180 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00003181 return Context->getConstantArrayType(ElementType, Size,
3182 ASM, IndexTypeQuals);
3183 }
3184
Sebastian Redl539c5062010-08-18 23:57:32 +00003185 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003186 QualType ElementType = GetType(Record[0]);
3187 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3188 unsigned IndexTypeQuals = Record[2];
Chris Lattner8575daa2009-04-27 21:45:14 +00003189 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003190 }
3191
Sebastian Redl539c5062010-08-18 23:57:32 +00003192 case TYPE_VARIABLE_ARRAY: {
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003193 QualType ElementType = GetType(Record[0]);
3194 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3195 unsigned IndexTypeQuals = Record[2];
Sebastian Redl2c373b92010-10-05 15:59:54 +00003196 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3197 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
3198 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor04318252009-07-06 15:59:29 +00003199 ASM, IndexTypeQuals,
3200 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003201 }
3202
Sebastian Redl539c5062010-08-18 23:57:32 +00003203 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00003204 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003205 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003206 return QualType();
3207 }
3208
3209 QualType ElementType = GetType(Record[0]);
3210 unsigned NumElements = Record[1];
Bob Wilsonaeb56442010-11-10 21:56:12 +00003211 unsigned VecKind = Record[2];
Chris Lattner37141f42010-06-23 06:00:24 +00003212 return Context->getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00003213 (VectorType::VectorKind)VecKind);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003214 }
3215
Sebastian Redl539c5062010-08-18 23:57:32 +00003216 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00003217 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003218 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003219 return QualType();
3220 }
3221
3222 QualType ElementType = GetType(Record[0]);
3223 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00003224 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003225 }
3226
Sebastian Redl539c5062010-08-18 23:57:32 +00003227 case TYPE_FUNCTION_NO_PROTO: {
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003228 if (Record.size() != 5) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00003229 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003230 return QualType();
3231 }
3232 QualType ResultType = GetType(Record[0]);
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003233 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3], (CallingConv)Record[4]);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00003234 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003235 }
3236
Sebastian Redl539c5062010-08-18 23:57:32 +00003237 case TYPE_FUNCTION_PROTO: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003238 QualType ResultType = GetType(Record[0]);
John McCalldb40c7f2010-12-14 08:05:40 +00003239
3240 FunctionProtoType::ExtProtoInfo EPI;
3241 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003242 /*hasregparm*/ Record[2],
3243 /*regparm*/ Record[3],
3244 static_cast<CallingConv>(Record[4]));
John McCalldb40c7f2010-12-14 08:05:40 +00003245
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003246 unsigned Idx = 5;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003247 unsigned NumParams = Record[Idx++];
3248 llvm::SmallVector<QualType, 16> ParamTypes;
3249 for (unsigned I = 0; I != NumParams; ++I)
3250 ParamTypes.push_back(GetType(Record[Idx++]));
John McCalldb40c7f2010-12-14 08:05:40 +00003251
3252 EPI.Variadic = Record[Idx++];
3253 EPI.TypeQuals = Record[Idx++];
Douglas Gregordb9d6642011-01-26 05:01:58 +00003254 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003255 ExceptionSpecificationType EST =
3256 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3257 EPI.ExceptionSpecType = EST;
3258 if (EST == EST_Dynamic) {
3259 EPI.NumExceptions = Record[Idx++];
3260 llvm::SmallVector<QualType, 2> Exceptions;
3261 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
3262 Exceptions.push_back(GetType(Record[Idx++]));
3263 EPI.Exceptions = Exceptions.data();
3264 } else if (EST == EST_ComputedNoexcept) {
3265 EPI.NoexceptExpr = ReadExpr(*Loc.F);
3266 }
Jay Foad7d0479f2009-05-21 09:52:38 +00003267 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalldb40c7f2010-12-14 08:05:40 +00003268 EPI);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003269 }
3270
Sebastian Redl539c5062010-08-18 23:57:32 +00003271 case TYPE_UNRESOLVED_USING:
John McCallb96ec562009-12-04 22:46:56 +00003272 return Context->getTypeDeclType(
3273 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
3274
Sebastian Redl539c5062010-08-18 23:57:32 +00003275 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003276 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003277 Error("incorrect encoding of typedef type");
3278 return QualType();
3279 }
Richard Smithdda56e42011-04-15 14:24:37 +00003280 TypedefNameDecl *Decl = cast<TypedefNameDecl>(GetDecl(Record[0]));
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003281 QualType Canonical = GetType(Record[1]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003282 if (!Canonical.isNull())
3283 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003284 return Context->getTypedefType(Decl, Canonical);
3285 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003286
Sebastian Redl539c5062010-08-18 23:57:32 +00003287 case TYPE_TYPEOF_EXPR:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003288 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003289
Sebastian Redl539c5062010-08-18 23:57:32 +00003290 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003291 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003292 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003293 return QualType();
3294 }
3295 QualType UnderlyingType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003296 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003297 }
Mike Stump11289f42009-09-09 15:08:12 +00003298
Sebastian Redl539c5062010-08-18 23:57:32 +00003299 case TYPE_DECLTYPE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003300 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson81df7b82009-06-24 19:06:50 +00003301
Alexis Hunte852b102011-05-24 22:41:36 +00003302 case TYPE_UNARY_TRANSFORM: {
3303 QualType BaseType = GetType(Record[0]);
3304 QualType UnderlyingType = GetType(Record[1]);
3305 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
3306 return Context->getUnaryTransformType(BaseType, UnderlyingType, UKind);
3307 }
3308
Richard Smith30482bc2011-02-20 03:19:35 +00003309 case TYPE_AUTO:
3310 return Context->getAutoType(GetType(Record[0]));
3311
Sebastian Redl539c5062010-08-18 23:57:32 +00003312 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003313 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003314 Error("incorrect encoding of record type");
3315 return QualType();
3316 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003317 bool IsDependent = Record[0];
3318 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCall424cec92011-01-19 06:33:43 +00003319 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003320 return T;
3321 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003322
Sebastian Redl539c5062010-08-18 23:57:32 +00003323 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003324 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003325 Error("incorrect encoding of enum type");
3326 return QualType();
3327 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003328 bool IsDependent = Record[0];
3329 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCall424cec92011-01-19 06:33:43 +00003330 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003331 return T;
3332 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00003333
John McCall81904512011-01-06 01:58:22 +00003334 case TYPE_ATTRIBUTED: {
3335 if (Record.size() != 3) {
3336 Error("incorrect encoding of attributed type");
3337 return QualType();
3338 }
3339 QualType modifiedType = GetType(Record[0]);
3340 QualType equivalentType = GetType(Record[1]);
3341 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3342 return Context->getAttributedType(kind, modifiedType, equivalentType);
3343 }
3344
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003345 case TYPE_PAREN: {
3346 if (Record.size() != 1) {
3347 Error("incorrect encoding of paren type");
3348 return QualType();
3349 }
3350 QualType InnerType = GetType(Record[0]);
3351 return Context->getParenType(InnerType);
3352 }
3353
Douglas Gregord2fa7662010-12-20 02:24:11 +00003354 case TYPE_PACK_EXPANSION: {
Douglas Gregor17328502011-02-01 15:24:58 +00003355 if (Record.size() != 2) {
Douglas Gregord2fa7662010-12-20 02:24:11 +00003356 Error("incorrect encoding of pack expansion type");
3357 return QualType();
3358 }
3359 QualType Pattern = GetType(Record[0]);
3360 if (Pattern.isNull())
3361 return QualType();
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00003362 llvm::Optional<unsigned> NumExpansions;
3363 if (Record[1])
3364 NumExpansions = Record[1] - 1;
3365 return Context->getPackExpansionType(Pattern, NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00003366 }
3367
Sebastian Redl539c5062010-08-18 23:57:32 +00003368 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003369 unsigned Idx = 0;
3370 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3371 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3372 QualType NamedType = GetType(Record[Idx++]);
3373 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00003374 }
3375
Sebastian Redl539c5062010-08-18 23:57:32 +00003376 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00003377 unsigned Idx = 0;
3378 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00003379 return Context->getObjCInterfaceType(ItfD);
3380 }
3381
Sebastian Redl539c5062010-08-18 23:57:32 +00003382 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00003383 unsigned Idx = 0;
3384 QualType Base = GetType(Record[Idx++]);
Chris Lattner587cbe12009-04-22 06:45:28 +00003385 unsigned NumProtos = Record[Idx++];
3386 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
3387 for (unsigned I = 0; I != NumProtos; ++I)
3388 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003389 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00003390 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003391
Sebastian Redl539c5062010-08-18 23:57:32 +00003392 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00003393 unsigned Idx = 0;
John McCall8b07ec22010-05-15 11:32:37 +00003394 QualType Pointee = GetType(Record[Idx++]);
3395 return Context->getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00003396 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00003397
Sebastian Redl539c5062010-08-18 23:57:32 +00003398 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00003399 unsigned Idx = 0;
3400 QualType Parm = GetType(Record[Idx++]);
3401 QualType Replacement = GetType(Record[Idx++]);
3402 return
3403 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3404 Replacement);
3405 }
John McCalle78aac42010-03-10 03:28:59 +00003406
Douglas Gregorada4b792011-01-14 02:55:32 +00003407 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3408 unsigned Idx = 0;
3409 QualType Parm = GetType(Record[Idx++]);
3410 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3411 return Context->getSubstTemplateTypeParmPackType(
3412 cast<TemplateTypeParmType>(Parm),
3413 ArgPack);
3414 }
3415
Sebastian Redl539c5062010-08-18 23:57:32 +00003416 case TYPE_INJECTED_CLASS_NAME: {
John McCalle78aac42010-03-10 03:28:59 +00003417 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
3418 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003419 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003420 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003421 return
3422 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00003423 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003424
Sebastian Redl539c5062010-08-18 23:57:32 +00003425 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003426 unsigned Idx = 0;
3427 unsigned Depth = Record[Idx++];
3428 unsigned Index = Record[Idx++];
3429 bool Pack = Record[Idx++];
Chandler Carruth08836322011-05-01 00:51:33 +00003430 TemplateTypeParmDecl *D =
3431 cast_or_null<TemplateTypeParmDecl>(GetDecl(Record[Idx++]));
3432 return Context->getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003433 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003434
Sebastian Redl539c5062010-08-18 23:57:32 +00003435 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003436 unsigned Idx = 0;
3437 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3438 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3439 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00003440 QualType Canon = GetType(Record[Idx++]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003441 if (!Canon.isNull())
3442 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00003443 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003444 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003445
Sebastian Redl539c5062010-08-18 23:57:32 +00003446 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003447 unsigned Idx = 0;
3448 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3449 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3450 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3451 unsigned NumArgs = Record[Idx++];
3452 llvm::SmallVector<TemplateArgument, 8> Args;
3453 Args.reserve(NumArgs);
3454 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003455 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003456 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3457 Args.size(), Args.data());
3458 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003459
Sebastian Redl539c5062010-08-18 23:57:32 +00003460 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003461 unsigned Idx = 0;
3462
3463 // ArrayType
3464 QualType ElementType = GetType(Record[Idx++]);
3465 ArrayType::ArraySizeModifier ASM
3466 = (ArrayType::ArraySizeModifier)Record[Idx++];
3467 unsigned IndexTypeQuals = Record[Idx++];
3468
3469 // DependentSizedArrayType
Sebastian Redl2c373b92010-10-05 15:59:54 +00003470 Expr *NumElts = ReadExpr(*Loc.F);
3471 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003472
3473 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3474 IndexTypeQuals, Brackets);
3475 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003476
Sebastian Redl539c5062010-08-18 23:57:32 +00003477 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003478 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003479 bool IsDependent = Record[Idx++];
Douglas Gregor5590be02011-01-15 06:45:20 +00003480 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003481 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003482 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Richard Smith3f1b5d02011-05-05 21:57:07 +00003483 QualType Underlying = GetType(Record[Idx++]);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003484 QualType T;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003485 if (Underlying.isNull())
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003486 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3487 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003488 else
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003489 T = Context->getTemplateSpecializationType(Name, Args.data(),
Richard Smith3f1b5d02011-05-05 21:57:07 +00003490 Args.size(), Underlying);
John McCall424cec92011-01-19 06:33:43 +00003491 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003492 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003493 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003494 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003495 // Suppress a GCC warning
3496 return QualType();
3497}
3498
Sebastian Redl2c373b92010-10-05 15:59:54 +00003499class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00003500 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003501 ASTReader::PerFileData &F;
Sebastian Redlc67764e2010-07-22 22:43:28 +00003502 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redl2c499f62010-08-18 23:56:43 +00003503 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00003504 unsigned &Idx;
3505
Sebastian Redl2c373b92010-10-05 15:59:54 +00003506 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3507 unsigned &I) {
3508 return Reader.ReadSourceLocation(F, R, I);
3509 }
3510
John McCall8f115c62009-10-16 21:56:05 +00003511public:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003512 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl2c499f62010-08-18 23:56:43 +00003513 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003514 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3515 { }
John McCall8f115c62009-10-16 21:56:05 +00003516
John McCall17001972009-10-18 01:05:36 +00003517 // We want compile-time assurance that we've enumerated all of
3518 // these, so unfortunately we have to declare them first, then
3519 // define them out-of-line.
3520#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00003521#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00003522 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00003523#include "clang/AST/TypeLocNodes.def"
3524
John McCall17001972009-10-18 01:05:36 +00003525 void VisitFunctionTypeLoc(FunctionTypeLoc);
3526 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00003527};
3528
John McCall17001972009-10-18 01:05:36 +00003529void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00003530 // nothing to do
3531}
John McCall17001972009-10-18 01:05:36 +00003532void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003533 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorc9b7a592010-01-18 18:04:31 +00003534 if (TL.needsExtraLocalData()) {
3535 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3536 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3537 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3538 TL.setModeAttr(Record[Idx++]);
3539 }
John McCall8f115c62009-10-16 21:56:05 +00003540}
John McCall17001972009-10-18 01:05:36 +00003541void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003542 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003543}
John McCall17001972009-10-18 01:05:36 +00003544void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003545 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003546}
John McCall17001972009-10-18 01:05:36 +00003547void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003548 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003549}
John McCall17001972009-10-18 01:05:36 +00003550void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003551 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003552}
John McCall17001972009-10-18 01:05:36 +00003553void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003554 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003555}
John McCall17001972009-10-18 01:05:36 +00003556void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003557 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara509357842011-03-05 14:42:21 +00003558 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003559}
John McCall17001972009-10-18 01:05:36 +00003560void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003561 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3562 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003563 if (Record[Idx++])
Sebastian Redl2c373b92010-10-05 15:59:54 +00003564 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003565 else
John McCall17001972009-10-18 01:05:36 +00003566 TL.setSizeExpr(0);
3567}
3568void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3569 VisitArrayTypeLoc(TL);
3570}
3571void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3572 VisitArrayTypeLoc(TL);
3573}
3574void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3575 VisitArrayTypeLoc(TL);
3576}
3577void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3578 DependentSizedArrayTypeLoc TL) {
3579 VisitArrayTypeLoc(TL);
3580}
3581void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3582 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003583 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003584}
3585void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003586 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003587}
3588void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003589 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003590}
3591void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003592 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
3593 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Douglas Gregor7fb25412010-10-01 18:44:50 +00003594 TL.setTrailingReturn(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00003595 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCalle6347002009-10-23 01:28:53 +00003596 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall17001972009-10-18 01:05:36 +00003597 }
3598}
3599void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3600 VisitFunctionTypeLoc(TL);
3601}
3602void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3603 VisitFunctionTypeLoc(TL);
3604}
John McCallb96ec562009-12-04 22:46:56 +00003605void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003606 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallb96ec562009-12-04 22:46:56 +00003607}
John McCall17001972009-10-18 01:05:36 +00003608void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003609 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003610}
3611void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003612 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3613 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3614 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003615}
3616void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003617 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3618 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3619 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3620 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003621}
3622void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003623 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003624}
Alexis Hunte852b102011-05-24 22:41:36 +00003625void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3626 TL.setKWLoc(ReadSourceLocation(Record, Idx));
3627 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3628 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3629 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3630}
Richard Smith30482bc2011-02-20 03:19:35 +00003631void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3632 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3633}
John McCall17001972009-10-18 01:05:36 +00003634void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003635 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003636}
3637void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003638 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003639}
John McCall81904512011-01-06 01:58:22 +00003640void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3641 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3642 if (TL.hasAttrOperand()) {
3643 SourceRange range;
3644 range.setBegin(ReadSourceLocation(Record, Idx));
3645 range.setEnd(ReadSourceLocation(Record, Idx));
3646 TL.setAttrOperandParensRange(range);
3647 }
3648 if (TL.hasAttrExprOperand()) {
3649 if (Record[Idx++])
3650 TL.setAttrExprOperand(Reader.ReadExpr(F));
3651 else
3652 TL.setAttrExprOperand(0);
3653 } else if (TL.hasAttrEnumOperand())
3654 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3655}
John McCall17001972009-10-18 01:05:36 +00003656void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003657 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003658}
John McCallcebee162009-10-18 09:09:24 +00003659void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3660 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003661 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallcebee162009-10-18 09:09:24 +00003662}
Douglas Gregorada4b792011-01-14 02:55:32 +00003663void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3664 SubstTemplateTypeParmPackTypeLoc TL) {
3665 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3666}
John McCall17001972009-10-18 01:05:36 +00003667void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3668 TemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003669 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3670 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3671 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall0ad16662009-10-29 08:12:44 +00003672 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3673 TL.setArgLocInfo(i,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003674 Reader.GetTemplateArgumentLocInfo(F,
3675 TL.getTypePtr()->getArg(i).getKind(),
3676 Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003677}
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003678void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3679 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3680 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3681}
Abramo Bagnara6150c882010-05-11 21:36:43 +00003682void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003683 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor844cb502011-03-01 18:12:44 +00003684 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003685}
John McCalle78aac42010-03-10 03:28:59 +00003686void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003687 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalle78aac42010-03-10 03:28:59 +00003688}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00003689void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003690 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor3d0da5f2011-03-01 01:34:45 +00003691 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00003692 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003693}
John McCallc392f372010-06-11 00:33:02 +00003694void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3695 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003696 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregora7a795b2011-03-01 20:11:18 +00003697 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00003698 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3699 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3700 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003701 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3702 TL.setArgLocInfo(I,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003703 Reader.GetTemplateArgumentLocInfo(F,
3704 TL.getTypePtr()->getArg(I).getKind(),
3705 Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003706}
Douglas Gregord2fa7662010-12-20 02:24:11 +00003707void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3708 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3709}
John McCall17001972009-10-18 01:05:36 +00003710void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003711 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8b07ec22010-05-15 11:32:37 +00003712}
3713void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3714 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003715 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3716 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003717 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003718 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003719}
John McCallfc93cf92009-10-22 22:37:11 +00003720void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003721 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCallfc93cf92009-10-22 22:37:11 +00003722}
John McCall8f115c62009-10-16 21:56:05 +00003723
Sebastian Redl2c373b92010-10-05 15:59:54 +00003724TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003725 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00003726 unsigned &Idx) {
3727 QualType InfoTy = GetType(Record[Idx++]);
3728 if (InfoTy.isNull())
3729 return 0;
3730
John McCallbcd03502009-12-07 02:54:59 +00003731 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003732 TypeLocReader TLR(*this, F, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00003733 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00003734 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00003735 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00003736}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003737
Sebastian Redl539c5062010-08-18 23:57:32 +00003738QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00003739 unsigned FastQuals = ID & Qualifiers::FastMask;
3740 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003741
Sebastian Redl539c5062010-08-18 23:57:32 +00003742 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003743 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00003744 switch ((PredefinedTypeIDs)Index) {
3745 case PREDEF_TYPE_NULL_ID: return QualType();
3746 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3747 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003748
Sebastian Redl539c5062010-08-18 23:57:32 +00003749 case PREDEF_TYPE_CHAR_U_ID:
3750 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003751 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00003752 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003753 break;
3754
Sebastian Redl539c5062010-08-18 23:57:32 +00003755 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3756 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3757 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3758 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3759 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3760 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3761 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3762 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3763 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3764 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3765 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3766 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3767 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3768 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3769 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3770 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3771 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
John McCall0009fcc2011-04-26 20:42:42 +00003772 case PREDEF_TYPE_BOUND_MEMBER: T = Context->BoundMemberTy; break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003773 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
John McCall31996342011-04-07 08:22:57 +00003774 case PREDEF_TYPE_UNKNOWN_ANY: T = Context->UnknownAnyTy; break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003775 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3776 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3777 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3778 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3779 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3780 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003781 }
3782
3783 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00003784 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003785 }
3786
Sebastian Redl539c5062010-08-18 23:57:32 +00003787 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003788 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00003789 if (TypesLoaded[Index].isNull()) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003790 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003791 if (TypesLoaded[Index].isNull())
3792 return QualType();
3793
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003794 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003795 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003796 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003797 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003798 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00003799 }
Mike Stump11289f42009-09-09 15:08:12 +00003800
John McCall8ccfcb52009-09-24 19:53:00 +00003801 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003802}
3803
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003804TypeID ASTReader::GetTypeID(QualType T) const {
3805 return MakeTypeID(T,
3806 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3807}
3808
3809TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3810 if (T.isNull())
3811 return TypeIdx();
3812 assert(!T.getLocalFastQualifiers());
3813
3814 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3815 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3816 // comparing keys of ASTDeclContextNameLookupTable.
3817 // If the type didn't come from the AST file use a specially marked index
3818 // so that any hash/key comparison fail since no such index is stored
3819 // in a AST file.
3820 if (I == TypeIdxs.end())
3821 return TypeIdx(-1);
3822 return I->second;
3823}
3824
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003825unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3826 unsigned Result = 0;
3827 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3828 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3829
3830 return Result;
3831}
3832
John McCall0ad16662009-10-29 08:12:44 +00003833TemplateArgumentLocInfo
Sebastian Redl2c373b92010-10-05 15:59:54 +00003834ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3835 TemplateArgument::ArgKind Kind,
John McCall0ad16662009-10-29 08:12:44 +00003836 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003837 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00003838 switch (Kind) {
3839 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003840 return ReadExpr(F);
John McCall0ad16662009-10-29 08:12:44 +00003841 case TemplateArgument::Type:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003842 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003843 case TemplateArgument::Template: {
Douglas Gregor9d802122011-03-02 17:09:35 +00003844 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3845 Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003846 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00003847 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003848 SourceLocation());
3849 }
3850 case TemplateArgument::TemplateExpansion: {
Douglas Gregor9d802122011-03-02 17:09:35 +00003851 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3852 Index);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003853 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003854 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00003855 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregoreb29d182011-01-05 17:40:24 +00003856 EllipsisLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003857 }
John McCall0ad16662009-10-29 08:12:44 +00003858 case TemplateArgument::Null:
3859 case TemplateArgument::Integral:
3860 case TemplateArgument::Declaration:
3861 case TemplateArgument::Pack:
3862 return TemplateArgumentLocInfo();
3863 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00003864 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00003865 return TemplateArgumentLocInfo();
3866}
3867
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003868TemplateArgumentLoc
Sebastian Redl2c373b92010-10-05 15:59:54 +00003869ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003870 const RecordData &Record, unsigned &Index) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003871 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003872
3873 if (Arg.getKind() == TemplateArgument::Expression) {
3874 if (Record[Index++]) // bool InfoHasSameExpr.
3875 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3876 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00003877 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003878 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003879}
3880
Sebastian Redl2c499f62010-08-18 23:56:43 +00003881Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00003882 return GetDecl(ID);
3883}
3884
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003885uint64_t
3886ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3887 if (ID == 0)
3888 return 0;
3889
3890 --ID;
3891 uint64_t Offset = 0;
3892 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003893 PerFileData &F = *Chain[N - I - 1];
3894
3895 if (ID < F.LocalNumCXXBaseSpecifiers)
3896 return Offset + F.CXXBaseSpecifiersOffsets[ID];
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003897
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003898 ID -= F.LocalNumCXXBaseSpecifiers;
3899 Offset += F.SizeInBits;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003900 }
3901
3902 assert(false && "CXXBaseSpecifiers not found");
3903 return 0;
3904}
3905
3906CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3907 // Figure out which AST file contains this offset.
3908 PerFileData *F = 0;
3909 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003910 if (Offset < Chain[N - I - 1]->SizeInBits) {
3911 F = Chain[N - I - 1];
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003912 break;
3913 }
3914
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003915 Offset -= Chain[N - I - 1]->SizeInBits;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003916 }
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003917
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003918 if (!F) {
3919 Error("Malformed AST file: C++ base specifiers at impossible offset");
3920 return 0;
3921 }
3922
3923 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3924 SavedStreamPosition SavedPosition(Cursor);
3925 Cursor.JumpToBit(Offset);
3926 ReadingKindTracker ReadingKind(Read_Decl, *this);
3927 RecordData Record;
3928 unsigned Code = Cursor.ReadCode();
3929 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3930 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3931 Error("Malformed AST file: missing C++ base specifiers");
3932 return 0;
3933 }
3934
3935 unsigned Idx = 0;
3936 unsigned NumBases = Record[Idx++];
3937 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3938 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3939 for (unsigned I = 0; I != NumBases; ++I)
3940 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3941 return Bases;
3942}
3943
Sebastian Redl2c499f62010-08-18 23:56:43 +00003944TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003945 if (!DeclsLoaded[0]) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00003946 ReadDeclRecord(0, 1);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003947 if (DeserializationListener)
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003948 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003949 }
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00003950
3951 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3952}
3953
Sebastian Redl539c5062010-08-18 23:57:32 +00003954Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003955 if (ID == 0)
3956 return 0;
3957
Douglas Gregor745ed142009-04-25 18:35:21 +00003958 if (ID > DeclsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003959 Error("declaration ID out-of-range for AST file");
Douglas Gregor745ed142009-04-25 18:35:21 +00003960 return 0;
3961 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003962
Douglas Gregor745ed142009-04-25 18:35:21 +00003963 unsigned Index = ID - 1;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003964 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003965 ReadDeclRecord(Index, ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003966 if (DeserializationListener)
3967 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3968 }
Douglas Gregor745ed142009-04-25 18:35:21 +00003969
3970 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003971}
3972
Chris Lattner9c28af02009-04-27 05:46:25 +00003973/// \brief Resolve the offset of a statement into a statement.
3974///
3975/// This operation will read a new statement from the external
3976/// source each time it is called, and is meant to be used via a
3977/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00003978Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00003979 // Switch case IDs are per Decl.
3980 ClearSwitchCaseIDs();
3981
Sebastian Redl5c415f32010-07-22 17:01:13 +00003982 // Offset here is a global offset across the entire chain.
3983 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3984 PerFileData &F = *Chain[N - I - 1];
3985 if (Offset < F.SizeInBits) {
3986 // Since we know that this statement is part of a decl, make sure to use
3987 // the decl cursor to read it.
3988 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003989 return ReadStmtFromStream(F);
Sebastian Redl5c415f32010-07-22 17:01:13 +00003990 }
3991 Offset -= F.SizeInBits;
3992 }
3993 llvm_unreachable("Broken chain");
Douglas Gregor3c3aa612009-04-18 00:07:54 +00003994}
3995
Sebastian Redl2c499f62010-08-18 23:56:43 +00003996bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003997 bool (*isKindWeWant)(Decl::Kind),
John McCall75b960e2010-06-01 09:23:16 +00003998 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump11289f42009-09-09 15:08:12 +00003999 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004000 "DeclContext has no lexical decls in storage");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004001
Sebastian Redl5c415f32010-07-22 17:01:13 +00004002 // There might be lexical decls in multiple parts of the chain, for the TU
4003 // at least.
Sebastian Redlda6a21c2010-09-28 02:24:44 +00004004 // DeclContextOffsets might reallocate as we load additional decls below,
4005 // so make a copy of the vector.
4006 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl5c415f32010-07-22 17:01:13 +00004007 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
4008 I != E; ++I) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004009 // IDs can be 0 if this context doesn't contain declarations.
4010 if (!I->LexicalDecls)
Sebastian Redl5c415f32010-07-22 17:01:13 +00004011 continue;
Sebastian Redl5c415f32010-07-22 17:01:13 +00004012
4013 // Load all of the declaration IDs
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00004014 for (const KindDeclIDPair *ID = I->LexicalDecls,
4015 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
4016 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
4017 continue;
4018
4019 Decl *D = GetDecl(ID->second);
Sebastian Redlda6a21c2010-09-28 02:24:44 +00004020 assert(D && "Null decl in lexical decls");
4021 Decls.push_back(D);
4022 }
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004023 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004024
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004025 ++NumLexicalDeclContextsRead;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004026 return false;
4027}
4028
John McCall75b960e2010-06-01 09:23:16 +00004029DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00004030ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00004031 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00004032 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004033 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004034 if (!Name)
4035 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
4036 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004037
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004038 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl471ac2f2010-08-24 00:49:55 +00004039 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl9617e7e2010-08-24 00:50:16 +00004040 // and namespaces. For any given name, the last available results replace
4041 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl5c415f32010-07-22 17:01:13 +00004042 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl9617e7e2010-08-24 00:50:16 +00004043 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl5c415f32010-07-22 17:01:13 +00004044 I != E; ++I) {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004045 if (!I->NameLookupTableData)
Sebastian Redl5c415f32010-07-22 17:01:13 +00004046 continue;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004047
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004048 ASTDeclContextNameLookupTable *LookupTable =
4049 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
4050 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
4051 if (Pos == LookupTable->end())
Sebastian Redl5c415f32010-07-22 17:01:13 +00004052 continue;
4053
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004054 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
4055 for (; Data.first != Data.second; ++Data.first)
4056 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl9617e7e2010-08-24 00:50:16 +00004057 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004058 }
4059
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004060 ++NumVisibleDeclContextsRead;
John McCall75b960e2010-06-01 09:23:16 +00004061
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004062 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00004063 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004064}
4065
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00004066void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
4067 assert(DC->hasExternalVisibleStorage() &&
4068 "DeclContext has no visible decls in storage");
4069
4070 llvm::SmallVector<NamedDecl *, 64> Decls;
4071 // There might be visible decls in multiple parts of the chain, for the TU
4072 // and namespaces.
4073 DeclContextInfos &Infos = DeclContextOffsets[DC];
4074 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
4075 I != E; ++I) {
4076 if (!I->NameLookupTableData)
4077 continue;
4078
4079 ASTDeclContextNameLookupTable *LookupTable =
4080 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
4081 for (ASTDeclContextNameLookupTable::item_iterator
4082 ItemI = LookupTable->item_begin(),
4083 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
4084 ASTDeclContextNameLookupTable::item_iterator::value_type Val
4085 = *ItemI;
4086 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
4087 Decls.clear();
4088 for (; Data.first != Data.second; ++Data.first)
4089 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
4090 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
4091 }
4092 }
4093}
4094
Sebastian Redl2c499f62010-08-18 23:56:43 +00004095void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004096 assert(Consumer);
4097 while (!InterestingDecls.empty()) {
4098 DeclGroupRef DG(InterestingDecls.front());
4099 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00004100 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004101 }
4102}
4103
Sebastian Redl2c499f62010-08-18 23:56:43 +00004104void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00004105 this->Consumer = Consumer;
4106
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004107 if (!Consumer)
4108 return;
4109
4110 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004111 // Force deserialization of this decl, which will cause it to be queued for
4112 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00004113 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004114 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00004115
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004116 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004117}
4118
Sebastian Redl2c499f62010-08-18 23:56:43 +00004119void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004120 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004121
Mike Stump11289f42009-09-09 15:08:12 +00004122 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00004123 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00004124 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00004125 unsigned NumDeclsLoaded
4126 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
4127 (Decl *)0);
4128 unsigned NumIdentifiersLoaded
4129 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
4130 IdentifiersLoaded.end(),
4131 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00004132 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00004133 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
4134 SelectorsLoaded.end(),
4135 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00004136
Douglas Gregorc5046832009-04-27 18:38:38 +00004137 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
4138 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor258ae542009-04-27 06:38:32 +00004139 if (TotalNumSLocEntries)
4140 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
4141 NumSLocEntriesRead, TotalNumSLocEntries,
4142 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00004143 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004144 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00004145 NumTypesLoaded, (unsigned)TypesLoaded.size(),
4146 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
4147 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004148 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00004149 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
4150 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00004151 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004152 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00004153 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
4154 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00004155 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004156 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00004157 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
4158 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00004159 if (TotalNumStatements)
4160 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
4161 NumStatementsRead, TotalNumStatements,
4162 ((float)NumStatementsRead/TotalNumStatements * 100));
4163 if (TotalNumMacros)
4164 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
4165 NumMacrosRead, TotalNumMacros,
4166 ((float)NumMacrosRead/TotalNumMacros * 100));
4167 if (TotalLexicalDeclContexts)
4168 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
4169 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4170 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4171 * 100));
4172 if (TotalVisibleDeclContexts)
4173 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
4174 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4175 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4176 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004177 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004178 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004179 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4180 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00004181 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004182 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00004183 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004184 std::fprintf(stderr, "\n");
4185}
4186
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00004187/// Return the amount of memory used by memory buffers, breaking down
4188/// by heap-backed versus mmap'ed memory.
4189void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
4190 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4191 if (llvm::MemoryBuffer *buf = Chain[i]->Buffer.get()) {
4192 size_t bytes = buf->getBufferSize();
4193 switch (buf->getBufferKind()) {
4194 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
4195 sizes.malloc_bytes += bytes;
4196 break;
4197 case llvm::MemoryBuffer::MemoryBuffer_MMap:
4198 sizes.mmap_bytes += bytes;
4199 break;
4200 }
4201 }
4202}
4203
Sebastian Redl2c499f62010-08-18 23:56:43 +00004204void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004205 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004206 S.ExternalSource = this;
4207
Douglas Gregor7cd60f72009-04-22 21:15:06 +00004208 // Makes sure any declarations that were deserialized "too early"
4209 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004210 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4211 if (SemaObj->TUScope)
John McCall48871652010-08-21 09:40:31 +00004212 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004213
4214 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004215 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00004216 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00004217
4218 // If there were any tentative definitions, deserialize them and add
Sebastian Redl35351a92010-01-31 22:27:38 +00004219 // them to Sema's list of tentative definitions.
Douglas Gregord4df8652009-04-22 22:02:47 +00004220 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4221 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redl35351a92010-01-31 22:27:38 +00004222 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregord4df8652009-04-22 22:02:47 +00004223 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00004224
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004225 // If there were any unused file scoped decls, deserialize them and add to
4226 // Sema's list of unused file scoped decls.
4227 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4228 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4229 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattner90073802010-02-12 00:07:30 +00004230 }
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004231
Alexis Hunt27a761d2011-05-04 23:29:54 +00004232 // If there were any delegating constructors, add them to Sema's list
4233 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
4234 CXXConstructorDecl *D
4235 = cast<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
4236 SemaObj->DelegatingCtorDecls.push_back(D);
4237 }
4238
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004239 // If there were any locally-scoped external declarations,
4240 // deserialize them and add them to Sema's table of locally-scoped
4241 // external declarations.
4242 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4243 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4244 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
4245 }
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004246
4247 // If there were any ext_vector type declarations, deserialize them
4248 // and add them to Sema's vector of such declarations.
4249 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
4250 SemaObj->ExtVectorDecls.push_back(
Richard Smithdda56e42011-04-15 14:24:37 +00004251 cast<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004252
4253 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
4254 // Can we cut them down before writing them ?
4255
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004256 // If there were any dynamic classes declarations, deserialize them
4257 // and add them to Sema's vector of such declarations.
4258 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
4259 SemaObj->DynamicClasses.push_back(
4260 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004261
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004262 // Load the offsets of the declarations that Sema references.
4263 // They will be lazily deserialized when needed.
4264 if (!SemaDeclRefs.empty()) {
4265 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
4266 SemaObj->StdNamespace = SemaDeclRefs[0];
4267 SemaObj->StdBadAlloc = SemaDeclRefs[1];
4268 }
4269
Sebastian Redl2c373b92010-10-05 15:59:54 +00004270 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
4271
4272 // If there are @selector references added them to its pool. This is for
4273 // implementation of -Wselector.
4274 if (!F->ReferencedSelectorsData.empty()) {
4275 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
4276 unsigned I = 0;
4277 while (I < DataSize) {
4278 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
4279 SourceLocation SelLoc = ReadSourceLocation(
4280 *F, F->ReferencedSelectorsData, I);
4281 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
4282 }
4283 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00004284 }
4285
Sebastian Redl14afaf02011-04-24 16:27:30 +00004286 // The special data sets below always come from the most recent PCH,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004287 // which is at the front of the chain.
4288 PerFileData &F = *Chain.front();
4289
Sebastian Redl14afaf02011-04-24 16:27:30 +00004290 // If there were any pending implicit instantiations, deserialize them
4291 // and add them to Sema's queue of such instantiations.
4292 assert(F.PendingInstantiations.size() % 2 == 0 &&
4293 "Expected pairs of entries");
4294 for (unsigned Idx = 0, N = F.PendingInstantiations.size(); Idx < N;) {
4295 ValueDecl *D=cast<ValueDecl>(GetDecl(F.PendingInstantiations[Idx++]));
4296 SourceLocation Loc = ReadSourceLocation(F, F.PendingInstantiations,Idx);
4297 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
4298 }
4299
Sebastian Redl2c373b92010-10-05 15:59:54 +00004300 // If there were any weak undeclared identifiers, deserialize them and add to
4301 // Sema's list of weak undeclared identifiers.
4302 if (!WeakUndeclaredIdentifiers.empty()) {
4303 unsigned Idx = 0;
4304 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
4305 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4306 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4307 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
4308 bool Used = WeakUndeclaredIdentifiers[Idx++];
4309 Sema::WeakInfo WI(AliasId, Loc);
4310 WI.setUsed(Used);
4311 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
4312 }
4313 }
4314
4315 // If there were any VTable uses, deserialize the information and add it
4316 // to Sema's vector and map of VTable uses.
4317 if (!VTableUses.empty()) {
4318 unsigned Idx = 0;
4319 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
4320 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4321 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
4322 bool DefinitionRequired = VTableUses[Idx++];
4323 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
4324 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004325 }
4326 }
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004327
4328 if (!FPPragmaOptions.empty()) {
4329 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4330 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4331 }
4332
4333 if (!OpenCLExtensions.empty()) {
4334 unsigned I = 0;
4335#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4336#include "clang/Basic/OpenCLExtensions.def"
4337
4338 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4339 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00004340}
4341
Sebastian Redl2c499f62010-08-18 23:56:43 +00004342IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redl78f51772010-08-02 18:30:12 +00004343 // Try to find this name within our on-disk hash tables. We start with the
4344 // most recent one, since that one contains the most up-to-date info.
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004345 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004346 ASTIdentifierLookupTable *IdTable
4347 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl5c415f32010-07-22 17:01:13 +00004348 if (!IdTable)
4349 continue;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004350 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004351 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004352 if (Pos == IdTable->end())
4353 continue;
Douglas Gregora868bbd2009-04-21 22:25:48 +00004354
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004355 // Dereferencing the iterator has the effect of building the
4356 // IdentifierInfo node and populating it with the various
4357 // declarations it needs.
Sebastian Redl78f51772010-08-02 18:30:12 +00004358 return *Pos;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004359 }
Sebastian Redl78f51772010-08-02 18:30:12 +00004360 return 0;
Douglas Gregora868bbd2009-04-21 22:25:48 +00004361}
4362
Douglas Gregor57756ea2010-10-14 22:11:03 +00004363namespace clang {
4364 /// \brief An identifier-lookup iterator that enumerates all of the
4365 /// identifiers stored within a set of AST files.
4366 class ASTIdentifierIterator : public IdentifierIterator {
4367 /// \brief The AST reader whose identifiers are being enumerated.
4368 const ASTReader &Reader;
4369
4370 /// \brief The current index into the chain of AST files stored in
4371 /// the AST reader.
4372 unsigned Index;
4373
4374 /// \brief The current position within the identifier lookup table
4375 /// of the current AST file.
4376 ASTIdentifierLookupTable::key_iterator Current;
4377
4378 /// \brief The end position within the identifier lookup table of
4379 /// the current AST file.
4380 ASTIdentifierLookupTable::key_iterator End;
4381
4382 public:
4383 explicit ASTIdentifierIterator(const ASTReader &Reader);
4384
4385 virtual llvm::StringRef Next();
4386 };
4387}
4388
4389ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
4390 : Reader(Reader), Index(Reader.Chain.size() - 1) {
4391 ASTIdentifierLookupTable *IdTable
4392 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4393 Current = IdTable->key_begin();
4394 End = IdTable->key_end();
4395}
4396
4397llvm::StringRef ASTIdentifierIterator::Next() {
4398 while (Current == End) {
4399 // If we have exhausted all of our AST files, we're done.
4400 if (Index == 0)
4401 return llvm::StringRef();
4402
4403 --Index;
4404 ASTIdentifierLookupTable *IdTable
4405 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4406 Current = IdTable->key_begin();
4407 End = IdTable->key_end();
4408 }
4409
4410 // We have any identifiers remaining in the current AST file; return
4411 // the next one.
4412 std::pair<const char*, unsigned> Key = *Current;
4413 ++Current;
4414 return llvm::StringRef(Key.first, Key.second);
4415}
4416
4417IdentifierIterator *ASTReader::getIdentifiers() const {
4418 return new ASTIdentifierIterator(*this);
4419}
4420
Mike Stump11289f42009-09-09 15:08:12 +00004421std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redl2c499f62010-08-18 23:56:43 +00004422ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redlada023c2010-08-04 20:40:17 +00004423 // Find this selector in a hash table. We want to find the most recent entry.
4424 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4425 PerFileData &F = *Chain[I];
4426 if (!F.SelectorLookupTable)
4427 continue;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004428
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004429 ASTSelectorLookupTable *PoolTable
4430 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
4431 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redlada023c2010-08-04 20:40:17 +00004432 if (Pos != PoolTable->end()) {
4433 ++NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004434 // FIXME: Not quite happy with the statistics here. We probably should
4435 // disable this tracking when called via LoadSelector.
4436 // Also, should entries without methods count as misses?
4437 ++NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004438 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redlada023c2010-08-04 20:40:17 +00004439 if (DeserializationListener)
4440 DeserializationListener->SelectorRead(Data.ID, Sel);
4441 return std::make_pair(Data.Instance, Data.Factory);
4442 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004443 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00004444
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004445 ++NumMethodPoolMisses;
Sebastian Redlada023c2010-08-04 20:40:17 +00004446 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorc78d3462009-04-24 21:10:55 +00004447}
4448
Sebastian Redl2c499f62010-08-18 23:56:43 +00004449void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004450 // It would be complicated to avoid reading the methods anyway. So don't.
4451 ReadMethodPool(Sel);
4452}
4453
Sebastian Redl2c499f62010-08-18 23:56:43 +00004454void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004455 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00004456 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00004457 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00004458 if (DeserializationListener)
4459 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004460}
4461
Douglas Gregor1342e842009-07-06 18:54:52 +00004462/// \brief Set the globally-visible declarations associated with the given
4463/// identifier.
4464///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004465/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00004466/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00004467/// them.
4468///
4469/// \param II an IdentifierInfo that refers to one or more globally-visible
4470/// declarations.
4471///
4472/// \param DeclIDs the set of declaration IDs with the name @p II that are
4473/// visible at global scope.
4474///
4475/// \param Nonrecursive should be true to indicate that the caller knows that
4476/// this call is non-recursive, and therefore the globally-visible declarations
4477/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00004478void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004479ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregor1342e842009-07-06 18:54:52 +00004480 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
4481 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004482 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004483 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4484 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4485 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004486 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00004487 return;
4488 }
Mike Stump11289f42009-09-09 15:08:12 +00004489
Douglas Gregor1342e842009-07-06 18:54:52 +00004490 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4491 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4492 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004493 if (SemaObj->TUScope) {
4494 // Introduce this declaration into the translation-unit scope
4495 // and add it to the declaration chain for this identifier, so
4496 // that (unqualified) name lookup will find it.
John McCall48871652010-08-21 09:40:31 +00004497 SemaObj->TUScope->AddDecl(D);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004498 }
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004499 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregor1342e842009-07-06 18:54:52 +00004500 } else {
4501 // Queue this declaration so that it will be added to the
4502 // translation unit scope and identifier's declaration chain
4503 // once a Sema object is known.
4504 PreloadedDecls.push_back(D);
4505 }
4506 }
4507}
4508
Sebastian Redl2c499f62010-08-18 23:56:43 +00004509IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004510 if (ID == 0)
4511 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00004512
Sebastian Redlc713b962010-07-21 00:46:22 +00004513 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004514 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004515 return 0;
4516 }
Mike Stump11289f42009-09-09 15:08:12 +00004517
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004518 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc713b962010-07-21 00:46:22 +00004519 ID -= 1;
4520 if (!IdentifiersLoaded[ID]) {
4521 unsigned Index = ID;
4522 const char *Str = 0;
4523 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4524 PerFileData *F = Chain[N - I - 1];
4525 if (Index < F->LocalNumIdentifiers) {
4526 uint32_t Offset = F->IdentifierOffsets[Index];
4527 Str = F->IdentifierTableData + Offset;
4528 break;
4529 }
4530 Index -= F->LocalNumIdentifiers;
4531 }
4532 assert(Str && "Broken Chain");
Douglas Gregor5287b4e2009-04-25 21:04:17 +00004533
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004534 // All of the strings in the AST file are preceded by a 16-bit length.
4535 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00004536 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4537 // unsigned integers. This is important to avoid integer overflow when
4538 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00004539 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00004540 unsigned StrLen = (((unsigned) StrLenPtr[0])
4541 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00004542 IdentifiersLoaded[ID]
Kovarththanan Rajaratnama3b09592010-03-12 10:32:27 +00004543 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlff4a2952010-07-23 23:49:55 +00004544 if (DeserializationListener)
4545 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004546 }
Mike Stump11289f42009-09-09 15:08:12 +00004547
Sebastian Redlc713b962010-07-21 00:46:22 +00004548 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004549}
4550
Douglas Gregor49f754f2011-04-20 00:21:03 +00004551bool ASTReader::ReadSLocEntry(unsigned ID) {
4552 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor258ae542009-04-27 06:38:32 +00004553}
4554
Sebastian Redl2c499f62010-08-18 23:56:43 +00004555Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00004556 if (ID == 0)
4557 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00004558
Sebastian Redlada023c2010-08-04 20:40:17 +00004559 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004560 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00004561 return Selector();
4562 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004563
Sebastian Redlada023c2010-08-04 20:40:17 +00004564 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004565 // Load this selector from the selector table.
Sebastian Redlada023c2010-08-04 20:40:17 +00004566 unsigned Idx = ID - 1;
4567 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4568 PerFileData &F = *Chain[N - I - 1];
4569 if (Idx < F.LocalNumSelectors) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004570 ASTSelectorLookupTrait Trait(*this);
Sebastian Redlada023c2010-08-04 20:40:17 +00004571 SelectorsLoaded[ID - 1] =
4572 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4573 if (DeserializationListener)
4574 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4575 break;
4576 }
4577 Idx -= F.LocalNumSelectors;
4578 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004579 }
4580
Sebastian Redlada023c2010-08-04 20:40:17 +00004581 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00004582}
4583
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004584Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00004585 return DecodeSelector(ID);
4586}
4587
Sebastian Redl2c499f62010-08-18 23:56:43 +00004588uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00004589 // ID 0 (the null selector) is considered an external selector.
4590 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00004591}
4592
Mike Stump11289f42009-09-09 15:08:12 +00004593DeclarationName
Sebastian Redl2c499f62010-08-18 23:56:43 +00004594ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004595 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4596 switch (Kind) {
4597 case DeclarationName::Identifier:
4598 return DeclarationName(GetIdentifierInfo(Record, Idx));
4599
4600 case DeclarationName::ObjCZeroArgSelector:
4601 case DeclarationName::ObjCOneArgSelector:
4602 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff3c301dc2009-04-23 15:15:40 +00004603 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004604
4605 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004606 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004607 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004608
4609 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004610 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004611 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004612
4613 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004614 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004615 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004616
4617 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004618 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004619 (OverloadedOperatorKind)Record[Idx++]);
4620
Alexis Hunt3d221f22009-11-29 07:34:05 +00004621 case DeclarationName::CXXLiteralOperatorName:
4622 return Context->DeclarationNames.getCXXLiteralOperatorName(
4623 GetIdentifierInfo(Record, Idx));
4624
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004625 case DeclarationName::CXXUsingDirective:
4626 return DeclarationName::getUsingDirectiveName();
4627 }
4628
4629 // Required to silence GCC warning
4630 return DeclarationName();
4631}
Douglas Gregor55abb232009-04-10 20:39:37 +00004632
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004633void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4634 DeclarationNameLoc &DNLoc,
4635 DeclarationName Name,
4636 const RecordData &Record, unsigned &Idx) {
4637 switch (Name.getNameKind()) {
4638 case DeclarationName::CXXConstructorName:
4639 case DeclarationName::CXXDestructorName:
4640 case DeclarationName::CXXConversionFunctionName:
4641 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4642 break;
4643
4644 case DeclarationName::CXXOperatorName:
4645 DNLoc.CXXOperatorName.BeginOpNameLoc
4646 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4647 DNLoc.CXXOperatorName.EndOpNameLoc
4648 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4649 break;
4650
4651 case DeclarationName::CXXLiteralOperatorName:
4652 DNLoc.CXXLiteralOperatorName.OpNameLoc
4653 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4654 break;
4655
4656 case DeclarationName::Identifier:
4657 case DeclarationName::ObjCZeroArgSelector:
4658 case DeclarationName::ObjCOneArgSelector:
4659 case DeclarationName::ObjCMultiArgSelector:
4660 case DeclarationName::CXXUsingDirective:
4661 break;
4662 }
4663}
4664
4665void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4666 DeclarationNameInfo &NameInfo,
4667 const RecordData &Record, unsigned &Idx) {
4668 NameInfo.setName(ReadDeclarationName(Record, Idx));
4669 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4670 DeclarationNameLoc DNLoc;
4671 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4672 NameInfo.setInfo(DNLoc);
4673}
4674
4675void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4676 const RecordData &Record, unsigned &Idx) {
Douglas Gregor14454802011-02-25 02:25:35 +00004677 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004678 unsigned NumTPLists = Record[Idx++];
4679 Info.NumTemplParamLists = NumTPLists;
4680 if (NumTPLists) {
4681 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4682 for (unsigned i=0; i != NumTPLists; ++i)
4683 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4684 }
4685}
4686
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004687TemplateName
Douglas Gregor5590be02011-01-15 06:45:20 +00004688ASTReader::ReadTemplateName(PerFileData &F, const RecordData &Record,
4689 unsigned &Idx) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004690 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004691 switch (Kind) {
4692 case TemplateName::Template:
4693 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4694
4695 case TemplateName::OverloadedTemplate: {
4696 unsigned size = Record[Idx++];
4697 UnresolvedSet<8> Decls;
4698 while (size--)
4699 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4700
4701 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4702 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004703
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004704 case TemplateName::QualifiedTemplate: {
4705 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4706 bool hasTemplKeyword = Record[Idx++];
4707 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4708 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4709 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004710
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004711 case TemplateName::DependentTemplate: {
4712 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4713 if (Record[Idx++]) // isIdentifier
4714 return Context->getDependentTemplateName(NNS,
4715 GetIdentifierInfo(Record, Idx));
4716 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004717 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004718 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004719
4720 case TemplateName::SubstTemplateTemplateParmPack: {
4721 TemplateTemplateParmDecl *Param
4722 = cast_or_null<TemplateTemplateParmDecl>(GetDecl(Record[Idx++]));
4723 if (!Param)
4724 return TemplateName();
4725
4726 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
4727 if (ArgPack.getKind() != TemplateArgument::Pack)
4728 return TemplateName();
4729
4730 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
4731 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004732 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004733
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004734 assert(0 && "Unhandled template name kind!");
4735 return TemplateName();
4736}
4737
4738TemplateArgument
Sebastian Redl2c373b92010-10-05 15:59:54 +00004739ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00004740 const RecordData &Record, unsigned &Idx) {
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004741 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
4742 switch (Kind) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004743 case TemplateArgument::Null:
4744 return TemplateArgument();
4745 case TemplateArgument::Type:
4746 return TemplateArgument(GetType(Record[Idx++]));
4747 case TemplateArgument::Declaration:
4748 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00004749 case TemplateArgument::Integral: {
4750 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4751 QualType T = GetType(Record[Idx++]);
4752 return TemplateArgument(Value, T);
4753 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004754 case TemplateArgument::Template:
Douglas Gregor5590be02011-01-15 06:45:20 +00004755 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004756 case TemplateArgument::TemplateExpansion: {
Douglas Gregor5590be02011-01-15 06:45:20 +00004757 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregore1d60df2011-01-14 23:41:42 +00004758 llvm::Optional<unsigned> NumTemplateExpansions;
4759 if (unsigned NumExpansions = Record[Idx++])
4760 NumTemplateExpansions = NumExpansions - 1;
4761 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004762 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004763 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004764 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004765 case TemplateArgument::Pack: {
4766 unsigned NumArgs = Record[Idx++];
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004767 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4768 for (unsigned I = 0; I != NumArgs; ++I)
4769 Args[I] = ReadTemplateArgument(F, Record, Idx);
4770 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004771 }
4772 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004773
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004774 assert(0 && "Unhandled template argument kind!");
4775 return TemplateArgument();
4776}
4777
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004778TemplateParameterList *
Sebastian Redl2c373b92010-10-05 15:59:54 +00004779ASTReader::ReadTemplateParameterList(PerFileData &F,
4780 const RecordData &Record, unsigned &Idx) {
4781 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4782 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4783 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004784
4785 unsigned NumParams = Record[Idx++];
4786 llvm::SmallVector<NamedDecl *, 16> Params;
4787 Params.reserve(NumParams);
4788 while (NumParams--)
4789 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004790
4791 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004792 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4793 Params.data(), Params.size(), RAngleLoc);
4794 return TemplateParams;
4795}
4796
4797void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004798ASTReader::
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004799ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004800 PerFileData &F, const RecordData &Record,
4801 unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004802 unsigned NumTemplateArgs = Record[Idx++];
4803 TemplArgs.reserve(NumTemplateArgs);
4804 while (NumTemplateArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004805 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004806}
4807
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004808/// \brief Read a UnresolvedSet structure.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004809void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004810 const RecordData &Record, unsigned &Idx) {
4811 unsigned NumDecls = Record[Idx++];
4812 while (NumDecls--) {
4813 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4814 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4815 Set.addDecl(D, AS);
4816 }
4817}
4818
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004819CXXBaseSpecifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00004820ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky19b9f952010-07-26 16:56:01 +00004821 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004822 bool isVirtual = static_cast<bool>(Record[Idx++]);
4823 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4824 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redl08905022011-02-05 19:23:19 +00004825 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004826 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4827 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor752a5952011-01-03 22:36:02 +00004828 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl08905022011-02-05 19:23:19 +00004829 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregor752a5952011-01-03 22:36:02 +00004830 EllipsisLoc);
Sebastian Redl08905022011-02-05 19:23:19 +00004831 Result.setInheritConstructors(inheritConstructors);
4832 return Result;
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004833}
4834
Alexis Hunt1d792652011-01-08 20:30:50 +00004835std::pair<CXXCtorInitializer **, unsigned>
4836ASTReader::ReadCXXCtorInitializers(PerFileData &F, const RecordData &Record,
4837 unsigned &Idx) {
4838 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004839 unsigned NumInitializers = Record[Idx++];
4840 if (NumInitializers) {
4841 ASTContext &C = *getContext();
4842
Alexis Hunt1d792652011-01-08 20:30:50 +00004843 CtorInitializers
4844 = new (C) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004845 for (unsigned i=0; i != NumInitializers; ++i) {
4846 TypeSourceInfo *BaseClassInfo = 0;
4847 bool IsBaseVirtual = false;
4848 FieldDecl *Member = 0;
Francois Pichetd583da02010-12-04 09:14:42 +00004849 IndirectFieldDecl *IndirectMember = 0;
Alexis Hunt37a477f2011-05-04 01:19:08 +00004850 CXXConstructorDecl *Target = 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004851
Alexis Hunt37a477f2011-05-04 01:19:08 +00004852 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
4853 switch (Type) {
4854 case CTOR_INITIALIZER_BASE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004855 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004856 IsBaseVirtual = Record[Idx++];
Alexis Hunt37a477f2011-05-04 01:19:08 +00004857 break;
4858
4859 case CTOR_INITIALIZER_DELEGATING:
4860 Target = cast<CXXConstructorDecl>(GetDecl(Record[Idx++]));
4861 break;
4862
4863 case CTOR_INITIALIZER_MEMBER:
4864 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
4865 break;
4866
4867 case CTOR_INITIALIZER_INDIRECT_MEMBER:
4868 IndirectMember = cast<IndirectFieldDecl>(GetDecl(Record[Idx++]));
4869 break;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004870 }
Alexis Hunt37a477f2011-05-04 01:19:08 +00004871
Douglas Gregor44e7df62011-01-04 00:32:56 +00004872 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004873 Expr *Init = ReadExpr(F);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004874 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4875 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004876 bool IsWritten = Record[Idx++];
4877 unsigned SourceOrderOrNumArrayIndices;
4878 llvm::SmallVector<VarDecl *, 8> Indices;
4879 if (IsWritten) {
4880 SourceOrderOrNumArrayIndices = Record[Idx++];
4881 } else {
4882 SourceOrderOrNumArrayIndices = Record[Idx++];
4883 Indices.reserve(SourceOrderOrNumArrayIndices);
4884 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4885 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4886 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004887
Alexis Hunt1d792652011-01-08 20:30:50 +00004888 CXXCtorInitializer *BOMInit;
Alexis Hunt37a477f2011-05-04 01:19:08 +00004889 if (Type == CTOR_INITIALIZER_BASE) {
Alexis Hunt1d792652011-01-08 20:30:50 +00004890 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
4891 LParenLoc, Init, RParenLoc,
4892 MemberOrEllipsisLoc);
Alexis Hunt37a477f2011-05-04 01:19:08 +00004893 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
4894 BOMInit = new (C) CXXCtorInitializer(C, MemberOrEllipsisLoc, LParenLoc,
4895 Target, Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004896 } else if (IsWritten) {
Francois Pichetd583da02010-12-04 09:14:42 +00004897 if (Member)
Alexis Hunt1d792652011-01-08 20:30:50 +00004898 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
4899 LParenLoc, Init, RParenLoc);
Francois Pichetd583da02010-12-04 09:14:42 +00004900 else
Alexis Hunt1d792652011-01-08 20:30:50 +00004901 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
4902 MemberOrEllipsisLoc, LParenLoc,
4903 Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004904 } else {
Alexis Hunt1d792652011-01-08 20:30:50 +00004905 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
4906 LParenLoc, Init, RParenLoc,
4907 Indices.data(), Indices.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004908 }
4909
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00004910 if (IsWritten)
4911 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Alexis Hunt1d792652011-01-08 20:30:50 +00004912 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004913 }
4914 }
4915
Alexis Hunt1d792652011-01-08 20:30:50 +00004916 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004917}
4918
Chris Lattnerca025db2010-05-07 21:43:38 +00004919NestedNameSpecifier *
Sebastian Redl2c499f62010-08-18 23:56:43 +00004920ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004921 unsigned N = Record[Idx++];
4922 NestedNameSpecifier *NNS = 0, *Prev = 0;
4923 for (unsigned I = 0; I != N; ++I) {
4924 NestedNameSpecifier::SpecifierKind Kind
4925 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4926 switch (Kind) {
4927 case NestedNameSpecifier::Identifier: {
4928 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4929 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4930 break;
4931 }
4932
4933 case NestedNameSpecifier::Namespace: {
4934 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4935 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4936 break;
4937 }
4938
Douglas Gregor7b26ff92011-02-24 02:36:08 +00004939 case NestedNameSpecifier::NamespaceAlias: {
4940 NamespaceAliasDecl *Alias
4941 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++]));
4942 NNS = NestedNameSpecifier::Create(*Context, Prev, Alias);
4943 break;
4944 }
4945
Chris Lattnerca025db2010-05-07 21:43:38 +00004946 case NestedNameSpecifier::TypeSpec:
4947 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall424cec92011-01-19 06:33:43 +00004948 const Type *T = GetType(Record[Idx++]).getTypePtrOrNull();
Douglas Gregor0cdc8322010-12-10 17:03:06 +00004949 if (!T)
4950 return 0;
4951
Chris Lattnerca025db2010-05-07 21:43:38 +00004952 bool Template = Record[Idx++];
4953 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4954 break;
4955 }
4956
4957 case NestedNameSpecifier::Global: {
4958 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4959 // No associated value, and there can't be a prefix.
4960 break;
4961 }
Chris Lattnerca025db2010-05-07 21:43:38 +00004962 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00004963 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00004964 }
4965 return NNS;
4966}
4967
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004968NestedNameSpecifierLoc
4969ASTReader::ReadNestedNameSpecifierLoc(PerFileData &F, const RecordData &Record,
4970 unsigned &Idx) {
4971 unsigned N = Record[Idx++];
Douglas Gregor9b272512011-02-28 23:58:31 +00004972 NestedNameSpecifierLocBuilder Builder;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004973 for (unsigned I = 0; I != N; ++I) {
4974 NestedNameSpecifier::SpecifierKind Kind
4975 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4976 switch (Kind) {
4977 case NestedNameSpecifier::Identifier: {
Douglas Gregor9b272512011-02-28 23:58:31 +00004978 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004979 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00004980 Builder.Extend(*Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004981 break;
4982 }
4983
4984 case NestedNameSpecifier::Namespace: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004985 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004986 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00004987 Builder.Extend(*Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004988 break;
4989 }
4990
4991 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004992 NamespaceAliasDecl *Alias
4993 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++]));
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004994 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00004995 Builder.Extend(*Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004996 break;
4997 }
4998
4999 case NestedNameSpecifier::TypeSpec:
5000 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005001 bool Template = Record[Idx++];
5002 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
5003 if (!T)
5004 return NestedNameSpecifierLoc();
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005005 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005006
5007 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
5008 Builder.Extend(*Context,
5009 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
5010 T->getTypeLoc(), ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005011 break;
5012 }
5013
5014 case NestedNameSpecifier::Global: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005015 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005016 Builder.MakeGlobal(*Context, ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005017 break;
5018 }
5019 }
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005020 }
5021
Douglas Gregor9b272512011-02-28 23:58:31 +00005022 return Builder.getWithLocInContext(*Context);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005023}
5024
Chris Lattnerca025db2010-05-07 21:43:38 +00005025SourceRange
Sebastian Redl2c373b92010-10-05 15:59:54 +00005026ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
5027 unsigned &Idx) {
5028 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
5029 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00005030 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00005031}
5032
Douglas Gregor1daeb692009-04-13 18:14:40 +00005033/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005034llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00005035 unsigned BitWidth = Record[Idx++];
5036 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
5037 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
5038 Idx += NumWords;
5039 return Result;
5040}
5041
5042/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005043llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00005044 bool isUnsigned = Record[Idx++];
5045 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
5046}
5047
Douglas Gregore0a3a512009-04-14 21:55:33 +00005048/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005049llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00005050 return llvm::APFloat(ReadAPInt(Record, Idx));
5051}
5052
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005053// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00005054std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005055 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00005056 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005057 Idx += Len;
5058 return Result;
5059}
5060
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005061VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
5062 unsigned &Idx) {
5063 unsigned Major = Record[Idx++];
5064 unsigned Minor = Record[Idx++];
5065 unsigned Subminor = Record[Idx++];
5066 if (Minor == 0)
5067 return VersionTuple(Major);
5068 if (Subminor == 0)
5069 return VersionTuple(Major, Minor - 1);
5070 return VersionTuple(Major, Minor - 1, Subminor - 1);
5071}
5072
Sebastian Redl2c499f62010-08-18 23:56:43 +00005073CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00005074 unsigned &Idx) {
5075 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
5076 return CXXTemporary::Create(*Context, Decl);
5077}
5078
Sebastian Redl2c499f62010-08-18 23:56:43 +00005079DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00005080 return Diag(SourceLocation(), DiagID);
5081}
5082
Sebastian Redl2c499f62010-08-18 23:56:43 +00005083DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005084 return Diags.Report(Loc, DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00005085}
Douglas Gregora9af1d12009-04-17 00:04:06 +00005086
Douglas Gregora868bbd2009-04-21 22:25:48 +00005087/// \brief Retrieve the identifier table associated with the
5088/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005089IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00005090 assert(PP && "Forgot to set Preprocessor ?");
5091 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00005092}
5093
Douglas Gregora9af1d12009-04-17 00:04:06 +00005094/// \brief Record that the given ID maps to the given switch-case
5095/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005096void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00005097 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
5098 SwitchCaseStmts[ID] = SC;
5099}
5100
5101/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005102SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00005103 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
5104 return SwitchCaseStmts[ID];
5105}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00005106
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00005107void ASTReader::ClearSwitchCaseIDs() {
5108 SwitchCaseStmts.clear();
5109}
5110
Sebastian Redl2c499f62010-08-18 23:56:43 +00005111void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005112 assert(NumCurrentElementsDeserializing &&
5113 "FinishedDeserializing not paired with StartedDeserializing");
5114 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00005115 // If any identifiers with corresponding top-level declarations have
5116 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005117 while (!PendingIdentifierInfos.empty()) {
5118 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
5119 PendingIdentifierInfos.front().DeclIDs, true);
5120 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00005121 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005122
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00005123 // Ready to load previous declarations of Decls that were delayed.
5124 while (!PendingPreviousDecls.empty()) {
5125 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
5126 PendingPreviousDecls.front().second);
5127 PendingPreviousDecls.pop_front();
5128 }
5129
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005130 // We are not in recursive loading, so it's safe to pass the "interesting"
5131 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005132 if (Consumer)
5133 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidisad5f95c2010-10-24 17:26:31 +00005134
5135 assert(PendingForwardRefs.size() == 0 &&
5136 "Some forward refs did not get linked to the definition!");
Douglas Gregor1342e842009-07-06 18:54:52 +00005137 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005138 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00005139}
Douglas Gregorb473b072010-08-19 00:28:17 +00005140
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005141ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005142 const char *isysroot, bool DisableValidation,
5143 bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005144 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
5145 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
5146 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
5147 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005148 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
5149 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NextSLocOffset(0),
5150 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
5151 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
5152 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
5153 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
5154 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
5155 NumCurrentElementsDeserializing(0)
5156{
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005157 RelocatablePCH = false;
5158}
5159
5160ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
5161 Diagnostic &Diags, const char *isysroot,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005162 bool DisableValidation, bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005163 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
5164 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005165 isysroot(isysroot), DisableValidation(DisableValidation),
5166 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
5167 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00005168 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
5169 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
5170 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
5171 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
5172 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
5173 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005174 RelocatablePCH = false;
5175}
5176
5177ASTReader::~ASTReader() {
5178 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
5179 delete Chain[e - i - 1];
5180 // Delete all visible decl lookup tables
5181 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
5182 E = DeclContextOffsets.end();
5183 I != E; ++I) {
5184 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
5185 J != F; ++J) {
5186 if (J->NameLookupTableData)
5187 delete static_cast<ASTDeclContextNameLookupTable*>(
5188 J->NameLookupTableData);
5189 }
5190 }
5191 for (DeclContextVisibleUpdatesPending::iterator
5192 I = PendingVisibleUpdates.begin(),
5193 E = PendingVisibleUpdates.end();
5194 I != E; ++I) {
5195 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5196 F = I->second.end();
5197 J != F; ++J)
5198 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
5199 }
5200}
5201
Sebastian Redl009e7f22010-10-05 16:15:19 +00005202ASTReader::PerFileData::PerFileData(ASTFileType Ty)
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00005203 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0),
5204 SLocFileOffsets(0), LocalSLocSize(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00005205 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
5206 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
Douglas Gregor09b69892011-02-10 17:09:37 +00005207 MacroDefinitionOffsets(0),
5208 LocalNumHeaderFileInfos(0), HeaderFileInfoTableData(0),
5209 HeaderFileInfoTable(0),
5210 LocalNumSelectors(0), SelectorOffsets(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00005211 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005212 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
5213 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redl3f6b7532010-10-01 19:59:12 +00005214 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregorb473b072010-08-19 00:28:17 +00005215{}
5216
5217ASTReader::PerFileData::~PerFileData() {
5218 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
Douglas Gregor09b69892011-02-10 17:09:37 +00005219 delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable);
Douglas Gregorb473b072010-08-19 00:28:17 +00005220 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
5221}