blob: 3c3993f412d30b6724d1c884c3ddf2fc690511f7 [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);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +000095 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian62c56022010-04-22 21:01:59 +000096 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000097 PARSE_LANGOPT_BENIGN(PascalStrings);
98 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump11289f42009-09-09 15:08:12 +000099 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000100 diag::warn_pch_lax_vector_conversions);
Nate Begeman9d905792009-06-25 22:57:40 +0000101 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000102 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Anders Carlssonce8dd3a2011-02-19 23:53:54 +0000103 PARSE_LANGOPT_IMPORTANT(ObjCExceptions, diag::warn_pch_objc_exceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +0000104 PARSE_LANGOPT_IMPORTANT(CXXExceptions, diag::warn_pch_cxx_exceptions);
105 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Douglas Gregordbe39272011-02-01 15:15:22 +0000106 PARSE_LANGOPT_IMPORTANT(MSBitfields, diag::warn_pch_ms_bitfields);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000107 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
108 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
109 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump11289f42009-09-09 15:08:12 +0000110 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000111 diag::warn_pch_thread_safe_statics);
Daniel Dunbara77eaeb2009-09-03 04:54:28 +0000112 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000113 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
114 PARSE_LANGOPT_BENIGN(EmitAllDecls);
115 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattner51924e512010-06-26 21:25:03 +0000116 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump11289f42009-09-09 15:08:12 +0000117 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000118 diag::warn_pch_heinous_extensions);
119 // FIXME: Most of the options below are benign if the macro wasn't
120 // used. Unfortunately, this means that a PCH compiled without
121 // optimization can't be used with optimization turned on, even
122 // though the only thing that changes is whether __OPTIMIZE__ was
123 // defined... but if __OPTIMIZE__ never showed up in the header, it
124 // doesn't matter. We could consider making this some special kind
125 // of check.
126 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
127 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
128 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
129 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
130 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
131 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
Chandler Carruth7ffce732011-04-23 20:05:38 +0000132 PARSE_LANGOPT_IMPORTANT(Deprecated, diag::warn_pch_deprecated);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000133 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
134 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsoned4e2952009-11-05 20:14:16 +0000135 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +0000136 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000137 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump11289f42009-09-09 15:08:12 +0000138 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000139 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
140 return true;
141 }
142 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000143 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
144 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000145 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman9d905792009-06-25 22:57:40 +0000146 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Peter Collingbourne546d0792010-12-01 19:14:57 +0000147 PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda);
Mike Stumpd9546382009-12-12 01:27:46 +0000148 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbar143021e2009-09-21 04:16:19 +0000149 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000150 PARSE_LANGOPT_BENIGN(SpellChecking);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000151 PARSE_LANGOPT_BENIGN(DefaultFPContract);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +0000152#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000153#undef PARSE_LANGOPT_BENIGN
154
155 return false;
156}
157
Daniel Dunbar20a682d2009-11-11 00:52:11 +0000158bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
159 if (Triple == PP.getTargetInfo().getTriple().str())
160 return false;
161
162 Reader.Diag(diag::warn_pch_target_triple)
163 << Triple << PP.getTargetInfo().getTriple().str();
164 return true;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000165}
166
Benjamin Kramer90b5b682010-11-25 18:29:30 +0000167namespace {
168 struct EmptyStringRef {
169 bool operator ()(llvm::StringRef r) const { return r.empty(); }
170 };
171 struct EmptyBlock {
172 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
173 };
174}
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000175
176static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
177 PCHPredefinesBlocks R) {
178 // First, sum up the lengths.
179 unsigned LL = 0, RL = 0;
180 for (unsigned I = 0, N = L.size(); I != N; ++I) {
181 LL += L[I].size();
182 }
183 for (unsigned I = 0, N = R.size(); I != N; ++I) {
184 RL += R[I].Data.size();
185 }
186 if (LL != RL)
187 return false;
188 if (LL == 0 && RL == 0)
189 return true;
190
191 // Kick out empty parts, they confuse the algorithm below.
192 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
193 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
194
195 // Do it the hard way. At this point, both vectors must be non-empty.
196 llvm::StringRef LR = L[0], RR = R[0].Data;
197 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbar01ad0a72010-07-16 00:00:11 +0000198 (void) RN;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000199 for (;;) {
200 // Compare the current pieces.
201 if (LR.size() == RR.size()) {
202 // If they're the same length, it's pretty easy.
203 if (LR != RR)
204 return false;
205 // Both pieces are done, advance.
206 ++LI;
207 ++RI;
208 // If either string is done, they're both done, since they're the same
209 // length.
210 if (LI == LN) {
211 assert(RI == RN && "Strings not the same length after all?");
212 return true;
213 }
214 LR = L[LI];
215 RR = R[RI].Data;
216 } else if (LR.size() < RR.size()) {
217 // Right piece is longer.
218 if (!RR.startswith(LR))
219 return false;
220 ++LI;
221 assert(LI != LN && "Strings not the same length after all?");
222 RR = RR.substr(LR.size());
223 LR = L[LI];
224 } else {
225 // Left piece is longer.
226 if (!LR.startswith(RR))
227 return false;
228 ++RI;
229 assert(RI != RN && "Strings not the same length after all?");
230 LR = LR.substr(RR.size());
231 RR = R[RI].Data;
232 }
233 }
234}
235
236static std::pair<FileID, llvm::StringRef::size_type>
237FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
238 std::pair<FileID, llvm::StringRef::size_type> Res;
239 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
240 Res.second = Buffers[I].Data.find(MacroDef);
241 if (Res.second != llvm::StringRef::npos) {
242 Res.first = Buffers[I].BufferID;
243 break;
244 }
245 }
246 return Res;
247}
248
249bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000250 llvm::StringRef OriginalFileName,
Nick Lewycky36079892011-02-23 21:16:44 +0000251 std::string &SuggestedPredefines,
252 FileManager &FileMgr) {
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000253 // We are in the context of an implicit include, so the predefines buffer will
254 // have a #include entry for the PCH file itself (as normalized by the
255 // preprocessor initialization). Find it and skip over it in the checking
256 // below.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000257 llvm::SmallString<256> PCHInclude;
258 PCHInclude += "#include \"";
Nick Lewycky36079892011-02-23 21:16:44 +0000259 PCHInclude += NormalizeDashIncludePath(OriginalFileName, FileMgr);
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000260 PCHInclude += "\"\n";
261 std::pair<llvm::StringRef,llvm::StringRef> Split =
262 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
263 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000264 if (Left == PP.getPredefines()) {
265 Error("Missing PCH include entry!");
266 return true;
267 }
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000268
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000269 // If the concatenation of all the PCH buffers is equal to the adjusted
270 // command line, we're done.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000271 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
272 CommandLine.push_back(Left);
273 CommandLine.push_back(Right);
274 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000275 return false;
276
277 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000278
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000279 // The predefines buffers are different. Determine what the differences are,
280 // and whether they require us to reject the PCH file.
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000281 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000282 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
283 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000284
285 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
286 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000287
288 // Pick out implicit #includes after the PCH and don't consider them for
289 // validation; we will insert them into SuggestedPredefines so that the
290 // preprocessor includes them.
291 std::string IncludesAfterPCH;
292 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
293 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
294 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
295 if (AfterPCHLines[i].startswith("#include ")) {
296 IncludesAfterPCH += AfterPCHLines[i];
297 IncludesAfterPCH += '\n';
298 } else {
299 CmdLineLines.push_back(AfterPCHLines[i]);
300 }
301 }
302
303 // Make sure we add the includes last into SuggestedPredefines before we
304 // exit this function.
305 struct AddIncludesRAII {
306 std::string &SuggestedPredefines;
307 std::string &IncludesAfterPCH;
308
309 AddIncludesRAII(std::string &SuggestedPredefines,
310 std::string &IncludesAfterPCH)
311 : SuggestedPredefines(SuggestedPredefines),
312 IncludesAfterPCH(IncludesAfterPCH) { }
313 ~AddIncludesRAII() {
314 SuggestedPredefines += IncludesAfterPCH;
315 }
316 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000317
Daniel Dunbar499baed2009-11-11 05:26:28 +0000318 // Sort both sets of predefined buffer lines, since we allow some extra
319 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000320 std::sort(CmdLineLines.begin(), CmdLineLines.end());
321 std::sort(PCHLines.begin(), PCHLines.end());
322
Daniel Dunbar499baed2009-11-11 05:26:28 +0000323 // Determine which predefines that were used to build the PCH file are missing
324 // from the command line.
325 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000326 std::set_difference(PCHLines.begin(), PCHLines.end(),
327 CmdLineLines.begin(), CmdLineLines.end(),
328 std::back_inserter(MissingPredefines));
329
330 bool MissingDefines = false;
331 bool ConflictingDefines = false;
332 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000333 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000334 if (Missing.startswith("#include ")) {
335 // An -include was specified when generating the PCH; it is included in
336 // the PCH, just ignore it.
337 continue;
338 }
Daniel Dunbar499baed2009-11-11 05:26:28 +0000339 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000340 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
341 return true;
342 }
Mike Stump11289f42009-09-09 15:08:12 +0000343
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000344 // This is a macro definition. Determine the name of the macro we're
345 // defining.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000346 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000347 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000348 = Missing.find_first_of("( \n\r", StartOfMacroName);
349 assert(EndOfMacroName != std::string::npos &&
350 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000351 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000352
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000353 // Determine whether this macro was given a different definition on the
354 // command line.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000355 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000356 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000357 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000358 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
359 MacroDefStart);
360 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000361 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000362 // Different macro; we're done.
363 ConflictPos = CmdLineLines.end();
Mike Stump11289f42009-09-09 15:08:12 +0000364 break;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000365 }
Mike Stump11289f42009-09-09 15:08:12 +0000366
367 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000368 "Invalid #define in predefines buffer?");
Mike Stump11289f42009-09-09 15:08:12 +0000369 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000370 (*ConflictPos)[MacroDefLen] != '(')
371 continue; // Longer macro name; keep trying.
Mike Stump11289f42009-09-09 15:08:12 +0000372
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000373 // We found a conflicting macro definition.
374 break;
375 }
Mike Stump11289f42009-09-09 15:08:12 +0000376
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000377 if (ConflictPos != CmdLineLines.end()) {
378 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
379 << MacroName;
380
381 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000382 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
383 FindMacro(Buffers, Missing);
384 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
385 SourceLocation PCHMissingLoc =
386 SourceMgr.getLocForStartOfFile(MacroLoc.first)
387 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar499baed2009-11-11 05:26:28 +0000388 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000389
390 ConflictingDefines = true;
391 continue;
392 }
Mike Stump11289f42009-09-09 15:08:12 +0000393
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000394 // If the macro doesn't conflict, then we'll just pick up the macro
395 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000396 if (ConflictingDefines)
397 continue; // Don't complain if there are already conflicting defs
Mike Stump11289f42009-09-09 15:08:12 +0000398
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000399 if (!MissingDefines) {
400 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
401 MissingDefines = true;
402 }
403
404 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000405 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
406 FindMacro(Buffers, Missing);
407 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
408 SourceLocation PCHMissingLoc =
409 SourceMgr.getLocForStartOfFile(MacroLoc.first)
410 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000411 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
412 }
Mike Stump11289f42009-09-09 15:08:12 +0000413
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000414 if (ConflictingDefines)
415 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000416
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000417 // Determine what predefines were introduced based on command-line
418 // parameters that were not present when building the PCH
419 // file. Extra #defines are okay, so long as the identifiers being
420 // defined were not used within the precompiled header.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000421 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000422 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
423 PCHLines.begin(), PCHLines.end(),
Mike Stump11289f42009-09-09 15:08:12 +0000424 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000425 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000426 llvm::StringRef &Extra = ExtraPredefines[I];
427 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000428 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
429 return true;
430 }
431
432 // This is an extra macro definition. Determine the name of the
433 // macro we're defining.
434 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000435 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000436 = Extra.find_first_of("( \n\r", StartOfMacroName);
437 assert(EndOfMacroName != std::string::npos &&
438 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000439 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000440
441 // Check whether this name was used somewhere in the PCH file. If
442 // so, defining it as a macro could change behavior, so we reject
443 // the PCH file.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000444 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar045c92f2009-11-11 00:52:00 +0000445 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000446 return true;
447 }
448
449 // Add this definition to the suggested predefines buffer.
450 SuggestedPredefines += Extra;
451 SuggestedPredefines += '\n';
452 }
453
454 // If we get here, it's because the predefines buffer had compatible
455 // contents. Accept the PCH file.
456 return false;
457}
458
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000459void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
460 unsigned ID) {
461 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
462 ++NumHeaderInfos;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000463}
464
465void PCHValidator::ReadCounter(unsigned Value) {
466 PP.setCounterValue(Value);
467}
468
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000469//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +0000470// AST reader implementation
Douglas Gregora868bbd2009-04-21 22:25:48 +0000471//===----------------------------------------------------------------------===//
472
Sebastian Redl07a89a82010-07-30 00:29:29 +0000473void
Sebastian Redl3e31c722010-08-18 23:56:56 +0000474ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redl07a89a82010-07-30 00:29:29 +0000475 DeserializationListener = Listener;
Sebastian Redl07a89a82010-07-30 00:29:29 +0000476}
477
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000478
Douglas Gregora868bbd2009-04-21 22:25:48 +0000479namespace {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000480class ASTSelectorLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000481 ASTReader &Reader;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000482
483public:
Sebastian Redl834bb972010-08-04 17:20:04 +0000484 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +0000485 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +0000486 ObjCMethodList Instance, Factory;
487 };
Douglas Gregorc78d3462009-04-24 21:10:55 +0000488
489 typedef Selector external_key_type;
490 typedef external_key_type internal_key_type;
491
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000492 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump11289f42009-09-09 15:08:12 +0000493
Douglas Gregorc78d3462009-04-24 21:10:55 +0000494 static bool EqualKey(const internal_key_type& a,
495 const internal_key_type& b) {
496 return a == b;
497 }
Mike Stump11289f42009-09-09 15:08:12 +0000498
Douglas Gregorc78d3462009-04-24 21:10:55 +0000499 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +0000500 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000501 }
Mike Stump11289f42009-09-09 15:08:12 +0000502
Douglas Gregorc78d3462009-04-24 21:10:55 +0000503 // This hopefully will just get inlined and removed by the optimizer.
504 static const internal_key_type&
505 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000506
Douglas Gregorc78d3462009-04-24 21:10:55 +0000507 static std::pair<unsigned, unsigned>
508 ReadKeyDataLength(const unsigned char*& d) {
509 using namespace clang::io;
510 unsigned KeyLen = ReadUnalignedLE16(d);
511 unsigned DataLen = ReadUnalignedLE16(d);
512 return std::make_pair(KeyLen, DataLen);
513 }
Mike Stump11289f42009-09-09 15:08:12 +0000514
Douglas Gregor95c13f52009-04-25 17:48:32 +0000515 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000516 using namespace clang::io;
Chris Lattner8575daa2009-04-27 21:45:14 +0000517 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000518 unsigned N = ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000519 IdentifierInfo *FirstII
Douglas Gregorc78d3462009-04-24 21:10:55 +0000520 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
521 if (N == 0)
522 return SelTable.getNullarySelector(FirstII);
523 else if (N == 1)
524 return SelTable.getUnarySelector(FirstII);
525
526 llvm::SmallVector<IdentifierInfo *, 16> Args;
527 Args.push_back(FirstII);
528 for (unsigned I = 1; I != N; ++I)
529 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
530
Douglas Gregor038c3382009-05-22 22:45:36 +0000531 return SelTable.getSelector(N, Args.data());
Douglas Gregorc78d3462009-04-24 21:10:55 +0000532 }
Mike Stump11289f42009-09-09 15:08:12 +0000533
Douglas Gregorc78d3462009-04-24 21:10:55 +0000534 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
535 using namespace clang::io;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000536
537 data_type Result;
538
Sebastian Redl834bb972010-08-04 17:20:04 +0000539 Result.ID = ReadUnalignedLE32(d);
540 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
541 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
542
Douglas Gregorc78d3462009-04-24 21:10:55 +0000543 // Load instance methods
544 ObjCMethodList *Prev = 0;
545 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000546 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000547 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000548 if (!Result.Instance.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000549 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000550 Result.Instance.Method = Method;
551 Prev = &Result.Instance;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000552 continue;
553 }
554
Ted Kremenekda4abf12010-02-11 00:53:01 +0000555 ObjCMethodList *Mem =
556 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
557 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000558 Prev = Prev->Next;
559 }
560
561 // Load factory methods
562 Prev = 0;
563 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000564 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000565 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000566 if (!Result.Factory.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000567 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000568 Result.Factory.Method = Method;
569 Prev = &Result.Factory;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000570 continue;
571 }
572
Ted Kremenekda4abf12010-02-11 00:53:01 +0000573 ObjCMethodList *Mem =
574 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
575 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000576 Prev = Prev->Next;
577 }
578
579 return Result;
580 }
581};
Mike Stump11289f42009-09-09 15:08:12 +0000582
583} // end anonymous namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +0000584
585/// \brief The on-disk hash table used for the global method pool.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000586typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
587 ASTSelectorLookupTable;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000588
Sebastian Redl2c373b92010-10-05 15:59:54 +0000589namespace clang {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000590class ASTIdentifierLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000591 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +0000592 ASTReader::PerFileData &F;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000593
594 // If we know the IdentifierInfo in advance, it is here and we will
595 // not build a new one. Used when deserializing information about an
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000596 // identifier that was constructed before the AST file was read.
Douglas Gregora868bbd2009-04-21 22:25:48 +0000597 IdentifierInfo *KnownII;
598
599public:
600 typedef IdentifierInfo * data_type;
601
602 typedef const std::pair<const char*, unsigned> external_key_type;
603
604 typedef external_key_type internal_key_type;
605
Sebastian Redl2c373b92010-10-05 15:59:54 +0000606 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000607 IdentifierInfo *II = 0)
Sebastian Redl2c373b92010-10-05 15:59:54 +0000608 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump11289f42009-09-09 15:08:12 +0000609
Douglas Gregora868bbd2009-04-21 22:25:48 +0000610 static bool EqualKey(const internal_key_type& a,
611 const internal_key_type& b) {
612 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
613 : false;
614 }
Mike Stump11289f42009-09-09 15:08:12 +0000615
Douglas Gregora868bbd2009-04-21 22:25:48 +0000616 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000617 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregora868bbd2009-04-21 22:25:48 +0000618 }
Mike Stump11289f42009-09-09 15:08:12 +0000619
Douglas Gregora868bbd2009-04-21 22:25:48 +0000620 // This hopefully will just get inlined and removed by the optimizer.
621 static const internal_key_type&
622 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000623
Douglas Gregor57756ea2010-10-14 22:11:03 +0000624 // This hopefully will just get inlined and removed by the optimizer.
625 static const external_key_type&
626 GetExternalKey(const internal_key_type& x) { return x; }
627
Douglas Gregora868bbd2009-04-21 22:25:48 +0000628 static std::pair<unsigned, unsigned>
629 ReadKeyDataLength(const unsigned char*& d) {
630 using namespace clang::io;
Douglas Gregor6b7bf5a2009-04-25 20:26:24 +0000631 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor5287b4e2009-04-25 21:04:17 +0000632 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000633 return std::make_pair(KeyLen, DataLen);
634 }
Mike Stump11289f42009-09-09 15:08:12 +0000635
Douglas Gregora868bbd2009-04-21 22:25:48 +0000636 static std::pair<const char*, unsigned>
637 ReadKey(const unsigned char* d, unsigned n) {
638 assert(n >= 2 && d[n-1] == '\0');
639 return std::make_pair((const char*) d, n-1);
640 }
Mike Stump11289f42009-09-09 15:08:12 +0000641
642 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregora868bbd2009-04-21 22:25:48 +0000643 const unsigned char* d,
644 unsigned DataLen) {
645 using namespace clang::io;
Sebastian Redl539c5062010-08-18 23:57:32 +0000646 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000647 bool IsInteresting = ID & 0x01;
648
649 // Wipe out the "is interesting" bit.
650 ID = ID >> 1;
651
652 if (!IsInteresting) {
Sebastian Redl98912122010-07-27 23:01:28 +0000653 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregor1d583f22009-04-28 21:18:29 +0000654 // and associate it with the persistent ID.
655 IdentifierInfo *II = KnownII;
656 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000657 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000658 Reader.SetIdentifierInfo(ID, II);
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000659 II->setIsFromAST();
Douglas Gregor1d583f22009-04-28 21:18:29 +0000660 return II;
661 }
662
Douglas Gregorb9256522009-04-28 21:32:13 +0000663 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000664 bool CPlusPlusOperatorKeyword = Bits & 0x01;
665 Bits >>= 1;
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000666 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
667 Bits >>= 1;
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000668 bool Poisoned = Bits & 0x01;
669 Bits >>= 1;
670 bool ExtensionToken = Bits & 0x01;
671 Bits >>= 1;
672 bool hasMacroDefinition = Bits & 0x01;
673 Bits >>= 1;
674 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
675 Bits >>= 10;
Mike Stump11289f42009-09-09 15:08:12 +0000676
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000677 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorb9256522009-04-28 21:32:13 +0000678 DataLen -= 6;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000679
680 // Build the IdentifierInfo itself and link the identifier ID with
681 // the new IdentifierInfo.
682 IdentifierInfo *II = KnownII;
683 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000684 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000685 Reader.SetIdentifierInfo(ID, II);
686
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000687 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000688 // Token IDs are read-only.
689 if (HasRevertedTokenIDToIdentifier)
690 II->RevertTokenIDToIdentifier();
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000691 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump11289f42009-09-09 15:08:12 +0000692 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000693 "Incorrect extension token flag");
694 (void)ExtensionToken;
695 II->setIsPoisoned(Poisoned);
696 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
697 "Incorrect C++ operator keyword flag");
698 (void)CPlusPlusOperatorKeyword;
699
Douglas Gregorc3366a52009-04-21 23:56:24 +0000700 // If this identifier is a macro, deserialize the macro
701 // definition.
702 if (hasMacroDefinition) {
Douglas Gregorb9256522009-04-28 21:32:13 +0000703 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor5ef9e332010-10-30 00:23:06 +0000704 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregorb9256522009-04-28 21:32:13 +0000705 DataLen -= 4;
Douglas Gregorc3366a52009-04-21 23:56:24 +0000706 }
Douglas Gregora868bbd2009-04-21 22:25:48 +0000707
708 // Read all of the declarations visible at global scope with this
709 // name.
Chris Lattner1d728882009-04-27 22:17:41 +0000710 if (Reader.getContext() == 0) return II;
Douglas Gregor1342e842009-07-06 18:54:52 +0000711 if (DataLen > 0) {
712 llvm::SmallVector<uint32_t, 4> DeclIDs;
713 for (; DataLen > 0; DataLen -= 4)
714 DeclIDs.push_back(ReadUnalignedLE32(d));
715 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000716 }
Mike Stump11289f42009-09-09 15:08:12 +0000717
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000718 II->setIsFromAST();
Douglas Gregora868bbd2009-04-21 22:25:48 +0000719 return II;
720 }
721};
Mike Stump11289f42009-09-09 15:08:12 +0000722
723} // end anonymous namespace
Douglas Gregora868bbd2009-04-21 22:25:48 +0000724
725/// \brief The on-disk hash table used to contain information about
726/// all of the identifiers in the program.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000727typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
728 ASTIdentifierLookupTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000729
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000730namespace {
731class ASTDeclContextNameLookupTrait {
732 ASTReader &Reader;
733
734public:
735 /// \brief Pair of begin/end iterators for DeclIDs.
736 typedef std::pair<DeclID *, DeclID *> data_type;
737
738 /// \brief Special internal key for declaration names.
739 /// The hash table creates keys for comparison; we do not create
740 /// a DeclarationName for the internal key to avoid deserializing types.
741 struct DeclNameKey {
742 DeclarationName::NameKind Kind;
743 uint64_t Data;
744 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
745 };
746
747 typedef DeclarationName external_key_type;
748 typedef DeclNameKey internal_key_type;
749
750 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
751
752 static bool EqualKey(const internal_key_type& a,
753 const internal_key_type& b) {
754 return a.Kind == b.Kind && a.Data == b.Data;
755 }
756
757 unsigned ComputeHash(const DeclNameKey &Key) const {
758 llvm::FoldingSetNodeID ID;
759 ID.AddInteger(Key.Kind);
760
761 switch (Key.Kind) {
762 case DeclarationName::Identifier:
763 case DeclarationName::CXXLiteralOperatorName:
764 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
765 break;
766 case DeclarationName::ObjCZeroArgSelector:
767 case DeclarationName::ObjCOneArgSelector:
768 case DeclarationName::ObjCMultiArgSelector:
769 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
770 break;
771 case DeclarationName::CXXConstructorName:
772 case DeclarationName::CXXDestructorName:
773 case DeclarationName::CXXConversionFunctionName:
774 ID.AddInteger((TypeID)Key.Data);
775 break;
776 case DeclarationName::CXXOperatorName:
777 ID.AddInteger((OverloadedOperatorKind)Key.Data);
778 break;
779 case DeclarationName::CXXUsingDirective:
780 break;
781 }
782
783 return ID.ComputeHash();
784 }
785
786 internal_key_type GetInternalKey(const external_key_type& Name) const {
787 DeclNameKey Key;
788 Key.Kind = Name.getNameKind();
789 switch (Name.getNameKind()) {
790 case DeclarationName::Identifier:
791 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
792 break;
793 case DeclarationName::ObjCZeroArgSelector:
794 case DeclarationName::ObjCOneArgSelector:
795 case DeclarationName::ObjCMultiArgSelector:
796 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
797 break;
798 case DeclarationName::CXXConstructorName:
799 case DeclarationName::CXXDestructorName:
800 case DeclarationName::CXXConversionFunctionName:
801 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
802 break;
803 case DeclarationName::CXXOperatorName:
804 Key.Data = Name.getCXXOverloadedOperator();
805 break;
806 case DeclarationName::CXXLiteralOperatorName:
807 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
808 break;
809 case DeclarationName::CXXUsingDirective:
810 break;
811 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000812
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000813 return Key;
814 }
815
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000816 external_key_type GetExternalKey(const internal_key_type& Key) const {
817 ASTContext *Context = Reader.getContext();
818 switch (Key.Kind) {
819 case DeclarationName::Identifier:
820 return DeclarationName((IdentifierInfo*)Key.Data);
821
822 case DeclarationName::ObjCZeroArgSelector:
823 case DeclarationName::ObjCOneArgSelector:
824 case DeclarationName::ObjCMultiArgSelector:
825 return DeclarationName(Selector(Key.Data));
826
827 case DeclarationName::CXXConstructorName:
828 return Context->DeclarationNames.getCXXConstructorName(
829 Context->getCanonicalType(Reader.GetType(Key.Data)));
830
831 case DeclarationName::CXXDestructorName:
832 return Context->DeclarationNames.getCXXDestructorName(
833 Context->getCanonicalType(Reader.GetType(Key.Data)));
834
835 case DeclarationName::CXXConversionFunctionName:
836 return Context->DeclarationNames.getCXXConversionFunctionName(
837 Context->getCanonicalType(Reader.GetType(Key.Data)));
838
839 case DeclarationName::CXXOperatorName:
840 return Context->DeclarationNames.getCXXOperatorName(
841 (OverloadedOperatorKind)Key.Data);
842
843 case DeclarationName::CXXLiteralOperatorName:
844 return Context->DeclarationNames.getCXXLiteralOperatorName(
845 (IdentifierInfo*)Key.Data);
846
847 case DeclarationName::CXXUsingDirective:
848 return DeclarationName::getUsingDirectiveName();
849 }
850
851 llvm_unreachable("Invalid Name Kind ?");
852 }
853
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000854 static std::pair<unsigned, unsigned>
855 ReadKeyDataLength(const unsigned char*& d) {
856 using namespace clang::io;
857 unsigned KeyLen = ReadUnalignedLE16(d);
858 unsigned DataLen = ReadUnalignedLE16(d);
859 return std::make_pair(KeyLen, DataLen);
860 }
861
862 internal_key_type ReadKey(const unsigned char* d, unsigned) {
863 using namespace clang::io;
864
865 DeclNameKey Key;
866 Key.Kind = (DeclarationName::NameKind)*d++;
867 switch (Key.Kind) {
868 case DeclarationName::Identifier:
869 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
870 break;
871 case DeclarationName::ObjCZeroArgSelector:
872 case DeclarationName::ObjCOneArgSelector:
873 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000874 Key.Data =
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000875 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
876 break;
877 case DeclarationName::CXXConstructorName:
878 case DeclarationName::CXXDestructorName:
879 case DeclarationName::CXXConversionFunctionName:
880 Key.Data = ReadUnalignedLE32(d); // TypeID
881 break;
882 case DeclarationName::CXXOperatorName:
883 Key.Data = *d++; // OverloadedOperatorKind
884 break;
885 case DeclarationName::CXXLiteralOperatorName:
886 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
887 break;
888 case DeclarationName::CXXUsingDirective:
889 break;
890 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000891
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000892 return Key;
893 }
894
895 data_type ReadData(internal_key_type, const unsigned char* d,
896 unsigned DataLen) {
897 using namespace clang::io;
898 unsigned NumDecls = ReadUnalignedLE16(d);
899 DeclID *Start = (DeclID *)d;
900 return std::make_pair(Start, Start + NumDecls);
901 }
902};
903
904} // end anonymous namespace
905
906/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
907typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
908 ASTDeclContextNameLookupTable;
909
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000910bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
911 const std::pair<uint64_t, uint64_t> &Offsets,
912 DeclContextInfo &Info) {
913 SavedStreamPosition SavedPosition(Cursor);
914 // First the lexical decls.
915 if (Offsets.first != 0) {
916 Cursor.JumpToBit(Offsets.first);
917
918 RecordData Record;
919 const char *Blob;
920 unsigned BlobLen;
921 unsigned Code = Cursor.ReadCode();
922 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
923 if (RecCode != DECL_CONTEXT_LEXICAL) {
924 Error("Expected lexical block");
925 return true;
926 }
927
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000928 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
929 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000930 } else {
931 Info.LexicalDecls = 0;
932 Info.NumLexicalDecls = 0;
933 }
934
935 // Now the lookup table.
936 if (Offsets.second != 0) {
937 Cursor.JumpToBit(Offsets.second);
938
939 RecordData Record;
940 const char *Blob;
941 unsigned BlobLen;
942 unsigned Code = Cursor.ReadCode();
943 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
944 if (RecCode != DECL_CONTEXT_VISIBLE) {
945 Error("Expected visible lookup table block");
946 return true;
947 }
948 Info.NameLookupTableData
949 = ASTDeclContextNameLookupTable::Create(
950 (const unsigned char *)Blob + Record[0],
951 (const unsigned char *)Blob,
952 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl9d8f58b2010-08-24 00:50:00 +0000953 } else {
954 Info.NameLookupTableData = 0;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000955 }
956
957 return false;
958}
959
Argyrios Kyrtzidisdaa41f52011-04-25 22:23:56 +0000960void ASTReader::Error(llvm::StringRef Msg) {
961 Error(diag::err_fe_pch_malformed, Msg);
962}
963
964void ASTReader::Error(unsigned DiagID,
965 llvm::StringRef Arg1, llvm::StringRef Arg2) {
966 if (Diags.isDiagnosticInFlight())
967 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
968 else
969 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000970}
971
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000972/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000973bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000974 if (Listener)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000975 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000976 ActualOriginalFileName,
Nick Lewycky36079892011-02-23 21:16:44 +0000977 SuggestedPredefines,
978 FileMgr);
Douglas Gregorc379c072009-04-28 18:58:38 +0000979 return false;
Douglas Gregor92863e42009-04-10 23:10:45 +0000980}
981
Douglas Gregorc5046832009-04-27 18:38:38 +0000982//===----------------------------------------------------------------------===//
983// Source Manager Deserialization
984//===----------------------------------------------------------------------===//
985
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000986/// \brief Read the line table in the source manager block.
Sebastian Redl2c373b92010-10-05 15:59:54 +0000987/// \returns true if there was an error.
988bool ASTReader::ParseLineTable(PerFileData &F,
989 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000990 unsigned Idx = 0;
991 LineTableInfo &LineTable = SourceMgr.getLineTable();
992
993 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +0000994 std::map<int, int> FileIDs;
995 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000996 // Extract the file name
997 unsigned FilenameLen = Record[Idx++];
998 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
999 Idx += FilenameLen;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001000 MaybeAddSystemRootToFilename(Filename);
Mike Stump11289f42009-09-09 15:08:12 +00001001 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregora8854652009-04-13 17:12:42 +00001002 Filename.size());
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001003 }
1004
1005 // Parse the line entries
1006 std::vector<LineEntry> Entries;
1007 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +00001008 int FID = Record[Idx++];
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001009
1010 // Extract the line entries
1011 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +00001012 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001013 Entries.clear();
1014 Entries.reserve(NumEntries);
1015 for (unsigned I = 0; I != NumEntries; ++I) {
1016 unsigned FileOffset = Record[Idx++];
1017 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +00001018 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +00001019 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001020 = (SrcMgr::CharacteristicKind)Record[Idx++];
1021 unsigned IncludeOffset = Record[Idx++];
1022 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1023 FileKind, IncludeOffset));
1024 }
1025 LineTable.AddEntry(FID, Entries);
1026 }
1027
1028 return false;
1029}
1030
Douglas Gregorc5046832009-04-27 18:38:38 +00001031namespace {
1032
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001033class ASTStatData {
Douglas Gregorc5046832009-04-27 18:38:38 +00001034public:
Douglas Gregorc5046832009-04-27 18:38:38 +00001035 const ino_t ino;
1036 const dev_t dev;
1037 const mode_t mode;
1038 const time_t mtime;
1039 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +00001040
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001041 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner2a6fa472010-11-23 19:28:12 +00001042 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregorc5046832009-04-27 18:38:38 +00001043};
1044
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001045class ASTStatLookupTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001046 public:
1047 typedef const char *external_key_type;
1048 typedef const char *internal_key_type;
1049
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001050 typedef ASTStatData data_type;
Douglas Gregorc5046832009-04-27 18:38:38 +00001051
1052 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001053 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001054 }
1055
1056 static internal_key_type GetInternalKey(const char *path) { return path; }
1057
1058 static bool EqualKey(internal_key_type a, internal_key_type b) {
1059 return strcmp(a, b) == 0;
1060 }
1061
1062 static std::pair<unsigned, unsigned>
1063 ReadKeyDataLength(const unsigned char*& d) {
1064 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1065 unsigned DataLen = (unsigned) *d++;
1066 return std::make_pair(KeyLen + 1, DataLen);
1067 }
1068
1069 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1070 return (const char *)d;
1071 }
1072
1073 static data_type ReadData(const internal_key_type, const unsigned char *d,
1074 unsigned /*DataLen*/) {
1075 using namespace clang::io;
1076
Douglas Gregorc5046832009-04-27 18:38:38 +00001077 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1078 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1079 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +00001080 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +00001081 off_t size = (off_t) ReadUnalignedLE64(d);
1082 return data_type(ino, dev, mode, mtime, size);
1083 }
1084};
1085
1086/// \brief stat() cache for precompiled headers.
1087///
1088/// This cache is very similar to the stat cache used by pretokenized
1089/// headers.
Chris Lattner226efd32010-11-23 19:19:34 +00001090class ASTStatCache : public FileSystemStatCache {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001091 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregorc5046832009-04-27 18:38:38 +00001092 CacheTy *Cache;
1093
1094 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +00001095public:
Chris Lattner2a6fa472010-11-23 19:28:12 +00001096 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1097 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +00001098 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1099 Cache = CacheTy::Create(Buckets, Base);
1100 }
1101
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001102 ~ASTStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +00001103
Chris Lattnerdd278432010-11-23 21:17:56 +00001104 LookupResult getStat(const char *Path, struct stat &StatBuf,
1105 int *FileDescriptor) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001106 // Do the lookup for the file's data in the AST file.
Chris Lattner226efd32010-11-23 19:19:34 +00001107 CacheTy::iterator I = Cache->find(Path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001108
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001109 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregorc5046832009-04-27 18:38:38 +00001110 if (I == Cache->end()) {
1111 ++NumStatMisses;
Chris Lattnerdd278432010-11-23 21:17:56 +00001112 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregorc5046832009-04-27 18:38:38 +00001113 }
Mike Stump11289f42009-09-09 15:08:12 +00001114
Douglas Gregorc5046832009-04-27 18:38:38 +00001115 ++NumStatHits;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001116 ASTStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001117
Chris Lattner226efd32010-11-23 19:19:34 +00001118 StatBuf.st_ino = Data.ino;
1119 StatBuf.st_dev = Data.dev;
1120 StatBuf.st_mtime = Data.mtime;
1121 StatBuf.st_mode = Data.mode;
1122 StatBuf.st_size = Data.size;
Chris Lattner8f0583d2010-11-23 20:05:15 +00001123 return CacheExists;
Douglas Gregorc5046832009-04-27 18:38:38 +00001124 }
1125};
1126} // end anonymous namespace
1127
1128
Sebastian Redl393f8b72010-07-19 20:52:06 +00001129/// \brief Read a source manager block
Sebastian Redl2c499f62010-08-18 23:56:43 +00001130ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001131 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +00001132
Sebastian Redl393f8b72010-07-19 20:52:06 +00001133 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001134
Douglas Gregor258ae542009-04-27 06:38:32 +00001135 // Set the source-location entry cursor to the current position in
1136 // the stream. This cursor will be used to read the contents of the
1137 // source manager block initially, and then lazily read
1138 // source-location entries as needed.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001139 SLocEntryCursor = F.Stream;
Douglas Gregor258ae542009-04-27 06:38:32 +00001140
1141 // The stream itself is going to skip over the source manager block.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001142 if (F.Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001143 Error("malformed block record in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001144 return Failure;
1145 }
1146
1147 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001148 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001149 Error("malformed source manager block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001150 return Failure;
1151 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001152
Douglas Gregora7f71a92009-04-10 03:52:48 +00001153 RecordData Record;
1154 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001155 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001156 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001157 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001158 Error("error at end of Source Manager block in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001159 return Failure;
1160 }
Douglas Gregor92863e42009-04-10 23:10:45 +00001161 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001162 }
Mike Stump11289f42009-09-09 15:08:12 +00001163
Douglas Gregora7f71a92009-04-10 03:52:48 +00001164 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1165 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +00001166 SLocEntryCursor.ReadSubBlockID();
1167 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001168 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001169 return Failure;
1170 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001171 continue;
1172 }
Mike Stump11289f42009-09-09 15:08:12 +00001173
Douglas Gregora7f71a92009-04-10 03:52:48 +00001174 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001175 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001176 continue;
1177 }
Mike Stump11289f42009-09-09 15:08:12 +00001178
Douglas Gregora7f71a92009-04-10 03:52:48 +00001179 // Read a record.
1180 const char *BlobStart;
1181 unsigned BlobLen;
1182 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +00001183 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001184 default: // Default behavior: ignore.
1185 break;
1186
Sebastian Redl539c5062010-08-18 23:57:32 +00001187 case SM_LINE_TABLE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00001188 if (ParseLineTable(F, Record))
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001189 return Failure;
Chris Lattner184e65d2009-04-14 23:22:57 +00001190 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00001191
Sebastian Redl539c5062010-08-18 23:57:32 +00001192 case SM_SLOC_FILE_ENTRY:
1193 case SM_SLOC_BUFFER_ENTRY:
1194 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor258ae542009-04-27 06:38:32 +00001195 // Once we hit one of the source location entries, we're done.
1196 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001197 }
1198 }
1199}
1200
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001201/// \brief If a header file is not found at the path that we expect it to be
1202/// and the PCH file was moved from its original location, try to resolve the
1203/// file by assuming that header+PCH were moved together and the header is in
1204/// the same place relative to the PCH.
1205static std::string
1206resolveFileRelativeToOriginalDir(const std::string &Filename,
1207 const std::string &OriginalDir,
1208 const std::string &CurrDir) {
1209 assert(OriginalDir != CurrDir &&
1210 "No point trying to resolve the file if the PCH dir didn't change");
1211 using namespace llvm::sys;
1212 llvm::SmallString<128> filePath(Filename);
1213 fs::make_absolute(filePath);
1214 assert(path::is_absolute(OriginalDir));
1215 llvm::SmallString<128> currPCHPath(CurrDir);
1216
1217 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1218 fileDirE = path::end(path::parent_path(filePath));
1219 path::const_iterator origDirI = path::begin(OriginalDir),
1220 origDirE = path::end(OriginalDir);
1221 // Skip the common path components from filePath and OriginalDir.
1222 while (fileDirI != fileDirE && origDirI != origDirE &&
1223 *fileDirI == *origDirI) {
1224 ++fileDirI;
1225 ++origDirI;
1226 }
1227 for (; origDirI != origDirE; ++origDirI)
1228 path::append(currPCHPath, "..");
1229 path::append(currPCHPath, fileDirI, fileDirE);
1230 path::append(currPCHPath, path::filename(Filename));
1231 return currPCHPath.str();
1232}
1233
Sebastian Redl06750302010-07-20 21:50:20 +00001234/// \brief Get a cursor that's correctly positioned for reading the source
1235/// location entry with the given ID.
Sebastian Redl2c373b92010-10-05 15:59:54 +00001236ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl06750302010-07-20 21:50:20 +00001237 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1238 "SLocCursorForID should only be called for real IDs.");
1239
1240 ID -= 1;
1241 PerFileData *F = 0;
1242 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1243 F = Chain[N - I - 1];
1244 if (ID < F->LocalNumSLocEntries)
1245 break;
1246 ID -= F->LocalNumSLocEntries;
1247 }
1248 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1249
1250 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00001251 return F;
Sebastian Redl06750302010-07-20 21:50:20 +00001252}
1253
Douglas Gregor258ae542009-04-27 06:38:32 +00001254/// \brief Read in the source location entry with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001255ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001256 if (ID == 0)
1257 return Success;
1258
1259 if (ID > TotalNumSLocEntries) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001260 Error("source location entry ID out-of-range for AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001261 return Failure;
1262 }
1263
Sebastian Redl2c373b92010-10-05 15:59:54 +00001264 PerFileData *F = SLocCursorForID(ID);
1265 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001266
Douglas Gregor258ae542009-04-27 06:38:32 +00001267 ++NumSLocEntriesRead;
Douglas Gregor258ae542009-04-27 06:38:32 +00001268 unsigned Code = SLocEntryCursor.ReadCode();
1269 if (Code == llvm::bitc::END_BLOCK ||
1270 Code == llvm::bitc::ENTER_SUBBLOCK ||
1271 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001272 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001273 return Failure;
1274 }
1275
Douglas Gregor258ae542009-04-27 06:38:32 +00001276 RecordData Record;
1277 const char *BlobStart;
1278 unsigned BlobLen;
1279 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1280 default:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001281 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001282 return Failure;
1283
Sebastian Redl539c5062010-08-18 23:57:32 +00001284 case SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001285 std::string Filename(BlobStart, BlobStart + BlobLen);
1286 MaybeAddSystemRootToFilename(Filename);
Chris Lattner5159f612010-11-23 08:35:12 +00001287 const FileEntry *File = FileMgr.getFile(Filename);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001288 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1289 OriginalDir != CurrentDir) {
1290 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1291 OriginalDir,
1292 CurrentDir);
1293 if (!resolved.empty())
1294 File = FileMgr.getFile(resolved);
1295 }
Axel Naumann63fbaed2011-01-27 10:55:51 +00001296 if (File == 0)
1297 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1298 (time_t)Record[5]);
Chris Lattnerd20dc872009-06-15 04:35:16 +00001299 if (File == 0) {
1300 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001301 ErrorStr += Filename;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001302 ErrorStr += "' referenced by AST file";
Chris Lattnerd20dc872009-06-15 04:35:16 +00001303 Error(ErrorStr.c_str());
1304 return Failure;
1305 }
Mike Stump11289f42009-09-09 15:08:12 +00001306
Douglas Gregor09b69892011-02-10 17:09:37 +00001307 if (Record.size() < 6) {
Ted Kremenekabb1ddd2010-03-18 21:23:05 +00001308 Error("source location entry is incorrect");
1309 return Failure;
1310 }
1311
Douglas Gregorce3a8292010-07-27 00:27:13 +00001312 if (!DisableValidation &&
1313 ((off_t)Record[4] != File->getSize()
Douglas Gregor08288f22010-04-09 15:54:22 +00001314#if !defined(LLVM_ON_WIN32)
1315 // In our regression testing, the Windows file system seems to
1316 // have inconsistent modification times that sometimes
1317 // erroneously trigger this error-handling path.
Douglas Gregorce3a8292010-07-27 00:27:13 +00001318 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor08288f22010-04-09 15:54:22 +00001319#endif
Douglas Gregorce3a8292010-07-27 00:27:13 +00001320 )) {
Argyrios Kyrtzidisdaa41f52011-04-25 22:23:56 +00001321 Error(diag::err_fe_pch_file_modified, Filename);
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001322 return Failure;
1323 }
1324
Chris Lattner26b5c192010-11-23 09:19:42 +00001325 FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]),
1326 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor258ae542009-04-27 06:38:32 +00001327 ID, Record[0]);
1328 if (Record[3])
1329 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1330 .setHasLineDirectives();
Douglas Gregor09b69892011-02-10 17:09:37 +00001331
Douglas Gregor258ae542009-04-27 06:38:32 +00001332 break;
1333 }
1334
Sebastian Redl539c5062010-08-18 23:57:32 +00001335 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor258ae542009-04-27 06:38:32 +00001336 const char *Name = BlobStart;
1337 unsigned Offset = Record[0];
1338 unsigned Code = SLocEntryCursor.ReadCode();
1339 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001340 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +00001341 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001342
Sebastian Redl539c5062010-08-18 23:57:32 +00001343 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001344 Error("AST record has invalid code");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001345 return Failure;
1346 }
1347
Douglas Gregor258ae542009-04-27 06:38:32 +00001348 llvm::MemoryBuffer *Buffer
Chris Lattner58c79342010-04-05 22:42:27 +00001349 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1350 Name);
Douglas Gregor258ae542009-04-27 06:38:32 +00001351 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump11289f42009-09-09 15:08:12 +00001352
Douglas Gregore6648fb2009-04-28 20:33:11 +00001353 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001354 PCHPredefinesBlock Block = {
1355 BufferID,
1356 llvm::StringRef(BlobStart, BlobLen - 1)
1357 };
1358 PCHPredefinesBuffers.push_back(Block);
Douglas Gregore6648fb2009-04-28 20:33:11 +00001359 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001360
1361 break;
1362 }
1363
Sebastian Redl539c5062010-08-18 23:57:32 +00001364 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001365 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001366 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001367 ReadSourceLocation(*F, Record[2]),
1368 ReadSourceLocation(*F, Record[3]),
Douglas Gregor258ae542009-04-27 06:38:32 +00001369 Record[4],
1370 ID,
1371 Record[0]);
1372 break;
Mike Stump11289f42009-09-09 15:08:12 +00001373 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001374 }
1375
1376 return Success;
1377}
1378
Chris Lattnere78a6be2009-04-27 01:05:14 +00001379/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1380/// specified cursor. Read the abbreviations that are at the top of the block
1381/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001382bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001383 unsigned BlockID) {
1384 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001385 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001386 return Failure;
1387 }
Mike Stump11289f42009-09-09 15:08:12 +00001388
Chris Lattnere78a6be2009-04-27 01:05:14 +00001389 while (true) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001390 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattnere78a6be2009-04-27 01:05:14 +00001391 unsigned Code = Cursor.ReadCode();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001392
Chris Lattnere78a6be2009-04-27 01:05:14 +00001393 // We expect all abbrevs to be at the start of the block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001394 if (Code != llvm::bitc::DEFINE_ABBREV) {
1395 Cursor.JumpToBit(Offset);
Chris Lattnere78a6be2009-04-27 01:05:14 +00001396 return false;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001397 }
Chris Lattnere78a6be2009-04-27 01:05:14 +00001398 Cursor.ReadAbbrevRecord();
1399 }
1400}
1401
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001402PreprocessedEntity *ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001403 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregor796d76a2010-10-20 22:00:55 +00001404 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump11289f42009-09-09 15:08:12 +00001405
Douglas Gregorc3366a52009-04-21 23:56:24 +00001406 // Keep track of where we are in the stream, then jump back there
1407 // after reading this macro.
1408 SavedStreamPosition SavedPosition(Stream);
1409
1410 Stream.JumpToBit(Offset);
1411 RecordData Record;
1412 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1413 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001414
Douglas Gregorc3366a52009-04-21 23:56:24 +00001415 while (true) {
1416 unsigned Code = Stream.ReadCode();
1417 switch (Code) {
1418 case llvm::bitc::END_BLOCK:
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001419 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001420
1421 case llvm::bitc::ENTER_SUBBLOCK:
1422 // No known subblocks, always skip them.
1423 Stream.ReadSubBlockID();
1424 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001425 Error("malformed block record in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001426 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001427 }
1428 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001429
Douglas Gregorc3366a52009-04-21 23:56:24 +00001430 case llvm::bitc::DEFINE_ABBREV:
1431 Stream.ReadAbbrevRecord();
1432 continue;
1433 default: break;
1434 }
1435
1436 // Read a record.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001437 const char *BlobStart = 0;
1438 unsigned BlobLen = 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001439 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001440 PreprocessorRecordTypes RecType =
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001441 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001442 BlobLen);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001443 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001444 case PP_MACRO_OBJECT_LIKE:
1445 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001446 // If we already have a macro, that means that we've hit the end
1447 // of the definition of the macro we were looking for. We're
1448 // done.
1449 if (Macro)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001450 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001451
1452 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1453 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001454 Error("macro must have a name in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001455 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001456 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00001457 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001458 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001459
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001460 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001461 MI->setIsUsed(isUsed);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001462 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001463
Douglas Gregoraae92242010-03-19 21:51:54 +00001464 unsigned NextIndex = 3;
Sebastian Redl539c5062010-08-18 23:57:32 +00001465 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001466 // Decode function-like macro info.
1467 bool isC99VarArgs = Record[3];
1468 bool isGNUVarArgs = Record[4];
1469 MacroArgs.clear();
1470 unsigned NumArgs = Record[5];
Douglas Gregoraae92242010-03-19 21:51:54 +00001471 NextIndex = 6 + NumArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001472 for (unsigned i = 0; i != NumArgs; ++i)
1473 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1474
1475 // Install function-like macro info.
1476 MI->setIsFunctionLike();
1477 if (isC99VarArgs) MI->setIsC99Varargs();
1478 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001479 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001480 PP->getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001481 }
1482
1483 // Finally, install the macro.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001484 PP->setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001485
1486 // Remember that we saw this macro last so that we add the tokens that
1487 // form its body to it.
1488 Macro = MI;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001489
Douglas Gregoraae92242010-03-19 21:51:54 +00001490 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1491 // We have a macro definition. Load it now.
1492 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1493 getMacroDefinition(Record[NextIndex]));
1494 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001495
Douglas Gregorc3366a52009-04-21 23:56:24 +00001496 ++NumMacrosRead;
1497 break;
1498 }
Mike Stump11289f42009-09-09 15:08:12 +00001499
Sebastian Redl539c5062010-08-18 23:57:32 +00001500 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001501 // If we see a TOKEN before a PP_MACRO_*, then the file is
1502 // erroneous, just pretend we didn't see this.
1503 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001504
Douglas Gregorc3366a52009-04-21 23:56:24 +00001505 Token Tok;
1506 Tok.startToken();
Sebastian Redl2c373b92010-10-05 15:59:54 +00001507 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001508 Tok.setLength(Record[1]);
1509 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1510 Tok.setIdentifierInfo(II);
1511 Tok.setKind((tok::TokenKind)Record[3]);
1512 Tok.setFlag((Token::TokenFlags)Record[4]);
1513 Macro->AddTokenToBody(Tok);
1514 break;
1515 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001516 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001517 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001518
1519 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001520}
1521
Douglas Gregor92a96f52011-02-08 21:58:10 +00001522PreprocessedEntity *ASTReader::LoadPreprocessedEntity(PerFileData &F) {
1523 assert(PP && "Forgot to set Preprocessor ?");
1524 unsigned Code = F.PreprocessorDetailCursor.ReadCode();
1525 switch (Code) {
1526 case llvm::bitc::END_BLOCK:
1527 return 0;
1528
1529 case llvm::bitc::ENTER_SUBBLOCK:
1530 Error("unexpected subblock record in preprocessor detail block");
1531 return 0;
1532
1533 case llvm::bitc::DEFINE_ABBREV:
1534 Error("unexpected abbrevation record in preprocessor detail block");
1535 return 0;
1536
1537 default:
1538 break;
1539 }
1540
1541 if (!PP->getPreprocessingRecord()) {
1542 Error("no preprocessing record");
1543 return 0;
1544 }
1545
1546 // Read the record.
1547 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1548 const char *BlobStart = 0;
1549 unsigned BlobLen = 0;
1550 RecordData Record;
1551 PreprocessorDetailRecordTypes RecType =
1552 (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord(
1553 Code, Record, BlobStart, BlobLen);
1554 switch (RecType) {
1555 case PPD_MACRO_INSTANTIATION: {
1556 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1557 return PE;
1558
1559 MacroInstantiation *MI
1560 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1561 SourceRange(ReadSourceLocation(F, Record[1]),
1562 ReadSourceLocation(F, Record[2])),
1563 getMacroDefinition(Record[4]));
1564 PPRec.SetPreallocatedEntity(Record[0], MI);
1565 return MI;
1566 }
1567
1568 case PPD_MACRO_DEFINITION: {
1569 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1570 return PE;
1571
1572 if (Record[1] > MacroDefinitionsLoaded.size()) {
1573 Error("out-of-bounds macro definition record");
1574 return 0;
1575 }
1576
1577 // Decode the identifier info and then check again; if the macro is
1578 // still defined and associated with the identifier,
1579 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1580 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1581 MacroDefinition *MD
1582 = new (PPRec) MacroDefinition(II,
1583 ReadSourceLocation(F, Record[5]),
1584 SourceRange(
1585 ReadSourceLocation(F, Record[2]),
1586 ReadSourceLocation(F, Record[3])));
1587
1588 PPRec.SetPreallocatedEntity(Record[0], MD);
1589 MacroDefinitionsLoaded[Record[1] - 1] = MD;
1590
1591 if (DeserializationListener)
1592 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1593 }
1594
1595 return MacroDefinitionsLoaded[Record[1] - 1];
1596 }
1597
1598 case PPD_INCLUSION_DIRECTIVE: {
1599 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1600 return PE;
1601
1602 const char *FullFileNameStart = BlobStart + Record[3];
1603 const FileEntry *File
1604 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
1605 BlobLen - Record[3]));
1606
1607 // FIXME: Stable encoding
1608 InclusionDirective::InclusionKind Kind
1609 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1610 InclusionDirective *ID
1611 = new (PPRec) InclusionDirective(PPRec, Kind,
1612 llvm::StringRef(BlobStart, Record[3]),
1613 Record[4],
1614 File,
1615 SourceRange(ReadSourceLocation(F, Record[1]),
1616 ReadSourceLocation(F, Record[2])));
1617 PPRec.SetPreallocatedEntity(Record[0], ID);
1618 return ID;
1619 }
1620 }
1621
1622 Error("invalid offset in preprocessor detail block");
1623 return 0;
1624}
1625
Douglas Gregor09b69892011-02-10 17:09:37 +00001626namespace {
1627 /// \brief Trait class used to search the on-disk hash table containing all of
1628 /// the header search information.
1629 ///
1630 /// The on-disk hash table contains a mapping from each header path to
1631 /// information about that header (how many times it has been included, its
1632 /// controlling macro, etc.). Note that we actually hash based on the
1633 /// filename, and support "deep" comparisons of file names based on current
1634 /// inode numbers, so that the search can cope with non-normalized path names
1635 /// and symlinks.
1636 class HeaderFileInfoTrait {
1637 const char *SearchPath;
1638 struct stat SearchPathStatBuf;
1639 llvm::Optional<int> SearchPathStatResult;
1640
1641 int StatSimpleCache(const char *Path, struct stat *StatBuf) {
1642 if (Path == SearchPath) {
1643 if (!SearchPathStatResult)
1644 SearchPathStatResult = stat(Path, &SearchPathStatBuf);
1645
1646 *StatBuf = SearchPathStatBuf;
1647 return *SearchPathStatResult;
1648 }
1649
1650 return stat(Path, StatBuf);
1651 }
1652
1653 public:
1654 typedef const char *external_key_type;
1655 typedef const char *internal_key_type;
1656
1657 typedef HeaderFileInfo data_type;
1658
1659 HeaderFileInfoTrait(const char *SearchPath = 0) : SearchPath(SearchPath) { }
1660
1661 static unsigned ComputeHash(const char *path) {
1662 return llvm::HashString(llvm::sys::path::filename(path));
1663 }
1664
1665 static internal_key_type GetInternalKey(const char *path) { return path; }
1666
1667 bool EqualKey(internal_key_type a, internal_key_type b) {
1668 if (strcmp(a, b) == 0)
1669 return true;
1670
1671 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1672 return false;
1673
1674 // The file names match, but the path names don't. stat() the files to
1675 // see if they are the same.
1676 struct stat StatBufA, StatBufB;
1677 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1678 return false;
1679
1680 return StatBufA.st_ino == StatBufB.st_ino;
1681 }
1682
1683 static std::pair<unsigned, unsigned>
1684 ReadKeyDataLength(const unsigned char*& d) {
1685 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1686 unsigned DataLen = (unsigned) *d++;
1687 return std::make_pair(KeyLen + 1, DataLen);
1688 }
1689
1690 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1691 return (const char *)d;
1692 }
1693
1694 static data_type ReadData(const internal_key_type, const unsigned char *d,
1695 unsigned DataLen) {
1696 const unsigned char *End = d + DataLen;
1697 using namespace clang::io;
1698 HeaderFileInfo HFI;
1699 unsigned Flags = *d++;
Douglas Gregor37aa4932011-05-04 00:14:37 +00001700 HFI.isImport = (Flags >> 4) & 0x01;
1701 HFI.isPragmaOnce = (Flags >> 3) & 0x01;
Douglas Gregor09b69892011-02-10 17:09:37 +00001702 HFI.DirInfo = (Flags >> 1) & 0x03;
1703 HFI.Resolved = Flags & 0x01;
1704 HFI.NumIncludes = ReadUnalignedLE16(d);
1705 HFI.ControllingMacroID = ReadUnalignedLE32(d);
1706 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1707 (void)End;
1708
1709 // This HeaderFileInfo was externally loaded.
1710 HFI.External = true;
1711 return HFI;
1712 }
1713 };
1714}
1715
1716/// \brief The on-disk hash table used for the global method pool.
1717typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
1718 HeaderFileInfoLookupTable;
1719
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001720void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1721 uint64_t Offset) {
1722 // Note that this identifier has a macro definition.
1723 II->setHasMacroDefinition(true);
1724
1725 // Adjust the offset based on our position in the chain.
1726 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1727 if (Chain[I] == &F)
1728 break;
1729
1730 Offset += Chain[I]->SizeInBits;
1731 }
1732
1733 UnreadMacroRecordOffsets[II] = Offset;
1734}
1735
Sebastian Redl2c499f62010-08-18 23:56:43 +00001736void ASTReader::ReadDefinedMacros() {
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001737 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001738 PerFileData &F = *Chain[N - I - 1];
1739 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001740
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001741 // If there was no preprocessor block, skip this file.
1742 if (!MacroCursor.getBitStreamReader())
1743 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001744
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001745 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001746 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001747
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001748 RecordData Record;
1749 while (true) {
1750 unsigned Code = Cursor.ReadCode();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001751 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001752 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001753
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001754 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1755 // No known subblocks, always skip them.
1756 Cursor.ReadSubBlockID();
1757 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001758 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001759 return;
1760 }
1761 continue;
1762 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001763
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001764 if (Code == llvm::bitc::DEFINE_ABBREV) {
1765 Cursor.ReadAbbrevRecord();
1766 continue;
1767 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001768
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001769 // Read a record.
1770 const char *BlobStart;
1771 unsigned BlobLen;
1772 Record.clear();
1773 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1774 default: // Default behavior: ignore.
1775 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001776
Sebastian Redl539c5062010-08-18 23:57:32 +00001777 case PP_MACRO_OBJECT_LIKE:
1778 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001779 DecodeIdentifierInfo(Record[0]);
1780 break;
1781
Sebastian Redl539c5062010-08-18 23:57:32 +00001782 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001783 // Ignore tokens.
1784 break;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001785 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001786 }
1787 }
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001788
1789 // Drain the unread macro-record offsets map.
1790 while (!UnreadMacroRecordOffsets.empty())
1791 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1792}
1793
1794void ASTReader::LoadMacroDefinition(
1795 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1796 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1797 PerFileData *F = 0;
1798 uint64_t Offset = Pos->second;
1799 UnreadMacroRecordOffsets.erase(Pos);
1800
1801 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1802 if (Offset < Chain[I]->SizeInBits) {
1803 F = Chain[I];
1804 break;
1805 }
1806
1807 Offset -= Chain[I]->SizeInBits;
1808 }
1809 if (!F) {
1810 Error("Malformed macro record offset");
1811 return;
1812 }
1813
1814 ReadMacroRecord(*F, Offset);
1815}
1816
1817void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1818 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1819 = UnreadMacroRecordOffsets.find(II);
1820 LoadMacroDefinition(Pos);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001821}
1822
Sebastian Redl50e26582010-09-15 19:54:06 +00001823MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor91096292010-10-02 19:29:26 +00001824 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregoraae92242010-03-19 21:51:54 +00001825 return 0;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001826
Douglas Gregor91096292010-10-02 19:29:26 +00001827 if (!MacroDefinitionsLoaded[ID - 1]) {
1828 unsigned Index = ID - 1;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001829 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1830 PerFileData &F = *Chain[N - I - 1];
1831 if (Index < F.LocalNumMacroDefinitions) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001832 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
1833 F.PreprocessorDetailCursor.JumpToBit(F.MacroDefinitionOffsets[Index]);
1834 LoadPreprocessedEntity(F);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001835 break;
1836 }
1837 Index -= F.LocalNumMacroDefinitions;
1838 }
Douglas Gregor91096292010-10-02 19:29:26 +00001839 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001840 }
1841
Douglas Gregor91096292010-10-02 19:29:26 +00001842 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001843}
1844
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00001845const FileEntry *ASTReader::getFileEntry(llvm::StringRef filenameStrRef) {
1846 std::string Filename = filenameStrRef;
1847 MaybeAddSystemRootToFilename(Filename);
1848 const FileEntry *File = FileMgr.getFile(Filename);
1849 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1850 OriginalDir != CurrentDir) {
1851 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1852 OriginalDir,
1853 CurrentDir);
1854 if (!resolved.empty())
1855 File = FileMgr.getFile(resolved);
1856 }
1857
1858 return File;
1859}
1860
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001861/// \brief If we are loading a relocatable PCH file, and the filename is
1862/// not an absolute path, add the system root to the beginning of the file
1863/// name.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001864void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001865 // If this is not a relocatable PCH file, there's nothing to do.
1866 if (!RelocatablePCH)
1867 return;
Mike Stump11289f42009-09-09 15:08:12 +00001868
Michael J. Spencerf28df4c2010-12-17 21:22:22 +00001869 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001870 return;
1871
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001872 if (isysroot == 0) {
1873 // If no system root was given, default to '/'
1874 Filename.insert(Filename.begin(), '/');
1875 return;
1876 }
Mike Stump11289f42009-09-09 15:08:12 +00001877
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001878 unsigned Length = strlen(isysroot);
1879 if (isysroot[Length - 1] != '/')
1880 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001881
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001882 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1883}
1884
Sebastian Redl2c499f62010-08-18 23:56:43 +00001885ASTReader::ASTReadResult
Sebastian Redl3e31c722010-08-18 23:56:56 +00001886ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001887 llvm::BitstreamCursor &Stream = F.Stream;
1888
Sebastian Redl539c5062010-08-18 23:57:32 +00001889 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001890 Error("malformed block record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001891 return Failure;
1892 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001893
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001894 // Read all of the records and blocks for the ASt file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001895 RecordData Record;
Sebastian Redl393f8b72010-07-19 20:52:06 +00001896 bool First = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001897 while (!Stream.AtEndOfStream()) {
1898 unsigned Code = Stream.ReadCode();
1899 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001900 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001901 Error("error at end of module block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001902 return Failure;
1903 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001904
Douglas Gregor55abb232009-04-10 20:39:37 +00001905 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001906 }
1907
1908 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1909 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001910 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001911 // We lazily load the decls block, but we want to set up the
1912 // DeclsCursor cursor to point into it. Clone our current bitcode
1913 // cursor to it, enter the block and read the abbrevs in that block.
1914 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001915 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001916 if (Stream.SkipBlock() || // Skip with the main cursor.
1917 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001918 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001919 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001920 return Failure;
1921 }
1922 break;
Mike Stump11289f42009-09-09 15:08:12 +00001923
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00001924 case DECL_UPDATES_BLOCK_ID:
1925 if (Stream.SkipBlock()) {
1926 Error("malformed block record in AST file");
1927 return Failure;
1928 }
1929 break;
1930
Sebastian Redl539c5062010-08-18 23:57:32 +00001931 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001932 F.MacroCursor = Stream;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001933 if (PP)
1934 PP->setExternalSource(this);
1935
Douglas Gregor796d76a2010-10-20 22:00:55 +00001936 if (Stream.SkipBlock() ||
1937 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001938 Error("malformed block record in AST file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001939 return Failure;
1940 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001941 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001942 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001943
Douglas Gregor92a96f52011-02-08 21:58:10 +00001944 case PREPROCESSOR_DETAIL_BLOCK_ID:
1945 F.PreprocessorDetailCursor = Stream;
1946 if (Stream.SkipBlock() ||
1947 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1948 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1949 Error("malformed preprocessor detail record in AST file");
1950 return Failure;
1951 }
1952 F.PreprocessorDetailStartOffset
1953 = F.PreprocessorDetailCursor.GetCurrentBitNo();
1954 break;
1955
Sebastian Redl539c5062010-08-18 23:57:32 +00001956 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001957 switch (ReadSourceManagerBlock(F)) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001958 case Success:
1959 break;
1960
1961 case Failure:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001962 Error("malformed source manager block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001963 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001964
1965 case IgnorePCH:
1966 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001967 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001968 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001969 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00001970 First = false;
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001971 continue;
1972 }
1973
1974 if (Code == llvm::bitc::DEFINE_ABBREV) {
1975 Stream.ReadAbbrevRecord();
1976 continue;
1977 }
1978
1979 // Read and process a record.
1980 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001981 const char *BlobStart = 0;
1982 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001983 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001984 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001985 default: // Default behavior: ignore.
1986 break;
1987
Sebastian Redl539c5062010-08-18 23:57:32 +00001988 case METADATA: {
1989 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1990 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001991 : diag::warn_pch_version_too_new);
1992 return IgnorePCH;
1993 }
1994
1995 RelocatablePCH = Record[4];
1996 if (Listener) {
1997 std::string TargetTriple(BlobStart, BlobLen);
1998 if (Listener->ReadTargetTriple(TargetTriple))
1999 return IgnorePCH;
2000 }
2001 break;
2002 }
2003
Sebastian Redl539c5062010-08-18 23:57:32 +00002004 case CHAINED_METADATA: {
Sebastian Redl393f8b72010-07-19 20:52:06 +00002005 if (!First) {
2006 Error("CHAINED_METADATA is not first record in block");
2007 return Failure;
2008 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002009 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2010 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002011 : diag::warn_pch_version_too_new);
2012 return IgnorePCH;
2013 }
2014
Sebastian Redl009e7f22010-10-05 16:15:19 +00002015 // Load the chained file, which is always a PCH file.
2016 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002017 case Failure: return Failure;
2018 // If we have to ignore the dependency, we'll have to ignore this too.
2019 case IgnorePCH: return IgnorePCH;
2020 case Success: break;
2021 }
2022 break;
2023 }
2024
Sebastian Redl539c5062010-08-18 23:57:32 +00002025 case TYPE_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00002026 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002027 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002028 return Failure;
2029 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002030 F.TypeOffsets = (const uint32_t *)BlobStart;
2031 F.LocalNumTypes = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002032 break;
2033
Sebastian Redl539c5062010-08-18 23:57:32 +00002034 case DECL_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00002035 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002036 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002037 return Failure;
2038 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002039 F.DeclOffsets = (const uint32_t *)BlobStart;
2040 F.LocalNumDecls = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002041 break;
Douglas Gregor55abb232009-04-10 20:39:37 +00002042
Sebastian Redl539c5062010-08-18 23:57:32 +00002043 case TU_UPDATE_LEXICAL: {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002044 DeclContextInfo Info = {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002045 /* No visible information */ 0,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002046 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
2047 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002048 };
Douglas Gregoraa433012010-10-01 01:18:02 +00002049 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
2050 .push_back(Info);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002051 break;
2052 }
2053
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002054 case UPDATE_VISIBLE: {
2055 serialization::DeclID ID = Record[0];
2056 void *Table = ASTDeclContextNameLookupTable::Create(
2057 (const unsigned char *)BlobStart + Record[1],
2058 (const unsigned char *)BlobStart,
2059 ASTDeclContextNameLookupTrait(*this));
Douglas Gregoraa433012010-10-01 01:18:02 +00002060 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002061 DeclContextInfo Info = {
2062 Table, /* No lexical inforamtion */ 0, 0
2063 };
2064 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
2065 } else
2066 PendingVisibleUpdates[ID].push_back(Table);
2067 break;
2068 }
2069
Sebastian Redl539c5062010-08-18 23:57:32 +00002070 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002071 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
2072 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002073 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002074 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
2075 Latest > FirstLatestDeclIDs[First]) &&
2076 "The new latest is supposed to come after the previous latest");
2077 FirstLatestDeclIDs[First] = Latest;
2078 }
2079 break;
2080 }
2081
Sebastian Redl539c5062010-08-18 23:57:32 +00002082 case LANGUAGE_OPTIONS:
Douglas Gregorce3a8292010-07-27 00:27:13 +00002083 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor55abb232009-04-10 20:39:37 +00002084 return IgnorePCH;
2085 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00002086
Sebastian Redl539c5062010-08-18 23:57:32 +00002087 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00002088 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00002089 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00002090 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002091 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00002092 (const unsigned char *)F.IdentifierTableData + Record[0],
2093 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl2c373b92010-10-05 15:59:54 +00002094 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002095 if (PP)
2096 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00002097 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002098 break;
2099
Sebastian Redl539c5062010-08-18 23:57:32 +00002100 case IDENTIFIER_OFFSET:
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002101 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002102 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002103 return Failure;
2104 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002105 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2106 F.LocalNumIdentifiers = Record[0];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002107 break;
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002108
Sebastian Redl539c5062010-08-18 23:57:32 +00002109 case EXTERNAL_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002110 // Optimization for the first block.
2111 if (ExternalDefinitions.empty())
2112 ExternalDefinitions.swap(Record);
2113 else
2114 ExternalDefinitions.insert(ExternalDefinitions.end(),
2115 Record.begin(), Record.end());
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002116 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00002117
Sebastian Redl539c5062010-08-18 23:57:32 +00002118 case SPECIAL_TYPES:
Sebastian Redlb293a452010-07-20 21:20:32 +00002119 // Optimization for the first block
2120 if (SpecialTypes.empty())
2121 SpecialTypes.swap(Record);
2122 else
2123 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002124 break;
2125
Sebastian Redl539c5062010-08-18 23:57:32 +00002126 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002127 TotalNumStatements += Record[0];
2128 TotalNumMacros += Record[1];
2129 TotalLexicalDeclContexts += Record[2];
2130 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00002131 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002132
Sebastian Redl539c5062010-08-18 23:57:32 +00002133 case UNUSED_FILESCOPED_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002134 // Optimization for the first block.
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002135 if (UnusedFileScopedDecls.empty())
2136 UnusedFileScopedDecls.swap(Record);
Sebastian Redlb293a452010-07-20 21:20:32 +00002137 else
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002138 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
2139 Record.begin(), Record.end());
Tanya Lattner90073802010-02-12 00:07:30 +00002140 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002141
Alexis Hunt27a761d2011-05-04 23:29:54 +00002142 case DELEGATING_CTORS:
2143 // Optimization for the first block.
2144 if (DelegatingCtorDecls.empty())
2145 DelegatingCtorDecls.swap(Record);
2146 else
2147 DelegatingCtorDecls.insert(DelegatingCtorDecls.end(),
2148 Record.begin(), Record.end());
2149 break;
2150
Sebastian Redl539c5062010-08-18 23:57:32 +00002151 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002152 // Later blocks overwrite earlier ones.
2153 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002154 break;
2155
Sebastian Redl539c5062010-08-18 23:57:32 +00002156 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002157 // Optimization for the first block.
2158 if (LocallyScopedExternalDecls.empty())
2159 LocallyScopedExternalDecls.swap(Record);
2160 else
2161 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
2162 Record.begin(), Record.end());
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002163 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002164
Sebastian Redl539c5062010-08-18 23:57:32 +00002165 case SELECTOR_OFFSETS:
Sebastian Redla19a67f2010-08-03 21:58:15 +00002166 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00002167 F.LocalNumSelectors = Record[0];
Douglas Gregor95c13f52009-04-25 17:48:32 +00002168 break;
2169
Sebastian Redl539c5062010-08-18 23:57:32 +00002170 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00002171 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002172 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00002173 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002174 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00002175 F.SelectorLookupTableData + Record[0],
2176 F.SelectorLookupTableData,
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002177 ASTSelectorLookupTrait(*this));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002178 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002179 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00002180
Sebastian Redl96371b42010-09-22 00:42:30 +00002181 case REFERENCED_SELECTOR_POOL:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002182 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002183 break;
2184
Sebastian Redl539c5062010-08-18 23:57:32 +00002185 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002186 if (!Record.empty() && Listener)
2187 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002188 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002189
Sebastian Redl539c5062010-08-18 23:57:32 +00002190 case SOURCE_LOCATION_OFFSETS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002191 F.SLocOffsets = (const uint32_t *)BlobStart;
2192 F.LocalNumSLocEntries = Record[0];
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002193 F.LocalSLocSize = Record[1];
Douglas Gregor258ae542009-04-27 06:38:32 +00002194 break;
2195
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002196 case FILE_SOURCE_LOCATION_OFFSETS:
2197 F.SLocFileOffsets = (const uint32_t *)BlobStart;
2198 F.LocalNumSLocFileEntries = Record[0];
2199 break;
2200
Sebastian Redl539c5062010-08-18 23:57:32 +00002201 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl96371b42010-09-22 00:42:30 +00002202 if (PreloadSLocEntries.empty())
2203 PreloadSLocEntries.swap(Record);
2204 else
2205 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2206 Record.begin(), Record.end());
Douglas Gregor258ae542009-04-27 06:38:32 +00002207 break;
Douglas Gregorc5046832009-04-27 18:38:38 +00002208
Sebastian Redl539c5062010-08-18 23:57:32 +00002209 case STAT_CACHE: {
Douglas Gregor606c4ac2011-02-05 19:42:43 +00002210 if (!DisableStatCache) {
2211 ASTStatCache *MyStatCache =
2212 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2213 (const unsigned char *)BlobStart,
2214 NumStatHits, NumStatMisses);
2215 FileMgr.addStatCache(MyStatCache);
2216 F.StatCache = MyStatCache;
2217 }
Douglas Gregorc5046832009-04-27 18:38:38 +00002218 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00002219 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002220
Sebastian Redl539c5062010-08-18 23:57:32 +00002221 case EXT_VECTOR_DECLS:
Sebastian Redl04f5c312010-07-28 21:38:49 +00002222 // Optimization for the first block.
2223 if (ExtVectorDecls.empty())
2224 ExtVectorDecls.swap(Record);
2225 else
2226 ExtVectorDecls.insert(ExtVectorDecls.end(),
2227 Record.begin(), Record.end());
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002228 break;
2229
Sebastian Redl539c5062010-08-18 23:57:32 +00002230 case VTABLE_USES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002231 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002232 VTableUses.swap(Record);
2233 break;
2234
Sebastian Redl539c5062010-08-18 23:57:32 +00002235 case DYNAMIC_CLASSES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002236 // Optimization for the first block.
2237 if (DynamicClasses.empty())
2238 DynamicClasses.swap(Record);
2239 else
2240 DynamicClasses.insert(DynamicClasses.end(),
2241 Record.begin(), Record.end());
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002242 break;
2243
Sebastian Redl539c5062010-08-18 23:57:32 +00002244 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002245 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002246 break;
2247
Sebastian Redl539c5062010-08-18 23:57:32 +00002248 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002249 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002250 SemaDeclRefs.swap(Record);
2251 break;
2252
Sebastian Redl539c5062010-08-18 23:57:32 +00002253 case ORIGINAL_FILE_NAME:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002254 // The primary AST will be the last to get here, so it will be the one
Sebastian Redlb293a452010-07-20 21:20:32 +00002255 // that's used.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00002256 ActualOriginalFileName.assign(BlobStart, BlobLen);
2257 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002258 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00002259 break;
Mike Stump11289f42009-09-09 15:08:12 +00002260
Douglas Gregora3b20262011-05-06 21:43:30 +00002261 case ORIGINAL_FILE_ID:
2262 OriginalFileID = FileID::get(Record[0]);
2263 break;
2264
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002265 case ORIGINAL_PCH_DIR:
2266 // The primary AST will be the last to get here, so it will be the one
2267 // that's used.
2268 OriginalDir.assign(BlobStart, BlobLen);
2269 break;
2270
Sebastian Redl539c5062010-08-18 23:57:32 +00002271 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00002272 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002273 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2274 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2275 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregord54f3a12009-10-05 21:07:28 +00002276 return IgnorePCH;
2277 }
2278 break;
2279 }
Sebastian Redlfa061442010-07-21 20:07:32 +00002280
Sebastian Redl539c5062010-08-18 23:57:32 +00002281 case MACRO_DEFINITION_OFFSETS:
Sebastian Redlfa061442010-07-21 20:07:32 +00002282 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2283 F.NumPreallocatedPreprocessingEntities = Record[0];
2284 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregoraae92242010-03-19 21:51:54 +00002285 break;
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002286
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002287 case DECL_UPDATE_OFFSETS: {
2288 if (Record.size() % 2 != 0) {
2289 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2290 return Failure;
2291 }
2292 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2293 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2294 .push_back(std::make_pair(&F, Record[I+1]));
2295 break;
2296 }
2297
Sebastian Redl539c5062010-08-18 23:57:32 +00002298 case DECL_REPLACEMENTS: {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002299 if (Record.size() % 2 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002300 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002301 return Failure;
2302 }
2303 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl539c5062010-08-18 23:57:32 +00002304 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002305 std::make_pair(&F, Record[I+1]);
2306 break;
2307 }
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002308
2309 case CXX_BASE_SPECIFIER_OFFSETS: {
2310 if (F.LocalNumCXXBaseSpecifiers != 0) {
2311 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2312 return Failure;
2313 }
2314
2315 F.LocalNumCXXBaseSpecifiers = Record[0];
2316 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2317 break;
2318 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002319
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002320 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002321 if (Record.size() % 2 != 0) {
2322 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2323 return Failure;
2324 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002325 if (PragmaDiagMappings.empty())
2326 PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002327 else
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002328 PragmaDiagMappings.insert(PragmaDiagMappings.end(),
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002329 Record.begin(), Record.end());
2330 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002331
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002332 case CUDA_SPECIAL_DECL_REFS:
2333 // Later tables overwrite earlier ones.
2334 CUDASpecialDeclRefs.swap(Record);
2335 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002336
2337 case HEADER_SEARCH_TABLE:
2338 F.HeaderFileInfoTableData = BlobStart;
2339 F.LocalNumHeaderFileInfos = Record[1];
2340 if (Record[0]) {
2341 F.HeaderFileInfoTable
2342 = HeaderFileInfoLookupTable::Create(
2343 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2344 (const unsigned char *)F.HeaderFileInfoTableData);
2345 if (PP)
2346 PP->getHeaderSearchInfo().SetExternalSource(this);
2347 }
2348 break;
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002349
2350 case FP_PRAGMA_OPTIONS:
2351 // Later tables overwrite earlier ones.
2352 FPPragmaOptions.swap(Record);
2353 break;
2354
2355 case OPENCL_EXTENSIONS:
2356 // Later tables overwrite earlier ones.
2357 OpenCLExtensions.swap(Record);
2358 break;
Alexis Hunt27a761d2011-05-04 23:29:54 +00002359
2360 case TENTATIVE_DEFINITIONS:
2361 // Optimization for the first block.
2362 if (TentativeDefinitions.empty())
2363 TentativeDefinitions.swap(Record);
2364 else
2365 TentativeDefinitions.insert(TentativeDefinitions.end(),
2366 Record.begin(), Record.end());
2367 break;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002368 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00002369 First = false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002370 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002371 Error("premature end of bitstream in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002372 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002373}
2374
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002375ASTReader::ASTReadResult ASTReader::validateFileEntries() {
2376 for (unsigned CI = 0, CN = Chain.size(); CI != CN; ++CI) {
2377 PerFileData *F = Chain[CI];
2378 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
2379
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002380 for (unsigned i = 0, e = F->LocalNumSLocFileEntries; i != e; ++i) {
2381 SLocEntryCursor.JumpToBit(F->SLocFileOffsets[i]);
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002382 unsigned Code = SLocEntryCursor.ReadCode();
2383 if (Code == llvm::bitc::END_BLOCK ||
2384 Code == llvm::bitc::ENTER_SUBBLOCK ||
2385 Code == llvm::bitc::DEFINE_ABBREV) {
2386 Error("incorrectly-formatted source location entry in AST file");
2387 return Failure;
2388 }
2389
2390 RecordData Record;
2391 const char *BlobStart;
2392 unsigned BlobLen;
2393 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2394 default:
2395 Error("incorrectly-formatted source location entry in AST file");
2396 return Failure;
2397
2398 case SM_SLOC_FILE_ENTRY: {
2399 llvm::StringRef Filename(BlobStart, BlobLen);
2400 const FileEntry *File = getFileEntry(Filename);
2401
2402 if (File == 0) {
2403 std::string ErrorStr = "could not find file '";
2404 ErrorStr += Filename;
2405 ErrorStr += "' referenced by AST file";
2406 Error(ErrorStr.c_str());
2407 return IgnorePCH;
2408 }
2409
2410 if (Record.size() < 6) {
2411 Error("source location entry is incorrect");
2412 return Failure;
2413 }
2414
2415 // The stat info from the FileEntry came from the cached stat
2416 // info of the PCH, so we cannot trust it.
2417 struct stat StatBuf;
2418 if (::stat(File->getName(), &StatBuf) != 0) {
2419 StatBuf.st_size = File->getSize();
2420 StatBuf.st_mtime = File->getModificationTime();
2421 }
2422
2423 if (((off_t)Record[4] != StatBuf.st_size
2424#if !defined(LLVM_ON_WIN32)
2425 // In our regression testing, the Windows file system seems to
2426 // have inconsistent modification times that sometimes
2427 // erroneously trigger this error-handling path.
2428 || (time_t)Record[5] != StatBuf.st_mtime
2429#endif
2430 )) {
2431 Error(diag::err_fe_pch_file_modified, Filename);
2432 return IgnorePCH;
2433 }
2434
2435 break;
2436 }
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002437 }
2438 }
2439 }
2440
2441 return Success;
2442}
2443
Sebastian Redl009e7f22010-10-05 16:15:19 +00002444ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2445 ASTFileType Type) {
2446 switch(ReadASTCore(FileName, Type)) {
Sebastian Redl2abc0382010-07-16 20:41:52 +00002447 case Failure: return Failure;
2448 case IgnorePCH: return IgnorePCH;
2449 case Success: break;
2450 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002451
2452 // Here comes stuff that we only do once the entire chain is loaded.
2453
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002454 if (!DisableValidation) {
2455 switch(validateFileEntries()) {
2456 case Failure: return Failure;
2457 case IgnorePCH: return IgnorePCH;
2458 case Success: break;
2459 }
2460 }
2461
Sebastian Redl96371b42010-09-22 00:42:30 +00002462 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redlfa061442010-07-21 20:07:32 +00002463 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redlada023c2010-08-04 20:40:17 +00002464 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2465 TotalNumSelectors = 0;
Sebastian Redl9e687992010-07-19 22:06:55 +00002466 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl96371b42010-09-22 00:42:30 +00002467 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002468 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002469 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl9e687992010-07-19 22:06:55 +00002470 TotalNumTypes += Chain[I]->LocalNumTypes;
2471 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redlfa061442010-07-21 20:07:32 +00002472 TotalNumPreallocatedPreprocessingEntities +=
2473 Chain[I]->NumPreallocatedPreprocessingEntities;
2474 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redlada023c2010-08-04 20:40:17 +00002475 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl9e687992010-07-19 22:06:55 +00002476 }
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002477 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002478 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl9e687992010-07-19 22:06:55 +00002479 TypesLoaded.resize(TotalNumTypes);
2480 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redlfa061442010-07-21 20:07:32 +00002481 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2482 if (PP) {
2483 if (TotalNumIdentifiers > 0)
2484 PP->getHeaderSearchInfo().SetExternalLookup(this);
2485 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2486 if (!PP->getPreprocessingRecord())
Douglas Gregor998caea2011-05-06 16:33:08 +00002487 PP->createPreprocessingRecord(true);
Sebastian Redlfa061442010-07-21 20:07:32 +00002488 PP->getPreprocessingRecord()->SetExternalSource(*this,
2489 TotalNumPreallocatedPreprocessingEntities);
2490 }
2491 }
Sebastian Redlada023c2010-08-04 20:40:17 +00002492 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl96371b42010-09-22 00:42:30 +00002493 // Preload SLocEntries.
2494 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2495 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2496 if (Result != Success)
2497 return Result;
2498 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002499
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002500 // Check the predefines buffers.
Douglas Gregorce3a8292010-07-27 00:27:13 +00002501 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002502 return IgnorePCH;
2503
2504 if (PP) {
2505 // Initialization of keywords and pragmas occurs before the
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002506 // AST file is read, so there may be some identifiers that were
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002507 // loaded into the IdentifierTable before we intercepted the
2508 // creation of identifiers. Iterate through the list of known
2509 // identifiers and determine whether we have to establish
2510 // preprocessor definitions or top-level identifier declaration
2511 // chains for those identifiers.
2512 //
2513 // We copy the IdentifierInfo pointers to a small vector first,
2514 // since de-serializing declarations or macro definitions can add
2515 // new entries into the identifier table, invalidating the
2516 // iterators.
2517 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2518 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2519 IdEnd = PP->getIdentifierTable().end();
2520 Id != IdEnd; ++Id)
2521 Identifiers.push_back(Id->second);
Sebastian Redlfa061442010-07-21 20:07:32 +00002522 // We need to search the tables in all files.
Sebastian Redlfa061442010-07-21 20:07:32 +00002523 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002524 ASTIdentifierLookupTable *IdTable
2525 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2526 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl5c415f32010-07-22 17:01:13 +00002527 // ones.
2528 if (!IdTable)
2529 continue;
Sebastian Redlfa061442010-07-21 20:07:32 +00002530 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2531 IdentifierInfo *II = Identifiers[I];
2532 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00002533 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redlfa061442010-07-21 20:07:32 +00002534 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002535 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redlb293a452010-07-20 21:20:32 +00002536 if (Pos == IdTable->end())
2537 continue;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002538
Sebastian Redlb293a452010-07-20 21:20:32 +00002539 // Dereferencing the iterator has the effect of populating the
2540 // IdentifierInfo node with the various declarations it needs.
2541 (void)*Pos;
2542 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002543 }
2544 }
2545
2546 if (Context)
2547 InitializeContext(*Context);
2548
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002549 if (DeserializationListener)
2550 DeserializationListener->ReaderInitialized(this);
2551
Douglas Gregor936a5b42010-11-30 05:23:00 +00002552 // If this AST file is a precompiled preamble, then set the main file ID of
2553 // the source manager to the file source file from which the preamble was
2554 // built. This is the only valid way to use a precompiled preamble.
2555 if (Type == Preamble) {
Douglas Gregora3b20262011-05-06 21:43:30 +00002556 if (OriginalFileID.isInvalid()) {
2557 SourceLocation Loc
2558 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2559 if (Loc.isValid())
2560 OriginalFileID = SourceMgr.getDecomposedLoc(Loc).first;
Douglas Gregor936a5b42010-11-30 05:23:00 +00002561 }
Douglas Gregora3b20262011-05-06 21:43:30 +00002562
2563 if (!OriginalFileID.isInvalid())
2564 SourceMgr.SetPreambleFileID(OriginalFileID);
Douglas Gregor936a5b42010-11-30 05:23:00 +00002565 }
2566
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002567 return Success;
2568}
2569
Sebastian Redl009e7f22010-10-05 16:15:19 +00002570ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2571 ASTFileType Type) {
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002572 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl009e7f22010-10-05 16:15:19 +00002573 Chain.push_back(new PerFileData(Type));
Sebastian Redl34522812010-07-16 17:50:48 +00002574 PerFileData &F = *Chain.back();
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002575 if (Prev)
2576 Prev->NextInSource = &F;
2577 else
2578 FirstInSource = &F;
2579 F.Loaders.push_back(Prev);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002580
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002581 // Set the AST file name.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002582 F.FileName = FileName;
2583
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002584 if (FileName != "-") {
2585 CurrentDir = llvm::sys::path::parent_path(FileName);
2586 if (CurrentDir.empty()) CurrentDir = ".";
2587 }
2588
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +00002589 if (!ASTBuffers.empty()) {
Sebastian Redleb931c62011-04-14 14:07:41 +00002590 F.Buffer.reset(ASTBuffers.back());
2591 ASTBuffers.pop_back();
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +00002592 assert(F.Buffer && "Passed null buffer");
2593 } else {
2594 // Open the AST file.
2595 //
2596 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2597 std::string ErrStr;
2598 llvm::error_code ec;
2599 if (FileName == "-") {
2600 ec = llvm::MemoryBuffer::getSTDIN(F.Buffer);
2601 if (ec)
2602 ErrStr = ec.message();
2603 } else
2604 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
2605 if (!F.Buffer) {
2606 Error(ErrStr.c_str());
2607 return IgnorePCH;
2608 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002609 }
2610
2611 // Initialize the stream
2612 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2613 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl34522812010-07-16 17:50:48 +00002614 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002615 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002616 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002617
2618 // Sniff for the signature.
2619 if (Stream.Read(8) != 'C' ||
2620 Stream.Read(8) != 'P' ||
2621 Stream.Read(8) != 'C' ||
2622 Stream.Read(8) != 'H') {
2623 Diag(diag::err_not_a_pch_file) << FileName;
2624 return Failure;
2625 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002626
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002627 while (!Stream.AtEndOfStream()) {
2628 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002629
Douglas Gregor92863e42009-04-10 23:10:45 +00002630 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002631 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002632 return Failure;
2633 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002634
2635 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002636
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002637 // We only know the AST subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002638 switch (BlockID) {
2639 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002640 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002641 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002642 return Failure;
2643 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002644 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00002645 case AST_BLOCK_ID:
Sebastian Redl3e31c722010-08-18 23:56:56 +00002646 switch (ReadASTBlock(F)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002647 case Success:
2648 break;
2649
2650 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00002651 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00002652
2653 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00002654 // FIXME: We could consider reading through to the end of this
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002655 // AST block, skipping subblocks, to see if there are other
2656 // AST blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00002657
2658 // Clear out any preallocated source location entries, so that
2659 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002660 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00002661
2662 // Remove the stat cache.
Sebastian Redl34522812010-07-16 17:50:48 +00002663 if (F.StatCache)
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002664 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00002665
Douglas Gregor92863e42009-04-10 23:10:45 +00002666 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002667 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002668 break;
2669 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002670 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002671 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002672 return Failure;
2673 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002674 break;
2675 }
Mike Stump11289f42009-09-09 15:08:12 +00002676 }
2677
Sebastian Redl2abc0382010-07-16 20:41:52 +00002678 return Success;
2679}
2680
Sebastian Redl2c499f62010-08-18 23:56:43 +00002681void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002682 PP = &pp;
Sebastian Redlfa061442010-07-21 20:07:32 +00002683
2684 unsigned TotalNum = 0;
2685 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2686 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2687 if (TotalNum) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002688 if (!PP->getPreprocessingRecord())
Douglas Gregor998caea2011-05-06 16:33:08 +00002689 PP->createPreprocessingRecord(true);
Sebastian Redlfa061442010-07-21 20:07:32 +00002690 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregoraae92242010-03-19 21:51:54 +00002691 }
2692}
2693
Sebastian Redl2c499f62010-08-18 23:56:43 +00002694void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002695 Context = &Ctx;
2696 assert(Context && "Passed null context!");
2697
2698 assert(PP && "Forgot to set Preprocessor ?");
2699 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2700 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00002701 PP->setExternalSource(this);
Douglas Gregor09b69892011-02-10 17:09:37 +00002702 PP->getHeaderSearchInfo().SetExternalSource(this);
2703
Douglas Gregoraa433012010-10-01 01:18:02 +00002704 // If we have an update block for the TU waiting, we have to add it before
2705 // deserializing the decl.
2706 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2707 if (DCU != DeclContextOffsets.end()) {
2708 // Insertion could invalidate map, so grab vector.
2709 DeclContextInfos T;
2710 T.swap(DCU->second);
2711 DeclContextOffsets.erase(DCU);
2712 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2713 }
2714
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002715 // Load the translation unit declaration
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00002716 GetTranslationUnitDecl();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002717
2718 // Load the special types.
2719 Context->setBuiltinVaListType(
Sebastian Redl539c5062010-08-18 23:57:32 +00002720 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2721 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002722 Context->setObjCIdType(GetType(Id));
Sebastian Redl539c5062010-08-18 23:57:32 +00002723 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002724 Context->setObjCSelType(GetType(Sel));
Sebastian Redl539c5062010-08-18 23:57:32 +00002725 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002726 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl539c5062010-08-18 23:57:32 +00002727 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002728 Context->setObjCClassType(GetType(Class));
Steve Naroff7cae42b2009-07-10 23:34:53 +00002729
Sebastian Redl539c5062010-08-18 23:57:32 +00002730 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002731 Context->setCFConstantStringType(GetType(String));
Mike Stump11289f42009-09-09 15:08:12 +00002732 if (unsigned FastEnum
Sebastian Redl539c5062010-08-18 23:57:32 +00002733 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002734 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl539c5062010-08-18 23:57:32 +00002735 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregor27821ce2009-07-07 16:35:42 +00002736 QualType FileType = GetType(File);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002737 if (FileType.isNull()) {
2738 Error("FILE type is NULL");
2739 return;
2740 }
John McCall9dd450b2009-09-21 23:43:11 +00002741 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregor27821ce2009-07-07 16:35:42 +00002742 Context->setFILEDecl(Typedef->getDecl());
2743 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002744 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002745 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002746 Error("Invalid FILE type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002747 return;
2748 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002749 Context->setFILEDecl(Tag->getDecl());
2750 }
2751 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002752 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002753 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002754 if (Jmp_bufType.isNull()) {
2755 Error("jmp_bug type is NULL");
2756 return;
2757 }
John McCall9dd450b2009-09-21 23:43:11 +00002758 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002759 Context->setjmp_bufDecl(Typedef->getDecl());
2760 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002761 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002762 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002763 Error("Invalid jmp_buf type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002764 return;
2765 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002766 Context->setjmp_bufDecl(Tag->getDecl());
2767 }
2768 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002769 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002770 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002771 if (Sigjmp_bufType.isNull()) {
2772 Error("sigjmp_buf type is NULL");
2773 return;
2774 }
John McCall9dd450b2009-09-21 23:43:11 +00002775 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002776 Context->setsigjmp_bufDecl(Typedef->getDecl());
2777 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002778 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002779 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stumpa4de80b2009-07-28 02:25:19 +00002780 Context->setsigjmp_bufDecl(Tag->getDecl());
2781 }
2782 }
Mike Stump11289f42009-09-09 15:08:12 +00002783 if (unsigned ObjCIdRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002784 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002785 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump11289f42009-09-09 15:08:12 +00002786 if (unsigned ObjCClassRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002787 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002788 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002789 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpd0153282009-10-20 02:12:22 +00002790 Context->setBlockDescriptorType(GetType(String));
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002791 if (unsigned String
Sebastian Redl539c5062010-08-18 23:57:32 +00002792 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002793 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002794 if (unsigned ObjCSelRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002795 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002796 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002797 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002798 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002799
Sebastian Redl539c5062010-08-18 23:57:32 +00002800 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002801 Context->setInt128Installed();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002802
Richard Smith02e85f32011-04-14 22:09:26 +00002803 if (unsigned AutoDeduct = SpecialTypes[SPECIAL_TYPE_AUTO_DEDUCT])
2804 Context->AutoDeductTy = GetType(AutoDeduct);
2805 if (unsigned AutoRRefDeduct = SpecialTypes[SPECIAL_TYPE_AUTO_RREF_DEDUCT])
2806 Context->AutoRRefDeductTy = GetType(AutoRRefDeduct);
2807
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002808 ReadPragmaDiagnosticMappings(Context->getDiagnostics());
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002809
2810 // If there were any CUDA special declarations, deserialize them.
2811 if (!CUDASpecialDeclRefs.empty()) {
2812 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
2813 Context->setcudaConfigureCallDecl(
2814 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2815 }
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002816}
2817
Douglas Gregor45fe0362009-05-12 01:31:05 +00002818/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002819/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00002820/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002821std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00002822 FileManager &FileMgr,
Daniel Dunbar3b951482009-12-03 09:13:06 +00002823 Diagnostic &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002824 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002825 std::string ErrStr;
2826 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner5159f612010-11-23 08:35:12 +00002827 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00002828 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00002829 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002830 return std::string();
2831 }
2832
2833 // Initialize the stream
2834 llvm::BitstreamReader StreamFile;
2835 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00002836 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00002837 (const unsigned char *)Buffer->getBufferEnd());
2838 Stream.init(StreamFile);
2839
2840 // Sniff for the signature.
2841 if (Stream.Read(8) != 'C' ||
2842 Stream.Read(8) != 'P' ||
2843 Stream.Read(8) != 'C' ||
2844 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002845 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002846 return std::string();
2847 }
2848
2849 RecordData Record;
2850 while (!Stream.AtEndOfStream()) {
2851 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002852
Douglas Gregor45fe0362009-05-12 01:31:05 +00002853 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2854 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00002855
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002856 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002857 switch (BlockID) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002858 case AST_BLOCK_ID:
2859 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002860 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002861 return std::string();
2862 }
2863 break;
Mike Stump11289f42009-09-09 15:08:12 +00002864
Douglas Gregor45fe0362009-05-12 01:31:05 +00002865 default:
2866 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002867 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002868 return std::string();
2869 }
2870 break;
2871 }
2872 continue;
2873 }
2874
2875 if (Code == llvm::bitc::END_BLOCK) {
2876 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002877 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002878 return std::string();
2879 }
2880 continue;
2881 }
2882
2883 if (Code == llvm::bitc::DEFINE_ABBREV) {
2884 Stream.ReadAbbrevRecord();
2885 continue;
2886 }
2887
2888 Record.clear();
2889 const char *BlobStart = 0;
2890 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002891 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl539c5062010-08-18 23:57:32 +00002892 == ORIGINAL_FILE_NAME)
Douglas Gregor45fe0362009-05-12 01:31:05 +00002893 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00002894 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00002895
2896 return std::string();
2897}
2898
Douglas Gregor55abb232009-04-10 20:39:37 +00002899/// \brief Parse the record that corresponds to a LangOptions data
2900/// structure.
2901///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002902/// This routine parses the language options from the AST file and then gives
2903/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00002904///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002905/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002906bool ASTReader::ParseLanguageOptions(
Douglas Gregor55abb232009-04-10 20:39:37 +00002907 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002908 if (Listener) {
2909 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00002910
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002911 #define PARSE_LANGOPT(Option) \
2912 LangOpts.Option = Record[Idx]; \
2913 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00002914
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002915 unsigned Idx = 0;
2916 PARSE_LANGOPT(Trigraphs);
2917 PARSE_LANGOPT(BCPLComment);
2918 PARSE_LANGOPT(DollarIdents);
2919 PARSE_LANGOPT(AsmPreprocessor);
2920 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00002921 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002922 PARSE_LANGOPT(ImplicitInt);
2923 PARSE_LANGOPT(Digraphs);
2924 PARSE_LANGOPT(HexFloats);
2925 PARSE_LANGOPT(C99);
Peter Collingbournea686b5f2011-04-15 00:35:23 +00002926 PARSE_LANGOPT(C1X);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002927 PARSE_LANGOPT(Microsoft);
2928 PARSE_LANGOPT(CPlusPlus);
2929 PARSE_LANGOPT(CPlusPlus0x);
2930 PARSE_LANGOPT(CXXOperatorNames);
2931 PARSE_LANGOPT(ObjC1);
2932 PARSE_LANGOPT(ObjC2);
2933 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00002934 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00002935 PARSE_LANGOPT(AppleKext);
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00002936 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00002937 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002938 PARSE_LANGOPT(PascalStrings);
2939 PARSE_LANGOPT(WritableStrings);
2940 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00002941 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002942 PARSE_LANGOPT(Exceptions);
Anders Carlssonce8dd3a2011-02-19 23:53:54 +00002943 PARSE_LANGOPT(ObjCExceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +00002944 PARSE_LANGOPT(CXXExceptions);
2945 PARSE_LANGOPT(SjLjExceptions);
Douglas Gregordbe39272011-02-01 15:15:22 +00002946 PARSE_LANGOPT(MSBitfields);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002947 PARSE_LANGOPT(NeXTRuntime);
2948 PARSE_LANGOPT(Freestanding);
2949 PARSE_LANGOPT(NoBuiltin);
2950 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00002951 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002952 PARSE_LANGOPT(Blocks);
2953 PARSE_LANGOPT(EmitAllDecls);
2954 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00002955 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2956 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002957 PARSE_LANGOPT(HeinousExtensions);
2958 PARSE_LANGOPT(Optimize);
2959 PARSE_LANGOPT(OptimizeSize);
2960 PARSE_LANGOPT(Static);
2961 PARSE_LANGOPT(PICLevel);
2962 PARSE_LANGOPT(GNUInline);
2963 PARSE_LANGOPT(NoInline);
Chandler Carruth7ffce732011-04-23 20:05:38 +00002964 PARSE_LANGOPT(Deprecated);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002965 PARSE_LANGOPT(AccessControl);
2966 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00002967 PARSE_LANGOPT(ShortWChar);
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00002968 PARSE_LANGOPT(ShortEnums);
Chris Lattner51924e512010-06-26 21:25:03 +00002969 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall457a04e2010-10-22 21:05:15 +00002970 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00002971 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00002972 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002973 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00002974 PARSE_LANGOPT(OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00002975 PARSE_LANGOPT(CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00002976 PARSE_LANGOPT(CatchUndefined);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002977 PARSE_LANGOPT(DefaultFPContract);
Roman Divackydc1f68d2011-03-01 17:36:40 +00002978 PARSE_LANGOPT(ElideConstructors);
2979 PARSE_LANGOPT(SpellChecking);
Roman Divacky65b88cd2011-03-01 17:40:53 +00002980 PARSE_LANGOPT(MRTD);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002981 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00002982
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002983 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00002984 }
Douglas Gregor55abb232009-04-10 20:39:37 +00002985
2986 return false;
2987}
2988
Sebastian Redl2c499f62010-08-18 23:56:43 +00002989void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002990 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2991 PerFileData &F = *Chain[I];
2992 if (!F.PreprocessorDetailCursor.getBitStreamReader())
2993 continue;
2994
2995 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
2996 F.PreprocessorDetailCursor.JumpToBit(F.PreprocessorDetailStartOffset);
2997 while (LoadPreprocessedEntity(F)) { }
2998 }
Douglas Gregoraae92242010-03-19 21:51:54 +00002999}
3000
Douglas Gregor46c50012011-02-11 19:46:30 +00003001PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003002 PerFileData *F = 0;
3003 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3004 if (Offset < Chain[I]->SizeInBits) {
3005 F = Chain[I];
3006 break;
3007 }
3008
3009 Offset -= Chain[I]->SizeInBits;
3010 }
3011
3012 if (!F) {
3013 Error("Malformed preprocessed entity offset");
3014 return 0;
3015 }
3016
Douglas Gregor92a96f52011-02-08 21:58:10 +00003017 // Keep track of where we are in the stream, then jump back there
3018 // after reading this entity.
3019 SavedStreamPosition SavedPosition(F->PreprocessorDetailCursor);
3020 F->PreprocessorDetailCursor.JumpToBit(Offset);
3021 return LoadPreprocessedEntity(*F);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003022}
3023
Douglas Gregor09b69892011-02-10 17:09:37 +00003024HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
3025 HeaderFileInfoTrait Trait(FE->getName());
3026 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3027 PerFileData &F = *Chain[I];
3028 HeaderFileInfoLookupTable *Table
3029 = static_cast<HeaderFileInfoLookupTable *>(F.HeaderFileInfoTable);
3030 if (!Table)
3031 continue;
3032
3033 // Look in the on-disk hash table for an entry for this file name.
3034 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE->getName(),
3035 &Trait);
3036 if (Pos == Table->end())
3037 continue;
3038
3039 HeaderFileInfo HFI = *Pos;
3040 if (Listener)
3041 Listener->ReadHeaderFileInfo(HFI, FE->getUID());
3042
3043 return HFI;
3044 }
3045
3046 return HeaderFileInfo();
3047}
3048
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003049void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003050 unsigned Idx = 0;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003051 while (Idx < PragmaDiagMappings.size()) {
3052 SourceLocation
3053 Loc = SourceLocation::getFromRawEncoding(PragmaDiagMappings[Idx++]);
3054 while (1) {
3055 assert(Idx < PragmaDiagMappings.size() &&
3056 "Invalid data, didn't find '-1' marking end of diag/map pairs");
3057 if (Idx >= PragmaDiagMappings.size())
3058 break; // Something is messed up but at least avoid infinite loop in
3059 // release build.
3060 unsigned DiagID = PragmaDiagMappings[Idx++];
3061 if (DiagID == (unsigned)-1)
3062 break; // no more diag/map pairs for this location.
3063 diag::Mapping Map = (diag::Mapping)PragmaDiagMappings[Idx++];
3064 Diag.setDiagnosticMapping(DiagID, Map, Loc);
3065 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003066 }
3067}
3068
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003069/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003070ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003071 PerFileData *F = 0;
3072 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3073 F = Chain[N - I - 1];
3074 if (Index < F->LocalNumTypes)
3075 break;
3076 Index -= F->LocalNumTypes;
3077 }
3078 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redl2c373b92010-10-05 15:59:54 +00003079 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003080}
3081
3082/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003083///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003084/// The index is the type ID, shifted and minus the number of predefs. This
3085/// routine actually reads the record corresponding to the type at the given
3086/// location. It is a helper routine for GetType, which deals with reading type
3087/// IDs.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003088QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003089 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003090 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00003091
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003092 // Keep track of where we are in the stream, then jump back there
3093 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00003094 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003095
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003096 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00003097
Douglas Gregor1342e842009-07-06 18:54:52 +00003098 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003099 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00003100
Sebastian Redl2c373b92010-10-05 15:59:54 +00003101 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003102 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003103 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00003104 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3105 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003106 if (Record.size() != 2) {
3107 Error("Incorrect encoding of extended qualifier type");
3108 return QualType();
3109 }
Douglas Gregor455b8f42009-04-15 22:00:08 +00003110 QualType Base = GetType(Record[0]);
John McCall8ccfcb52009-09-24 19:53:00 +00003111 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
3112 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00003113 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003114
Sebastian Redl539c5062010-08-18 23:57:32 +00003115 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003116 if (Record.size() != 1) {
3117 Error("Incorrect encoding of complex type");
3118 return QualType();
3119 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003120 QualType ElemType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003121 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003122 }
3123
Sebastian Redl539c5062010-08-18 23:57:32 +00003124 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003125 if (Record.size() != 1) {
3126 Error("Incorrect encoding of pointer type");
3127 return QualType();
3128 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003129 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003130 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003131 }
3132
Sebastian Redl539c5062010-08-18 23:57:32 +00003133 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003134 if (Record.size() != 1) {
3135 Error("Incorrect encoding of block pointer type");
3136 return QualType();
3137 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003138 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003139 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003140 }
3141
Sebastian Redl539c5062010-08-18 23:57:32 +00003142 case TYPE_LVALUE_REFERENCE: {
Richard Smith0f538462011-04-12 10:38:03 +00003143 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003144 Error("Incorrect encoding of lvalue reference type");
3145 return QualType();
3146 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003147 QualType PointeeType = GetType(Record[0]);
Richard Smith0f538462011-04-12 10:38:03 +00003148 return Context->getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003149 }
3150
Sebastian Redl539c5062010-08-18 23:57:32 +00003151 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003152 if (Record.size() != 1) {
3153 Error("Incorrect encoding of rvalue reference type");
3154 return QualType();
3155 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003156 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003157 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003158 }
3159
Sebastian Redl539c5062010-08-18 23:57:32 +00003160 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00003161 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003162 Error("Incorrect encoding of member pointer type");
3163 return QualType();
3164 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003165 QualType PointeeType = GetType(Record[0]);
3166 QualType ClassType = GetType(Record[1]);
Douglas Gregor0cdc8322010-12-10 17:03:06 +00003167 if (PointeeType.isNull() || ClassType.isNull())
3168 return QualType();
3169
Chris Lattner8575daa2009-04-27 21:45:14 +00003170 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003171 }
3172
Sebastian Redl539c5062010-08-18 23:57:32 +00003173 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003174 QualType ElementType = GetType(Record[0]);
3175 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3176 unsigned IndexTypeQuals = Record[2];
3177 unsigned Idx = 3;
3178 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00003179 return Context->getConstantArrayType(ElementType, Size,
3180 ASM, IndexTypeQuals);
3181 }
3182
Sebastian Redl539c5062010-08-18 23:57:32 +00003183 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003184 QualType ElementType = GetType(Record[0]);
3185 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3186 unsigned IndexTypeQuals = Record[2];
Chris Lattner8575daa2009-04-27 21:45:14 +00003187 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003188 }
3189
Sebastian Redl539c5062010-08-18 23:57:32 +00003190 case TYPE_VARIABLE_ARRAY: {
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003191 QualType ElementType = GetType(Record[0]);
3192 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3193 unsigned IndexTypeQuals = Record[2];
Sebastian Redl2c373b92010-10-05 15:59:54 +00003194 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3195 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
3196 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor04318252009-07-06 15:59:29 +00003197 ASM, IndexTypeQuals,
3198 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003199 }
3200
Sebastian Redl539c5062010-08-18 23:57:32 +00003201 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00003202 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003203 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003204 return QualType();
3205 }
3206
3207 QualType ElementType = GetType(Record[0]);
3208 unsigned NumElements = Record[1];
Bob Wilsonaeb56442010-11-10 21:56:12 +00003209 unsigned VecKind = Record[2];
Chris Lattner37141f42010-06-23 06:00:24 +00003210 return Context->getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00003211 (VectorType::VectorKind)VecKind);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003212 }
3213
Sebastian Redl539c5062010-08-18 23:57:32 +00003214 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00003215 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003216 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003217 return QualType();
3218 }
3219
3220 QualType ElementType = GetType(Record[0]);
3221 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00003222 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003223 }
3224
Sebastian Redl539c5062010-08-18 23:57:32 +00003225 case TYPE_FUNCTION_NO_PROTO: {
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003226 if (Record.size() != 5) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00003227 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003228 return QualType();
3229 }
3230 QualType ResultType = GetType(Record[0]);
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003231 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3], (CallingConv)Record[4]);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00003232 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003233 }
3234
Sebastian Redl539c5062010-08-18 23:57:32 +00003235 case TYPE_FUNCTION_PROTO: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003236 QualType ResultType = GetType(Record[0]);
John McCalldb40c7f2010-12-14 08:05:40 +00003237
3238 FunctionProtoType::ExtProtoInfo EPI;
3239 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003240 /*hasregparm*/ Record[2],
3241 /*regparm*/ Record[3],
3242 static_cast<CallingConv>(Record[4]));
John McCalldb40c7f2010-12-14 08:05:40 +00003243
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003244 unsigned Idx = 5;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003245 unsigned NumParams = Record[Idx++];
3246 llvm::SmallVector<QualType, 16> ParamTypes;
3247 for (unsigned I = 0; I != NumParams; ++I)
3248 ParamTypes.push_back(GetType(Record[Idx++]));
John McCalldb40c7f2010-12-14 08:05:40 +00003249
3250 EPI.Variadic = Record[Idx++];
3251 EPI.TypeQuals = Record[Idx++];
Douglas Gregordb9d6642011-01-26 05:01:58 +00003252 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003253 ExceptionSpecificationType EST =
3254 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3255 EPI.ExceptionSpecType = EST;
3256 if (EST == EST_Dynamic) {
3257 EPI.NumExceptions = Record[Idx++];
3258 llvm::SmallVector<QualType, 2> Exceptions;
3259 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
3260 Exceptions.push_back(GetType(Record[Idx++]));
3261 EPI.Exceptions = Exceptions.data();
3262 } else if (EST == EST_ComputedNoexcept) {
3263 EPI.NoexceptExpr = ReadExpr(*Loc.F);
3264 }
Jay Foad7d0479f2009-05-21 09:52:38 +00003265 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalldb40c7f2010-12-14 08:05:40 +00003266 EPI);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003267 }
3268
Sebastian Redl539c5062010-08-18 23:57:32 +00003269 case TYPE_UNRESOLVED_USING:
John McCallb96ec562009-12-04 22:46:56 +00003270 return Context->getTypeDeclType(
3271 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
3272
Sebastian Redl539c5062010-08-18 23:57:32 +00003273 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003274 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003275 Error("incorrect encoding of typedef type");
3276 return QualType();
3277 }
Richard Smithdda56e42011-04-15 14:24:37 +00003278 TypedefNameDecl *Decl = cast<TypedefNameDecl>(GetDecl(Record[0]));
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003279 QualType Canonical = GetType(Record[1]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003280 if (!Canonical.isNull())
3281 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003282 return Context->getTypedefType(Decl, Canonical);
3283 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003284
Sebastian Redl539c5062010-08-18 23:57:32 +00003285 case TYPE_TYPEOF_EXPR:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003286 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003287
Sebastian Redl539c5062010-08-18 23:57:32 +00003288 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003289 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003290 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003291 return QualType();
3292 }
3293 QualType UnderlyingType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003294 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003295 }
Mike Stump11289f42009-09-09 15:08:12 +00003296
Sebastian Redl539c5062010-08-18 23:57:32 +00003297 case TYPE_DECLTYPE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003298 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson81df7b82009-06-24 19:06:50 +00003299
Alexis Hunte852b102011-05-24 22:41:36 +00003300 case TYPE_UNARY_TRANSFORM: {
3301 QualType BaseType = GetType(Record[0]);
3302 QualType UnderlyingType = GetType(Record[1]);
3303 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
3304 return Context->getUnaryTransformType(BaseType, UnderlyingType, UKind);
3305 }
3306
Richard Smith30482bc2011-02-20 03:19:35 +00003307 case TYPE_AUTO:
3308 return Context->getAutoType(GetType(Record[0]));
3309
Sebastian Redl539c5062010-08-18 23:57:32 +00003310 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003311 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003312 Error("incorrect encoding of record type");
3313 return QualType();
3314 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003315 bool IsDependent = Record[0];
3316 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCall424cec92011-01-19 06:33:43 +00003317 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003318 return T;
3319 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003320
Sebastian Redl539c5062010-08-18 23:57:32 +00003321 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003322 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003323 Error("incorrect encoding of enum type");
3324 return QualType();
3325 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003326 bool IsDependent = Record[0];
3327 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCall424cec92011-01-19 06:33:43 +00003328 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003329 return T;
3330 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00003331
John McCall81904512011-01-06 01:58:22 +00003332 case TYPE_ATTRIBUTED: {
3333 if (Record.size() != 3) {
3334 Error("incorrect encoding of attributed type");
3335 return QualType();
3336 }
3337 QualType modifiedType = GetType(Record[0]);
3338 QualType equivalentType = GetType(Record[1]);
3339 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3340 return Context->getAttributedType(kind, modifiedType, equivalentType);
3341 }
3342
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003343 case TYPE_PAREN: {
3344 if (Record.size() != 1) {
3345 Error("incorrect encoding of paren type");
3346 return QualType();
3347 }
3348 QualType InnerType = GetType(Record[0]);
3349 return Context->getParenType(InnerType);
3350 }
3351
Douglas Gregord2fa7662010-12-20 02:24:11 +00003352 case TYPE_PACK_EXPANSION: {
Douglas Gregor17328502011-02-01 15:24:58 +00003353 if (Record.size() != 2) {
Douglas Gregord2fa7662010-12-20 02:24:11 +00003354 Error("incorrect encoding of pack expansion type");
3355 return QualType();
3356 }
3357 QualType Pattern = GetType(Record[0]);
3358 if (Pattern.isNull())
3359 return QualType();
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00003360 llvm::Optional<unsigned> NumExpansions;
3361 if (Record[1])
3362 NumExpansions = Record[1] - 1;
3363 return Context->getPackExpansionType(Pattern, NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00003364 }
3365
Sebastian Redl539c5062010-08-18 23:57:32 +00003366 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003367 unsigned Idx = 0;
3368 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3369 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3370 QualType NamedType = GetType(Record[Idx++]);
3371 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00003372 }
3373
Sebastian Redl539c5062010-08-18 23:57:32 +00003374 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00003375 unsigned Idx = 0;
3376 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00003377 return Context->getObjCInterfaceType(ItfD);
3378 }
3379
Sebastian Redl539c5062010-08-18 23:57:32 +00003380 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00003381 unsigned Idx = 0;
3382 QualType Base = GetType(Record[Idx++]);
Chris Lattner587cbe12009-04-22 06:45:28 +00003383 unsigned NumProtos = Record[Idx++];
3384 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
3385 for (unsigned I = 0; I != NumProtos; ++I)
3386 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003387 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00003388 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003389
Sebastian Redl539c5062010-08-18 23:57:32 +00003390 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00003391 unsigned Idx = 0;
John McCall8b07ec22010-05-15 11:32:37 +00003392 QualType Pointee = GetType(Record[Idx++]);
3393 return Context->getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00003394 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00003395
Sebastian Redl539c5062010-08-18 23:57:32 +00003396 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00003397 unsigned Idx = 0;
3398 QualType Parm = GetType(Record[Idx++]);
3399 QualType Replacement = GetType(Record[Idx++]);
3400 return
3401 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3402 Replacement);
3403 }
John McCalle78aac42010-03-10 03:28:59 +00003404
Douglas Gregorada4b792011-01-14 02:55:32 +00003405 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3406 unsigned Idx = 0;
3407 QualType Parm = GetType(Record[Idx++]);
3408 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3409 return Context->getSubstTemplateTypeParmPackType(
3410 cast<TemplateTypeParmType>(Parm),
3411 ArgPack);
3412 }
3413
Sebastian Redl539c5062010-08-18 23:57:32 +00003414 case TYPE_INJECTED_CLASS_NAME: {
John McCalle78aac42010-03-10 03:28:59 +00003415 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
3416 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003417 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003418 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003419 return
3420 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00003421 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003422
Sebastian Redl539c5062010-08-18 23:57:32 +00003423 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003424 unsigned Idx = 0;
3425 unsigned Depth = Record[Idx++];
3426 unsigned Index = Record[Idx++];
3427 bool Pack = Record[Idx++];
Chandler Carruth08836322011-05-01 00:51:33 +00003428 TemplateTypeParmDecl *D =
3429 cast_or_null<TemplateTypeParmDecl>(GetDecl(Record[Idx++]));
3430 return Context->getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003431 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003432
Sebastian Redl539c5062010-08-18 23:57:32 +00003433 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003434 unsigned Idx = 0;
3435 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3436 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3437 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00003438 QualType Canon = GetType(Record[Idx++]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003439 if (!Canon.isNull())
3440 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00003441 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003442 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003443
Sebastian Redl539c5062010-08-18 23:57:32 +00003444 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003445 unsigned Idx = 0;
3446 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3447 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3448 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3449 unsigned NumArgs = Record[Idx++];
3450 llvm::SmallVector<TemplateArgument, 8> Args;
3451 Args.reserve(NumArgs);
3452 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003453 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003454 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3455 Args.size(), Args.data());
3456 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003457
Sebastian Redl539c5062010-08-18 23:57:32 +00003458 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003459 unsigned Idx = 0;
3460
3461 // ArrayType
3462 QualType ElementType = GetType(Record[Idx++]);
3463 ArrayType::ArraySizeModifier ASM
3464 = (ArrayType::ArraySizeModifier)Record[Idx++];
3465 unsigned IndexTypeQuals = Record[Idx++];
3466
3467 // DependentSizedArrayType
Sebastian Redl2c373b92010-10-05 15:59:54 +00003468 Expr *NumElts = ReadExpr(*Loc.F);
3469 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003470
3471 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3472 IndexTypeQuals, Brackets);
3473 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003474
Sebastian Redl539c5062010-08-18 23:57:32 +00003475 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003476 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003477 bool IsDependent = Record[Idx++];
Douglas Gregor5590be02011-01-15 06:45:20 +00003478 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003479 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003480 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Richard Smith3f1b5d02011-05-05 21:57:07 +00003481 QualType Underlying = GetType(Record[Idx++]);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003482 QualType T;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003483 if (Underlying.isNull())
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003484 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3485 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003486 else
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003487 T = Context->getTemplateSpecializationType(Name, Args.data(),
Richard Smith3f1b5d02011-05-05 21:57:07 +00003488 Args.size(), Underlying);
John McCall424cec92011-01-19 06:33:43 +00003489 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003490 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003491 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003492 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003493 // Suppress a GCC warning
3494 return QualType();
3495}
3496
Sebastian Redl2c373b92010-10-05 15:59:54 +00003497class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00003498 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003499 ASTReader::PerFileData &F;
Sebastian Redlc67764e2010-07-22 22:43:28 +00003500 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redl2c499f62010-08-18 23:56:43 +00003501 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00003502 unsigned &Idx;
3503
Sebastian Redl2c373b92010-10-05 15:59:54 +00003504 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3505 unsigned &I) {
3506 return Reader.ReadSourceLocation(F, R, I);
3507 }
3508
John McCall8f115c62009-10-16 21:56:05 +00003509public:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003510 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl2c499f62010-08-18 23:56:43 +00003511 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003512 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3513 { }
John McCall8f115c62009-10-16 21:56:05 +00003514
John McCall17001972009-10-18 01:05:36 +00003515 // We want compile-time assurance that we've enumerated all of
3516 // these, so unfortunately we have to declare them first, then
3517 // define them out-of-line.
3518#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00003519#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00003520 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00003521#include "clang/AST/TypeLocNodes.def"
3522
John McCall17001972009-10-18 01:05:36 +00003523 void VisitFunctionTypeLoc(FunctionTypeLoc);
3524 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00003525};
3526
John McCall17001972009-10-18 01:05:36 +00003527void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00003528 // nothing to do
3529}
John McCall17001972009-10-18 01:05:36 +00003530void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003531 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorc9b7a592010-01-18 18:04:31 +00003532 if (TL.needsExtraLocalData()) {
3533 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3534 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3535 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3536 TL.setModeAttr(Record[Idx++]);
3537 }
John McCall8f115c62009-10-16 21:56:05 +00003538}
John McCall17001972009-10-18 01:05:36 +00003539void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003540 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003541}
John McCall17001972009-10-18 01:05:36 +00003542void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003543 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003544}
John McCall17001972009-10-18 01:05:36 +00003545void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003546 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003547}
John McCall17001972009-10-18 01:05:36 +00003548void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003549 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003550}
John McCall17001972009-10-18 01:05:36 +00003551void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003552 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003553}
John McCall17001972009-10-18 01:05:36 +00003554void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003555 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara509357842011-03-05 14:42:21 +00003556 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003557}
John McCall17001972009-10-18 01:05:36 +00003558void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003559 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3560 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003561 if (Record[Idx++])
Sebastian Redl2c373b92010-10-05 15:59:54 +00003562 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003563 else
John McCall17001972009-10-18 01:05:36 +00003564 TL.setSizeExpr(0);
3565}
3566void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3567 VisitArrayTypeLoc(TL);
3568}
3569void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3570 VisitArrayTypeLoc(TL);
3571}
3572void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3573 VisitArrayTypeLoc(TL);
3574}
3575void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3576 DependentSizedArrayTypeLoc TL) {
3577 VisitArrayTypeLoc(TL);
3578}
3579void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3580 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003581 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003582}
3583void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003584 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003585}
3586void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003587 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003588}
3589void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003590 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
3591 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Douglas Gregor7fb25412010-10-01 18:44:50 +00003592 TL.setTrailingReturn(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00003593 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCalle6347002009-10-23 01:28:53 +00003594 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall17001972009-10-18 01:05:36 +00003595 }
3596}
3597void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3598 VisitFunctionTypeLoc(TL);
3599}
3600void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3601 VisitFunctionTypeLoc(TL);
3602}
John McCallb96ec562009-12-04 22:46:56 +00003603void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003604 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallb96ec562009-12-04 22:46:56 +00003605}
John McCall17001972009-10-18 01:05:36 +00003606void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003607 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003608}
3609void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003610 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3611 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3612 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003613}
3614void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003615 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3616 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3617 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3618 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003619}
3620void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003621 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003622}
Alexis Hunte852b102011-05-24 22:41:36 +00003623void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3624 TL.setKWLoc(ReadSourceLocation(Record, Idx));
3625 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3626 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3627 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3628}
Richard Smith30482bc2011-02-20 03:19:35 +00003629void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3630 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3631}
John McCall17001972009-10-18 01:05:36 +00003632void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003633 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003634}
3635void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003636 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003637}
John McCall81904512011-01-06 01:58:22 +00003638void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3639 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3640 if (TL.hasAttrOperand()) {
3641 SourceRange range;
3642 range.setBegin(ReadSourceLocation(Record, Idx));
3643 range.setEnd(ReadSourceLocation(Record, Idx));
3644 TL.setAttrOperandParensRange(range);
3645 }
3646 if (TL.hasAttrExprOperand()) {
3647 if (Record[Idx++])
3648 TL.setAttrExprOperand(Reader.ReadExpr(F));
3649 else
3650 TL.setAttrExprOperand(0);
3651 } else if (TL.hasAttrEnumOperand())
3652 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3653}
John McCall17001972009-10-18 01:05:36 +00003654void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003655 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003656}
John McCallcebee162009-10-18 09:09:24 +00003657void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3658 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003659 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallcebee162009-10-18 09:09:24 +00003660}
Douglas Gregorada4b792011-01-14 02:55:32 +00003661void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3662 SubstTemplateTypeParmPackTypeLoc TL) {
3663 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3664}
John McCall17001972009-10-18 01:05:36 +00003665void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3666 TemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003667 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3668 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3669 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall0ad16662009-10-29 08:12:44 +00003670 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3671 TL.setArgLocInfo(i,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003672 Reader.GetTemplateArgumentLocInfo(F,
3673 TL.getTypePtr()->getArg(i).getKind(),
3674 Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003675}
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003676void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3677 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3678 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3679}
Abramo Bagnara6150c882010-05-11 21:36:43 +00003680void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003681 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor844cb502011-03-01 18:12:44 +00003682 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003683}
John McCalle78aac42010-03-10 03:28:59 +00003684void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003685 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalle78aac42010-03-10 03:28:59 +00003686}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00003687void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003688 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor3d0da5f2011-03-01 01:34:45 +00003689 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00003690 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003691}
John McCallc392f372010-06-11 00:33:02 +00003692void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3693 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003694 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregora7a795b2011-03-01 20:11:18 +00003695 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00003696 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3697 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3698 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003699 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3700 TL.setArgLocInfo(I,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003701 Reader.GetTemplateArgumentLocInfo(F,
3702 TL.getTypePtr()->getArg(I).getKind(),
3703 Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003704}
Douglas Gregord2fa7662010-12-20 02:24:11 +00003705void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3706 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3707}
John McCall17001972009-10-18 01:05:36 +00003708void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003709 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8b07ec22010-05-15 11:32:37 +00003710}
3711void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3712 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003713 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3714 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003715 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003716 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003717}
John McCallfc93cf92009-10-22 22:37:11 +00003718void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003719 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCallfc93cf92009-10-22 22:37:11 +00003720}
John McCall8f115c62009-10-16 21:56:05 +00003721
Sebastian Redl2c373b92010-10-05 15:59:54 +00003722TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003723 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00003724 unsigned &Idx) {
3725 QualType InfoTy = GetType(Record[Idx++]);
3726 if (InfoTy.isNull())
3727 return 0;
3728
John McCallbcd03502009-12-07 02:54:59 +00003729 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003730 TypeLocReader TLR(*this, F, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00003731 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00003732 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00003733 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00003734}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003735
Sebastian Redl539c5062010-08-18 23:57:32 +00003736QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00003737 unsigned FastQuals = ID & Qualifiers::FastMask;
3738 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003739
Sebastian Redl539c5062010-08-18 23:57:32 +00003740 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003741 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00003742 switch ((PredefinedTypeIDs)Index) {
3743 case PREDEF_TYPE_NULL_ID: return QualType();
3744 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3745 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003746
Sebastian Redl539c5062010-08-18 23:57:32 +00003747 case PREDEF_TYPE_CHAR_U_ID:
3748 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003749 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00003750 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003751 break;
3752
Sebastian Redl539c5062010-08-18 23:57:32 +00003753 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3754 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3755 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3756 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3757 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3758 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3759 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3760 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3761 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3762 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3763 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3764 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3765 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3766 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3767 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3768 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3769 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
John McCall0009fcc2011-04-26 20:42:42 +00003770 case PREDEF_TYPE_BOUND_MEMBER: T = Context->BoundMemberTy; break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003771 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
John McCall31996342011-04-07 08:22:57 +00003772 case PREDEF_TYPE_UNKNOWN_ANY: T = Context->UnknownAnyTy; break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003773 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3774 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3775 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3776 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3777 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3778 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003779 }
3780
3781 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00003782 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003783 }
3784
Sebastian Redl539c5062010-08-18 23:57:32 +00003785 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003786 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00003787 if (TypesLoaded[Index].isNull()) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003788 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003789 if (TypesLoaded[Index].isNull())
3790 return QualType();
3791
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003792 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003793 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003794 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003795 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003796 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00003797 }
Mike Stump11289f42009-09-09 15:08:12 +00003798
John McCall8ccfcb52009-09-24 19:53:00 +00003799 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003800}
3801
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003802TypeID ASTReader::GetTypeID(QualType T) const {
3803 return MakeTypeID(T,
3804 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3805}
3806
3807TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3808 if (T.isNull())
3809 return TypeIdx();
3810 assert(!T.getLocalFastQualifiers());
3811
3812 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3813 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3814 // comparing keys of ASTDeclContextNameLookupTable.
3815 // If the type didn't come from the AST file use a specially marked index
3816 // so that any hash/key comparison fail since no such index is stored
3817 // in a AST file.
3818 if (I == TypeIdxs.end())
3819 return TypeIdx(-1);
3820 return I->second;
3821}
3822
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003823unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3824 unsigned Result = 0;
3825 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3826 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3827
3828 return Result;
3829}
3830
John McCall0ad16662009-10-29 08:12:44 +00003831TemplateArgumentLocInfo
Sebastian Redl2c373b92010-10-05 15:59:54 +00003832ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3833 TemplateArgument::ArgKind Kind,
John McCall0ad16662009-10-29 08:12:44 +00003834 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003835 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00003836 switch (Kind) {
3837 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003838 return ReadExpr(F);
John McCall0ad16662009-10-29 08:12:44 +00003839 case TemplateArgument::Type:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003840 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003841 case TemplateArgument::Template: {
Douglas Gregor9d802122011-03-02 17:09:35 +00003842 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3843 Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003844 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00003845 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003846 SourceLocation());
3847 }
3848 case TemplateArgument::TemplateExpansion: {
Douglas Gregor9d802122011-03-02 17:09:35 +00003849 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3850 Index);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003851 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003852 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00003853 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregoreb29d182011-01-05 17:40:24 +00003854 EllipsisLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003855 }
John McCall0ad16662009-10-29 08:12:44 +00003856 case TemplateArgument::Null:
3857 case TemplateArgument::Integral:
3858 case TemplateArgument::Declaration:
3859 case TemplateArgument::Pack:
3860 return TemplateArgumentLocInfo();
3861 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00003862 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00003863 return TemplateArgumentLocInfo();
3864}
3865
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003866TemplateArgumentLoc
Sebastian Redl2c373b92010-10-05 15:59:54 +00003867ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003868 const RecordData &Record, unsigned &Index) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003869 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003870
3871 if (Arg.getKind() == TemplateArgument::Expression) {
3872 if (Record[Index++]) // bool InfoHasSameExpr.
3873 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3874 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00003875 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003876 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003877}
3878
Sebastian Redl2c499f62010-08-18 23:56:43 +00003879Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00003880 return GetDecl(ID);
3881}
3882
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003883uint64_t
3884ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3885 if (ID == 0)
3886 return 0;
3887
3888 --ID;
3889 uint64_t Offset = 0;
3890 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003891 PerFileData &F = *Chain[N - I - 1];
3892
3893 if (ID < F.LocalNumCXXBaseSpecifiers)
3894 return Offset + F.CXXBaseSpecifiersOffsets[ID];
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003895
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003896 ID -= F.LocalNumCXXBaseSpecifiers;
3897 Offset += F.SizeInBits;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003898 }
3899
3900 assert(false && "CXXBaseSpecifiers not found");
3901 return 0;
3902}
3903
3904CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3905 // Figure out which AST file contains this offset.
3906 PerFileData *F = 0;
3907 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003908 if (Offset < Chain[N - I - 1]->SizeInBits) {
3909 F = Chain[N - I - 1];
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003910 break;
3911 }
3912
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003913 Offset -= Chain[N - I - 1]->SizeInBits;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003914 }
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003915
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003916 if (!F) {
3917 Error("Malformed AST file: C++ base specifiers at impossible offset");
3918 return 0;
3919 }
3920
3921 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3922 SavedStreamPosition SavedPosition(Cursor);
3923 Cursor.JumpToBit(Offset);
3924 ReadingKindTracker ReadingKind(Read_Decl, *this);
3925 RecordData Record;
3926 unsigned Code = Cursor.ReadCode();
3927 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3928 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3929 Error("Malformed AST file: missing C++ base specifiers");
3930 return 0;
3931 }
3932
3933 unsigned Idx = 0;
3934 unsigned NumBases = Record[Idx++];
3935 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3936 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3937 for (unsigned I = 0; I != NumBases; ++I)
3938 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3939 return Bases;
3940}
3941
Sebastian Redl2c499f62010-08-18 23:56:43 +00003942TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003943 if (!DeclsLoaded[0]) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00003944 ReadDeclRecord(0, 1);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003945 if (DeserializationListener)
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003946 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003947 }
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00003948
3949 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3950}
3951
Sebastian Redl539c5062010-08-18 23:57:32 +00003952Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003953 if (ID == 0)
3954 return 0;
3955
Douglas Gregor745ed142009-04-25 18:35:21 +00003956 if (ID > DeclsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003957 Error("declaration ID out-of-range for AST file");
Douglas Gregor745ed142009-04-25 18:35:21 +00003958 return 0;
3959 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003960
Douglas Gregor745ed142009-04-25 18:35:21 +00003961 unsigned Index = ID - 1;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003962 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003963 ReadDeclRecord(Index, ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003964 if (DeserializationListener)
3965 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3966 }
Douglas Gregor745ed142009-04-25 18:35:21 +00003967
3968 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003969}
3970
Chris Lattner9c28af02009-04-27 05:46:25 +00003971/// \brief Resolve the offset of a statement into a statement.
3972///
3973/// This operation will read a new statement from the external
3974/// source each time it is called, and is meant to be used via a
3975/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00003976Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00003977 // Switch case IDs are per Decl.
3978 ClearSwitchCaseIDs();
3979
Sebastian Redl5c415f32010-07-22 17:01:13 +00003980 // Offset here is a global offset across the entire chain.
3981 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3982 PerFileData &F = *Chain[N - I - 1];
3983 if (Offset < F.SizeInBits) {
3984 // Since we know that this statement is part of a decl, make sure to use
3985 // the decl cursor to read it.
3986 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003987 return ReadStmtFromStream(F);
Sebastian Redl5c415f32010-07-22 17:01:13 +00003988 }
3989 Offset -= F.SizeInBits;
3990 }
3991 llvm_unreachable("Broken chain");
Douglas Gregor3c3aa612009-04-18 00:07:54 +00003992}
3993
Sebastian Redl2c499f62010-08-18 23:56:43 +00003994bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003995 bool (*isKindWeWant)(Decl::Kind),
John McCall75b960e2010-06-01 09:23:16 +00003996 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump11289f42009-09-09 15:08:12 +00003997 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003998 "DeclContext has no lexical decls in storage");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003999
Sebastian Redl5c415f32010-07-22 17:01:13 +00004000 // There might be lexical decls in multiple parts of the chain, for the TU
4001 // at least.
Sebastian Redlda6a21c2010-09-28 02:24:44 +00004002 // DeclContextOffsets might reallocate as we load additional decls below,
4003 // so make a copy of the vector.
4004 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl5c415f32010-07-22 17:01:13 +00004005 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
4006 I != E; ++I) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004007 // IDs can be 0 if this context doesn't contain declarations.
4008 if (!I->LexicalDecls)
Sebastian Redl5c415f32010-07-22 17:01:13 +00004009 continue;
Sebastian Redl5c415f32010-07-22 17:01:13 +00004010
4011 // Load all of the declaration IDs
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00004012 for (const KindDeclIDPair *ID = I->LexicalDecls,
4013 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
4014 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
4015 continue;
4016
4017 Decl *D = GetDecl(ID->second);
Sebastian Redlda6a21c2010-09-28 02:24:44 +00004018 assert(D && "Null decl in lexical decls");
4019 Decls.push_back(D);
4020 }
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004021 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004022
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004023 ++NumLexicalDeclContextsRead;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004024 return false;
4025}
4026
John McCall75b960e2010-06-01 09:23:16 +00004027DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00004028ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00004029 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00004030 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004031 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004032 if (!Name)
4033 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
4034 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004035
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004036 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl471ac2f2010-08-24 00:49:55 +00004037 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl9617e7e2010-08-24 00:50:16 +00004038 // and namespaces. For any given name, the last available results replace
4039 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl5c415f32010-07-22 17:01:13 +00004040 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl9617e7e2010-08-24 00:50:16 +00004041 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl5c415f32010-07-22 17:01:13 +00004042 I != E; ++I) {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004043 if (!I->NameLookupTableData)
Sebastian Redl5c415f32010-07-22 17:01:13 +00004044 continue;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004045
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004046 ASTDeclContextNameLookupTable *LookupTable =
4047 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
4048 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
4049 if (Pos == LookupTable->end())
Sebastian Redl5c415f32010-07-22 17:01:13 +00004050 continue;
4051
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004052 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
4053 for (; Data.first != Data.second; ++Data.first)
4054 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl9617e7e2010-08-24 00:50:16 +00004055 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004056 }
4057
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004058 ++NumVisibleDeclContextsRead;
John McCall75b960e2010-06-01 09:23:16 +00004059
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004060 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00004061 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004062}
4063
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00004064void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
4065 assert(DC->hasExternalVisibleStorage() &&
4066 "DeclContext has no visible decls in storage");
4067
4068 llvm::SmallVector<NamedDecl *, 64> Decls;
4069 // There might be visible decls in multiple parts of the chain, for the TU
4070 // and namespaces.
4071 DeclContextInfos &Infos = DeclContextOffsets[DC];
4072 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
4073 I != E; ++I) {
4074 if (!I->NameLookupTableData)
4075 continue;
4076
4077 ASTDeclContextNameLookupTable *LookupTable =
4078 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
4079 for (ASTDeclContextNameLookupTable::item_iterator
4080 ItemI = LookupTable->item_begin(),
4081 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
4082 ASTDeclContextNameLookupTable::item_iterator::value_type Val
4083 = *ItemI;
4084 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
4085 Decls.clear();
4086 for (; Data.first != Data.second; ++Data.first)
4087 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
4088 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
4089 }
4090 }
4091}
4092
Sebastian Redl2c499f62010-08-18 23:56:43 +00004093void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004094 assert(Consumer);
4095 while (!InterestingDecls.empty()) {
4096 DeclGroupRef DG(InterestingDecls.front());
4097 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00004098 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004099 }
4100}
4101
Sebastian Redl2c499f62010-08-18 23:56:43 +00004102void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00004103 this->Consumer = Consumer;
4104
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004105 if (!Consumer)
4106 return;
4107
4108 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004109 // Force deserialization of this decl, which will cause it to be queued for
4110 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00004111 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004112 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00004113
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004114 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004115}
4116
Sebastian Redl2c499f62010-08-18 23:56:43 +00004117void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004118 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004119
Mike Stump11289f42009-09-09 15:08:12 +00004120 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00004121 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00004122 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00004123 unsigned NumDeclsLoaded
4124 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
4125 (Decl *)0);
4126 unsigned NumIdentifiersLoaded
4127 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
4128 IdentifiersLoaded.end(),
4129 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00004130 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00004131 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
4132 SelectorsLoaded.end(),
4133 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00004134
Douglas Gregorc5046832009-04-27 18:38:38 +00004135 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
4136 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor258ae542009-04-27 06:38:32 +00004137 if (TotalNumSLocEntries)
4138 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
4139 NumSLocEntriesRead, TotalNumSLocEntries,
4140 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00004141 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004142 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00004143 NumTypesLoaded, (unsigned)TypesLoaded.size(),
4144 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
4145 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004146 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00004147 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
4148 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00004149 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004150 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00004151 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
4152 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00004153 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004154 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00004155 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
4156 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00004157 if (TotalNumStatements)
4158 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
4159 NumStatementsRead, TotalNumStatements,
4160 ((float)NumStatementsRead/TotalNumStatements * 100));
4161 if (TotalNumMacros)
4162 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
4163 NumMacrosRead, TotalNumMacros,
4164 ((float)NumMacrosRead/TotalNumMacros * 100));
4165 if (TotalLexicalDeclContexts)
4166 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
4167 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4168 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4169 * 100));
4170 if (TotalVisibleDeclContexts)
4171 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
4172 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4173 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4174 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004175 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004176 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004177 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4178 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00004179 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004180 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00004181 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004182 std::fprintf(stderr, "\n");
4183}
4184
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00004185/// Return the amount of memory used by memory buffers, breaking down
4186/// by heap-backed versus mmap'ed memory.
4187void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
4188 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4189 if (llvm::MemoryBuffer *buf = Chain[i]->Buffer.get()) {
4190 size_t bytes = buf->getBufferSize();
4191 switch (buf->getBufferKind()) {
4192 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
4193 sizes.malloc_bytes += bytes;
4194 break;
4195 case llvm::MemoryBuffer::MemoryBuffer_MMap:
4196 sizes.mmap_bytes += bytes;
4197 break;
4198 }
4199 }
4200}
4201
Sebastian Redl2c499f62010-08-18 23:56:43 +00004202void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004203 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004204 S.ExternalSource = this;
4205
Douglas Gregor7cd60f72009-04-22 21:15:06 +00004206 // Makes sure any declarations that were deserialized "too early"
4207 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004208 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4209 if (SemaObj->TUScope)
John McCall48871652010-08-21 09:40:31 +00004210 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004211
4212 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004213 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00004214 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00004215
4216 // If there were any tentative definitions, deserialize them and add
Sebastian Redl35351a92010-01-31 22:27:38 +00004217 // them to Sema's list of tentative definitions.
Douglas Gregord4df8652009-04-22 22:02:47 +00004218 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4219 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redl35351a92010-01-31 22:27:38 +00004220 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregord4df8652009-04-22 22:02:47 +00004221 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00004222
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004223 // If there were any unused file scoped decls, deserialize them and add to
4224 // Sema's list of unused file scoped decls.
4225 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4226 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4227 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattner90073802010-02-12 00:07:30 +00004228 }
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004229
Alexis Hunt27a761d2011-05-04 23:29:54 +00004230 // If there were any delegating constructors, add them to Sema's list
4231 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
4232 CXXConstructorDecl *D
4233 = cast<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
4234 SemaObj->DelegatingCtorDecls.push_back(D);
4235 }
4236
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004237 // If there were any locally-scoped external declarations,
4238 // deserialize them and add them to Sema's table of locally-scoped
4239 // external declarations.
4240 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4241 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4242 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
4243 }
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004244
4245 // If there were any ext_vector type declarations, deserialize them
4246 // and add them to Sema's vector of such declarations.
4247 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
4248 SemaObj->ExtVectorDecls.push_back(
Richard Smithdda56e42011-04-15 14:24:37 +00004249 cast<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004250
4251 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
4252 // Can we cut them down before writing them ?
4253
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004254 // If there were any dynamic classes declarations, deserialize them
4255 // and add them to Sema's vector of such declarations.
4256 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
4257 SemaObj->DynamicClasses.push_back(
4258 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004259
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004260 // Load the offsets of the declarations that Sema references.
4261 // They will be lazily deserialized when needed.
4262 if (!SemaDeclRefs.empty()) {
4263 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
4264 SemaObj->StdNamespace = SemaDeclRefs[0];
4265 SemaObj->StdBadAlloc = SemaDeclRefs[1];
4266 }
4267
Sebastian Redl2c373b92010-10-05 15:59:54 +00004268 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
4269
4270 // If there are @selector references added them to its pool. This is for
4271 // implementation of -Wselector.
4272 if (!F->ReferencedSelectorsData.empty()) {
4273 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
4274 unsigned I = 0;
4275 while (I < DataSize) {
4276 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
4277 SourceLocation SelLoc = ReadSourceLocation(
4278 *F, F->ReferencedSelectorsData, I);
4279 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
4280 }
4281 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00004282 }
4283
Sebastian Redl14afaf02011-04-24 16:27:30 +00004284 // The special data sets below always come from the most recent PCH,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004285 // which is at the front of the chain.
4286 PerFileData &F = *Chain.front();
4287
Sebastian Redl14afaf02011-04-24 16:27:30 +00004288 // If there were any pending implicit instantiations, deserialize them
4289 // and add them to Sema's queue of such instantiations.
4290 assert(F.PendingInstantiations.size() % 2 == 0 &&
4291 "Expected pairs of entries");
4292 for (unsigned Idx = 0, N = F.PendingInstantiations.size(); Idx < N;) {
4293 ValueDecl *D=cast<ValueDecl>(GetDecl(F.PendingInstantiations[Idx++]));
4294 SourceLocation Loc = ReadSourceLocation(F, F.PendingInstantiations,Idx);
4295 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
4296 }
4297
Sebastian Redl2c373b92010-10-05 15:59:54 +00004298 // If there were any weak undeclared identifiers, deserialize them and add to
4299 // Sema's list of weak undeclared identifiers.
4300 if (!WeakUndeclaredIdentifiers.empty()) {
4301 unsigned Idx = 0;
4302 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
4303 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4304 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4305 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
4306 bool Used = WeakUndeclaredIdentifiers[Idx++];
4307 Sema::WeakInfo WI(AliasId, Loc);
4308 WI.setUsed(Used);
4309 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
4310 }
4311 }
4312
4313 // If there were any VTable uses, deserialize the information and add it
4314 // to Sema's vector and map of VTable uses.
4315 if (!VTableUses.empty()) {
4316 unsigned Idx = 0;
4317 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
4318 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4319 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
4320 bool DefinitionRequired = VTableUses[Idx++];
4321 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
4322 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004323 }
4324 }
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004325
4326 if (!FPPragmaOptions.empty()) {
4327 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4328 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4329 }
4330
4331 if (!OpenCLExtensions.empty()) {
4332 unsigned I = 0;
4333#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4334#include "clang/Basic/OpenCLExtensions.def"
4335
4336 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4337 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00004338}
4339
Sebastian Redl2c499f62010-08-18 23:56:43 +00004340IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redl78f51772010-08-02 18:30:12 +00004341 // Try to find this name within our on-disk hash tables. We start with the
4342 // most recent one, since that one contains the most up-to-date info.
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004343 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004344 ASTIdentifierLookupTable *IdTable
4345 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl5c415f32010-07-22 17:01:13 +00004346 if (!IdTable)
4347 continue;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004348 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004349 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004350 if (Pos == IdTable->end())
4351 continue;
Douglas Gregora868bbd2009-04-21 22:25:48 +00004352
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004353 // Dereferencing the iterator has the effect of building the
4354 // IdentifierInfo node and populating it with the various
4355 // declarations it needs.
Sebastian Redl78f51772010-08-02 18:30:12 +00004356 return *Pos;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004357 }
Sebastian Redl78f51772010-08-02 18:30:12 +00004358 return 0;
Douglas Gregora868bbd2009-04-21 22:25:48 +00004359}
4360
Douglas Gregor57756ea2010-10-14 22:11:03 +00004361namespace clang {
4362 /// \brief An identifier-lookup iterator that enumerates all of the
4363 /// identifiers stored within a set of AST files.
4364 class ASTIdentifierIterator : public IdentifierIterator {
4365 /// \brief The AST reader whose identifiers are being enumerated.
4366 const ASTReader &Reader;
4367
4368 /// \brief The current index into the chain of AST files stored in
4369 /// the AST reader.
4370 unsigned Index;
4371
4372 /// \brief The current position within the identifier lookup table
4373 /// of the current AST file.
4374 ASTIdentifierLookupTable::key_iterator Current;
4375
4376 /// \brief The end position within the identifier lookup table of
4377 /// the current AST file.
4378 ASTIdentifierLookupTable::key_iterator End;
4379
4380 public:
4381 explicit ASTIdentifierIterator(const ASTReader &Reader);
4382
4383 virtual llvm::StringRef Next();
4384 };
4385}
4386
4387ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
4388 : Reader(Reader), Index(Reader.Chain.size() - 1) {
4389 ASTIdentifierLookupTable *IdTable
4390 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4391 Current = IdTable->key_begin();
4392 End = IdTable->key_end();
4393}
4394
4395llvm::StringRef ASTIdentifierIterator::Next() {
4396 while (Current == End) {
4397 // If we have exhausted all of our AST files, we're done.
4398 if (Index == 0)
4399 return llvm::StringRef();
4400
4401 --Index;
4402 ASTIdentifierLookupTable *IdTable
4403 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4404 Current = IdTable->key_begin();
4405 End = IdTable->key_end();
4406 }
4407
4408 // We have any identifiers remaining in the current AST file; return
4409 // the next one.
4410 std::pair<const char*, unsigned> Key = *Current;
4411 ++Current;
4412 return llvm::StringRef(Key.first, Key.second);
4413}
4414
4415IdentifierIterator *ASTReader::getIdentifiers() const {
4416 return new ASTIdentifierIterator(*this);
4417}
4418
Mike Stump11289f42009-09-09 15:08:12 +00004419std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redl2c499f62010-08-18 23:56:43 +00004420ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redlada023c2010-08-04 20:40:17 +00004421 // Find this selector in a hash table. We want to find the most recent entry.
4422 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4423 PerFileData &F = *Chain[I];
4424 if (!F.SelectorLookupTable)
4425 continue;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004426
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004427 ASTSelectorLookupTable *PoolTable
4428 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
4429 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redlada023c2010-08-04 20:40:17 +00004430 if (Pos != PoolTable->end()) {
4431 ++NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004432 // FIXME: Not quite happy with the statistics here. We probably should
4433 // disable this tracking when called via LoadSelector.
4434 // Also, should entries without methods count as misses?
4435 ++NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004436 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redlada023c2010-08-04 20:40:17 +00004437 if (DeserializationListener)
4438 DeserializationListener->SelectorRead(Data.ID, Sel);
4439 return std::make_pair(Data.Instance, Data.Factory);
4440 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004441 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00004442
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004443 ++NumMethodPoolMisses;
Sebastian Redlada023c2010-08-04 20:40:17 +00004444 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorc78d3462009-04-24 21:10:55 +00004445}
4446
Sebastian Redl2c499f62010-08-18 23:56:43 +00004447void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004448 // It would be complicated to avoid reading the methods anyway. So don't.
4449 ReadMethodPool(Sel);
4450}
4451
Sebastian Redl2c499f62010-08-18 23:56:43 +00004452void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004453 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00004454 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00004455 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00004456 if (DeserializationListener)
4457 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004458}
4459
Douglas Gregor1342e842009-07-06 18:54:52 +00004460/// \brief Set the globally-visible declarations associated with the given
4461/// identifier.
4462///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004463/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00004464/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00004465/// them.
4466///
4467/// \param II an IdentifierInfo that refers to one or more globally-visible
4468/// declarations.
4469///
4470/// \param DeclIDs the set of declaration IDs with the name @p II that are
4471/// visible at global scope.
4472///
4473/// \param Nonrecursive should be true to indicate that the caller knows that
4474/// this call is non-recursive, and therefore the globally-visible declarations
4475/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00004476void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004477ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregor1342e842009-07-06 18:54:52 +00004478 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
4479 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004480 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004481 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4482 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4483 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004484 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00004485 return;
4486 }
Mike Stump11289f42009-09-09 15:08:12 +00004487
Douglas Gregor1342e842009-07-06 18:54:52 +00004488 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4489 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4490 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004491 if (SemaObj->TUScope) {
4492 // Introduce this declaration into the translation-unit scope
4493 // and add it to the declaration chain for this identifier, so
4494 // that (unqualified) name lookup will find it.
John McCall48871652010-08-21 09:40:31 +00004495 SemaObj->TUScope->AddDecl(D);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004496 }
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004497 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregor1342e842009-07-06 18:54:52 +00004498 } else {
4499 // Queue this declaration so that it will be added to the
4500 // translation unit scope and identifier's declaration chain
4501 // once a Sema object is known.
4502 PreloadedDecls.push_back(D);
4503 }
4504 }
4505}
4506
Sebastian Redl2c499f62010-08-18 23:56:43 +00004507IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004508 if (ID == 0)
4509 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00004510
Sebastian Redlc713b962010-07-21 00:46:22 +00004511 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004512 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004513 return 0;
4514 }
Mike Stump11289f42009-09-09 15:08:12 +00004515
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004516 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc713b962010-07-21 00:46:22 +00004517 ID -= 1;
4518 if (!IdentifiersLoaded[ID]) {
4519 unsigned Index = ID;
4520 const char *Str = 0;
4521 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4522 PerFileData *F = Chain[N - I - 1];
4523 if (Index < F->LocalNumIdentifiers) {
4524 uint32_t Offset = F->IdentifierOffsets[Index];
4525 Str = F->IdentifierTableData + Offset;
4526 break;
4527 }
4528 Index -= F->LocalNumIdentifiers;
4529 }
4530 assert(Str && "Broken Chain");
Douglas Gregor5287b4e2009-04-25 21:04:17 +00004531
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004532 // All of the strings in the AST file are preceded by a 16-bit length.
4533 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00004534 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4535 // unsigned integers. This is important to avoid integer overflow when
4536 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00004537 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00004538 unsigned StrLen = (((unsigned) StrLenPtr[0])
4539 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00004540 IdentifiersLoaded[ID]
Kovarththanan Rajaratnama3b09592010-03-12 10:32:27 +00004541 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlff4a2952010-07-23 23:49:55 +00004542 if (DeserializationListener)
4543 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004544 }
Mike Stump11289f42009-09-09 15:08:12 +00004545
Sebastian Redlc713b962010-07-21 00:46:22 +00004546 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004547}
4548
Douglas Gregor49f754f2011-04-20 00:21:03 +00004549bool ASTReader::ReadSLocEntry(unsigned ID) {
4550 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor258ae542009-04-27 06:38:32 +00004551}
4552
Sebastian Redl2c499f62010-08-18 23:56:43 +00004553Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00004554 if (ID == 0)
4555 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00004556
Sebastian Redlada023c2010-08-04 20:40:17 +00004557 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004558 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00004559 return Selector();
4560 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004561
Sebastian Redlada023c2010-08-04 20:40:17 +00004562 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004563 // Load this selector from the selector table.
Sebastian Redlada023c2010-08-04 20:40:17 +00004564 unsigned Idx = ID - 1;
4565 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4566 PerFileData &F = *Chain[N - I - 1];
4567 if (Idx < F.LocalNumSelectors) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004568 ASTSelectorLookupTrait Trait(*this);
Sebastian Redlada023c2010-08-04 20:40:17 +00004569 SelectorsLoaded[ID - 1] =
4570 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4571 if (DeserializationListener)
4572 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4573 break;
4574 }
4575 Idx -= F.LocalNumSelectors;
4576 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004577 }
4578
Sebastian Redlada023c2010-08-04 20:40:17 +00004579 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00004580}
4581
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004582Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00004583 return DecodeSelector(ID);
4584}
4585
Sebastian Redl2c499f62010-08-18 23:56:43 +00004586uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00004587 // ID 0 (the null selector) is considered an external selector.
4588 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00004589}
4590
Mike Stump11289f42009-09-09 15:08:12 +00004591DeclarationName
Sebastian Redl2c499f62010-08-18 23:56:43 +00004592ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004593 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4594 switch (Kind) {
4595 case DeclarationName::Identifier:
4596 return DeclarationName(GetIdentifierInfo(Record, Idx));
4597
4598 case DeclarationName::ObjCZeroArgSelector:
4599 case DeclarationName::ObjCOneArgSelector:
4600 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff3c301dc2009-04-23 15:15:40 +00004601 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004602
4603 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004604 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004605 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004606
4607 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004608 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004609 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004610
4611 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004612 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004613 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004614
4615 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004616 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004617 (OverloadedOperatorKind)Record[Idx++]);
4618
Alexis Hunt3d221f22009-11-29 07:34:05 +00004619 case DeclarationName::CXXLiteralOperatorName:
4620 return Context->DeclarationNames.getCXXLiteralOperatorName(
4621 GetIdentifierInfo(Record, Idx));
4622
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004623 case DeclarationName::CXXUsingDirective:
4624 return DeclarationName::getUsingDirectiveName();
4625 }
4626
4627 // Required to silence GCC warning
4628 return DeclarationName();
4629}
Douglas Gregor55abb232009-04-10 20:39:37 +00004630
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004631void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4632 DeclarationNameLoc &DNLoc,
4633 DeclarationName Name,
4634 const RecordData &Record, unsigned &Idx) {
4635 switch (Name.getNameKind()) {
4636 case DeclarationName::CXXConstructorName:
4637 case DeclarationName::CXXDestructorName:
4638 case DeclarationName::CXXConversionFunctionName:
4639 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4640 break;
4641
4642 case DeclarationName::CXXOperatorName:
4643 DNLoc.CXXOperatorName.BeginOpNameLoc
4644 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4645 DNLoc.CXXOperatorName.EndOpNameLoc
4646 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4647 break;
4648
4649 case DeclarationName::CXXLiteralOperatorName:
4650 DNLoc.CXXLiteralOperatorName.OpNameLoc
4651 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4652 break;
4653
4654 case DeclarationName::Identifier:
4655 case DeclarationName::ObjCZeroArgSelector:
4656 case DeclarationName::ObjCOneArgSelector:
4657 case DeclarationName::ObjCMultiArgSelector:
4658 case DeclarationName::CXXUsingDirective:
4659 break;
4660 }
4661}
4662
4663void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4664 DeclarationNameInfo &NameInfo,
4665 const RecordData &Record, unsigned &Idx) {
4666 NameInfo.setName(ReadDeclarationName(Record, Idx));
4667 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4668 DeclarationNameLoc DNLoc;
4669 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4670 NameInfo.setInfo(DNLoc);
4671}
4672
4673void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4674 const RecordData &Record, unsigned &Idx) {
Douglas Gregor14454802011-02-25 02:25:35 +00004675 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004676 unsigned NumTPLists = Record[Idx++];
4677 Info.NumTemplParamLists = NumTPLists;
4678 if (NumTPLists) {
4679 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4680 for (unsigned i=0; i != NumTPLists; ++i)
4681 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4682 }
4683}
4684
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004685TemplateName
Douglas Gregor5590be02011-01-15 06:45:20 +00004686ASTReader::ReadTemplateName(PerFileData &F, const RecordData &Record,
4687 unsigned &Idx) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004688 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004689 switch (Kind) {
4690 case TemplateName::Template:
4691 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4692
4693 case TemplateName::OverloadedTemplate: {
4694 unsigned size = Record[Idx++];
4695 UnresolvedSet<8> Decls;
4696 while (size--)
4697 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4698
4699 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4700 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004701
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004702 case TemplateName::QualifiedTemplate: {
4703 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4704 bool hasTemplKeyword = Record[Idx++];
4705 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4706 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4707 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004708
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004709 case TemplateName::DependentTemplate: {
4710 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4711 if (Record[Idx++]) // isIdentifier
4712 return Context->getDependentTemplateName(NNS,
4713 GetIdentifierInfo(Record, Idx));
4714 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004715 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004716 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004717
4718 case TemplateName::SubstTemplateTemplateParmPack: {
4719 TemplateTemplateParmDecl *Param
4720 = cast_or_null<TemplateTemplateParmDecl>(GetDecl(Record[Idx++]));
4721 if (!Param)
4722 return TemplateName();
4723
4724 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
4725 if (ArgPack.getKind() != TemplateArgument::Pack)
4726 return TemplateName();
4727
4728 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
4729 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004730 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004731
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004732 assert(0 && "Unhandled template name kind!");
4733 return TemplateName();
4734}
4735
4736TemplateArgument
Sebastian Redl2c373b92010-10-05 15:59:54 +00004737ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00004738 const RecordData &Record, unsigned &Idx) {
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004739 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
4740 switch (Kind) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004741 case TemplateArgument::Null:
4742 return TemplateArgument();
4743 case TemplateArgument::Type:
4744 return TemplateArgument(GetType(Record[Idx++]));
4745 case TemplateArgument::Declaration:
4746 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00004747 case TemplateArgument::Integral: {
4748 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4749 QualType T = GetType(Record[Idx++]);
4750 return TemplateArgument(Value, T);
4751 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004752 case TemplateArgument::Template:
Douglas Gregor5590be02011-01-15 06:45:20 +00004753 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004754 case TemplateArgument::TemplateExpansion: {
Douglas Gregor5590be02011-01-15 06:45:20 +00004755 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregore1d60df2011-01-14 23:41:42 +00004756 llvm::Optional<unsigned> NumTemplateExpansions;
4757 if (unsigned NumExpansions = Record[Idx++])
4758 NumTemplateExpansions = NumExpansions - 1;
4759 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004760 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004761 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004762 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004763 case TemplateArgument::Pack: {
4764 unsigned NumArgs = Record[Idx++];
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004765 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4766 for (unsigned I = 0; I != NumArgs; ++I)
4767 Args[I] = ReadTemplateArgument(F, Record, Idx);
4768 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004769 }
4770 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004771
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004772 assert(0 && "Unhandled template argument kind!");
4773 return TemplateArgument();
4774}
4775
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004776TemplateParameterList *
Sebastian Redl2c373b92010-10-05 15:59:54 +00004777ASTReader::ReadTemplateParameterList(PerFileData &F,
4778 const RecordData &Record, unsigned &Idx) {
4779 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4780 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4781 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004782
4783 unsigned NumParams = Record[Idx++];
4784 llvm::SmallVector<NamedDecl *, 16> Params;
4785 Params.reserve(NumParams);
4786 while (NumParams--)
4787 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004788
4789 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004790 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4791 Params.data(), Params.size(), RAngleLoc);
4792 return TemplateParams;
4793}
4794
4795void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004796ASTReader::
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004797ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004798 PerFileData &F, const RecordData &Record,
4799 unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004800 unsigned NumTemplateArgs = Record[Idx++];
4801 TemplArgs.reserve(NumTemplateArgs);
4802 while (NumTemplateArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004803 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004804}
4805
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004806/// \brief Read a UnresolvedSet structure.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004807void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004808 const RecordData &Record, unsigned &Idx) {
4809 unsigned NumDecls = Record[Idx++];
4810 while (NumDecls--) {
4811 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4812 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4813 Set.addDecl(D, AS);
4814 }
4815}
4816
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004817CXXBaseSpecifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00004818ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky19b9f952010-07-26 16:56:01 +00004819 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004820 bool isVirtual = static_cast<bool>(Record[Idx++]);
4821 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4822 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redl08905022011-02-05 19:23:19 +00004823 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004824 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4825 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor752a5952011-01-03 22:36:02 +00004826 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl08905022011-02-05 19:23:19 +00004827 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregor752a5952011-01-03 22:36:02 +00004828 EllipsisLoc);
Sebastian Redl08905022011-02-05 19:23:19 +00004829 Result.setInheritConstructors(inheritConstructors);
4830 return Result;
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004831}
4832
Alexis Hunt1d792652011-01-08 20:30:50 +00004833std::pair<CXXCtorInitializer **, unsigned>
4834ASTReader::ReadCXXCtorInitializers(PerFileData &F, const RecordData &Record,
4835 unsigned &Idx) {
4836 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004837 unsigned NumInitializers = Record[Idx++];
4838 if (NumInitializers) {
4839 ASTContext &C = *getContext();
4840
Alexis Hunt1d792652011-01-08 20:30:50 +00004841 CtorInitializers
4842 = new (C) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004843 for (unsigned i=0; i != NumInitializers; ++i) {
4844 TypeSourceInfo *BaseClassInfo = 0;
4845 bool IsBaseVirtual = false;
4846 FieldDecl *Member = 0;
Francois Pichetd583da02010-12-04 09:14:42 +00004847 IndirectFieldDecl *IndirectMember = 0;
Alexis Hunt37a477f2011-05-04 01:19:08 +00004848 CXXConstructorDecl *Target = 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004849
Alexis Hunt37a477f2011-05-04 01:19:08 +00004850 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
4851 switch (Type) {
4852 case CTOR_INITIALIZER_BASE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004853 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004854 IsBaseVirtual = Record[Idx++];
Alexis Hunt37a477f2011-05-04 01:19:08 +00004855 break;
4856
4857 case CTOR_INITIALIZER_DELEGATING:
4858 Target = cast<CXXConstructorDecl>(GetDecl(Record[Idx++]));
4859 break;
4860
4861 case CTOR_INITIALIZER_MEMBER:
4862 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
4863 break;
4864
4865 case CTOR_INITIALIZER_INDIRECT_MEMBER:
4866 IndirectMember = cast<IndirectFieldDecl>(GetDecl(Record[Idx++]));
4867 break;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004868 }
Alexis Hunt37a477f2011-05-04 01:19:08 +00004869
Douglas Gregor44e7df62011-01-04 00:32:56 +00004870 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004871 Expr *Init = ReadExpr(F);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004872 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4873 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004874 bool IsWritten = Record[Idx++];
4875 unsigned SourceOrderOrNumArrayIndices;
4876 llvm::SmallVector<VarDecl *, 8> Indices;
4877 if (IsWritten) {
4878 SourceOrderOrNumArrayIndices = Record[Idx++];
4879 } else {
4880 SourceOrderOrNumArrayIndices = Record[Idx++];
4881 Indices.reserve(SourceOrderOrNumArrayIndices);
4882 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4883 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4884 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004885
Alexis Hunt1d792652011-01-08 20:30:50 +00004886 CXXCtorInitializer *BOMInit;
Alexis Hunt37a477f2011-05-04 01:19:08 +00004887 if (Type == CTOR_INITIALIZER_BASE) {
Alexis Hunt1d792652011-01-08 20:30:50 +00004888 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
4889 LParenLoc, Init, RParenLoc,
4890 MemberOrEllipsisLoc);
Alexis Hunt37a477f2011-05-04 01:19:08 +00004891 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
4892 BOMInit = new (C) CXXCtorInitializer(C, MemberOrEllipsisLoc, LParenLoc,
4893 Target, Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004894 } else if (IsWritten) {
Francois Pichetd583da02010-12-04 09:14:42 +00004895 if (Member)
Alexis Hunt1d792652011-01-08 20:30:50 +00004896 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
4897 LParenLoc, Init, RParenLoc);
Francois Pichetd583da02010-12-04 09:14:42 +00004898 else
Alexis Hunt1d792652011-01-08 20:30:50 +00004899 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
4900 MemberOrEllipsisLoc, LParenLoc,
4901 Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004902 } else {
Alexis Hunt1d792652011-01-08 20:30:50 +00004903 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
4904 LParenLoc, Init, RParenLoc,
4905 Indices.data(), Indices.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004906 }
4907
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00004908 if (IsWritten)
4909 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Alexis Hunt1d792652011-01-08 20:30:50 +00004910 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004911 }
4912 }
4913
Alexis Hunt1d792652011-01-08 20:30:50 +00004914 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004915}
4916
Chris Lattnerca025db2010-05-07 21:43:38 +00004917NestedNameSpecifier *
Sebastian Redl2c499f62010-08-18 23:56:43 +00004918ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004919 unsigned N = Record[Idx++];
4920 NestedNameSpecifier *NNS = 0, *Prev = 0;
4921 for (unsigned I = 0; I != N; ++I) {
4922 NestedNameSpecifier::SpecifierKind Kind
4923 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4924 switch (Kind) {
4925 case NestedNameSpecifier::Identifier: {
4926 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4927 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4928 break;
4929 }
4930
4931 case NestedNameSpecifier::Namespace: {
4932 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4933 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4934 break;
4935 }
4936
Douglas Gregor7b26ff92011-02-24 02:36:08 +00004937 case NestedNameSpecifier::NamespaceAlias: {
4938 NamespaceAliasDecl *Alias
4939 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++]));
4940 NNS = NestedNameSpecifier::Create(*Context, Prev, Alias);
4941 break;
4942 }
4943
Chris Lattnerca025db2010-05-07 21:43:38 +00004944 case NestedNameSpecifier::TypeSpec:
4945 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall424cec92011-01-19 06:33:43 +00004946 const Type *T = GetType(Record[Idx++]).getTypePtrOrNull();
Douglas Gregor0cdc8322010-12-10 17:03:06 +00004947 if (!T)
4948 return 0;
4949
Chris Lattnerca025db2010-05-07 21:43:38 +00004950 bool Template = Record[Idx++];
4951 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4952 break;
4953 }
4954
4955 case NestedNameSpecifier::Global: {
4956 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4957 // No associated value, and there can't be a prefix.
4958 break;
4959 }
Chris Lattnerca025db2010-05-07 21:43:38 +00004960 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00004961 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00004962 }
4963 return NNS;
4964}
4965
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004966NestedNameSpecifierLoc
4967ASTReader::ReadNestedNameSpecifierLoc(PerFileData &F, const RecordData &Record,
4968 unsigned &Idx) {
4969 unsigned N = Record[Idx++];
Douglas Gregor9b272512011-02-28 23:58:31 +00004970 NestedNameSpecifierLocBuilder Builder;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004971 for (unsigned I = 0; I != N; ++I) {
4972 NestedNameSpecifier::SpecifierKind Kind
4973 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4974 switch (Kind) {
4975 case NestedNameSpecifier::Identifier: {
Douglas Gregor9b272512011-02-28 23:58:31 +00004976 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004977 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00004978 Builder.Extend(*Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004979 break;
4980 }
4981
4982 case NestedNameSpecifier::Namespace: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004983 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004984 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00004985 Builder.Extend(*Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004986 break;
4987 }
4988
4989 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004990 NamespaceAliasDecl *Alias
4991 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++]));
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004992 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00004993 Builder.Extend(*Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004994 break;
4995 }
4996
4997 case NestedNameSpecifier::TypeSpec:
4998 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004999 bool Template = Record[Idx++];
5000 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
5001 if (!T)
5002 return NestedNameSpecifierLoc();
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005003 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005004
5005 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
5006 Builder.Extend(*Context,
5007 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
5008 T->getTypeLoc(), ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005009 break;
5010 }
5011
5012 case NestedNameSpecifier::Global: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005013 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005014 Builder.MakeGlobal(*Context, ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005015 break;
5016 }
5017 }
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005018 }
5019
Douglas Gregor9b272512011-02-28 23:58:31 +00005020 return Builder.getWithLocInContext(*Context);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005021}
5022
Chris Lattnerca025db2010-05-07 21:43:38 +00005023SourceRange
Sebastian Redl2c373b92010-10-05 15:59:54 +00005024ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
5025 unsigned &Idx) {
5026 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
5027 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00005028 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00005029}
5030
Douglas Gregor1daeb692009-04-13 18:14:40 +00005031/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005032llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00005033 unsigned BitWidth = Record[Idx++];
5034 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
5035 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
5036 Idx += NumWords;
5037 return Result;
5038}
5039
5040/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005041llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00005042 bool isUnsigned = Record[Idx++];
5043 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
5044}
5045
Douglas Gregore0a3a512009-04-14 21:55:33 +00005046/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005047llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00005048 return llvm::APFloat(ReadAPInt(Record, Idx));
5049}
5050
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005051// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00005052std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005053 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00005054 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005055 Idx += Len;
5056 return Result;
5057}
5058
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005059VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
5060 unsigned &Idx) {
5061 unsigned Major = Record[Idx++];
5062 unsigned Minor = Record[Idx++];
5063 unsigned Subminor = Record[Idx++];
5064 if (Minor == 0)
5065 return VersionTuple(Major);
5066 if (Subminor == 0)
5067 return VersionTuple(Major, Minor - 1);
5068 return VersionTuple(Major, Minor - 1, Subminor - 1);
5069}
5070
Sebastian Redl2c499f62010-08-18 23:56:43 +00005071CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00005072 unsigned &Idx) {
5073 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
5074 return CXXTemporary::Create(*Context, Decl);
5075}
5076
Sebastian Redl2c499f62010-08-18 23:56:43 +00005077DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00005078 return Diag(SourceLocation(), DiagID);
5079}
5080
Sebastian Redl2c499f62010-08-18 23:56:43 +00005081DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005082 return Diags.Report(Loc, DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00005083}
Douglas Gregora9af1d12009-04-17 00:04:06 +00005084
Douglas Gregora868bbd2009-04-21 22:25:48 +00005085/// \brief Retrieve the identifier table associated with the
5086/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005087IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00005088 assert(PP && "Forgot to set Preprocessor ?");
5089 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00005090}
5091
Douglas Gregora9af1d12009-04-17 00:04:06 +00005092/// \brief Record that the given ID maps to the given switch-case
5093/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005094void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00005095 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
5096 SwitchCaseStmts[ID] = SC;
5097}
5098
5099/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005100SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00005101 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
5102 return SwitchCaseStmts[ID];
5103}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00005104
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00005105void ASTReader::ClearSwitchCaseIDs() {
5106 SwitchCaseStmts.clear();
5107}
5108
Sebastian Redl2c499f62010-08-18 23:56:43 +00005109void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005110 assert(NumCurrentElementsDeserializing &&
5111 "FinishedDeserializing not paired with StartedDeserializing");
5112 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00005113 // If any identifiers with corresponding top-level declarations have
5114 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005115 while (!PendingIdentifierInfos.empty()) {
5116 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
5117 PendingIdentifierInfos.front().DeclIDs, true);
5118 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00005119 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005120
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00005121 // Ready to load previous declarations of Decls that were delayed.
5122 while (!PendingPreviousDecls.empty()) {
5123 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
5124 PendingPreviousDecls.front().second);
5125 PendingPreviousDecls.pop_front();
5126 }
5127
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005128 // We are not in recursive loading, so it's safe to pass the "interesting"
5129 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005130 if (Consumer)
5131 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidisad5f95c2010-10-24 17:26:31 +00005132
5133 assert(PendingForwardRefs.size() == 0 &&
5134 "Some forward refs did not get linked to the definition!");
Douglas Gregor1342e842009-07-06 18:54:52 +00005135 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005136 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00005137}
Douglas Gregorb473b072010-08-19 00:28:17 +00005138
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005139ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005140 const char *isysroot, bool DisableValidation,
5141 bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005142 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
5143 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
5144 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
5145 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005146 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
5147 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NextSLocOffset(0),
5148 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
5149 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
5150 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
5151 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
5152 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
5153 NumCurrentElementsDeserializing(0)
5154{
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005155 RelocatablePCH = false;
5156}
5157
5158ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
5159 Diagnostic &Diags, const char *isysroot,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005160 bool DisableValidation, bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005161 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
5162 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005163 isysroot(isysroot), DisableValidation(DisableValidation),
5164 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
5165 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00005166 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
5167 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
5168 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
5169 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
5170 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
5171 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005172 RelocatablePCH = false;
5173}
5174
5175ASTReader::~ASTReader() {
5176 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
5177 delete Chain[e - i - 1];
5178 // Delete all visible decl lookup tables
5179 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
5180 E = DeclContextOffsets.end();
5181 I != E; ++I) {
5182 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
5183 J != F; ++J) {
5184 if (J->NameLookupTableData)
5185 delete static_cast<ASTDeclContextNameLookupTable*>(
5186 J->NameLookupTableData);
5187 }
5188 }
5189 for (DeclContextVisibleUpdatesPending::iterator
5190 I = PendingVisibleUpdates.begin(),
5191 E = PendingVisibleUpdates.end();
5192 I != E; ++I) {
5193 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5194 F = I->second.end();
5195 J != F; ++J)
5196 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
5197 }
5198}
5199
Sebastian Redl009e7f22010-10-05 16:15:19 +00005200ASTReader::PerFileData::PerFileData(ASTFileType Ty)
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00005201 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0),
5202 SLocFileOffsets(0), LocalSLocSize(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00005203 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
5204 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
Douglas Gregor09b69892011-02-10 17:09:37 +00005205 MacroDefinitionOffsets(0),
5206 LocalNumHeaderFileInfos(0), HeaderFileInfoTableData(0),
5207 HeaderFileInfoTable(0),
5208 LocalNumSelectors(0), SelectorOffsets(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00005209 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005210 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
5211 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redl3f6b7532010-10-01 19:59:12 +00005212 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregorb473b072010-08-19 00:28:17 +00005213{}
5214
5215ASTReader::PerFileData::~PerFileData() {
5216 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
Douglas Gregor09b69892011-02-10 17:09:37 +00005217 delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable);
Douglas Gregorb473b072010-08-19 00:28:17 +00005218 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
5219}