blob: 0ab1704b117de5dc1637df5e3d5084bb0c515182 [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
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001845/// \brief If we are loading a relocatable PCH file, and the filename is
1846/// not an absolute path, add the system root to the beginning of the file
1847/// name.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001848void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001849 // If this is not a relocatable PCH file, there's nothing to do.
1850 if (!RelocatablePCH)
1851 return;
Mike Stump11289f42009-09-09 15:08:12 +00001852
Michael J. Spencerf28df4c2010-12-17 21:22:22 +00001853 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001854 return;
1855
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001856 if (isysroot == 0) {
1857 // If no system root was given, default to '/'
1858 Filename.insert(Filename.begin(), '/');
1859 return;
1860 }
Mike Stump11289f42009-09-09 15:08:12 +00001861
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001862 unsigned Length = strlen(isysroot);
1863 if (isysroot[Length - 1] != '/')
1864 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001865
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001866 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1867}
1868
Sebastian Redl2c499f62010-08-18 23:56:43 +00001869ASTReader::ASTReadResult
Sebastian Redl3e31c722010-08-18 23:56:56 +00001870ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001871 llvm::BitstreamCursor &Stream = F.Stream;
1872
Sebastian Redl539c5062010-08-18 23:57:32 +00001873 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001874 Error("malformed block record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001875 return Failure;
1876 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001877
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001878 // Read all of the records and blocks for the ASt file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001879 RecordData Record;
Sebastian Redl393f8b72010-07-19 20:52:06 +00001880 bool First = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001881 while (!Stream.AtEndOfStream()) {
1882 unsigned Code = Stream.ReadCode();
1883 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001884 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001885 Error("error at end of module block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001886 return Failure;
1887 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001888
Douglas Gregor55abb232009-04-10 20:39:37 +00001889 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001890 }
1891
1892 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1893 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001894 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001895 // We lazily load the decls block, but we want to set up the
1896 // DeclsCursor cursor to point into it. Clone our current bitcode
1897 // cursor to it, enter the block and read the abbrevs in that block.
1898 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001899 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001900 if (Stream.SkipBlock() || // Skip with the main cursor.
1901 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001902 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001903 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001904 return Failure;
1905 }
1906 break;
Mike Stump11289f42009-09-09 15:08:12 +00001907
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00001908 case DECL_UPDATES_BLOCK_ID:
1909 if (Stream.SkipBlock()) {
1910 Error("malformed block record in AST file");
1911 return Failure;
1912 }
1913 break;
1914
Sebastian Redl539c5062010-08-18 23:57:32 +00001915 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001916 F.MacroCursor = Stream;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001917 if (PP)
1918 PP->setExternalSource(this);
1919
Douglas Gregor796d76a2010-10-20 22:00:55 +00001920 if (Stream.SkipBlock() ||
1921 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001922 Error("malformed block record in AST file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001923 return Failure;
1924 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001925 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001926 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001927
Douglas Gregor92a96f52011-02-08 21:58:10 +00001928 case PREPROCESSOR_DETAIL_BLOCK_ID:
1929 F.PreprocessorDetailCursor = Stream;
1930 if (Stream.SkipBlock() ||
1931 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1932 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1933 Error("malformed preprocessor detail record in AST file");
1934 return Failure;
1935 }
1936 F.PreprocessorDetailStartOffset
1937 = F.PreprocessorDetailCursor.GetCurrentBitNo();
1938 break;
1939
Sebastian Redl539c5062010-08-18 23:57:32 +00001940 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001941 switch (ReadSourceManagerBlock(F)) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001942 case Success:
1943 break;
1944
1945 case Failure:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001946 Error("malformed source manager block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001947 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001948
1949 case IgnorePCH:
1950 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001951 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001952 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001953 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00001954 First = false;
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001955 continue;
1956 }
1957
1958 if (Code == llvm::bitc::DEFINE_ABBREV) {
1959 Stream.ReadAbbrevRecord();
1960 continue;
1961 }
1962
1963 // Read and process a record.
1964 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001965 const char *BlobStart = 0;
1966 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001967 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001968 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001969 default: // Default behavior: ignore.
1970 break;
1971
Sebastian Redl539c5062010-08-18 23:57:32 +00001972 case METADATA: {
1973 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1974 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001975 : diag::warn_pch_version_too_new);
1976 return IgnorePCH;
1977 }
1978
1979 RelocatablePCH = Record[4];
1980 if (Listener) {
1981 std::string TargetTriple(BlobStart, BlobLen);
1982 if (Listener->ReadTargetTriple(TargetTriple))
1983 return IgnorePCH;
1984 }
1985 break;
1986 }
1987
Sebastian Redl539c5062010-08-18 23:57:32 +00001988 case CHAINED_METADATA: {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001989 if (!First) {
1990 Error("CHAINED_METADATA is not first record in block");
1991 return Failure;
1992 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001993 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1994 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001995 : diag::warn_pch_version_too_new);
1996 return IgnorePCH;
1997 }
1998
Sebastian Redl009e7f22010-10-05 16:15:19 +00001999 // Load the chained file, which is always a PCH file.
2000 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002001 case Failure: return Failure;
2002 // If we have to ignore the dependency, we'll have to ignore this too.
2003 case IgnorePCH: return IgnorePCH;
2004 case Success: break;
2005 }
2006 break;
2007 }
2008
Sebastian Redl539c5062010-08-18 23:57:32 +00002009 case TYPE_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00002010 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002011 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002012 return Failure;
2013 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002014 F.TypeOffsets = (const uint32_t *)BlobStart;
2015 F.LocalNumTypes = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002016 break;
2017
Sebastian Redl539c5062010-08-18 23:57:32 +00002018 case DECL_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00002019 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002020 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002021 return Failure;
2022 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002023 F.DeclOffsets = (const uint32_t *)BlobStart;
2024 F.LocalNumDecls = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002025 break;
Douglas Gregor55abb232009-04-10 20:39:37 +00002026
Sebastian Redl539c5062010-08-18 23:57:32 +00002027 case TU_UPDATE_LEXICAL: {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002028 DeclContextInfo Info = {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002029 /* No visible information */ 0,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002030 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
2031 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002032 };
Douglas Gregoraa433012010-10-01 01:18:02 +00002033 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
2034 .push_back(Info);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002035 break;
2036 }
2037
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002038 case UPDATE_VISIBLE: {
2039 serialization::DeclID ID = Record[0];
2040 void *Table = ASTDeclContextNameLookupTable::Create(
2041 (const unsigned char *)BlobStart + Record[1],
2042 (const unsigned char *)BlobStart,
2043 ASTDeclContextNameLookupTrait(*this));
Douglas Gregoraa433012010-10-01 01:18:02 +00002044 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002045 DeclContextInfo Info = {
2046 Table, /* No lexical inforamtion */ 0, 0
2047 };
2048 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
2049 } else
2050 PendingVisibleUpdates[ID].push_back(Table);
2051 break;
2052 }
2053
Sebastian Redl539c5062010-08-18 23:57:32 +00002054 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002055 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
2056 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002057 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002058 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
2059 Latest > FirstLatestDeclIDs[First]) &&
2060 "The new latest is supposed to come after the previous latest");
2061 FirstLatestDeclIDs[First] = Latest;
2062 }
2063 break;
2064 }
2065
Sebastian Redl539c5062010-08-18 23:57:32 +00002066 case LANGUAGE_OPTIONS:
Douglas Gregorce3a8292010-07-27 00:27:13 +00002067 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor55abb232009-04-10 20:39:37 +00002068 return IgnorePCH;
2069 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00002070
Sebastian Redl539c5062010-08-18 23:57:32 +00002071 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00002072 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00002073 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00002074 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002075 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00002076 (const unsigned char *)F.IdentifierTableData + Record[0],
2077 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl2c373b92010-10-05 15:59:54 +00002078 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002079 if (PP)
2080 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00002081 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002082 break;
2083
Sebastian Redl539c5062010-08-18 23:57:32 +00002084 case IDENTIFIER_OFFSET:
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002085 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002086 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002087 return Failure;
2088 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002089 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2090 F.LocalNumIdentifiers = Record[0];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002091 break;
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002092
Sebastian Redl539c5062010-08-18 23:57:32 +00002093 case EXTERNAL_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002094 // Optimization for the first block.
2095 if (ExternalDefinitions.empty())
2096 ExternalDefinitions.swap(Record);
2097 else
2098 ExternalDefinitions.insert(ExternalDefinitions.end(),
2099 Record.begin(), Record.end());
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002100 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00002101
Sebastian Redl539c5062010-08-18 23:57:32 +00002102 case SPECIAL_TYPES:
Sebastian Redlb293a452010-07-20 21:20:32 +00002103 // Optimization for the first block
2104 if (SpecialTypes.empty())
2105 SpecialTypes.swap(Record);
2106 else
2107 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002108 break;
2109
Sebastian Redl539c5062010-08-18 23:57:32 +00002110 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002111 TotalNumStatements += Record[0];
2112 TotalNumMacros += Record[1];
2113 TotalLexicalDeclContexts += Record[2];
2114 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00002115 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002116
Sebastian Redl539c5062010-08-18 23:57:32 +00002117 case UNUSED_FILESCOPED_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002118 // Optimization for the first block.
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002119 if (UnusedFileScopedDecls.empty())
2120 UnusedFileScopedDecls.swap(Record);
Sebastian Redlb293a452010-07-20 21:20:32 +00002121 else
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002122 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
2123 Record.begin(), Record.end());
Tanya Lattner90073802010-02-12 00:07:30 +00002124 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002125
Alexis Hunt27a761d2011-05-04 23:29:54 +00002126 case DELEGATING_CTORS:
2127 // Optimization for the first block.
2128 if (DelegatingCtorDecls.empty())
2129 DelegatingCtorDecls.swap(Record);
2130 else
2131 DelegatingCtorDecls.insert(DelegatingCtorDecls.end(),
2132 Record.begin(), Record.end());
2133 break;
2134
Sebastian Redl539c5062010-08-18 23:57:32 +00002135 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002136 // Later blocks overwrite earlier ones.
2137 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002138 break;
2139
Sebastian Redl539c5062010-08-18 23:57:32 +00002140 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002141 // Optimization for the first block.
2142 if (LocallyScopedExternalDecls.empty())
2143 LocallyScopedExternalDecls.swap(Record);
2144 else
2145 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
2146 Record.begin(), Record.end());
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002147 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002148
Sebastian Redl539c5062010-08-18 23:57:32 +00002149 case SELECTOR_OFFSETS:
Sebastian Redla19a67f2010-08-03 21:58:15 +00002150 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00002151 F.LocalNumSelectors = Record[0];
Douglas Gregor95c13f52009-04-25 17:48:32 +00002152 break;
2153
Sebastian Redl539c5062010-08-18 23:57:32 +00002154 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00002155 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002156 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00002157 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002158 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00002159 F.SelectorLookupTableData + Record[0],
2160 F.SelectorLookupTableData,
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002161 ASTSelectorLookupTrait(*this));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002162 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002163 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00002164
Sebastian Redl96371b42010-09-22 00:42:30 +00002165 case REFERENCED_SELECTOR_POOL:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002166 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002167 break;
2168
Sebastian Redl539c5062010-08-18 23:57:32 +00002169 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002170 if (!Record.empty() && Listener)
2171 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002172 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002173
Sebastian Redl539c5062010-08-18 23:57:32 +00002174 case SOURCE_LOCATION_OFFSETS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002175 F.SLocOffsets = (const uint32_t *)BlobStart;
2176 F.LocalNumSLocEntries = Record[0];
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002177 F.LocalSLocSize = Record[1];
Douglas Gregor258ae542009-04-27 06:38:32 +00002178 break;
2179
Sebastian Redl539c5062010-08-18 23:57:32 +00002180 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl96371b42010-09-22 00:42:30 +00002181 if (PreloadSLocEntries.empty())
2182 PreloadSLocEntries.swap(Record);
2183 else
2184 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2185 Record.begin(), Record.end());
Douglas Gregor258ae542009-04-27 06:38:32 +00002186 break;
Douglas Gregorc5046832009-04-27 18:38:38 +00002187
Sebastian Redl539c5062010-08-18 23:57:32 +00002188 case STAT_CACHE: {
Douglas Gregor606c4ac2011-02-05 19:42:43 +00002189 if (!DisableStatCache) {
2190 ASTStatCache *MyStatCache =
2191 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2192 (const unsigned char *)BlobStart,
2193 NumStatHits, NumStatMisses);
2194 FileMgr.addStatCache(MyStatCache);
2195 F.StatCache = MyStatCache;
2196 }
Douglas Gregorc5046832009-04-27 18:38:38 +00002197 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00002198 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002199
Sebastian Redl539c5062010-08-18 23:57:32 +00002200 case EXT_VECTOR_DECLS:
Sebastian Redl04f5c312010-07-28 21:38:49 +00002201 // Optimization for the first block.
2202 if (ExtVectorDecls.empty())
2203 ExtVectorDecls.swap(Record);
2204 else
2205 ExtVectorDecls.insert(ExtVectorDecls.end(),
2206 Record.begin(), Record.end());
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002207 break;
2208
Sebastian Redl539c5062010-08-18 23:57:32 +00002209 case VTABLE_USES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002210 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002211 VTableUses.swap(Record);
2212 break;
2213
Sebastian Redl539c5062010-08-18 23:57:32 +00002214 case DYNAMIC_CLASSES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002215 // Optimization for the first block.
2216 if (DynamicClasses.empty())
2217 DynamicClasses.swap(Record);
2218 else
2219 DynamicClasses.insert(DynamicClasses.end(),
2220 Record.begin(), Record.end());
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002221 break;
2222
Sebastian Redl539c5062010-08-18 23:57:32 +00002223 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002224 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002225 break;
2226
Sebastian Redl539c5062010-08-18 23:57:32 +00002227 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002228 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002229 SemaDeclRefs.swap(Record);
2230 break;
2231
Sebastian Redl539c5062010-08-18 23:57:32 +00002232 case ORIGINAL_FILE_NAME:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002233 // The primary AST will be the last to get here, so it will be the one
Sebastian Redlb293a452010-07-20 21:20:32 +00002234 // that's used.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00002235 ActualOriginalFileName.assign(BlobStart, BlobLen);
2236 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002237 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00002238 break;
Mike Stump11289f42009-09-09 15:08:12 +00002239
Douglas Gregora3b20262011-05-06 21:43:30 +00002240 case ORIGINAL_FILE_ID:
2241 OriginalFileID = FileID::get(Record[0]);
2242 break;
2243
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002244 case ORIGINAL_PCH_DIR:
2245 // The primary AST will be the last to get here, so it will be the one
2246 // that's used.
2247 OriginalDir.assign(BlobStart, BlobLen);
2248 break;
2249
Sebastian Redl539c5062010-08-18 23:57:32 +00002250 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00002251 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002252 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2253 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2254 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregord54f3a12009-10-05 21:07:28 +00002255 return IgnorePCH;
2256 }
2257 break;
2258 }
Sebastian Redlfa061442010-07-21 20:07:32 +00002259
Sebastian Redl539c5062010-08-18 23:57:32 +00002260 case MACRO_DEFINITION_OFFSETS:
Sebastian Redlfa061442010-07-21 20:07:32 +00002261 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2262 F.NumPreallocatedPreprocessingEntities = Record[0];
2263 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregoraae92242010-03-19 21:51:54 +00002264 break;
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002265
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002266 case DECL_UPDATE_OFFSETS: {
2267 if (Record.size() % 2 != 0) {
2268 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2269 return Failure;
2270 }
2271 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2272 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2273 .push_back(std::make_pair(&F, Record[I+1]));
2274 break;
2275 }
2276
Sebastian Redl539c5062010-08-18 23:57:32 +00002277 case DECL_REPLACEMENTS: {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002278 if (Record.size() % 2 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002279 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002280 return Failure;
2281 }
2282 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl539c5062010-08-18 23:57:32 +00002283 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002284 std::make_pair(&F, Record[I+1]);
2285 break;
2286 }
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002287
2288 case CXX_BASE_SPECIFIER_OFFSETS: {
2289 if (F.LocalNumCXXBaseSpecifiers != 0) {
2290 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2291 return Failure;
2292 }
2293
2294 F.LocalNumCXXBaseSpecifiers = Record[0];
2295 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2296 break;
2297 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002298
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002299 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002300 if (Record.size() % 2 != 0) {
2301 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2302 return Failure;
2303 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002304 if (PragmaDiagMappings.empty())
2305 PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002306 else
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002307 PragmaDiagMappings.insert(PragmaDiagMappings.end(),
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002308 Record.begin(), Record.end());
2309 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002310
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002311 case CUDA_SPECIAL_DECL_REFS:
2312 // Later tables overwrite earlier ones.
2313 CUDASpecialDeclRefs.swap(Record);
2314 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002315
2316 case HEADER_SEARCH_TABLE:
2317 F.HeaderFileInfoTableData = BlobStart;
2318 F.LocalNumHeaderFileInfos = Record[1];
2319 if (Record[0]) {
2320 F.HeaderFileInfoTable
2321 = HeaderFileInfoLookupTable::Create(
2322 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2323 (const unsigned char *)F.HeaderFileInfoTableData);
2324 if (PP)
2325 PP->getHeaderSearchInfo().SetExternalSource(this);
2326 }
2327 break;
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002328
2329 case FP_PRAGMA_OPTIONS:
2330 // Later tables overwrite earlier ones.
2331 FPPragmaOptions.swap(Record);
2332 break;
2333
2334 case OPENCL_EXTENSIONS:
2335 // Later tables overwrite earlier ones.
2336 OpenCLExtensions.swap(Record);
2337 break;
Alexis Hunt27a761d2011-05-04 23:29:54 +00002338
2339 case TENTATIVE_DEFINITIONS:
2340 // Optimization for the first block.
2341 if (TentativeDefinitions.empty())
2342 TentativeDefinitions.swap(Record);
2343 else
2344 TentativeDefinitions.insert(TentativeDefinitions.end(),
2345 Record.begin(), Record.end());
2346 break;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002347 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00002348 First = false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002349 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002350 Error("premature end of bitstream in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002351 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002352}
2353
Sebastian Redl009e7f22010-10-05 16:15:19 +00002354ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2355 ASTFileType Type) {
2356 switch(ReadASTCore(FileName, Type)) {
Sebastian Redl2abc0382010-07-16 20:41:52 +00002357 case Failure: return Failure;
2358 case IgnorePCH: return IgnorePCH;
2359 case Success: break;
2360 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002361
2362 // Here comes stuff that we only do once the entire chain is loaded.
2363
Sebastian Redl96371b42010-09-22 00:42:30 +00002364 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redlfa061442010-07-21 20:07:32 +00002365 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redlada023c2010-08-04 20:40:17 +00002366 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2367 TotalNumSelectors = 0;
Sebastian Redl9e687992010-07-19 22:06:55 +00002368 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl96371b42010-09-22 00:42:30 +00002369 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002370 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002371 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl9e687992010-07-19 22:06:55 +00002372 TotalNumTypes += Chain[I]->LocalNumTypes;
2373 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redlfa061442010-07-21 20:07:32 +00002374 TotalNumPreallocatedPreprocessingEntities +=
2375 Chain[I]->NumPreallocatedPreprocessingEntities;
2376 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redlada023c2010-08-04 20:40:17 +00002377 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl9e687992010-07-19 22:06:55 +00002378 }
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002379 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002380 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl9e687992010-07-19 22:06:55 +00002381 TypesLoaded.resize(TotalNumTypes);
2382 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redlfa061442010-07-21 20:07:32 +00002383 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2384 if (PP) {
2385 if (TotalNumIdentifiers > 0)
2386 PP->getHeaderSearchInfo().SetExternalLookup(this);
2387 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2388 if (!PP->getPreprocessingRecord())
Douglas Gregor998caea2011-05-06 16:33:08 +00002389 PP->createPreprocessingRecord(true);
Sebastian Redlfa061442010-07-21 20:07:32 +00002390 PP->getPreprocessingRecord()->SetExternalSource(*this,
2391 TotalNumPreallocatedPreprocessingEntities);
2392 }
2393 }
Sebastian Redlada023c2010-08-04 20:40:17 +00002394 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl96371b42010-09-22 00:42:30 +00002395 // Preload SLocEntries.
2396 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2397 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2398 if (Result != Success)
2399 return Result;
2400 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002401
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002402 // Check the predefines buffers.
Douglas Gregorce3a8292010-07-27 00:27:13 +00002403 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002404 return IgnorePCH;
2405
2406 if (PP) {
2407 // Initialization of keywords and pragmas occurs before the
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002408 // AST file is read, so there may be some identifiers that were
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002409 // loaded into the IdentifierTable before we intercepted the
2410 // creation of identifiers. Iterate through the list of known
2411 // identifiers and determine whether we have to establish
2412 // preprocessor definitions or top-level identifier declaration
2413 // chains for those identifiers.
2414 //
2415 // We copy the IdentifierInfo pointers to a small vector first,
2416 // since de-serializing declarations or macro definitions can add
2417 // new entries into the identifier table, invalidating the
2418 // iterators.
2419 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2420 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2421 IdEnd = PP->getIdentifierTable().end();
2422 Id != IdEnd; ++Id)
2423 Identifiers.push_back(Id->second);
Sebastian Redlfa061442010-07-21 20:07:32 +00002424 // We need to search the tables in all files.
Sebastian Redlfa061442010-07-21 20:07:32 +00002425 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002426 ASTIdentifierLookupTable *IdTable
2427 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2428 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl5c415f32010-07-22 17:01:13 +00002429 // ones.
2430 if (!IdTable)
2431 continue;
Sebastian Redlfa061442010-07-21 20:07:32 +00002432 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2433 IdentifierInfo *II = Identifiers[I];
2434 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00002435 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redlfa061442010-07-21 20:07:32 +00002436 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002437 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redlb293a452010-07-20 21:20:32 +00002438 if (Pos == IdTable->end())
2439 continue;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002440
Sebastian Redlb293a452010-07-20 21:20:32 +00002441 // Dereferencing the iterator has the effect of populating the
2442 // IdentifierInfo node with the various declarations it needs.
2443 (void)*Pos;
2444 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002445 }
2446 }
2447
2448 if (Context)
2449 InitializeContext(*Context);
2450
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002451 if (DeserializationListener)
2452 DeserializationListener->ReaderInitialized(this);
2453
Douglas Gregor936a5b42010-11-30 05:23:00 +00002454 // If this AST file is a precompiled preamble, then set the main file ID of
2455 // the source manager to the file source file from which the preamble was
2456 // built. This is the only valid way to use a precompiled preamble.
2457 if (Type == Preamble) {
Douglas Gregora3b20262011-05-06 21:43:30 +00002458 if (OriginalFileID.isInvalid()) {
2459 SourceLocation Loc
2460 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2461 if (Loc.isValid())
2462 OriginalFileID = SourceMgr.getDecomposedLoc(Loc).first;
Douglas Gregor936a5b42010-11-30 05:23:00 +00002463 }
Douglas Gregora3b20262011-05-06 21:43:30 +00002464
2465 if (!OriginalFileID.isInvalid())
2466 SourceMgr.SetPreambleFileID(OriginalFileID);
Douglas Gregor936a5b42010-11-30 05:23:00 +00002467 }
2468
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002469 return Success;
2470}
2471
Sebastian Redl009e7f22010-10-05 16:15:19 +00002472ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2473 ASTFileType Type) {
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002474 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl009e7f22010-10-05 16:15:19 +00002475 Chain.push_back(new PerFileData(Type));
Sebastian Redl34522812010-07-16 17:50:48 +00002476 PerFileData &F = *Chain.back();
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002477 if (Prev)
2478 Prev->NextInSource = &F;
2479 else
2480 FirstInSource = &F;
2481 F.Loaders.push_back(Prev);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002482
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002483 // Set the AST file name.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002484 F.FileName = FileName;
2485
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002486 if (FileName != "-") {
2487 CurrentDir = llvm::sys::path::parent_path(FileName);
2488 if (CurrentDir.empty()) CurrentDir = ".";
2489 }
2490
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +00002491 if (!ASTBuffers.empty()) {
Sebastian Redleb931c62011-04-14 14:07:41 +00002492 F.Buffer.reset(ASTBuffers.back());
2493 ASTBuffers.pop_back();
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +00002494 assert(F.Buffer && "Passed null buffer");
2495 } else {
2496 // Open the AST file.
2497 //
2498 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2499 std::string ErrStr;
2500 llvm::error_code ec;
2501 if (FileName == "-") {
2502 ec = llvm::MemoryBuffer::getSTDIN(F.Buffer);
2503 if (ec)
2504 ErrStr = ec.message();
2505 } else
2506 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
2507 if (!F.Buffer) {
2508 Error(ErrStr.c_str());
2509 return IgnorePCH;
2510 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002511 }
2512
2513 // Initialize the stream
2514 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2515 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl34522812010-07-16 17:50:48 +00002516 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002517 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002518 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002519
2520 // Sniff for the signature.
2521 if (Stream.Read(8) != 'C' ||
2522 Stream.Read(8) != 'P' ||
2523 Stream.Read(8) != 'C' ||
2524 Stream.Read(8) != 'H') {
2525 Diag(diag::err_not_a_pch_file) << FileName;
2526 return Failure;
2527 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002528
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002529 while (!Stream.AtEndOfStream()) {
2530 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002531
Douglas Gregor92863e42009-04-10 23:10:45 +00002532 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002533 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002534 return Failure;
2535 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002536
2537 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002538
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002539 // We only know the AST subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002540 switch (BlockID) {
2541 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002542 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002543 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002544 return Failure;
2545 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002546 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00002547 case AST_BLOCK_ID:
Sebastian Redl3e31c722010-08-18 23:56:56 +00002548 switch (ReadASTBlock(F)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002549 case Success:
2550 break;
2551
2552 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00002553 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00002554
2555 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00002556 // FIXME: We could consider reading through to the end of this
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002557 // AST block, skipping subblocks, to see if there are other
2558 // AST blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00002559
2560 // Clear out any preallocated source location entries, so that
2561 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002562 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00002563
2564 // Remove the stat cache.
Sebastian Redl34522812010-07-16 17:50:48 +00002565 if (F.StatCache)
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002566 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00002567
Douglas Gregor92863e42009-04-10 23:10:45 +00002568 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002569 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002570 break;
2571 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002572 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002573 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002574 return Failure;
2575 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002576 break;
2577 }
Mike Stump11289f42009-09-09 15:08:12 +00002578 }
2579
Sebastian Redl2abc0382010-07-16 20:41:52 +00002580 return Success;
2581}
2582
Sebastian Redl2c499f62010-08-18 23:56:43 +00002583void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002584 PP = &pp;
Sebastian Redlfa061442010-07-21 20:07:32 +00002585
2586 unsigned TotalNum = 0;
2587 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2588 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2589 if (TotalNum) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002590 if (!PP->getPreprocessingRecord())
Douglas Gregor998caea2011-05-06 16:33:08 +00002591 PP->createPreprocessingRecord(true);
Sebastian Redlfa061442010-07-21 20:07:32 +00002592 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregoraae92242010-03-19 21:51:54 +00002593 }
2594}
2595
Sebastian Redl2c499f62010-08-18 23:56:43 +00002596void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002597 Context = &Ctx;
2598 assert(Context && "Passed null context!");
2599
2600 assert(PP && "Forgot to set Preprocessor ?");
2601 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2602 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00002603 PP->setExternalSource(this);
Douglas Gregor09b69892011-02-10 17:09:37 +00002604 PP->getHeaderSearchInfo().SetExternalSource(this);
2605
Douglas Gregoraa433012010-10-01 01:18:02 +00002606 // If we have an update block for the TU waiting, we have to add it before
2607 // deserializing the decl.
2608 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2609 if (DCU != DeclContextOffsets.end()) {
2610 // Insertion could invalidate map, so grab vector.
2611 DeclContextInfos T;
2612 T.swap(DCU->second);
2613 DeclContextOffsets.erase(DCU);
2614 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2615 }
2616
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002617 // Load the translation unit declaration
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00002618 GetTranslationUnitDecl();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002619
2620 // Load the special types.
2621 Context->setBuiltinVaListType(
Sebastian Redl539c5062010-08-18 23:57:32 +00002622 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2623 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002624 Context->setObjCIdType(GetType(Id));
Sebastian Redl539c5062010-08-18 23:57:32 +00002625 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002626 Context->setObjCSelType(GetType(Sel));
Sebastian Redl539c5062010-08-18 23:57:32 +00002627 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002628 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl539c5062010-08-18 23:57:32 +00002629 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002630 Context->setObjCClassType(GetType(Class));
Steve Naroff7cae42b2009-07-10 23:34:53 +00002631
Sebastian Redl539c5062010-08-18 23:57:32 +00002632 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002633 Context->setCFConstantStringType(GetType(String));
Mike Stump11289f42009-09-09 15:08:12 +00002634 if (unsigned FastEnum
Sebastian Redl539c5062010-08-18 23:57:32 +00002635 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002636 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl539c5062010-08-18 23:57:32 +00002637 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregor27821ce2009-07-07 16:35:42 +00002638 QualType FileType = GetType(File);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002639 if (FileType.isNull()) {
2640 Error("FILE type is NULL");
2641 return;
2642 }
John McCall9dd450b2009-09-21 23:43:11 +00002643 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregor27821ce2009-07-07 16:35:42 +00002644 Context->setFILEDecl(Typedef->getDecl());
2645 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002646 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002647 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002648 Error("Invalid FILE type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002649 return;
2650 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002651 Context->setFILEDecl(Tag->getDecl());
2652 }
2653 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002654 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002655 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002656 if (Jmp_bufType.isNull()) {
2657 Error("jmp_bug type is NULL");
2658 return;
2659 }
John McCall9dd450b2009-09-21 23:43:11 +00002660 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002661 Context->setjmp_bufDecl(Typedef->getDecl());
2662 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002663 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002664 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002665 Error("Invalid jmp_buf type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002666 return;
2667 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002668 Context->setjmp_bufDecl(Tag->getDecl());
2669 }
2670 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002671 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002672 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002673 if (Sigjmp_bufType.isNull()) {
2674 Error("sigjmp_buf type is NULL");
2675 return;
2676 }
John McCall9dd450b2009-09-21 23:43:11 +00002677 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002678 Context->setsigjmp_bufDecl(Typedef->getDecl());
2679 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002680 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002681 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stumpa4de80b2009-07-28 02:25:19 +00002682 Context->setsigjmp_bufDecl(Tag->getDecl());
2683 }
2684 }
Mike Stump11289f42009-09-09 15:08:12 +00002685 if (unsigned ObjCIdRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002686 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002687 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump11289f42009-09-09 15:08:12 +00002688 if (unsigned ObjCClassRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002689 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002690 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002691 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpd0153282009-10-20 02:12:22 +00002692 Context->setBlockDescriptorType(GetType(String));
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002693 if (unsigned String
Sebastian Redl539c5062010-08-18 23:57:32 +00002694 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002695 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002696 if (unsigned ObjCSelRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002697 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002698 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002699 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002700 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002701
Sebastian Redl539c5062010-08-18 23:57:32 +00002702 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002703 Context->setInt128Installed();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002704
Richard Smith02e85f32011-04-14 22:09:26 +00002705 if (unsigned AutoDeduct = SpecialTypes[SPECIAL_TYPE_AUTO_DEDUCT])
2706 Context->AutoDeductTy = GetType(AutoDeduct);
2707 if (unsigned AutoRRefDeduct = SpecialTypes[SPECIAL_TYPE_AUTO_RREF_DEDUCT])
2708 Context->AutoRRefDeductTy = GetType(AutoRRefDeduct);
2709
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002710 ReadPragmaDiagnosticMappings(Context->getDiagnostics());
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002711
2712 // If there were any CUDA special declarations, deserialize them.
2713 if (!CUDASpecialDeclRefs.empty()) {
2714 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
2715 Context->setcudaConfigureCallDecl(
2716 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2717 }
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002718}
2719
Douglas Gregor45fe0362009-05-12 01:31:05 +00002720/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002721/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00002722/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002723std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00002724 FileManager &FileMgr,
Daniel Dunbar3b951482009-12-03 09:13:06 +00002725 Diagnostic &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002726 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002727 std::string ErrStr;
2728 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner5159f612010-11-23 08:35:12 +00002729 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00002730 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00002731 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002732 return std::string();
2733 }
2734
2735 // Initialize the stream
2736 llvm::BitstreamReader StreamFile;
2737 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00002738 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00002739 (const unsigned char *)Buffer->getBufferEnd());
2740 Stream.init(StreamFile);
2741
2742 // Sniff for the signature.
2743 if (Stream.Read(8) != 'C' ||
2744 Stream.Read(8) != 'P' ||
2745 Stream.Read(8) != 'C' ||
2746 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002747 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002748 return std::string();
2749 }
2750
2751 RecordData Record;
2752 while (!Stream.AtEndOfStream()) {
2753 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002754
Douglas Gregor45fe0362009-05-12 01:31:05 +00002755 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2756 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00002757
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002758 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002759 switch (BlockID) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002760 case AST_BLOCK_ID:
2761 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002762 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002763 return std::string();
2764 }
2765 break;
Mike Stump11289f42009-09-09 15:08:12 +00002766
Douglas Gregor45fe0362009-05-12 01:31:05 +00002767 default:
2768 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002769 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002770 return std::string();
2771 }
2772 break;
2773 }
2774 continue;
2775 }
2776
2777 if (Code == llvm::bitc::END_BLOCK) {
2778 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002779 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002780 return std::string();
2781 }
2782 continue;
2783 }
2784
2785 if (Code == llvm::bitc::DEFINE_ABBREV) {
2786 Stream.ReadAbbrevRecord();
2787 continue;
2788 }
2789
2790 Record.clear();
2791 const char *BlobStart = 0;
2792 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002793 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl539c5062010-08-18 23:57:32 +00002794 == ORIGINAL_FILE_NAME)
Douglas Gregor45fe0362009-05-12 01:31:05 +00002795 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00002796 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00002797
2798 return std::string();
2799}
2800
Douglas Gregor55abb232009-04-10 20:39:37 +00002801/// \brief Parse the record that corresponds to a LangOptions data
2802/// structure.
2803///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002804/// This routine parses the language options from the AST file and then gives
2805/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00002806///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002807/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002808bool ASTReader::ParseLanguageOptions(
Douglas Gregor55abb232009-04-10 20:39:37 +00002809 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002810 if (Listener) {
2811 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00002812
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002813 #define PARSE_LANGOPT(Option) \
2814 LangOpts.Option = Record[Idx]; \
2815 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00002816
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002817 unsigned Idx = 0;
2818 PARSE_LANGOPT(Trigraphs);
2819 PARSE_LANGOPT(BCPLComment);
2820 PARSE_LANGOPT(DollarIdents);
2821 PARSE_LANGOPT(AsmPreprocessor);
2822 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00002823 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002824 PARSE_LANGOPT(ImplicitInt);
2825 PARSE_LANGOPT(Digraphs);
2826 PARSE_LANGOPT(HexFloats);
2827 PARSE_LANGOPT(C99);
Peter Collingbournea686b5f2011-04-15 00:35:23 +00002828 PARSE_LANGOPT(C1X);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002829 PARSE_LANGOPT(Microsoft);
2830 PARSE_LANGOPT(CPlusPlus);
2831 PARSE_LANGOPT(CPlusPlus0x);
2832 PARSE_LANGOPT(CXXOperatorNames);
2833 PARSE_LANGOPT(ObjC1);
2834 PARSE_LANGOPT(ObjC2);
2835 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00002836 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00002837 PARSE_LANGOPT(AppleKext);
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00002838 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00002839 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002840 PARSE_LANGOPT(PascalStrings);
2841 PARSE_LANGOPT(WritableStrings);
2842 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00002843 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002844 PARSE_LANGOPT(Exceptions);
Anders Carlssonce8dd3a2011-02-19 23:53:54 +00002845 PARSE_LANGOPT(ObjCExceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +00002846 PARSE_LANGOPT(CXXExceptions);
2847 PARSE_LANGOPT(SjLjExceptions);
Douglas Gregordbe39272011-02-01 15:15:22 +00002848 PARSE_LANGOPT(MSBitfields);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002849 PARSE_LANGOPT(NeXTRuntime);
2850 PARSE_LANGOPT(Freestanding);
2851 PARSE_LANGOPT(NoBuiltin);
2852 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00002853 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002854 PARSE_LANGOPT(Blocks);
2855 PARSE_LANGOPT(EmitAllDecls);
2856 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00002857 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2858 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002859 PARSE_LANGOPT(HeinousExtensions);
2860 PARSE_LANGOPT(Optimize);
2861 PARSE_LANGOPT(OptimizeSize);
2862 PARSE_LANGOPT(Static);
2863 PARSE_LANGOPT(PICLevel);
2864 PARSE_LANGOPT(GNUInline);
2865 PARSE_LANGOPT(NoInline);
Chandler Carruth7ffce732011-04-23 20:05:38 +00002866 PARSE_LANGOPT(Deprecated);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002867 PARSE_LANGOPT(AccessControl);
2868 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00002869 PARSE_LANGOPT(ShortWChar);
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00002870 PARSE_LANGOPT(ShortEnums);
Chris Lattner51924e512010-06-26 21:25:03 +00002871 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall457a04e2010-10-22 21:05:15 +00002872 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00002873 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00002874 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002875 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00002876 PARSE_LANGOPT(OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00002877 PARSE_LANGOPT(CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00002878 PARSE_LANGOPT(CatchUndefined);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002879 PARSE_LANGOPT(DefaultFPContract);
Roman Divackydc1f68d2011-03-01 17:36:40 +00002880 PARSE_LANGOPT(ElideConstructors);
2881 PARSE_LANGOPT(SpellChecking);
Roman Divacky65b88cd2011-03-01 17:40:53 +00002882 PARSE_LANGOPT(MRTD);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002883 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00002884
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002885 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00002886 }
Douglas Gregor55abb232009-04-10 20:39:37 +00002887
2888 return false;
2889}
2890
Sebastian Redl2c499f62010-08-18 23:56:43 +00002891void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002892 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2893 PerFileData &F = *Chain[I];
2894 if (!F.PreprocessorDetailCursor.getBitStreamReader())
2895 continue;
2896
2897 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
2898 F.PreprocessorDetailCursor.JumpToBit(F.PreprocessorDetailStartOffset);
2899 while (LoadPreprocessedEntity(F)) { }
2900 }
Douglas Gregoraae92242010-03-19 21:51:54 +00002901}
2902
Douglas Gregor46c50012011-02-11 19:46:30 +00002903PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002904 PerFileData *F = 0;
2905 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2906 if (Offset < Chain[I]->SizeInBits) {
2907 F = Chain[I];
2908 break;
2909 }
2910
2911 Offset -= Chain[I]->SizeInBits;
2912 }
2913
2914 if (!F) {
2915 Error("Malformed preprocessed entity offset");
2916 return 0;
2917 }
2918
Douglas Gregor92a96f52011-02-08 21:58:10 +00002919 // Keep track of where we are in the stream, then jump back there
2920 // after reading this entity.
2921 SavedStreamPosition SavedPosition(F->PreprocessorDetailCursor);
2922 F->PreprocessorDetailCursor.JumpToBit(Offset);
2923 return LoadPreprocessedEntity(*F);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002924}
2925
Douglas Gregor09b69892011-02-10 17:09:37 +00002926HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
2927 HeaderFileInfoTrait Trait(FE->getName());
2928 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2929 PerFileData &F = *Chain[I];
2930 HeaderFileInfoLookupTable *Table
2931 = static_cast<HeaderFileInfoLookupTable *>(F.HeaderFileInfoTable);
2932 if (!Table)
2933 continue;
2934
2935 // Look in the on-disk hash table for an entry for this file name.
2936 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE->getName(),
2937 &Trait);
2938 if (Pos == Table->end())
2939 continue;
2940
2941 HeaderFileInfo HFI = *Pos;
2942 if (Listener)
2943 Listener->ReadHeaderFileInfo(HFI, FE->getUID());
2944
2945 return HFI;
2946 }
2947
2948 return HeaderFileInfo();
2949}
2950
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002951void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002952 unsigned Idx = 0;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002953 while (Idx < PragmaDiagMappings.size()) {
2954 SourceLocation
2955 Loc = SourceLocation::getFromRawEncoding(PragmaDiagMappings[Idx++]);
2956 while (1) {
2957 assert(Idx < PragmaDiagMappings.size() &&
2958 "Invalid data, didn't find '-1' marking end of diag/map pairs");
2959 if (Idx >= PragmaDiagMappings.size())
2960 break; // Something is messed up but at least avoid infinite loop in
2961 // release build.
2962 unsigned DiagID = PragmaDiagMappings[Idx++];
2963 if (DiagID == (unsigned)-1)
2964 break; // no more diag/map pairs for this location.
2965 diag::Mapping Map = (diag::Mapping)PragmaDiagMappings[Idx++];
2966 Diag.setDiagnosticMapping(DiagID, Map, Loc);
2967 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002968 }
2969}
2970
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002971/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002972ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002973 PerFileData *F = 0;
2974 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2975 F = Chain[N - I - 1];
2976 if (Index < F->LocalNumTypes)
2977 break;
2978 Index -= F->LocalNumTypes;
2979 }
2980 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redl2c373b92010-10-05 15:59:54 +00002981 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002982}
2983
2984/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002985///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002986/// The index is the type ID, shifted and minus the number of predefs. This
2987/// routine actually reads the record corresponding to the type at the given
2988/// location. It is a helper routine for GetType, which deals with reading type
2989/// IDs.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002990QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002991 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00002992 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00002993
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002994 // Keep track of where we are in the stream, then jump back there
2995 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002996 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002997
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002998 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00002999
Douglas Gregor1342e842009-07-06 18:54:52 +00003000 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003001 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00003002
Sebastian Redl2c373b92010-10-05 15:59:54 +00003003 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003004 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003005 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00003006 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3007 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003008 if (Record.size() != 2) {
3009 Error("Incorrect encoding of extended qualifier type");
3010 return QualType();
3011 }
Douglas Gregor455b8f42009-04-15 22:00:08 +00003012 QualType Base = GetType(Record[0]);
John McCall8ccfcb52009-09-24 19:53:00 +00003013 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
3014 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00003015 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003016
Sebastian Redl539c5062010-08-18 23:57:32 +00003017 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003018 if (Record.size() != 1) {
3019 Error("Incorrect encoding of complex type");
3020 return QualType();
3021 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003022 QualType ElemType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003023 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003024 }
3025
Sebastian Redl539c5062010-08-18 23:57:32 +00003026 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003027 if (Record.size() != 1) {
3028 Error("Incorrect encoding of pointer type");
3029 return QualType();
3030 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003031 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003032 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003033 }
3034
Sebastian Redl539c5062010-08-18 23:57:32 +00003035 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003036 if (Record.size() != 1) {
3037 Error("Incorrect encoding of block pointer type");
3038 return QualType();
3039 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003040 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003041 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003042 }
3043
Sebastian Redl539c5062010-08-18 23:57:32 +00003044 case TYPE_LVALUE_REFERENCE: {
Richard Smith0f538462011-04-12 10:38:03 +00003045 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003046 Error("Incorrect encoding of lvalue reference type");
3047 return QualType();
3048 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003049 QualType PointeeType = GetType(Record[0]);
Richard Smith0f538462011-04-12 10:38:03 +00003050 return Context->getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003051 }
3052
Sebastian Redl539c5062010-08-18 23:57:32 +00003053 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003054 if (Record.size() != 1) {
3055 Error("Incorrect encoding of rvalue reference type");
3056 return QualType();
3057 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003058 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003059 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003060 }
3061
Sebastian Redl539c5062010-08-18 23:57:32 +00003062 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00003063 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003064 Error("Incorrect encoding of member pointer type");
3065 return QualType();
3066 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003067 QualType PointeeType = GetType(Record[0]);
3068 QualType ClassType = GetType(Record[1]);
Douglas Gregor0cdc8322010-12-10 17:03:06 +00003069 if (PointeeType.isNull() || ClassType.isNull())
3070 return QualType();
3071
Chris Lattner8575daa2009-04-27 21:45:14 +00003072 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003073 }
3074
Sebastian Redl539c5062010-08-18 23:57:32 +00003075 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003076 QualType ElementType = GetType(Record[0]);
3077 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3078 unsigned IndexTypeQuals = Record[2];
3079 unsigned Idx = 3;
3080 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00003081 return Context->getConstantArrayType(ElementType, Size,
3082 ASM, IndexTypeQuals);
3083 }
3084
Sebastian Redl539c5062010-08-18 23:57:32 +00003085 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003086 QualType ElementType = GetType(Record[0]);
3087 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3088 unsigned IndexTypeQuals = Record[2];
Chris Lattner8575daa2009-04-27 21:45:14 +00003089 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003090 }
3091
Sebastian Redl539c5062010-08-18 23:57:32 +00003092 case TYPE_VARIABLE_ARRAY: {
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003093 QualType ElementType = GetType(Record[0]);
3094 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3095 unsigned IndexTypeQuals = Record[2];
Sebastian Redl2c373b92010-10-05 15:59:54 +00003096 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3097 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
3098 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor04318252009-07-06 15:59:29 +00003099 ASM, IndexTypeQuals,
3100 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003101 }
3102
Sebastian Redl539c5062010-08-18 23:57:32 +00003103 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00003104 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003105 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003106 return QualType();
3107 }
3108
3109 QualType ElementType = GetType(Record[0]);
3110 unsigned NumElements = Record[1];
Bob Wilsonaeb56442010-11-10 21:56:12 +00003111 unsigned VecKind = Record[2];
Chris Lattner37141f42010-06-23 06:00:24 +00003112 return Context->getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00003113 (VectorType::VectorKind)VecKind);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003114 }
3115
Sebastian Redl539c5062010-08-18 23:57:32 +00003116 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00003117 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003118 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003119 return QualType();
3120 }
3121
3122 QualType ElementType = GetType(Record[0]);
3123 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00003124 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003125 }
3126
Sebastian Redl539c5062010-08-18 23:57:32 +00003127 case TYPE_FUNCTION_NO_PROTO: {
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003128 if (Record.size() != 5) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00003129 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003130 return QualType();
3131 }
3132 QualType ResultType = GetType(Record[0]);
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003133 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3], (CallingConv)Record[4]);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00003134 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003135 }
3136
Sebastian Redl539c5062010-08-18 23:57:32 +00003137 case TYPE_FUNCTION_PROTO: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003138 QualType ResultType = GetType(Record[0]);
John McCalldb40c7f2010-12-14 08:05:40 +00003139
3140 FunctionProtoType::ExtProtoInfo EPI;
3141 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003142 /*hasregparm*/ Record[2],
3143 /*regparm*/ Record[3],
3144 static_cast<CallingConv>(Record[4]));
John McCalldb40c7f2010-12-14 08:05:40 +00003145
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003146 unsigned Idx = 5;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003147 unsigned NumParams = Record[Idx++];
3148 llvm::SmallVector<QualType, 16> ParamTypes;
3149 for (unsigned I = 0; I != NumParams; ++I)
3150 ParamTypes.push_back(GetType(Record[Idx++]));
John McCalldb40c7f2010-12-14 08:05:40 +00003151
3152 EPI.Variadic = Record[Idx++];
3153 EPI.TypeQuals = Record[Idx++];
Douglas Gregordb9d6642011-01-26 05:01:58 +00003154 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003155 ExceptionSpecificationType EST =
3156 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3157 EPI.ExceptionSpecType = EST;
3158 if (EST == EST_Dynamic) {
3159 EPI.NumExceptions = Record[Idx++];
3160 llvm::SmallVector<QualType, 2> Exceptions;
3161 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
3162 Exceptions.push_back(GetType(Record[Idx++]));
3163 EPI.Exceptions = Exceptions.data();
3164 } else if (EST == EST_ComputedNoexcept) {
3165 EPI.NoexceptExpr = ReadExpr(*Loc.F);
3166 }
Jay Foad7d0479f2009-05-21 09:52:38 +00003167 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalldb40c7f2010-12-14 08:05:40 +00003168 EPI);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003169 }
3170
Sebastian Redl539c5062010-08-18 23:57:32 +00003171 case TYPE_UNRESOLVED_USING:
John McCallb96ec562009-12-04 22:46:56 +00003172 return Context->getTypeDeclType(
3173 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
3174
Sebastian Redl539c5062010-08-18 23:57:32 +00003175 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003176 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003177 Error("incorrect encoding of typedef type");
3178 return QualType();
3179 }
Richard Smithdda56e42011-04-15 14:24:37 +00003180 TypedefNameDecl *Decl = cast<TypedefNameDecl>(GetDecl(Record[0]));
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003181 QualType Canonical = GetType(Record[1]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003182 if (!Canonical.isNull())
3183 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003184 return Context->getTypedefType(Decl, Canonical);
3185 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003186
Sebastian Redl539c5062010-08-18 23:57:32 +00003187 case TYPE_TYPEOF_EXPR:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003188 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003189
Sebastian Redl539c5062010-08-18 23:57:32 +00003190 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003191 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003192 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003193 return QualType();
3194 }
3195 QualType UnderlyingType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003196 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003197 }
Mike Stump11289f42009-09-09 15:08:12 +00003198
Sebastian Redl539c5062010-08-18 23:57:32 +00003199 case TYPE_DECLTYPE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003200 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson81df7b82009-06-24 19:06:50 +00003201
Richard Smith30482bc2011-02-20 03:19:35 +00003202 case TYPE_AUTO:
3203 return Context->getAutoType(GetType(Record[0]));
3204
Sebastian Redl539c5062010-08-18 23:57:32 +00003205 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003206 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003207 Error("incorrect encoding of record type");
3208 return QualType();
3209 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003210 bool IsDependent = Record[0];
3211 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCall424cec92011-01-19 06:33:43 +00003212 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003213 return T;
3214 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003215
Sebastian Redl539c5062010-08-18 23:57:32 +00003216 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003217 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003218 Error("incorrect encoding of enum type");
3219 return QualType();
3220 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003221 bool IsDependent = Record[0];
3222 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCall424cec92011-01-19 06:33:43 +00003223 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003224 return T;
3225 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00003226
John McCall81904512011-01-06 01:58:22 +00003227 case TYPE_ATTRIBUTED: {
3228 if (Record.size() != 3) {
3229 Error("incorrect encoding of attributed type");
3230 return QualType();
3231 }
3232 QualType modifiedType = GetType(Record[0]);
3233 QualType equivalentType = GetType(Record[1]);
3234 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3235 return Context->getAttributedType(kind, modifiedType, equivalentType);
3236 }
3237
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003238 case TYPE_PAREN: {
3239 if (Record.size() != 1) {
3240 Error("incorrect encoding of paren type");
3241 return QualType();
3242 }
3243 QualType InnerType = GetType(Record[0]);
3244 return Context->getParenType(InnerType);
3245 }
3246
Douglas Gregord2fa7662010-12-20 02:24:11 +00003247 case TYPE_PACK_EXPANSION: {
Douglas Gregor17328502011-02-01 15:24:58 +00003248 if (Record.size() != 2) {
Douglas Gregord2fa7662010-12-20 02:24:11 +00003249 Error("incorrect encoding of pack expansion type");
3250 return QualType();
3251 }
3252 QualType Pattern = GetType(Record[0]);
3253 if (Pattern.isNull())
3254 return QualType();
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00003255 llvm::Optional<unsigned> NumExpansions;
3256 if (Record[1])
3257 NumExpansions = Record[1] - 1;
3258 return Context->getPackExpansionType(Pattern, NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00003259 }
3260
Sebastian Redl539c5062010-08-18 23:57:32 +00003261 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003262 unsigned Idx = 0;
3263 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3264 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3265 QualType NamedType = GetType(Record[Idx++]);
3266 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00003267 }
3268
Sebastian Redl539c5062010-08-18 23:57:32 +00003269 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00003270 unsigned Idx = 0;
3271 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00003272 return Context->getObjCInterfaceType(ItfD);
3273 }
3274
Sebastian Redl539c5062010-08-18 23:57:32 +00003275 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00003276 unsigned Idx = 0;
3277 QualType Base = GetType(Record[Idx++]);
Chris Lattner587cbe12009-04-22 06:45:28 +00003278 unsigned NumProtos = Record[Idx++];
3279 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
3280 for (unsigned I = 0; I != NumProtos; ++I)
3281 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003282 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00003283 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003284
Sebastian Redl539c5062010-08-18 23:57:32 +00003285 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00003286 unsigned Idx = 0;
John McCall8b07ec22010-05-15 11:32:37 +00003287 QualType Pointee = GetType(Record[Idx++]);
3288 return Context->getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00003289 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00003290
Sebastian Redl539c5062010-08-18 23:57:32 +00003291 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00003292 unsigned Idx = 0;
3293 QualType Parm = GetType(Record[Idx++]);
3294 QualType Replacement = GetType(Record[Idx++]);
3295 return
3296 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3297 Replacement);
3298 }
John McCalle78aac42010-03-10 03:28:59 +00003299
Douglas Gregorada4b792011-01-14 02:55:32 +00003300 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3301 unsigned Idx = 0;
3302 QualType Parm = GetType(Record[Idx++]);
3303 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3304 return Context->getSubstTemplateTypeParmPackType(
3305 cast<TemplateTypeParmType>(Parm),
3306 ArgPack);
3307 }
3308
Sebastian Redl539c5062010-08-18 23:57:32 +00003309 case TYPE_INJECTED_CLASS_NAME: {
John McCalle78aac42010-03-10 03:28:59 +00003310 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
3311 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003312 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003313 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003314 return
3315 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00003316 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003317
Sebastian Redl539c5062010-08-18 23:57:32 +00003318 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003319 unsigned Idx = 0;
3320 unsigned Depth = Record[Idx++];
3321 unsigned Index = Record[Idx++];
3322 bool Pack = Record[Idx++];
Chandler Carruth08836322011-05-01 00:51:33 +00003323 TemplateTypeParmDecl *D =
3324 cast_or_null<TemplateTypeParmDecl>(GetDecl(Record[Idx++]));
3325 return Context->getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003326 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003327
Sebastian Redl539c5062010-08-18 23:57:32 +00003328 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003329 unsigned Idx = 0;
3330 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3331 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3332 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00003333 QualType Canon = GetType(Record[Idx++]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003334 if (!Canon.isNull())
3335 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00003336 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003337 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003338
Sebastian Redl539c5062010-08-18 23:57:32 +00003339 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003340 unsigned Idx = 0;
3341 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3342 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3343 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3344 unsigned NumArgs = Record[Idx++];
3345 llvm::SmallVector<TemplateArgument, 8> Args;
3346 Args.reserve(NumArgs);
3347 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003348 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003349 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3350 Args.size(), Args.data());
3351 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003352
Sebastian Redl539c5062010-08-18 23:57:32 +00003353 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003354 unsigned Idx = 0;
3355
3356 // ArrayType
3357 QualType ElementType = GetType(Record[Idx++]);
3358 ArrayType::ArraySizeModifier ASM
3359 = (ArrayType::ArraySizeModifier)Record[Idx++];
3360 unsigned IndexTypeQuals = Record[Idx++];
3361
3362 // DependentSizedArrayType
Sebastian Redl2c373b92010-10-05 15:59:54 +00003363 Expr *NumElts = ReadExpr(*Loc.F);
3364 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003365
3366 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3367 IndexTypeQuals, Brackets);
3368 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003369
Sebastian Redl539c5062010-08-18 23:57:32 +00003370 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003371 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003372 bool IsDependent = Record[Idx++];
Douglas Gregor5590be02011-01-15 06:45:20 +00003373 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003374 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003375 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Richard Smith3f1b5d02011-05-05 21:57:07 +00003376 QualType Underlying = GetType(Record[Idx++]);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003377 QualType T;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003378 if (Underlying.isNull())
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003379 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3380 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003381 else
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003382 T = Context->getTemplateSpecializationType(Name, Args.data(),
Richard Smith3f1b5d02011-05-05 21:57:07 +00003383 Args.size(), Underlying);
John McCall424cec92011-01-19 06:33:43 +00003384 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003385 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003386 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003387 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003388 // Suppress a GCC warning
3389 return QualType();
3390}
3391
Sebastian Redl2c373b92010-10-05 15:59:54 +00003392class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00003393 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003394 ASTReader::PerFileData &F;
Sebastian Redlc67764e2010-07-22 22:43:28 +00003395 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redl2c499f62010-08-18 23:56:43 +00003396 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00003397 unsigned &Idx;
3398
Sebastian Redl2c373b92010-10-05 15:59:54 +00003399 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3400 unsigned &I) {
3401 return Reader.ReadSourceLocation(F, R, I);
3402 }
3403
John McCall8f115c62009-10-16 21:56:05 +00003404public:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003405 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl2c499f62010-08-18 23:56:43 +00003406 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003407 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3408 { }
John McCall8f115c62009-10-16 21:56:05 +00003409
John McCall17001972009-10-18 01:05:36 +00003410 // We want compile-time assurance that we've enumerated all of
3411 // these, so unfortunately we have to declare them first, then
3412 // define them out-of-line.
3413#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00003414#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00003415 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00003416#include "clang/AST/TypeLocNodes.def"
3417
John McCall17001972009-10-18 01:05:36 +00003418 void VisitFunctionTypeLoc(FunctionTypeLoc);
3419 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00003420};
3421
John McCall17001972009-10-18 01:05:36 +00003422void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00003423 // nothing to do
3424}
John McCall17001972009-10-18 01:05:36 +00003425void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003426 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorc9b7a592010-01-18 18:04:31 +00003427 if (TL.needsExtraLocalData()) {
3428 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3429 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3430 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3431 TL.setModeAttr(Record[Idx++]);
3432 }
John McCall8f115c62009-10-16 21:56:05 +00003433}
John McCall17001972009-10-18 01:05:36 +00003434void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003435 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003436}
John McCall17001972009-10-18 01:05:36 +00003437void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003438 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003439}
John McCall17001972009-10-18 01:05:36 +00003440void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003441 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003442}
John McCall17001972009-10-18 01:05:36 +00003443void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003444 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003445}
John McCall17001972009-10-18 01:05:36 +00003446void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003447 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003448}
John McCall17001972009-10-18 01:05:36 +00003449void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003450 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara509357842011-03-05 14:42:21 +00003451 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003452}
John McCall17001972009-10-18 01:05:36 +00003453void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003454 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3455 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003456 if (Record[Idx++])
Sebastian Redl2c373b92010-10-05 15:59:54 +00003457 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003458 else
John McCall17001972009-10-18 01:05:36 +00003459 TL.setSizeExpr(0);
3460}
3461void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3462 VisitArrayTypeLoc(TL);
3463}
3464void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3465 VisitArrayTypeLoc(TL);
3466}
3467void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3468 VisitArrayTypeLoc(TL);
3469}
3470void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3471 DependentSizedArrayTypeLoc TL) {
3472 VisitArrayTypeLoc(TL);
3473}
3474void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3475 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003476 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003477}
3478void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003479 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003480}
3481void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003482 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003483}
3484void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003485 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
3486 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Douglas Gregor7fb25412010-10-01 18:44:50 +00003487 TL.setTrailingReturn(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00003488 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCalle6347002009-10-23 01:28:53 +00003489 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall17001972009-10-18 01:05:36 +00003490 }
3491}
3492void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3493 VisitFunctionTypeLoc(TL);
3494}
3495void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3496 VisitFunctionTypeLoc(TL);
3497}
John McCallb96ec562009-12-04 22:46:56 +00003498void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003499 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallb96ec562009-12-04 22:46:56 +00003500}
John McCall17001972009-10-18 01:05:36 +00003501void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003502 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003503}
3504void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003505 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3506 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3507 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003508}
3509void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003510 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3511 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3512 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3513 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003514}
3515void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003516 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003517}
Richard Smith30482bc2011-02-20 03:19:35 +00003518void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3519 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3520}
John McCall17001972009-10-18 01:05:36 +00003521void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003522 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003523}
3524void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003525 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003526}
John McCall81904512011-01-06 01:58:22 +00003527void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3528 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3529 if (TL.hasAttrOperand()) {
3530 SourceRange range;
3531 range.setBegin(ReadSourceLocation(Record, Idx));
3532 range.setEnd(ReadSourceLocation(Record, Idx));
3533 TL.setAttrOperandParensRange(range);
3534 }
3535 if (TL.hasAttrExprOperand()) {
3536 if (Record[Idx++])
3537 TL.setAttrExprOperand(Reader.ReadExpr(F));
3538 else
3539 TL.setAttrExprOperand(0);
3540 } else if (TL.hasAttrEnumOperand())
3541 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3542}
John McCall17001972009-10-18 01:05:36 +00003543void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003544 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003545}
John McCallcebee162009-10-18 09:09:24 +00003546void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3547 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003548 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallcebee162009-10-18 09:09:24 +00003549}
Douglas Gregorada4b792011-01-14 02:55:32 +00003550void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3551 SubstTemplateTypeParmPackTypeLoc TL) {
3552 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3553}
John McCall17001972009-10-18 01:05:36 +00003554void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3555 TemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003556 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3557 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3558 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall0ad16662009-10-29 08:12:44 +00003559 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3560 TL.setArgLocInfo(i,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003561 Reader.GetTemplateArgumentLocInfo(F,
3562 TL.getTypePtr()->getArg(i).getKind(),
3563 Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003564}
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003565void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3566 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3567 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3568}
Abramo Bagnara6150c882010-05-11 21:36:43 +00003569void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003570 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor844cb502011-03-01 18:12:44 +00003571 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003572}
John McCalle78aac42010-03-10 03:28:59 +00003573void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003574 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalle78aac42010-03-10 03:28:59 +00003575}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00003576void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003577 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor3d0da5f2011-03-01 01:34:45 +00003578 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00003579 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003580}
John McCallc392f372010-06-11 00:33:02 +00003581void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3582 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003583 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregora7a795b2011-03-01 20:11:18 +00003584 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00003585 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3586 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3587 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003588 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3589 TL.setArgLocInfo(I,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003590 Reader.GetTemplateArgumentLocInfo(F,
3591 TL.getTypePtr()->getArg(I).getKind(),
3592 Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003593}
Douglas Gregord2fa7662010-12-20 02:24:11 +00003594void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3595 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3596}
John McCall17001972009-10-18 01:05:36 +00003597void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003598 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8b07ec22010-05-15 11:32:37 +00003599}
3600void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3601 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003602 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3603 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003604 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003605 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003606}
John McCallfc93cf92009-10-22 22:37:11 +00003607void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003608 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCallfc93cf92009-10-22 22:37:11 +00003609}
John McCall8f115c62009-10-16 21:56:05 +00003610
Sebastian Redl2c373b92010-10-05 15:59:54 +00003611TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003612 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00003613 unsigned &Idx) {
3614 QualType InfoTy = GetType(Record[Idx++]);
3615 if (InfoTy.isNull())
3616 return 0;
3617
John McCallbcd03502009-12-07 02:54:59 +00003618 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003619 TypeLocReader TLR(*this, F, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00003620 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00003621 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00003622 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00003623}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003624
Sebastian Redl539c5062010-08-18 23:57:32 +00003625QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00003626 unsigned FastQuals = ID & Qualifiers::FastMask;
3627 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003628
Sebastian Redl539c5062010-08-18 23:57:32 +00003629 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003630 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00003631 switch ((PredefinedTypeIDs)Index) {
3632 case PREDEF_TYPE_NULL_ID: return QualType();
3633 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3634 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003635
Sebastian Redl539c5062010-08-18 23:57:32 +00003636 case PREDEF_TYPE_CHAR_U_ID:
3637 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003638 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00003639 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003640 break;
3641
Sebastian Redl539c5062010-08-18 23:57:32 +00003642 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3643 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3644 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3645 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3646 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3647 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3648 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3649 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3650 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3651 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3652 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3653 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3654 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3655 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3656 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3657 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3658 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
John McCall0009fcc2011-04-26 20:42:42 +00003659 case PREDEF_TYPE_BOUND_MEMBER: T = Context->BoundMemberTy; break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003660 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
John McCall31996342011-04-07 08:22:57 +00003661 case PREDEF_TYPE_UNKNOWN_ANY: T = Context->UnknownAnyTy; break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003662 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3663 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3664 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3665 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3666 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3667 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003668 }
3669
3670 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00003671 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003672 }
3673
Sebastian Redl539c5062010-08-18 23:57:32 +00003674 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003675 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00003676 if (TypesLoaded[Index].isNull()) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003677 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003678 if (TypesLoaded[Index].isNull())
3679 return QualType();
3680
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003681 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003682 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003683 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003684 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003685 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00003686 }
Mike Stump11289f42009-09-09 15:08:12 +00003687
John McCall8ccfcb52009-09-24 19:53:00 +00003688 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003689}
3690
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003691TypeID ASTReader::GetTypeID(QualType T) const {
3692 return MakeTypeID(T,
3693 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3694}
3695
3696TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3697 if (T.isNull())
3698 return TypeIdx();
3699 assert(!T.getLocalFastQualifiers());
3700
3701 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3702 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3703 // comparing keys of ASTDeclContextNameLookupTable.
3704 // If the type didn't come from the AST file use a specially marked index
3705 // so that any hash/key comparison fail since no such index is stored
3706 // in a AST file.
3707 if (I == TypeIdxs.end())
3708 return TypeIdx(-1);
3709 return I->second;
3710}
3711
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003712unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3713 unsigned Result = 0;
3714 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3715 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3716
3717 return Result;
3718}
3719
John McCall0ad16662009-10-29 08:12:44 +00003720TemplateArgumentLocInfo
Sebastian Redl2c373b92010-10-05 15:59:54 +00003721ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3722 TemplateArgument::ArgKind Kind,
John McCall0ad16662009-10-29 08:12:44 +00003723 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003724 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00003725 switch (Kind) {
3726 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003727 return ReadExpr(F);
John McCall0ad16662009-10-29 08:12:44 +00003728 case TemplateArgument::Type:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003729 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003730 case TemplateArgument::Template: {
Douglas Gregor9d802122011-03-02 17:09:35 +00003731 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3732 Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003733 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00003734 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003735 SourceLocation());
3736 }
3737 case TemplateArgument::TemplateExpansion: {
Douglas Gregor9d802122011-03-02 17:09:35 +00003738 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3739 Index);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003740 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003741 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00003742 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregoreb29d182011-01-05 17:40:24 +00003743 EllipsisLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003744 }
John McCall0ad16662009-10-29 08:12:44 +00003745 case TemplateArgument::Null:
3746 case TemplateArgument::Integral:
3747 case TemplateArgument::Declaration:
3748 case TemplateArgument::Pack:
3749 return TemplateArgumentLocInfo();
3750 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00003751 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00003752 return TemplateArgumentLocInfo();
3753}
3754
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003755TemplateArgumentLoc
Sebastian Redl2c373b92010-10-05 15:59:54 +00003756ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003757 const RecordData &Record, unsigned &Index) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003758 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003759
3760 if (Arg.getKind() == TemplateArgument::Expression) {
3761 if (Record[Index++]) // bool InfoHasSameExpr.
3762 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3763 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00003764 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003765 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003766}
3767
Sebastian Redl2c499f62010-08-18 23:56:43 +00003768Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00003769 return GetDecl(ID);
3770}
3771
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003772uint64_t
3773ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3774 if (ID == 0)
3775 return 0;
3776
3777 --ID;
3778 uint64_t Offset = 0;
3779 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003780 PerFileData &F = *Chain[N - I - 1];
3781
3782 if (ID < F.LocalNumCXXBaseSpecifiers)
3783 return Offset + F.CXXBaseSpecifiersOffsets[ID];
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003784
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003785 ID -= F.LocalNumCXXBaseSpecifiers;
3786 Offset += F.SizeInBits;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003787 }
3788
3789 assert(false && "CXXBaseSpecifiers not found");
3790 return 0;
3791}
3792
3793CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3794 // Figure out which AST file contains this offset.
3795 PerFileData *F = 0;
3796 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003797 if (Offset < Chain[N - I - 1]->SizeInBits) {
3798 F = Chain[N - I - 1];
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003799 break;
3800 }
3801
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003802 Offset -= Chain[N - I - 1]->SizeInBits;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003803 }
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003804
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003805 if (!F) {
3806 Error("Malformed AST file: C++ base specifiers at impossible offset");
3807 return 0;
3808 }
3809
3810 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3811 SavedStreamPosition SavedPosition(Cursor);
3812 Cursor.JumpToBit(Offset);
3813 ReadingKindTracker ReadingKind(Read_Decl, *this);
3814 RecordData Record;
3815 unsigned Code = Cursor.ReadCode();
3816 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3817 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3818 Error("Malformed AST file: missing C++ base specifiers");
3819 return 0;
3820 }
3821
3822 unsigned Idx = 0;
3823 unsigned NumBases = Record[Idx++];
3824 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3825 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3826 for (unsigned I = 0; I != NumBases; ++I)
3827 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3828 return Bases;
3829}
3830
Sebastian Redl2c499f62010-08-18 23:56:43 +00003831TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003832 if (!DeclsLoaded[0]) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00003833 ReadDeclRecord(0, 1);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003834 if (DeserializationListener)
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003835 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003836 }
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00003837
3838 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3839}
3840
Sebastian Redl539c5062010-08-18 23:57:32 +00003841Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003842 if (ID == 0)
3843 return 0;
3844
Douglas Gregor745ed142009-04-25 18:35:21 +00003845 if (ID > DeclsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003846 Error("declaration ID out-of-range for AST file");
Douglas Gregor745ed142009-04-25 18:35:21 +00003847 return 0;
3848 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003849
Douglas Gregor745ed142009-04-25 18:35:21 +00003850 unsigned Index = ID - 1;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003851 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003852 ReadDeclRecord(Index, ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003853 if (DeserializationListener)
3854 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3855 }
Douglas Gregor745ed142009-04-25 18:35:21 +00003856
3857 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003858}
3859
Chris Lattner9c28af02009-04-27 05:46:25 +00003860/// \brief Resolve the offset of a statement into a statement.
3861///
3862/// This operation will read a new statement from the external
3863/// source each time it is called, and is meant to be used via a
3864/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00003865Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00003866 // Switch case IDs are per Decl.
3867 ClearSwitchCaseIDs();
3868
Sebastian Redl5c415f32010-07-22 17:01:13 +00003869 // Offset here is a global offset across the entire chain.
3870 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3871 PerFileData &F = *Chain[N - I - 1];
3872 if (Offset < F.SizeInBits) {
3873 // Since we know that this statement is part of a decl, make sure to use
3874 // the decl cursor to read it.
3875 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003876 return ReadStmtFromStream(F);
Sebastian Redl5c415f32010-07-22 17:01:13 +00003877 }
3878 Offset -= F.SizeInBits;
3879 }
3880 llvm_unreachable("Broken chain");
Douglas Gregor3c3aa612009-04-18 00:07:54 +00003881}
3882
Sebastian Redl2c499f62010-08-18 23:56:43 +00003883bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003884 bool (*isKindWeWant)(Decl::Kind),
John McCall75b960e2010-06-01 09:23:16 +00003885 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump11289f42009-09-09 15:08:12 +00003886 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003887 "DeclContext has no lexical decls in storage");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003888
Sebastian Redl5c415f32010-07-22 17:01:13 +00003889 // There might be lexical decls in multiple parts of the chain, for the TU
3890 // at least.
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003891 // DeclContextOffsets might reallocate as we load additional decls below,
3892 // so make a copy of the vector.
3893 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl5c415f32010-07-22 17:01:13 +00003894 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3895 I != E; ++I) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003896 // IDs can be 0 if this context doesn't contain declarations.
3897 if (!I->LexicalDecls)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003898 continue;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003899
3900 // Load all of the declaration IDs
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003901 for (const KindDeclIDPair *ID = I->LexicalDecls,
3902 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3903 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3904 continue;
3905
3906 Decl *D = GetDecl(ID->second);
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003907 assert(D && "Null decl in lexical decls");
3908 Decls.push_back(D);
3909 }
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003910 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003911
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003912 ++NumLexicalDeclContextsRead;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003913 return false;
3914}
3915
John McCall75b960e2010-06-01 09:23:16 +00003916DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00003917ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00003918 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003919 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003920 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003921 if (!Name)
3922 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3923 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003924
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003925 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl471ac2f2010-08-24 00:49:55 +00003926 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003927 // and namespaces. For any given name, the last available results replace
3928 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl5c415f32010-07-22 17:01:13 +00003929 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003930 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl5c415f32010-07-22 17:01:13 +00003931 I != E; ++I) {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003932 if (!I->NameLookupTableData)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003933 continue;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003934
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003935 ASTDeclContextNameLookupTable *LookupTable =
3936 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3937 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3938 if (Pos == LookupTable->end())
Sebastian Redl5c415f32010-07-22 17:01:13 +00003939 continue;
3940
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003941 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3942 for (; Data.first != Data.second; ++Data.first)
3943 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003944 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003945 }
3946
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003947 ++NumVisibleDeclContextsRead;
John McCall75b960e2010-06-01 09:23:16 +00003948
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003949 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00003950 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003951}
3952
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00003953void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3954 assert(DC->hasExternalVisibleStorage() &&
3955 "DeclContext has no visible decls in storage");
3956
3957 llvm::SmallVector<NamedDecl *, 64> Decls;
3958 // There might be visible decls in multiple parts of the chain, for the TU
3959 // and namespaces.
3960 DeclContextInfos &Infos = DeclContextOffsets[DC];
3961 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3962 I != E; ++I) {
3963 if (!I->NameLookupTableData)
3964 continue;
3965
3966 ASTDeclContextNameLookupTable *LookupTable =
3967 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3968 for (ASTDeclContextNameLookupTable::item_iterator
3969 ItemI = LookupTable->item_begin(),
3970 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3971 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3972 = *ItemI;
3973 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3974 Decls.clear();
3975 for (; Data.first != Data.second; ++Data.first)
3976 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3977 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3978 }
3979 }
3980}
3981
Sebastian Redl2c499f62010-08-18 23:56:43 +00003982void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003983 assert(Consumer);
3984 while (!InterestingDecls.empty()) {
3985 DeclGroupRef DG(InterestingDecls.front());
3986 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00003987 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003988 }
3989}
3990
Sebastian Redl2c499f62010-08-18 23:56:43 +00003991void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00003992 this->Consumer = Consumer;
3993
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003994 if (!Consumer)
3995 return;
3996
3997 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003998 // Force deserialization of this decl, which will cause it to be queued for
3999 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00004000 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004001 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00004002
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004003 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004004}
4005
Sebastian Redl2c499f62010-08-18 23:56:43 +00004006void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004007 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004008
Mike Stump11289f42009-09-09 15:08:12 +00004009 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00004010 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00004011 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00004012 unsigned NumDeclsLoaded
4013 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
4014 (Decl *)0);
4015 unsigned NumIdentifiersLoaded
4016 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
4017 IdentifiersLoaded.end(),
4018 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00004019 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00004020 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
4021 SelectorsLoaded.end(),
4022 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00004023
Douglas Gregorc5046832009-04-27 18:38:38 +00004024 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
4025 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor258ae542009-04-27 06:38:32 +00004026 if (TotalNumSLocEntries)
4027 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
4028 NumSLocEntriesRead, TotalNumSLocEntries,
4029 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00004030 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004031 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00004032 NumTypesLoaded, (unsigned)TypesLoaded.size(),
4033 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
4034 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004035 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00004036 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
4037 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00004038 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004039 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00004040 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
4041 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00004042 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004043 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00004044 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
4045 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00004046 if (TotalNumStatements)
4047 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
4048 NumStatementsRead, TotalNumStatements,
4049 ((float)NumStatementsRead/TotalNumStatements * 100));
4050 if (TotalNumMacros)
4051 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
4052 NumMacrosRead, TotalNumMacros,
4053 ((float)NumMacrosRead/TotalNumMacros * 100));
4054 if (TotalLexicalDeclContexts)
4055 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
4056 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4057 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4058 * 100));
4059 if (TotalVisibleDeclContexts)
4060 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
4061 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4062 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4063 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004064 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004065 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004066 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4067 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00004068 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004069 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00004070 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004071 std::fprintf(stderr, "\n");
4072}
4073
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00004074/// Return the amount of memory used by memory buffers, breaking down
4075/// by heap-backed versus mmap'ed memory.
4076void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
4077 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4078 if (llvm::MemoryBuffer *buf = Chain[i]->Buffer.get()) {
4079 size_t bytes = buf->getBufferSize();
4080 switch (buf->getBufferKind()) {
4081 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
4082 sizes.malloc_bytes += bytes;
4083 break;
4084 case llvm::MemoryBuffer::MemoryBuffer_MMap:
4085 sizes.mmap_bytes += bytes;
4086 break;
4087 }
4088 }
4089}
4090
Sebastian Redl2c499f62010-08-18 23:56:43 +00004091void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004092 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004093 S.ExternalSource = this;
4094
Douglas Gregor7cd60f72009-04-22 21:15:06 +00004095 // Makes sure any declarations that were deserialized "too early"
4096 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004097 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4098 if (SemaObj->TUScope)
John McCall48871652010-08-21 09:40:31 +00004099 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004100
4101 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004102 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00004103 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00004104
4105 // If there were any tentative definitions, deserialize them and add
Sebastian Redl35351a92010-01-31 22:27:38 +00004106 // them to Sema's list of tentative definitions.
Douglas Gregord4df8652009-04-22 22:02:47 +00004107 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4108 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redl35351a92010-01-31 22:27:38 +00004109 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregord4df8652009-04-22 22:02:47 +00004110 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00004111
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004112 // If there were any unused file scoped decls, deserialize them and add to
4113 // Sema's list of unused file scoped decls.
4114 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4115 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4116 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattner90073802010-02-12 00:07:30 +00004117 }
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004118
Alexis Hunt27a761d2011-05-04 23:29:54 +00004119 // If there were any delegating constructors, add them to Sema's list
4120 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
4121 CXXConstructorDecl *D
4122 = cast<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
4123 SemaObj->DelegatingCtorDecls.push_back(D);
4124 }
4125
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004126 // If there were any locally-scoped external declarations,
4127 // deserialize them and add them to Sema's table of locally-scoped
4128 // external declarations.
4129 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4130 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4131 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
4132 }
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004133
4134 // If there were any ext_vector type declarations, deserialize them
4135 // and add them to Sema's vector of such declarations.
4136 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
4137 SemaObj->ExtVectorDecls.push_back(
Richard Smithdda56e42011-04-15 14:24:37 +00004138 cast<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004139
4140 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
4141 // Can we cut them down before writing them ?
4142
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004143 // If there were any dynamic classes declarations, deserialize them
4144 // and add them to Sema's vector of such declarations.
4145 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
4146 SemaObj->DynamicClasses.push_back(
4147 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004148
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004149 // Load the offsets of the declarations that Sema references.
4150 // They will be lazily deserialized when needed.
4151 if (!SemaDeclRefs.empty()) {
4152 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
4153 SemaObj->StdNamespace = SemaDeclRefs[0];
4154 SemaObj->StdBadAlloc = SemaDeclRefs[1];
4155 }
4156
Sebastian Redl2c373b92010-10-05 15:59:54 +00004157 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
4158
4159 // If there are @selector references added them to its pool. This is for
4160 // implementation of -Wselector.
4161 if (!F->ReferencedSelectorsData.empty()) {
4162 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
4163 unsigned I = 0;
4164 while (I < DataSize) {
4165 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
4166 SourceLocation SelLoc = ReadSourceLocation(
4167 *F, F->ReferencedSelectorsData, I);
4168 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
4169 }
4170 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00004171 }
4172
Sebastian Redl14afaf02011-04-24 16:27:30 +00004173 // The special data sets below always come from the most recent PCH,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004174 // which is at the front of the chain.
4175 PerFileData &F = *Chain.front();
4176
Sebastian Redl14afaf02011-04-24 16:27:30 +00004177 // If there were any pending implicit instantiations, deserialize them
4178 // and add them to Sema's queue of such instantiations.
4179 assert(F.PendingInstantiations.size() % 2 == 0 &&
4180 "Expected pairs of entries");
4181 for (unsigned Idx = 0, N = F.PendingInstantiations.size(); Idx < N;) {
4182 ValueDecl *D=cast<ValueDecl>(GetDecl(F.PendingInstantiations[Idx++]));
4183 SourceLocation Loc = ReadSourceLocation(F, F.PendingInstantiations,Idx);
4184 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
4185 }
4186
Sebastian Redl2c373b92010-10-05 15:59:54 +00004187 // If there were any weak undeclared identifiers, deserialize them and add to
4188 // Sema's list of weak undeclared identifiers.
4189 if (!WeakUndeclaredIdentifiers.empty()) {
4190 unsigned Idx = 0;
4191 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
4192 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4193 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4194 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
4195 bool Used = WeakUndeclaredIdentifiers[Idx++];
4196 Sema::WeakInfo WI(AliasId, Loc);
4197 WI.setUsed(Used);
4198 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
4199 }
4200 }
4201
4202 // If there were any VTable uses, deserialize the information and add it
4203 // to Sema's vector and map of VTable uses.
4204 if (!VTableUses.empty()) {
4205 unsigned Idx = 0;
4206 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
4207 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4208 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
4209 bool DefinitionRequired = VTableUses[Idx++];
4210 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
4211 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004212 }
4213 }
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004214
4215 if (!FPPragmaOptions.empty()) {
4216 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4217 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4218 }
4219
4220 if (!OpenCLExtensions.empty()) {
4221 unsigned I = 0;
4222#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4223#include "clang/Basic/OpenCLExtensions.def"
4224
4225 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4226 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00004227}
4228
Sebastian Redl2c499f62010-08-18 23:56:43 +00004229IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redl78f51772010-08-02 18:30:12 +00004230 // Try to find this name within our on-disk hash tables. We start with the
4231 // most recent one, since that one contains the most up-to-date info.
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004232 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004233 ASTIdentifierLookupTable *IdTable
4234 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl5c415f32010-07-22 17:01:13 +00004235 if (!IdTable)
4236 continue;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004237 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004238 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004239 if (Pos == IdTable->end())
4240 continue;
Douglas Gregora868bbd2009-04-21 22:25:48 +00004241
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004242 // Dereferencing the iterator has the effect of building the
4243 // IdentifierInfo node and populating it with the various
4244 // declarations it needs.
Sebastian Redl78f51772010-08-02 18:30:12 +00004245 return *Pos;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004246 }
Sebastian Redl78f51772010-08-02 18:30:12 +00004247 return 0;
Douglas Gregora868bbd2009-04-21 22:25:48 +00004248}
4249
Douglas Gregor57756ea2010-10-14 22:11:03 +00004250namespace clang {
4251 /// \brief An identifier-lookup iterator that enumerates all of the
4252 /// identifiers stored within a set of AST files.
4253 class ASTIdentifierIterator : public IdentifierIterator {
4254 /// \brief The AST reader whose identifiers are being enumerated.
4255 const ASTReader &Reader;
4256
4257 /// \brief The current index into the chain of AST files stored in
4258 /// the AST reader.
4259 unsigned Index;
4260
4261 /// \brief The current position within the identifier lookup table
4262 /// of the current AST file.
4263 ASTIdentifierLookupTable::key_iterator Current;
4264
4265 /// \brief The end position within the identifier lookup table of
4266 /// the current AST file.
4267 ASTIdentifierLookupTable::key_iterator End;
4268
4269 public:
4270 explicit ASTIdentifierIterator(const ASTReader &Reader);
4271
4272 virtual llvm::StringRef Next();
4273 };
4274}
4275
4276ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
4277 : Reader(Reader), Index(Reader.Chain.size() - 1) {
4278 ASTIdentifierLookupTable *IdTable
4279 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4280 Current = IdTable->key_begin();
4281 End = IdTable->key_end();
4282}
4283
4284llvm::StringRef ASTIdentifierIterator::Next() {
4285 while (Current == End) {
4286 // If we have exhausted all of our AST files, we're done.
4287 if (Index == 0)
4288 return llvm::StringRef();
4289
4290 --Index;
4291 ASTIdentifierLookupTable *IdTable
4292 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4293 Current = IdTable->key_begin();
4294 End = IdTable->key_end();
4295 }
4296
4297 // We have any identifiers remaining in the current AST file; return
4298 // the next one.
4299 std::pair<const char*, unsigned> Key = *Current;
4300 ++Current;
4301 return llvm::StringRef(Key.first, Key.second);
4302}
4303
4304IdentifierIterator *ASTReader::getIdentifiers() const {
4305 return new ASTIdentifierIterator(*this);
4306}
4307
Mike Stump11289f42009-09-09 15:08:12 +00004308std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redl2c499f62010-08-18 23:56:43 +00004309ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redlada023c2010-08-04 20:40:17 +00004310 // Find this selector in a hash table. We want to find the most recent entry.
4311 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4312 PerFileData &F = *Chain[I];
4313 if (!F.SelectorLookupTable)
4314 continue;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004315
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004316 ASTSelectorLookupTable *PoolTable
4317 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
4318 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redlada023c2010-08-04 20:40:17 +00004319 if (Pos != PoolTable->end()) {
4320 ++NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004321 // FIXME: Not quite happy with the statistics here. We probably should
4322 // disable this tracking when called via LoadSelector.
4323 // Also, should entries without methods count as misses?
4324 ++NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004325 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redlada023c2010-08-04 20:40:17 +00004326 if (DeserializationListener)
4327 DeserializationListener->SelectorRead(Data.ID, Sel);
4328 return std::make_pair(Data.Instance, Data.Factory);
4329 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004330 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00004331
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004332 ++NumMethodPoolMisses;
Sebastian Redlada023c2010-08-04 20:40:17 +00004333 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorc78d3462009-04-24 21:10:55 +00004334}
4335
Sebastian Redl2c499f62010-08-18 23:56:43 +00004336void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004337 // It would be complicated to avoid reading the methods anyway. So don't.
4338 ReadMethodPool(Sel);
4339}
4340
Sebastian Redl2c499f62010-08-18 23:56:43 +00004341void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004342 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00004343 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00004344 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00004345 if (DeserializationListener)
4346 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004347}
4348
Douglas Gregor1342e842009-07-06 18:54:52 +00004349/// \brief Set the globally-visible declarations associated with the given
4350/// identifier.
4351///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004352/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00004353/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00004354/// them.
4355///
4356/// \param II an IdentifierInfo that refers to one or more globally-visible
4357/// declarations.
4358///
4359/// \param DeclIDs the set of declaration IDs with the name @p II that are
4360/// visible at global scope.
4361///
4362/// \param Nonrecursive should be true to indicate that the caller knows that
4363/// this call is non-recursive, and therefore the globally-visible declarations
4364/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00004365void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004366ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregor1342e842009-07-06 18:54:52 +00004367 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
4368 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004369 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004370 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4371 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4372 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004373 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00004374 return;
4375 }
Mike Stump11289f42009-09-09 15:08:12 +00004376
Douglas Gregor1342e842009-07-06 18:54:52 +00004377 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4378 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4379 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004380 if (SemaObj->TUScope) {
4381 // Introduce this declaration into the translation-unit scope
4382 // and add it to the declaration chain for this identifier, so
4383 // that (unqualified) name lookup will find it.
John McCall48871652010-08-21 09:40:31 +00004384 SemaObj->TUScope->AddDecl(D);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004385 }
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004386 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregor1342e842009-07-06 18:54:52 +00004387 } else {
4388 // Queue this declaration so that it will be added to the
4389 // translation unit scope and identifier's declaration chain
4390 // once a Sema object is known.
4391 PreloadedDecls.push_back(D);
4392 }
4393 }
4394}
4395
Sebastian Redl2c499f62010-08-18 23:56:43 +00004396IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004397 if (ID == 0)
4398 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00004399
Sebastian Redlc713b962010-07-21 00:46:22 +00004400 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004401 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004402 return 0;
4403 }
Mike Stump11289f42009-09-09 15:08:12 +00004404
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004405 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc713b962010-07-21 00:46:22 +00004406 ID -= 1;
4407 if (!IdentifiersLoaded[ID]) {
4408 unsigned Index = ID;
4409 const char *Str = 0;
4410 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4411 PerFileData *F = Chain[N - I - 1];
4412 if (Index < F->LocalNumIdentifiers) {
4413 uint32_t Offset = F->IdentifierOffsets[Index];
4414 Str = F->IdentifierTableData + Offset;
4415 break;
4416 }
4417 Index -= F->LocalNumIdentifiers;
4418 }
4419 assert(Str && "Broken Chain");
Douglas Gregor5287b4e2009-04-25 21:04:17 +00004420
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004421 // All of the strings in the AST file are preceded by a 16-bit length.
4422 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00004423 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4424 // unsigned integers. This is important to avoid integer overflow when
4425 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00004426 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00004427 unsigned StrLen = (((unsigned) StrLenPtr[0])
4428 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00004429 IdentifiersLoaded[ID]
Kovarththanan Rajaratnama3b09592010-03-12 10:32:27 +00004430 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlff4a2952010-07-23 23:49:55 +00004431 if (DeserializationListener)
4432 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004433 }
Mike Stump11289f42009-09-09 15:08:12 +00004434
Sebastian Redlc713b962010-07-21 00:46:22 +00004435 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004436}
4437
Douglas Gregor49f754f2011-04-20 00:21:03 +00004438bool ASTReader::ReadSLocEntry(unsigned ID) {
4439 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor258ae542009-04-27 06:38:32 +00004440}
4441
Sebastian Redl2c499f62010-08-18 23:56:43 +00004442Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00004443 if (ID == 0)
4444 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00004445
Sebastian Redlada023c2010-08-04 20:40:17 +00004446 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004447 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00004448 return Selector();
4449 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004450
Sebastian Redlada023c2010-08-04 20:40:17 +00004451 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004452 // Load this selector from the selector table.
Sebastian Redlada023c2010-08-04 20:40:17 +00004453 unsigned Idx = ID - 1;
4454 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4455 PerFileData &F = *Chain[N - I - 1];
4456 if (Idx < F.LocalNumSelectors) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004457 ASTSelectorLookupTrait Trait(*this);
Sebastian Redlada023c2010-08-04 20:40:17 +00004458 SelectorsLoaded[ID - 1] =
4459 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4460 if (DeserializationListener)
4461 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4462 break;
4463 }
4464 Idx -= F.LocalNumSelectors;
4465 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004466 }
4467
Sebastian Redlada023c2010-08-04 20:40:17 +00004468 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00004469}
4470
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004471Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00004472 return DecodeSelector(ID);
4473}
4474
Sebastian Redl2c499f62010-08-18 23:56:43 +00004475uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00004476 // ID 0 (the null selector) is considered an external selector.
4477 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00004478}
4479
Mike Stump11289f42009-09-09 15:08:12 +00004480DeclarationName
Sebastian Redl2c499f62010-08-18 23:56:43 +00004481ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004482 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4483 switch (Kind) {
4484 case DeclarationName::Identifier:
4485 return DeclarationName(GetIdentifierInfo(Record, Idx));
4486
4487 case DeclarationName::ObjCZeroArgSelector:
4488 case DeclarationName::ObjCOneArgSelector:
4489 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff3c301dc2009-04-23 15:15:40 +00004490 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004491
4492 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004493 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004494 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004495
4496 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004497 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004498 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004499
4500 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004501 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004502 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004503
4504 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004505 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004506 (OverloadedOperatorKind)Record[Idx++]);
4507
Alexis Hunt3d221f22009-11-29 07:34:05 +00004508 case DeclarationName::CXXLiteralOperatorName:
4509 return Context->DeclarationNames.getCXXLiteralOperatorName(
4510 GetIdentifierInfo(Record, Idx));
4511
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004512 case DeclarationName::CXXUsingDirective:
4513 return DeclarationName::getUsingDirectiveName();
4514 }
4515
4516 // Required to silence GCC warning
4517 return DeclarationName();
4518}
Douglas Gregor55abb232009-04-10 20:39:37 +00004519
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004520void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4521 DeclarationNameLoc &DNLoc,
4522 DeclarationName Name,
4523 const RecordData &Record, unsigned &Idx) {
4524 switch (Name.getNameKind()) {
4525 case DeclarationName::CXXConstructorName:
4526 case DeclarationName::CXXDestructorName:
4527 case DeclarationName::CXXConversionFunctionName:
4528 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4529 break;
4530
4531 case DeclarationName::CXXOperatorName:
4532 DNLoc.CXXOperatorName.BeginOpNameLoc
4533 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4534 DNLoc.CXXOperatorName.EndOpNameLoc
4535 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4536 break;
4537
4538 case DeclarationName::CXXLiteralOperatorName:
4539 DNLoc.CXXLiteralOperatorName.OpNameLoc
4540 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4541 break;
4542
4543 case DeclarationName::Identifier:
4544 case DeclarationName::ObjCZeroArgSelector:
4545 case DeclarationName::ObjCOneArgSelector:
4546 case DeclarationName::ObjCMultiArgSelector:
4547 case DeclarationName::CXXUsingDirective:
4548 break;
4549 }
4550}
4551
4552void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4553 DeclarationNameInfo &NameInfo,
4554 const RecordData &Record, unsigned &Idx) {
4555 NameInfo.setName(ReadDeclarationName(Record, Idx));
4556 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4557 DeclarationNameLoc DNLoc;
4558 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4559 NameInfo.setInfo(DNLoc);
4560}
4561
4562void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4563 const RecordData &Record, unsigned &Idx) {
Douglas Gregor14454802011-02-25 02:25:35 +00004564 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004565 unsigned NumTPLists = Record[Idx++];
4566 Info.NumTemplParamLists = NumTPLists;
4567 if (NumTPLists) {
4568 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4569 for (unsigned i=0; i != NumTPLists; ++i)
4570 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4571 }
4572}
4573
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004574TemplateName
Douglas Gregor5590be02011-01-15 06:45:20 +00004575ASTReader::ReadTemplateName(PerFileData &F, const RecordData &Record,
4576 unsigned &Idx) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004577 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004578 switch (Kind) {
4579 case TemplateName::Template:
4580 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4581
4582 case TemplateName::OverloadedTemplate: {
4583 unsigned size = Record[Idx++];
4584 UnresolvedSet<8> Decls;
4585 while (size--)
4586 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4587
4588 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4589 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004590
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004591 case TemplateName::QualifiedTemplate: {
4592 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4593 bool hasTemplKeyword = Record[Idx++];
4594 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4595 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4596 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004597
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004598 case TemplateName::DependentTemplate: {
4599 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4600 if (Record[Idx++]) // isIdentifier
4601 return Context->getDependentTemplateName(NNS,
4602 GetIdentifierInfo(Record, Idx));
4603 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004604 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004605 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004606
4607 case TemplateName::SubstTemplateTemplateParmPack: {
4608 TemplateTemplateParmDecl *Param
4609 = cast_or_null<TemplateTemplateParmDecl>(GetDecl(Record[Idx++]));
4610 if (!Param)
4611 return TemplateName();
4612
4613 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
4614 if (ArgPack.getKind() != TemplateArgument::Pack)
4615 return TemplateName();
4616
4617 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
4618 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004619 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004620
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004621 assert(0 && "Unhandled template name kind!");
4622 return TemplateName();
4623}
4624
4625TemplateArgument
Sebastian Redl2c373b92010-10-05 15:59:54 +00004626ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00004627 const RecordData &Record, unsigned &Idx) {
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004628 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
4629 switch (Kind) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004630 case TemplateArgument::Null:
4631 return TemplateArgument();
4632 case TemplateArgument::Type:
4633 return TemplateArgument(GetType(Record[Idx++]));
4634 case TemplateArgument::Declaration:
4635 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00004636 case TemplateArgument::Integral: {
4637 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4638 QualType T = GetType(Record[Idx++]);
4639 return TemplateArgument(Value, T);
4640 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004641 case TemplateArgument::Template:
Douglas Gregor5590be02011-01-15 06:45:20 +00004642 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004643 case TemplateArgument::TemplateExpansion: {
Douglas Gregor5590be02011-01-15 06:45:20 +00004644 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregore1d60df2011-01-14 23:41:42 +00004645 llvm::Optional<unsigned> NumTemplateExpansions;
4646 if (unsigned NumExpansions = Record[Idx++])
4647 NumTemplateExpansions = NumExpansions - 1;
4648 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004649 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004650 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004651 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004652 case TemplateArgument::Pack: {
4653 unsigned NumArgs = Record[Idx++];
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004654 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4655 for (unsigned I = 0; I != NumArgs; ++I)
4656 Args[I] = ReadTemplateArgument(F, Record, Idx);
4657 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004658 }
4659 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004660
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004661 assert(0 && "Unhandled template argument kind!");
4662 return TemplateArgument();
4663}
4664
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004665TemplateParameterList *
Sebastian Redl2c373b92010-10-05 15:59:54 +00004666ASTReader::ReadTemplateParameterList(PerFileData &F,
4667 const RecordData &Record, unsigned &Idx) {
4668 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4669 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4670 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004671
4672 unsigned NumParams = Record[Idx++];
4673 llvm::SmallVector<NamedDecl *, 16> Params;
4674 Params.reserve(NumParams);
4675 while (NumParams--)
4676 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004677
4678 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004679 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4680 Params.data(), Params.size(), RAngleLoc);
4681 return TemplateParams;
4682}
4683
4684void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004685ASTReader::
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004686ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004687 PerFileData &F, const RecordData &Record,
4688 unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004689 unsigned NumTemplateArgs = Record[Idx++];
4690 TemplArgs.reserve(NumTemplateArgs);
4691 while (NumTemplateArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004692 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004693}
4694
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004695/// \brief Read a UnresolvedSet structure.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004696void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004697 const RecordData &Record, unsigned &Idx) {
4698 unsigned NumDecls = Record[Idx++];
4699 while (NumDecls--) {
4700 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4701 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4702 Set.addDecl(D, AS);
4703 }
4704}
4705
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004706CXXBaseSpecifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00004707ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky19b9f952010-07-26 16:56:01 +00004708 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004709 bool isVirtual = static_cast<bool>(Record[Idx++]);
4710 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4711 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redl08905022011-02-05 19:23:19 +00004712 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004713 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4714 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor752a5952011-01-03 22:36:02 +00004715 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl08905022011-02-05 19:23:19 +00004716 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregor752a5952011-01-03 22:36:02 +00004717 EllipsisLoc);
Sebastian Redl08905022011-02-05 19:23:19 +00004718 Result.setInheritConstructors(inheritConstructors);
4719 return Result;
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004720}
4721
Alexis Hunt1d792652011-01-08 20:30:50 +00004722std::pair<CXXCtorInitializer **, unsigned>
4723ASTReader::ReadCXXCtorInitializers(PerFileData &F, const RecordData &Record,
4724 unsigned &Idx) {
4725 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004726 unsigned NumInitializers = Record[Idx++];
4727 if (NumInitializers) {
4728 ASTContext &C = *getContext();
4729
Alexis Hunt1d792652011-01-08 20:30:50 +00004730 CtorInitializers
4731 = new (C) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004732 for (unsigned i=0; i != NumInitializers; ++i) {
4733 TypeSourceInfo *BaseClassInfo = 0;
4734 bool IsBaseVirtual = false;
4735 FieldDecl *Member = 0;
Francois Pichetd583da02010-12-04 09:14:42 +00004736 IndirectFieldDecl *IndirectMember = 0;
Alexis Hunt37a477f2011-05-04 01:19:08 +00004737 CXXConstructorDecl *Target = 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004738
Alexis Hunt37a477f2011-05-04 01:19:08 +00004739 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
4740 switch (Type) {
4741 case CTOR_INITIALIZER_BASE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004742 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004743 IsBaseVirtual = Record[Idx++];
Alexis Hunt37a477f2011-05-04 01:19:08 +00004744 break;
4745
4746 case CTOR_INITIALIZER_DELEGATING:
4747 Target = cast<CXXConstructorDecl>(GetDecl(Record[Idx++]));
4748 break;
4749
4750 case CTOR_INITIALIZER_MEMBER:
4751 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
4752 break;
4753
4754 case CTOR_INITIALIZER_INDIRECT_MEMBER:
4755 IndirectMember = cast<IndirectFieldDecl>(GetDecl(Record[Idx++]));
4756 break;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004757 }
Alexis Hunt37a477f2011-05-04 01:19:08 +00004758
Douglas Gregor44e7df62011-01-04 00:32:56 +00004759 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004760 Expr *Init = ReadExpr(F);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004761 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4762 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004763 bool IsWritten = Record[Idx++];
4764 unsigned SourceOrderOrNumArrayIndices;
4765 llvm::SmallVector<VarDecl *, 8> Indices;
4766 if (IsWritten) {
4767 SourceOrderOrNumArrayIndices = Record[Idx++];
4768 } else {
4769 SourceOrderOrNumArrayIndices = Record[Idx++];
4770 Indices.reserve(SourceOrderOrNumArrayIndices);
4771 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4772 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4773 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004774
Alexis Hunt1d792652011-01-08 20:30:50 +00004775 CXXCtorInitializer *BOMInit;
Alexis Hunt37a477f2011-05-04 01:19:08 +00004776 if (Type == CTOR_INITIALIZER_BASE) {
Alexis Hunt1d792652011-01-08 20:30:50 +00004777 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
4778 LParenLoc, Init, RParenLoc,
4779 MemberOrEllipsisLoc);
Alexis Hunt37a477f2011-05-04 01:19:08 +00004780 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
4781 BOMInit = new (C) CXXCtorInitializer(C, MemberOrEllipsisLoc, LParenLoc,
4782 Target, Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004783 } else if (IsWritten) {
Francois Pichetd583da02010-12-04 09:14:42 +00004784 if (Member)
Alexis Hunt1d792652011-01-08 20:30:50 +00004785 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
4786 LParenLoc, Init, RParenLoc);
Francois Pichetd583da02010-12-04 09:14:42 +00004787 else
Alexis Hunt1d792652011-01-08 20:30:50 +00004788 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
4789 MemberOrEllipsisLoc, LParenLoc,
4790 Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004791 } else {
Alexis Hunt1d792652011-01-08 20:30:50 +00004792 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
4793 LParenLoc, Init, RParenLoc,
4794 Indices.data(), Indices.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004795 }
4796
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00004797 if (IsWritten)
4798 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Alexis Hunt1d792652011-01-08 20:30:50 +00004799 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004800 }
4801 }
4802
Alexis Hunt1d792652011-01-08 20:30:50 +00004803 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004804}
4805
Chris Lattnerca025db2010-05-07 21:43:38 +00004806NestedNameSpecifier *
Sebastian Redl2c499f62010-08-18 23:56:43 +00004807ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004808 unsigned N = Record[Idx++];
4809 NestedNameSpecifier *NNS = 0, *Prev = 0;
4810 for (unsigned I = 0; I != N; ++I) {
4811 NestedNameSpecifier::SpecifierKind Kind
4812 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4813 switch (Kind) {
4814 case NestedNameSpecifier::Identifier: {
4815 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4816 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4817 break;
4818 }
4819
4820 case NestedNameSpecifier::Namespace: {
4821 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4822 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4823 break;
4824 }
4825
Douglas Gregor7b26ff92011-02-24 02:36:08 +00004826 case NestedNameSpecifier::NamespaceAlias: {
4827 NamespaceAliasDecl *Alias
4828 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++]));
4829 NNS = NestedNameSpecifier::Create(*Context, Prev, Alias);
4830 break;
4831 }
4832
Chris Lattnerca025db2010-05-07 21:43:38 +00004833 case NestedNameSpecifier::TypeSpec:
4834 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall424cec92011-01-19 06:33:43 +00004835 const Type *T = GetType(Record[Idx++]).getTypePtrOrNull();
Douglas Gregor0cdc8322010-12-10 17:03:06 +00004836 if (!T)
4837 return 0;
4838
Chris Lattnerca025db2010-05-07 21:43:38 +00004839 bool Template = Record[Idx++];
4840 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4841 break;
4842 }
4843
4844 case NestedNameSpecifier::Global: {
4845 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4846 // No associated value, and there can't be a prefix.
4847 break;
4848 }
Chris Lattnerca025db2010-05-07 21:43:38 +00004849 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00004850 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00004851 }
4852 return NNS;
4853}
4854
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004855NestedNameSpecifierLoc
4856ASTReader::ReadNestedNameSpecifierLoc(PerFileData &F, const RecordData &Record,
4857 unsigned &Idx) {
4858 unsigned N = Record[Idx++];
Douglas Gregor9b272512011-02-28 23:58:31 +00004859 NestedNameSpecifierLocBuilder Builder;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004860 for (unsigned I = 0; I != N; ++I) {
4861 NestedNameSpecifier::SpecifierKind Kind
4862 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4863 switch (Kind) {
4864 case NestedNameSpecifier::Identifier: {
Douglas Gregor9b272512011-02-28 23:58:31 +00004865 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004866 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00004867 Builder.Extend(*Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004868 break;
4869 }
4870
4871 case NestedNameSpecifier::Namespace: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004872 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004873 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00004874 Builder.Extend(*Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004875 break;
4876 }
4877
4878 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004879 NamespaceAliasDecl *Alias
4880 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++]));
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004881 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00004882 Builder.Extend(*Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004883 break;
4884 }
4885
4886 case NestedNameSpecifier::TypeSpec:
4887 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004888 bool Template = Record[Idx++];
4889 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
4890 if (!T)
4891 return NestedNameSpecifierLoc();
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004892 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00004893
4894 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
4895 Builder.Extend(*Context,
4896 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
4897 T->getTypeLoc(), ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004898 break;
4899 }
4900
4901 case NestedNameSpecifier::Global: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004902 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00004903 Builder.MakeGlobal(*Context, ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004904 break;
4905 }
4906 }
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004907 }
4908
Douglas Gregor9b272512011-02-28 23:58:31 +00004909 return Builder.getWithLocInContext(*Context);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004910}
4911
Chris Lattnerca025db2010-05-07 21:43:38 +00004912SourceRange
Sebastian Redl2c373b92010-10-05 15:59:54 +00004913ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4914 unsigned &Idx) {
4915 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4916 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00004917 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00004918}
4919
Douglas Gregor1daeb692009-04-13 18:14:40 +00004920/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004921llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004922 unsigned BitWidth = Record[Idx++];
4923 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4924 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4925 Idx += NumWords;
4926 return Result;
4927}
4928
4929/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004930llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004931 bool isUnsigned = Record[Idx++];
4932 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4933}
4934
Douglas Gregore0a3a512009-04-14 21:55:33 +00004935/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004936llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004937 return llvm::APFloat(ReadAPInt(Record, Idx));
4938}
4939
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004940// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00004941std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004942 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00004943 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004944 Idx += Len;
4945 return Result;
4946}
4947
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004948VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
4949 unsigned &Idx) {
4950 unsigned Major = Record[Idx++];
4951 unsigned Minor = Record[Idx++];
4952 unsigned Subminor = Record[Idx++];
4953 if (Minor == 0)
4954 return VersionTuple(Major);
4955 if (Subminor == 0)
4956 return VersionTuple(Major, Minor - 1);
4957 return VersionTuple(Major, Minor - 1, Subminor - 1);
4958}
4959
Sebastian Redl2c499f62010-08-18 23:56:43 +00004960CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00004961 unsigned &Idx) {
4962 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4963 return CXXTemporary::Create(*Context, Decl);
4964}
4965
Sebastian Redl2c499f62010-08-18 23:56:43 +00004966DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00004967 return Diag(SourceLocation(), DiagID);
4968}
4969
Sebastian Redl2c499f62010-08-18 23:56:43 +00004970DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004971 return Diags.Report(Loc, DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00004972}
Douglas Gregora9af1d12009-04-17 00:04:06 +00004973
Douglas Gregora868bbd2009-04-21 22:25:48 +00004974/// \brief Retrieve the identifier table associated with the
4975/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004976IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004977 assert(PP && "Forgot to set Preprocessor ?");
4978 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00004979}
4980
Douglas Gregora9af1d12009-04-17 00:04:06 +00004981/// \brief Record that the given ID maps to the given switch-case
4982/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004983void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004984 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4985 SwitchCaseStmts[ID] = SC;
4986}
4987
4988/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004989SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004990 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4991 return SwitchCaseStmts[ID];
4992}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004993
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00004994void ASTReader::ClearSwitchCaseIDs() {
4995 SwitchCaseStmts.clear();
4996}
4997
Sebastian Redl2c499f62010-08-18 23:56:43 +00004998void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004999 assert(NumCurrentElementsDeserializing &&
5000 "FinishedDeserializing not paired with StartedDeserializing");
5001 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00005002 // If any identifiers with corresponding top-level declarations have
5003 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005004 while (!PendingIdentifierInfos.empty()) {
5005 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
5006 PendingIdentifierInfos.front().DeclIDs, true);
5007 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00005008 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005009
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00005010 // Ready to load previous declarations of Decls that were delayed.
5011 while (!PendingPreviousDecls.empty()) {
5012 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
5013 PendingPreviousDecls.front().second);
5014 PendingPreviousDecls.pop_front();
5015 }
5016
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005017 // We are not in recursive loading, so it's safe to pass the "interesting"
5018 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005019 if (Consumer)
5020 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidisad5f95c2010-10-24 17:26:31 +00005021
5022 assert(PendingForwardRefs.size() == 0 &&
5023 "Some forward refs did not get linked to the definition!");
Douglas Gregor1342e842009-07-06 18:54:52 +00005024 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005025 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00005026}
Douglas Gregorb473b072010-08-19 00:28:17 +00005027
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005028ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005029 const char *isysroot, bool DisableValidation,
5030 bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005031 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
5032 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
5033 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
5034 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005035 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
5036 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NextSLocOffset(0),
5037 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
5038 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
5039 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
5040 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
5041 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
5042 NumCurrentElementsDeserializing(0)
5043{
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005044 RelocatablePCH = false;
5045}
5046
5047ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
5048 Diagnostic &Diags, const char *isysroot,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005049 bool DisableValidation, bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005050 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
5051 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005052 isysroot(isysroot), DisableValidation(DisableValidation),
5053 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
5054 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00005055 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
5056 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
5057 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
5058 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
5059 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
5060 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005061 RelocatablePCH = false;
5062}
5063
5064ASTReader::~ASTReader() {
5065 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
5066 delete Chain[e - i - 1];
5067 // Delete all visible decl lookup tables
5068 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
5069 E = DeclContextOffsets.end();
5070 I != E; ++I) {
5071 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
5072 J != F; ++J) {
5073 if (J->NameLookupTableData)
5074 delete static_cast<ASTDeclContextNameLookupTable*>(
5075 J->NameLookupTableData);
5076 }
5077 }
5078 for (DeclContextVisibleUpdatesPending::iterator
5079 I = PendingVisibleUpdates.begin(),
5080 E = PendingVisibleUpdates.end();
5081 I != E; ++I) {
5082 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5083 F = I->second.end();
5084 J != F; ++J)
5085 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
5086 }
5087}
5088
Sebastian Redl009e7f22010-10-05 16:15:19 +00005089ASTReader::PerFileData::PerFileData(ASTFileType Ty)
5090 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00005091 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
5092 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
Douglas Gregor09b69892011-02-10 17:09:37 +00005093 MacroDefinitionOffsets(0),
5094 LocalNumHeaderFileInfos(0), HeaderFileInfoTableData(0),
5095 HeaderFileInfoTable(0),
5096 LocalNumSelectors(0), SelectorOffsets(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00005097 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005098 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
5099 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redl3f6b7532010-10-01 19:59:12 +00005100 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregorb473b072010-08-19 00:28:17 +00005101{}
5102
5103ASTReader::PerFileData::~PerFileData() {
5104 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
Douglas Gregor09b69892011-02-10 17:09:37 +00005105 delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable);
Douglas Gregorb473b072010-08-19 00:28:17 +00005106 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
5107}