blob: 8b99fc7dfe6e2b4332fd5f70c4a4c9064602834d [file] [log] [blame]
Sebastian Redl3b3c8742010-08-18 23:57:11 +00001//===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redl2c499f62010-08-18 23:56:43 +000010// This file defines the ASTReader class, which reads AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner92ba5ff2009-04-27 05:14:47 +000013
Sebastian Redlf5b13462010-08-18 23:57:17 +000014#include "clang/Serialization/ASTReader.h"
15#include "clang/Serialization/ASTDeserializationListener.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregor55abb232009-04-10 20:39:37 +000017#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbar732ef8a2009-11-11 23:58:53 +000018#include "clang/Frontend/Utils.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000019#include "clang/Sema/Sema.h"
John McCallcc14d1f2010-08-24 08:50:51 +000020#include "clang/Sema/Scope.h"
Douglas Gregor1a0d0b92009-04-14 00:24:19 +000021#include "clang/AST/ASTConsumer.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000022#include "clang/AST/ASTContext.h"
John McCall19c1bfd2010-08-25 05:32:35 +000023#include "clang/AST/DeclTemplate.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregor9b272512011-02-28 23:58:31 +000026#include "clang/AST/NestedNameSpecifier.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000027#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000028#include "clang/AST/TypeLocVisitor.h"
Chris Lattner34321bc2009-04-10 21:41:48 +000029#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000030#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000031#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000032#include "clang/Lex/HeaderSearch.h"
Douglas Gregora868bbd2009-04-21 22:25:48 +000033#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000034#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000035#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000036#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000037#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000038#include "clang/Basic/TargetInfo.h"
Douglas Gregord54f3a12009-10-05 21:07:28 +000039#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000040#include "clang/Basic/VersionTuple.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000041#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000042#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000043#include "llvm/Support/MemoryBuffer.h"
John McCall0ad16662009-10-29 08:12:44 +000044#include "llvm/Support/ErrorHandling.h"
Douglas Gregor09b69892011-02-10 17:09:37 +000045#include "llvm/Support/FileSystem.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Michael J. Spencerf25faaa2010-12-09 17:36:38 +000047#include "llvm/Support/system_error.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000048#include <algorithm>
Douglas Gregorc379c072009-04-28 18:58:38 +000049#include <iterator>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000050#include <cstdio>
Douglas Gregorc5046832009-04-27 18:38:38 +000051#include <sys/stat.h>
Douglas Gregor09b69892011-02-10 17:09:37 +000052
Douglas Gregoref84c4b2009-04-09 22:27:44 +000053using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000054using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000055
56//===----------------------------------------------------------------------===//
Sebastian Redld44cd6a2010-08-18 23:57:06 +000057// PCH validator implementation
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000058//===----------------------------------------------------------------------===//
59
Sebastian Redl3e31c722010-08-18 23:56:56 +000060ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000061
62bool
63PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
64 const LangOptions &PPLangOpts = PP.getLangOptions();
65#define PARSE_LANGOPT_BENIGN(Option)
66#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
67 if (PPLangOpts.Option != LangOpts.Option) { \
68 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
69 return true; \
70 }
71
72 PARSE_LANGOPT_BENIGN(Trigraphs);
73 PARSE_LANGOPT_BENIGN(BCPLComment);
74 PARSE_LANGOPT_BENIGN(DollarIdents);
75 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
76 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carruthe03aa552010-04-17 20:17:31 +000077 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000078 PARSE_LANGOPT_BENIGN(ImplicitInt);
79 PARSE_LANGOPT_BENIGN(Digraphs);
80 PARSE_LANGOPT_BENIGN(HexFloats);
81 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
Peter Collingbournea686b5f2011-04-15 00:35:23 +000082 PARSE_LANGOPT_IMPORTANT(C1X, diag::warn_pch_c1x);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000083 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +000084 PARSE_LANGOPT_BENIGN(MSCVersion);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000085 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
86 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
87 PARSE_LANGOPT_BENIGN(CXXOperatorName);
88 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
89 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
90 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian45878032010-02-09 19:31:38 +000091 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +000092 PARSE_LANGOPT_IMPORTANT(AppleKext, diag::warn_pch_apple_kext);
Ted Kremenek1d56c9e2010-12-23 21:35:43 +000093 PARSE_LANGOPT_IMPORTANT(ObjCDefaultSynthProperties,
94 diag::warn_pch_objc_auto_properties);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +000095 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian62c56022010-04-22 21:01:59 +000096 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000097 PARSE_LANGOPT_BENIGN(PascalStrings);
98 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump11289f42009-09-09 15:08:12 +000099 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000100 diag::warn_pch_lax_vector_conversions);
Nate Begeman9d905792009-06-25 22:57:40 +0000101 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000102 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Anders Carlssonce8dd3a2011-02-19 23:53:54 +0000103 PARSE_LANGOPT_IMPORTANT(ObjCExceptions, diag::warn_pch_objc_exceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +0000104 PARSE_LANGOPT_IMPORTANT(CXXExceptions, diag::warn_pch_cxx_exceptions);
105 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Douglas Gregordbe39272011-02-01 15:15:22 +0000106 PARSE_LANGOPT_IMPORTANT(MSBitfields, diag::warn_pch_ms_bitfields);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000107 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
108 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
109 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump11289f42009-09-09 15:08:12 +0000110 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000111 diag::warn_pch_thread_safe_statics);
Daniel Dunbara77eaeb2009-09-03 04:54:28 +0000112 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000113 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
114 PARSE_LANGOPT_BENIGN(EmitAllDecls);
115 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattner51924e512010-06-26 21:25:03 +0000116 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump11289f42009-09-09 15:08:12 +0000117 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000118 diag::warn_pch_heinous_extensions);
119 // FIXME: Most of the options below are benign if the macro wasn't
120 // used. Unfortunately, this means that a PCH compiled without
121 // optimization can't be used with optimization turned on, even
122 // though the only thing that changes is whether __OPTIMIZE__ was
123 // defined... but if __OPTIMIZE__ never showed up in the header, it
124 // doesn't matter. We could consider making this some special kind
125 // of check.
126 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
127 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
128 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
129 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
130 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
131 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
Chandler Carruth7ffce732011-04-23 20:05:38 +0000132 PARSE_LANGOPT_IMPORTANT(Deprecated, diag::warn_pch_deprecated);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000133 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
134 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsoned4e2952009-11-05 20:14:16 +0000135 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +0000136 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000137 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump11289f42009-09-09 15:08:12 +0000138 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000139 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
140 return true;
141 }
142 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000143 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
144 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000145 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman9d905792009-06-25 22:57:40 +0000146 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Peter Collingbourne546d0792010-12-01 19:14:57 +0000147 PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda);
Mike Stumpd9546382009-12-12 01:27:46 +0000148 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbar143021e2009-09-21 04:16:19 +0000149 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000150 PARSE_LANGOPT_BENIGN(SpellChecking);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000151 PARSE_LANGOPT_BENIGN(DefaultFPContract);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +0000152#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000153#undef PARSE_LANGOPT_BENIGN
154
155 return false;
156}
157
Daniel Dunbar20a682d2009-11-11 00:52:11 +0000158bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
159 if (Triple == PP.getTargetInfo().getTriple().str())
160 return false;
161
162 Reader.Diag(diag::warn_pch_target_triple)
163 << Triple << PP.getTargetInfo().getTriple().str();
164 return true;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000165}
166
Benjamin Kramer90b5b682010-11-25 18:29:30 +0000167namespace {
168 struct EmptyStringRef {
169 bool operator ()(llvm::StringRef r) const { return r.empty(); }
170 };
171 struct EmptyBlock {
172 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
173 };
174}
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000175
176static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
177 PCHPredefinesBlocks R) {
178 // First, sum up the lengths.
179 unsigned LL = 0, RL = 0;
180 for (unsigned I = 0, N = L.size(); I != N; ++I) {
181 LL += L[I].size();
182 }
183 for (unsigned I = 0, N = R.size(); I != N; ++I) {
184 RL += R[I].Data.size();
185 }
186 if (LL != RL)
187 return false;
188 if (LL == 0 && RL == 0)
189 return true;
190
191 // Kick out empty parts, they confuse the algorithm below.
192 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
193 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
194
195 // Do it the hard way. At this point, both vectors must be non-empty.
196 llvm::StringRef LR = L[0], RR = R[0].Data;
197 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbar01ad0a72010-07-16 00:00:11 +0000198 (void) RN;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000199 for (;;) {
200 // Compare the current pieces.
201 if (LR.size() == RR.size()) {
202 // If they're the same length, it's pretty easy.
203 if (LR != RR)
204 return false;
205 // Both pieces are done, advance.
206 ++LI;
207 ++RI;
208 // If either string is done, they're both done, since they're the same
209 // length.
210 if (LI == LN) {
211 assert(RI == RN && "Strings not the same length after all?");
212 return true;
213 }
214 LR = L[LI];
215 RR = R[RI].Data;
216 } else if (LR.size() < RR.size()) {
217 // Right piece is longer.
218 if (!RR.startswith(LR))
219 return false;
220 ++LI;
221 assert(LI != LN && "Strings not the same length after all?");
222 RR = RR.substr(LR.size());
223 LR = L[LI];
224 } else {
225 // Left piece is longer.
226 if (!LR.startswith(RR))
227 return false;
228 ++RI;
229 assert(RI != RN && "Strings not the same length after all?");
230 LR = LR.substr(RR.size());
231 RR = R[RI].Data;
232 }
233 }
234}
235
236static std::pair<FileID, llvm::StringRef::size_type>
237FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
238 std::pair<FileID, llvm::StringRef::size_type> Res;
239 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
240 Res.second = Buffers[I].Data.find(MacroDef);
241 if (Res.second != llvm::StringRef::npos) {
242 Res.first = Buffers[I].BufferID;
243 break;
244 }
245 }
246 return Res;
247}
248
249bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000250 llvm::StringRef OriginalFileName,
Nick Lewycky36079892011-02-23 21:16:44 +0000251 std::string &SuggestedPredefines,
252 FileManager &FileMgr) {
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000253 // We are in the context of an implicit include, so the predefines buffer will
254 // have a #include entry for the PCH file itself (as normalized by the
255 // preprocessor initialization). Find it and skip over it in the checking
256 // below.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000257 llvm::SmallString<256> PCHInclude;
258 PCHInclude += "#include \"";
Nick Lewycky36079892011-02-23 21:16:44 +0000259 PCHInclude += NormalizeDashIncludePath(OriginalFileName, FileMgr);
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000260 PCHInclude += "\"\n";
261 std::pair<llvm::StringRef,llvm::StringRef> Split =
262 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
263 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000264 if (Left == PP.getPredefines()) {
265 Error("Missing PCH include entry!");
266 return true;
267 }
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000268
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000269 // If the concatenation of all the PCH buffers is equal to the adjusted
270 // command line, we're done.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000271 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
272 CommandLine.push_back(Left);
273 CommandLine.push_back(Right);
274 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000275 return false;
276
277 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000278
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000279 // The predefines buffers are different. Determine what the differences are,
280 // and whether they require us to reject the PCH file.
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000281 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000282 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
283 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000284
285 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
286 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000287
288 // Pick out implicit #includes after the PCH and don't consider them for
289 // validation; we will insert them into SuggestedPredefines so that the
290 // preprocessor includes them.
291 std::string IncludesAfterPCH;
292 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
293 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
294 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
295 if (AfterPCHLines[i].startswith("#include ")) {
296 IncludesAfterPCH += AfterPCHLines[i];
297 IncludesAfterPCH += '\n';
298 } else {
299 CmdLineLines.push_back(AfterPCHLines[i]);
300 }
301 }
302
303 // Make sure we add the includes last into SuggestedPredefines before we
304 // exit this function.
305 struct AddIncludesRAII {
306 std::string &SuggestedPredefines;
307 std::string &IncludesAfterPCH;
308
309 AddIncludesRAII(std::string &SuggestedPredefines,
310 std::string &IncludesAfterPCH)
311 : SuggestedPredefines(SuggestedPredefines),
312 IncludesAfterPCH(IncludesAfterPCH) { }
313 ~AddIncludesRAII() {
314 SuggestedPredefines += IncludesAfterPCH;
315 }
316 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000317
Daniel Dunbar499baed2009-11-11 05:26:28 +0000318 // Sort both sets of predefined buffer lines, since we allow some extra
319 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000320 std::sort(CmdLineLines.begin(), CmdLineLines.end());
321 std::sort(PCHLines.begin(), PCHLines.end());
322
Daniel Dunbar499baed2009-11-11 05:26:28 +0000323 // Determine which predefines that were used to build the PCH file are missing
324 // from the command line.
325 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000326 std::set_difference(PCHLines.begin(), PCHLines.end(),
327 CmdLineLines.begin(), CmdLineLines.end(),
328 std::back_inserter(MissingPredefines));
329
330 bool MissingDefines = false;
331 bool ConflictingDefines = false;
332 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000333 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000334 if (Missing.startswith("#include ")) {
335 // An -include was specified when generating the PCH; it is included in
336 // the PCH, just ignore it.
337 continue;
338 }
Daniel Dunbar499baed2009-11-11 05:26:28 +0000339 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000340 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
341 return true;
342 }
Mike Stump11289f42009-09-09 15:08:12 +0000343
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000344 // This is a macro definition. Determine the name of the macro we're
345 // defining.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000346 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000347 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000348 = Missing.find_first_of("( \n\r", StartOfMacroName);
349 assert(EndOfMacroName != std::string::npos &&
350 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000351 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000352
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000353 // Determine whether this macro was given a different definition on the
354 // command line.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000355 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000356 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000357 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000358 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
359 MacroDefStart);
360 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000361 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000362 // Different macro; we're done.
363 ConflictPos = CmdLineLines.end();
Mike Stump11289f42009-09-09 15:08:12 +0000364 break;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000365 }
Mike Stump11289f42009-09-09 15:08:12 +0000366
367 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000368 "Invalid #define in predefines buffer?");
Mike Stump11289f42009-09-09 15:08:12 +0000369 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000370 (*ConflictPos)[MacroDefLen] != '(')
371 continue; // Longer macro name; keep trying.
Mike Stump11289f42009-09-09 15:08:12 +0000372
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000373 // We found a conflicting macro definition.
374 break;
375 }
Mike Stump11289f42009-09-09 15:08:12 +0000376
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000377 if (ConflictPos != CmdLineLines.end()) {
378 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
379 << MacroName;
380
381 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000382 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
383 FindMacro(Buffers, Missing);
384 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
385 SourceLocation PCHMissingLoc =
386 SourceMgr.getLocForStartOfFile(MacroLoc.first)
387 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar499baed2009-11-11 05:26:28 +0000388 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000389
390 ConflictingDefines = true;
391 continue;
392 }
Mike Stump11289f42009-09-09 15:08:12 +0000393
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000394 // If the macro doesn't conflict, then we'll just pick up the macro
395 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000396 if (ConflictingDefines)
397 continue; // Don't complain if there are already conflicting defs
Mike Stump11289f42009-09-09 15:08:12 +0000398
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000399 if (!MissingDefines) {
400 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
401 MissingDefines = true;
402 }
403
404 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000405 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
406 FindMacro(Buffers, Missing);
407 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
408 SourceLocation PCHMissingLoc =
409 SourceMgr.getLocForStartOfFile(MacroLoc.first)
410 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000411 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
412 }
Mike Stump11289f42009-09-09 15:08:12 +0000413
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000414 if (ConflictingDefines)
415 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000416
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000417 // Determine what predefines were introduced based on command-line
418 // parameters that were not present when building the PCH
419 // file. Extra #defines are okay, so long as the identifiers being
420 // defined were not used within the precompiled header.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000421 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000422 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
423 PCHLines.begin(), PCHLines.end(),
Mike Stump11289f42009-09-09 15:08:12 +0000424 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000425 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000426 llvm::StringRef &Extra = ExtraPredefines[I];
427 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000428 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
429 return true;
430 }
431
432 // This is an extra macro definition. Determine the name of the
433 // macro we're defining.
434 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000435 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000436 = Extra.find_first_of("( \n\r", StartOfMacroName);
437 assert(EndOfMacroName != std::string::npos &&
438 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000439 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000440
441 // Check whether this name was used somewhere in the PCH file. If
442 // so, defining it as a macro could change behavior, so we reject
443 // the PCH file.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000444 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar045c92f2009-11-11 00:52:00 +0000445 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000446 return true;
447 }
448
449 // Add this definition to the suggested predefines buffer.
450 SuggestedPredefines += Extra;
451 SuggestedPredefines += '\n';
452 }
453
454 // If we get here, it's because the predefines buffer had compatible
455 // contents. Accept the PCH file.
456 return false;
457}
458
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000459void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
460 unsigned ID) {
461 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
462 ++NumHeaderInfos;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000463}
464
465void PCHValidator::ReadCounter(unsigned Value) {
466 PP.setCounterValue(Value);
467}
468
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000469//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +0000470// AST reader implementation
Douglas Gregora868bbd2009-04-21 22:25:48 +0000471//===----------------------------------------------------------------------===//
472
Sebastian Redl07a89a82010-07-30 00:29:29 +0000473void
Sebastian Redl3e31c722010-08-18 23:56:56 +0000474ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redl07a89a82010-07-30 00:29:29 +0000475 DeserializationListener = Listener;
Sebastian Redl07a89a82010-07-30 00:29:29 +0000476}
477
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000478
Douglas Gregora868bbd2009-04-21 22:25:48 +0000479namespace {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000480class ASTSelectorLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000481 ASTReader &Reader;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000482
483public:
Sebastian Redl834bb972010-08-04 17:20:04 +0000484 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +0000485 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +0000486 ObjCMethodList Instance, Factory;
487 };
Douglas Gregorc78d3462009-04-24 21:10:55 +0000488
489 typedef Selector external_key_type;
490 typedef external_key_type internal_key_type;
491
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000492 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump11289f42009-09-09 15:08:12 +0000493
Douglas Gregorc78d3462009-04-24 21:10:55 +0000494 static bool EqualKey(const internal_key_type& a,
495 const internal_key_type& b) {
496 return a == b;
497 }
Mike Stump11289f42009-09-09 15:08:12 +0000498
Douglas Gregorc78d3462009-04-24 21:10:55 +0000499 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +0000500 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000501 }
Mike Stump11289f42009-09-09 15:08:12 +0000502
Douglas Gregorc78d3462009-04-24 21:10:55 +0000503 // This hopefully will just get inlined and removed by the optimizer.
504 static const internal_key_type&
505 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000506
Douglas Gregorc78d3462009-04-24 21:10:55 +0000507 static std::pair<unsigned, unsigned>
508 ReadKeyDataLength(const unsigned char*& d) {
509 using namespace clang::io;
510 unsigned KeyLen = ReadUnalignedLE16(d);
511 unsigned DataLen = ReadUnalignedLE16(d);
512 return std::make_pair(KeyLen, DataLen);
513 }
Mike Stump11289f42009-09-09 15:08:12 +0000514
Douglas Gregor95c13f52009-04-25 17:48:32 +0000515 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000516 using namespace clang::io;
Chris Lattner8575daa2009-04-27 21:45:14 +0000517 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000518 unsigned N = ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000519 IdentifierInfo *FirstII
Douglas Gregorc78d3462009-04-24 21:10:55 +0000520 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
521 if (N == 0)
522 return SelTable.getNullarySelector(FirstII);
523 else if (N == 1)
524 return SelTable.getUnarySelector(FirstII);
525
526 llvm::SmallVector<IdentifierInfo *, 16> Args;
527 Args.push_back(FirstII);
528 for (unsigned I = 1; I != N; ++I)
529 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
530
Douglas Gregor038c3382009-05-22 22:45:36 +0000531 return SelTable.getSelector(N, Args.data());
Douglas Gregorc78d3462009-04-24 21:10:55 +0000532 }
Mike Stump11289f42009-09-09 15:08:12 +0000533
Douglas Gregorc78d3462009-04-24 21:10:55 +0000534 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
535 using namespace clang::io;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000536
537 data_type Result;
538
Sebastian Redl834bb972010-08-04 17:20:04 +0000539 Result.ID = ReadUnalignedLE32(d);
540 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
541 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
542
Douglas Gregorc78d3462009-04-24 21:10:55 +0000543 // Load instance methods
544 ObjCMethodList *Prev = 0;
545 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000546 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000547 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000548 if (!Result.Instance.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000549 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000550 Result.Instance.Method = Method;
551 Prev = &Result.Instance;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000552 continue;
553 }
554
Ted Kremenekda4abf12010-02-11 00:53:01 +0000555 ObjCMethodList *Mem =
556 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
557 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000558 Prev = Prev->Next;
559 }
560
561 // Load factory methods
562 Prev = 0;
563 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000564 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000565 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000566 if (!Result.Factory.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000567 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000568 Result.Factory.Method = Method;
569 Prev = &Result.Factory;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000570 continue;
571 }
572
Ted Kremenekda4abf12010-02-11 00:53:01 +0000573 ObjCMethodList *Mem =
574 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
575 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000576 Prev = Prev->Next;
577 }
578
579 return Result;
580 }
581};
Mike Stump11289f42009-09-09 15:08:12 +0000582
583} // end anonymous namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +0000584
585/// \brief The on-disk hash table used for the global method pool.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000586typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
587 ASTSelectorLookupTable;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000588
Sebastian Redl2c373b92010-10-05 15:59:54 +0000589namespace clang {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000590class ASTIdentifierLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000591 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +0000592 ASTReader::PerFileData &F;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000593
594 // If we know the IdentifierInfo in advance, it is here and we will
595 // not build a new one. Used when deserializing information about an
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000596 // identifier that was constructed before the AST file was read.
Douglas Gregora868bbd2009-04-21 22:25:48 +0000597 IdentifierInfo *KnownII;
598
599public:
600 typedef IdentifierInfo * data_type;
601
602 typedef const std::pair<const char*, unsigned> external_key_type;
603
604 typedef external_key_type internal_key_type;
605
Sebastian Redl2c373b92010-10-05 15:59:54 +0000606 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000607 IdentifierInfo *II = 0)
Sebastian Redl2c373b92010-10-05 15:59:54 +0000608 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump11289f42009-09-09 15:08:12 +0000609
Douglas Gregora868bbd2009-04-21 22:25:48 +0000610 static bool EqualKey(const internal_key_type& a,
611 const internal_key_type& b) {
612 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
613 : false;
614 }
Mike Stump11289f42009-09-09 15:08:12 +0000615
Douglas Gregora868bbd2009-04-21 22:25:48 +0000616 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000617 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregora868bbd2009-04-21 22:25:48 +0000618 }
Mike Stump11289f42009-09-09 15:08:12 +0000619
Douglas Gregora868bbd2009-04-21 22:25:48 +0000620 // This hopefully will just get inlined and removed by the optimizer.
621 static const internal_key_type&
622 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000623
Douglas Gregor57756ea2010-10-14 22:11:03 +0000624 // This hopefully will just get inlined and removed by the optimizer.
625 static const external_key_type&
626 GetExternalKey(const internal_key_type& x) { return x; }
627
Douglas Gregora868bbd2009-04-21 22:25:48 +0000628 static std::pair<unsigned, unsigned>
629 ReadKeyDataLength(const unsigned char*& d) {
630 using namespace clang::io;
Douglas Gregor6b7bf5a2009-04-25 20:26:24 +0000631 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor5287b4e2009-04-25 21:04:17 +0000632 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000633 return std::make_pair(KeyLen, DataLen);
634 }
Mike Stump11289f42009-09-09 15:08:12 +0000635
Douglas Gregora868bbd2009-04-21 22:25:48 +0000636 static std::pair<const char*, unsigned>
637 ReadKey(const unsigned char* d, unsigned n) {
638 assert(n >= 2 && d[n-1] == '\0');
639 return std::make_pair((const char*) d, n-1);
640 }
Mike Stump11289f42009-09-09 15:08:12 +0000641
642 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregora868bbd2009-04-21 22:25:48 +0000643 const unsigned char* d,
644 unsigned DataLen) {
645 using namespace clang::io;
Sebastian Redl539c5062010-08-18 23:57:32 +0000646 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000647 bool IsInteresting = ID & 0x01;
648
649 // Wipe out the "is interesting" bit.
650 ID = ID >> 1;
651
652 if (!IsInteresting) {
Sebastian Redl98912122010-07-27 23:01:28 +0000653 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregor1d583f22009-04-28 21:18:29 +0000654 // and associate it with the persistent ID.
655 IdentifierInfo *II = KnownII;
656 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000657 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000658 Reader.SetIdentifierInfo(ID, II);
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000659 II->setIsFromAST();
Douglas Gregor1d583f22009-04-28 21:18:29 +0000660 return II;
661 }
662
Douglas Gregorb9256522009-04-28 21:32:13 +0000663 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000664 bool CPlusPlusOperatorKeyword = Bits & 0x01;
665 Bits >>= 1;
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000666 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
667 Bits >>= 1;
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000668 bool Poisoned = Bits & 0x01;
669 Bits >>= 1;
670 bool ExtensionToken = Bits & 0x01;
671 Bits >>= 1;
672 bool hasMacroDefinition = Bits & 0x01;
673 Bits >>= 1;
674 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
675 Bits >>= 10;
Mike Stump11289f42009-09-09 15:08:12 +0000676
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000677 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorb9256522009-04-28 21:32:13 +0000678 DataLen -= 6;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000679
680 // Build the IdentifierInfo itself and link the identifier ID with
681 // the new IdentifierInfo.
682 IdentifierInfo *II = KnownII;
683 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000684 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000685 Reader.SetIdentifierInfo(ID, II);
686
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000687 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000688 // Token IDs are read-only.
689 if (HasRevertedTokenIDToIdentifier)
690 II->RevertTokenIDToIdentifier();
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000691 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump11289f42009-09-09 15:08:12 +0000692 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000693 "Incorrect extension token flag");
694 (void)ExtensionToken;
695 II->setIsPoisoned(Poisoned);
696 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
697 "Incorrect C++ operator keyword flag");
698 (void)CPlusPlusOperatorKeyword;
699
Douglas Gregorc3366a52009-04-21 23:56:24 +0000700 // If this identifier is a macro, deserialize the macro
701 // definition.
702 if (hasMacroDefinition) {
Douglas Gregorb9256522009-04-28 21:32:13 +0000703 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor5ef9e332010-10-30 00:23:06 +0000704 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregorb9256522009-04-28 21:32:13 +0000705 DataLen -= 4;
Douglas Gregorc3366a52009-04-21 23:56:24 +0000706 }
Douglas Gregora868bbd2009-04-21 22:25:48 +0000707
708 // Read all of the declarations visible at global scope with this
709 // name.
Chris Lattner1d728882009-04-27 22:17:41 +0000710 if (Reader.getContext() == 0) return II;
Douglas Gregor1342e842009-07-06 18:54:52 +0000711 if (DataLen > 0) {
712 llvm::SmallVector<uint32_t, 4> DeclIDs;
713 for (; DataLen > 0; DataLen -= 4)
714 DeclIDs.push_back(ReadUnalignedLE32(d));
715 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000716 }
Mike Stump11289f42009-09-09 15:08:12 +0000717
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000718 II->setIsFromAST();
Douglas Gregora868bbd2009-04-21 22:25:48 +0000719 return II;
720 }
721};
Mike Stump11289f42009-09-09 15:08:12 +0000722
723} // end anonymous namespace
Douglas Gregora868bbd2009-04-21 22:25:48 +0000724
725/// \brief The on-disk hash table used to contain information about
726/// all of the identifiers in the program.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000727typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
728 ASTIdentifierLookupTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000729
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000730namespace {
731class ASTDeclContextNameLookupTrait {
732 ASTReader &Reader;
733
734public:
735 /// \brief Pair of begin/end iterators for DeclIDs.
736 typedef std::pair<DeclID *, DeclID *> data_type;
737
738 /// \brief Special internal key for declaration names.
739 /// The hash table creates keys for comparison; we do not create
740 /// a DeclarationName for the internal key to avoid deserializing types.
741 struct DeclNameKey {
742 DeclarationName::NameKind Kind;
743 uint64_t Data;
744 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
745 };
746
747 typedef DeclarationName external_key_type;
748 typedef DeclNameKey internal_key_type;
749
750 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
751
752 static bool EqualKey(const internal_key_type& a,
753 const internal_key_type& b) {
754 return a.Kind == b.Kind && a.Data == b.Data;
755 }
756
757 unsigned ComputeHash(const DeclNameKey &Key) const {
758 llvm::FoldingSetNodeID ID;
759 ID.AddInteger(Key.Kind);
760
761 switch (Key.Kind) {
762 case DeclarationName::Identifier:
763 case DeclarationName::CXXLiteralOperatorName:
764 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
765 break;
766 case DeclarationName::ObjCZeroArgSelector:
767 case DeclarationName::ObjCOneArgSelector:
768 case DeclarationName::ObjCMultiArgSelector:
769 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
770 break;
771 case DeclarationName::CXXConstructorName:
772 case DeclarationName::CXXDestructorName:
773 case DeclarationName::CXXConversionFunctionName:
774 ID.AddInteger((TypeID)Key.Data);
775 break;
776 case DeclarationName::CXXOperatorName:
777 ID.AddInteger((OverloadedOperatorKind)Key.Data);
778 break;
779 case DeclarationName::CXXUsingDirective:
780 break;
781 }
782
783 return ID.ComputeHash();
784 }
785
786 internal_key_type GetInternalKey(const external_key_type& Name) const {
787 DeclNameKey Key;
788 Key.Kind = Name.getNameKind();
789 switch (Name.getNameKind()) {
790 case DeclarationName::Identifier:
791 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
792 break;
793 case DeclarationName::ObjCZeroArgSelector:
794 case DeclarationName::ObjCOneArgSelector:
795 case DeclarationName::ObjCMultiArgSelector:
796 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
797 break;
798 case DeclarationName::CXXConstructorName:
799 case DeclarationName::CXXDestructorName:
800 case DeclarationName::CXXConversionFunctionName:
801 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
802 break;
803 case DeclarationName::CXXOperatorName:
804 Key.Data = Name.getCXXOverloadedOperator();
805 break;
806 case DeclarationName::CXXLiteralOperatorName:
807 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
808 break;
809 case DeclarationName::CXXUsingDirective:
810 break;
811 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000812
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000813 return Key;
814 }
815
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000816 external_key_type GetExternalKey(const internal_key_type& Key) const {
817 ASTContext *Context = Reader.getContext();
818 switch (Key.Kind) {
819 case DeclarationName::Identifier:
820 return DeclarationName((IdentifierInfo*)Key.Data);
821
822 case DeclarationName::ObjCZeroArgSelector:
823 case DeclarationName::ObjCOneArgSelector:
824 case DeclarationName::ObjCMultiArgSelector:
825 return DeclarationName(Selector(Key.Data));
826
827 case DeclarationName::CXXConstructorName:
828 return Context->DeclarationNames.getCXXConstructorName(
829 Context->getCanonicalType(Reader.GetType(Key.Data)));
830
831 case DeclarationName::CXXDestructorName:
832 return Context->DeclarationNames.getCXXDestructorName(
833 Context->getCanonicalType(Reader.GetType(Key.Data)));
834
835 case DeclarationName::CXXConversionFunctionName:
836 return Context->DeclarationNames.getCXXConversionFunctionName(
837 Context->getCanonicalType(Reader.GetType(Key.Data)));
838
839 case DeclarationName::CXXOperatorName:
840 return Context->DeclarationNames.getCXXOperatorName(
841 (OverloadedOperatorKind)Key.Data);
842
843 case DeclarationName::CXXLiteralOperatorName:
844 return Context->DeclarationNames.getCXXLiteralOperatorName(
845 (IdentifierInfo*)Key.Data);
846
847 case DeclarationName::CXXUsingDirective:
848 return DeclarationName::getUsingDirectiveName();
849 }
850
851 llvm_unreachable("Invalid Name Kind ?");
852 }
853
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000854 static std::pair<unsigned, unsigned>
855 ReadKeyDataLength(const unsigned char*& d) {
856 using namespace clang::io;
857 unsigned KeyLen = ReadUnalignedLE16(d);
858 unsigned DataLen = ReadUnalignedLE16(d);
859 return std::make_pair(KeyLen, DataLen);
860 }
861
862 internal_key_type ReadKey(const unsigned char* d, unsigned) {
863 using namespace clang::io;
864
865 DeclNameKey Key;
866 Key.Kind = (DeclarationName::NameKind)*d++;
867 switch (Key.Kind) {
868 case DeclarationName::Identifier:
869 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
870 break;
871 case DeclarationName::ObjCZeroArgSelector:
872 case DeclarationName::ObjCOneArgSelector:
873 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000874 Key.Data =
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000875 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
876 break;
877 case DeclarationName::CXXConstructorName:
878 case DeclarationName::CXXDestructorName:
879 case DeclarationName::CXXConversionFunctionName:
880 Key.Data = ReadUnalignedLE32(d); // TypeID
881 break;
882 case DeclarationName::CXXOperatorName:
883 Key.Data = *d++; // OverloadedOperatorKind
884 break;
885 case DeclarationName::CXXLiteralOperatorName:
886 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
887 break;
888 case DeclarationName::CXXUsingDirective:
889 break;
890 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000891
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000892 return Key;
893 }
894
895 data_type ReadData(internal_key_type, const unsigned char* d,
896 unsigned DataLen) {
897 using namespace clang::io;
898 unsigned NumDecls = ReadUnalignedLE16(d);
899 DeclID *Start = (DeclID *)d;
900 return std::make_pair(Start, Start + NumDecls);
901 }
902};
903
904} // end anonymous namespace
905
906/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
907typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
908 ASTDeclContextNameLookupTable;
909
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000910bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
911 const std::pair<uint64_t, uint64_t> &Offsets,
912 DeclContextInfo &Info) {
913 SavedStreamPosition SavedPosition(Cursor);
914 // First the lexical decls.
915 if (Offsets.first != 0) {
916 Cursor.JumpToBit(Offsets.first);
917
918 RecordData Record;
919 const char *Blob;
920 unsigned BlobLen;
921 unsigned Code = Cursor.ReadCode();
922 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
923 if (RecCode != DECL_CONTEXT_LEXICAL) {
924 Error("Expected lexical block");
925 return true;
926 }
927
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000928 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
929 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000930 } else {
931 Info.LexicalDecls = 0;
932 Info.NumLexicalDecls = 0;
933 }
934
935 // Now the lookup table.
936 if (Offsets.second != 0) {
937 Cursor.JumpToBit(Offsets.second);
938
939 RecordData Record;
940 const char *Blob;
941 unsigned BlobLen;
942 unsigned Code = Cursor.ReadCode();
943 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
944 if (RecCode != DECL_CONTEXT_VISIBLE) {
945 Error("Expected visible lookup table block");
946 return true;
947 }
948 Info.NameLookupTableData
949 = ASTDeclContextNameLookupTable::Create(
950 (const unsigned char *)Blob + Record[0],
951 (const unsigned char *)Blob,
952 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl9d8f58b2010-08-24 00:50:00 +0000953 } else {
954 Info.NameLookupTableData = 0;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000955 }
956
957 return false;
958}
959
Argyrios Kyrtzidisdaa41f52011-04-25 22:23:56 +0000960void ASTReader::Error(llvm::StringRef Msg) {
961 Error(diag::err_fe_pch_malformed, Msg);
962}
963
964void ASTReader::Error(unsigned DiagID,
965 llvm::StringRef Arg1, llvm::StringRef Arg2) {
966 if (Diags.isDiagnosticInFlight())
967 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
968 else
969 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000970}
971
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000972/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000973bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000974 if (Listener)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000975 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000976 ActualOriginalFileName,
Nick Lewycky36079892011-02-23 21:16:44 +0000977 SuggestedPredefines,
978 FileMgr);
Douglas Gregorc379c072009-04-28 18:58:38 +0000979 return false;
Douglas Gregor92863e42009-04-10 23:10:45 +0000980}
981
Douglas Gregorc5046832009-04-27 18:38:38 +0000982//===----------------------------------------------------------------------===//
983// Source Manager Deserialization
984//===----------------------------------------------------------------------===//
985
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000986/// \brief Read the line table in the source manager block.
Sebastian Redl2c373b92010-10-05 15:59:54 +0000987/// \returns true if there was an error.
988bool ASTReader::ParseLineTable(PerFileData &F,
989 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000990 unsigned Idx = 0;
991 LineTableInfo &LineTable = SourceMgr.getLineTable();
992
993 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +0000994 std::map<int, int> FileIDs;
995 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000996 // Extract the file name
997 unsigned FilenameLen = Record[Idx++];
998 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
999 Idx += FilenameLen;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001000 MaybeAddSystemRootToFilename(Filename);
Mike Stump11289f42009-09-09 15:08:12 +00001001 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregora8854652009-04-13 17:12:42 +00001002 Filename.size());
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001003 }
1004
1005 // Parse the line entries
1006 std::vector<LineEntry> Entries;
1007 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +00001008 int FID = Record[Idx++];
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001009
1010 // Extract the line entries
1011 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +00001012 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001013 Entries.clear();
1014 Entries.reserve(NumEntries);
1015 for (unsigned I = 0; I != NumEntries; ++I) {
1016 unsigned FileOffset = Record[Idx++];
1017 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +00001018 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +00001019 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001020 = (SrcMgr::CharacteristicKind)Record[Idx++];
1021 unsigned IncludeOffset = Record[Idx++];
1022 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1023 FileKind, IncludeOffset));
1024 }
1025 LineTable.AddEntry(FID, Entries);
1026 }
1027
1028 return false;
1029}
1030
Douglas Gregorc5046832009-04-27 18:38:38 +00001031namespace {
1032
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001033class ASTStatData {
Douglas Gregorc5046832009-04-27 18:38:38 +00001034public:
Douglas Gregorc5046832009-04-27 18:38:38 +00001035 const ino_t ino;
1036 const dev_t dev;
1037 const mode_t mode;
1038 const time_t mtime;
1039 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +00001040
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001041 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner2a6fa472010-11-23 19:28:12 +00001042 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregorc5046832009-04-27 18:38:38 +00001043};
1044
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001045class ASTStatLookupTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001046 public:
1047 typedef const char *external_key_type;
1048 typedef const char *internal_key_type;
1049
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001050 typedef ASTStatData data_type;
Douglas Gregorc5046832009-04-27 18:38:38 +00001051
1052 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001053 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001054 }
1055
1056 static internal_key_type GetInternalKey(const char *path) { return path; }
1057
1058 static bool EqualKey(internal_key_type a, internal_key_type b) {
1059 return strcmp(a, b) == 0;
1060 }
1061
1062 static std::pair<unsigned, unsigned>
1063 ReadKeyDataLength(const unsigned char*& d) {
1064 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1065 unsigned DataLen = (unsigned) *d++;
1066 return std::make_pair(KeyLen + 1, DataLen);
1067 }
1068
1069 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1070 return (const char *)d;
1071 }
1072
1073 static data_type ReadData(const internal_key_type, const unsigned char *d,
1074 unsigned /*DataLen*/) {
1075 using namespace clang::io;
1076
Douglas Gregorc5046832009-04-27 18:38:38 +00001077 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1078 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1079 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +00001080 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +00001081 off_t size = (off_t) ReadUnalignedLE64(d);
1082 return data_type(ino, dev, mode, mtime, size);
1083 }
1084};
1085
1086/// \brief stat() cache for precompiled headers.
1087///
1088/// This cache is very similar to the stat cache used by pretokenized
1089/// headers.
Chris Lattner226efd32010-11-23 19:19:34 +00001090class ASTStatCache : public FileSystemStatCache {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001091 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregorc5046832009-04-27 18:38:38 +00001092 CacheTy *Cache;
1093
1094 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +00001095public:
Chris Lattner2a6fa472010-11-23 19:28:12 +00001096 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1097 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +00001098 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1099 Cache = CacheTy::Create(Buckets, Base);
1100 }
1101
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001102 ~ASTStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +00001103
Chris Lattnerdd278432010-11-23 21:17:56 +00001104 LookupResult getStat(const char *Path, struct stat &StatBuf,
1105 int *FileDescriptor) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001106 // Do the lookup for the file's data in the AST file.
Chris Lattner226efd32010-11-23 19:19:34 +00001107 CacheTy::iterator I = Cache->find(Path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001108
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001109 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregorc5046832009-04-27 18:38:38 +00001110 if (I == Cache->end()) {
1111 ++NumStatMisses;
Chris Lattnerdd278432010-11-23 21:17:56 +00001112 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregorc5046832009-04-27 18:38:38 +00001113 }
Mike Stump11289f42009-09-09 15:08:12 +00001114
Douglas Gregorc5046832009-04-27 18:38:38 +00001115 ++NumStatHits;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001116 ASTStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001117
Chris Lattner226efd32010-11-23 19:19:34 +00001118 StatBuf.st_ino = Data.ino;
1119 StatBuf.st_dev = Data.dev;
1120 StatBuf.st_mtime = Data.mtime;
1121 StatBuf.st_mode = Data.mode;
1122 StatBuf.st_size = Data.size;
Chris Lattner8f0583d2010-11-23 20:05:15 +00001123 return CacheExists;
Douglas Gregorc5046832009-04-27 18:38:38 +00001124 }
1125};
1126} // end anonymous namespace
1127
1128
Sebastian Redl393f8b72010-07-19 20:52:06 +00001129/// \brief Read a source manager block
Sebastian Redl2c499f62010-08-18 23:56:43 +00001130ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001131 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +00001132
Sebastian Redl393f8b72010-07-19 20:52:06 +00001133 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001134
Douglas Gregor258ae542009-04-27 06:38:32 +00001135 // Set the source-location entry cursor to the current position in
1136 // the stream. This cursor will be used to read the contents of the
1137 // source manager block initially, and then lazily read
1138 // source-location entries as needed.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001139 SLocEntryCursor = F.Stream;
Douglas Gregor258ae542009-04-27 06:38:32 +00001140
1141 // The stream itself is going to skip over the source manager block.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001142 if (F.Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001143 Error("malformed block record in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001144 return Failure;
1145 }
1146
1147 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001148 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001149 Error("malformed source manager block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001150 return Failure;
1151 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001152
Douglas Gregora7f71a92009-04-10 03:52:48 +00001153 RecordData Record;
1154 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001155 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001156 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001157 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001158 Error("error at end of Source Manager block in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001159 return Failure;
1160 }
Douglas Gregor92863e42009-04-10 23:10:45 +00001161 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001162 }
Mike Stump11289f42009-09-09 15:08:12 +00001163
Douglas Gregora7f71a92009-04-10 03:52:48 +00001164 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1165 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +00001166 SLocEntryCursor.ReadSubBlockID();
1167 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001168 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001169 return Failure;
1170 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001171 continue;
1172 }
Mike Stump11289f42009-09-09 15:08:12 +00001173
Douglas Gregora7f71a92009-04-10 03:52:48 +00001174 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001175 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001176 continue;
1177 }
Mike Stump11289f42009-09-09 15:08:12 +00001178
Douglas Gregora7f71a92009-04-10 03:52:48 +00001179 // Read a record.
1180 const char *BlobStart;
1181 unsigned BlobLen;
1182 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +00001183 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001184 default: // Default behavior: ignore.
1185 break;
1186
Sebastian Redl539c5062010-08-18 23:57:32 +00001187 case SM_LINE_TABLE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00001188 if (ParseLineTable(F, Record))
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001189 return Failure;
Chris Lattner184e65d2009-04-14 23:22:57 +00001190 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00001191
Sebastian Redl539c5062010-08-18 23:57:32 +00001192 case SM_SLOC_FILE_ENTRY:
1193 case SM_SLOC_BUFFER_ENTRY:
1194 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor258ae542009-04-27 06:38:32 +00001195 // Once we hit one of the source location entries, we're done.
1196 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001197 }
1198 }
1199}
1200
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001201/// \brief If a header file is not found at the path that we expect it to be
1202/// and the PCH file was moved from its original location, try to resolve the
1203/// file by assuming that header+PCH were moved together and the header is in
1204/// the same place relative to the PCH.
1205static std::string
1206resolveFileRelativeToOriginalDir(const std::string &Filename,
1207 const std::string &OriginalDir,
1208 const std::string &CurrDir) {
1209 assert(OriginalDir != CurrDir &&
1210 "No point trying to resolve the file if the PCH dir didn't change");
1211 using namespace llvm::sys;
1212 llvm::SmallString<128> filePath(Filename);
1213 fs::make_absolute(filePath);
1214 assert(path::is_absolute(OriginalDir));
1215 llvm::SmallString<128> currPCHPath(CurrDir);
1216
1217 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1218 fileDirE = path::end(path::parent_path(filePath));
1219 path::const_iterator origDirI = path::begin(OriginalDir),
1220 origDirE = path::end(OriginalDir);
1221 // Skip the common path components from filePath and OriginalDir.
1222 while (fileDirI != fileDirE && origDirI != origDirE &&
1223 *fileDirI == *origDirI) {
1224 ++fileDirI;
1225 ++origDirI;
1226 }
1227 for (; origDirI != origDirE; ++origDirI)
1228 path::append(currPCHPath, "..");
1229 path::append(currPCHPath, fileDirI, fileDirE);
1230 path::append(currPCHPath, path::filename(Filename));
1231 return currPCHPath.str();
1232}
1233
Sebastian Redl06750302010-07-20 21:50:20 +00001234/// \brief Get a cursor that's correctly positioned for reading the source
1235/// location entry with the given ID.
Sebastian Redl2c373b92010-10-05 15:59:54 +00001236ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl06750302010-07-20 21:50:20 +00001237 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1238 "SLocCursorForID should only be called for real IDs.");
1239
1240 ID -= 1;
1241 PerFileData *F = 0;
1242 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1243 F = Chain[N - I - 1];
1244 if (ID < F->LocalNumSLocEntries)
1245 break;
1246 ID -= F->LocalNumSLocEntries;
1247 }
1248 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1249
1250 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00001251 return F;
Sebastian Redl06750302010-07-20 21:50:20 +00001252}
1253
Douglas Gregor258ae542009-04-27 06:38:32 +00001254/// \brief Read in the source location entry with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001255ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001256 if (ID == 0)
1257 return Success;
1258
1259 if (ID > TotalNumSLocEntries) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001260 Error("source location entry ID out-of-range for AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001261 return Failure;
1262 }
1263
Sebastian Redl2c373b92010-10-05 15:59:54 +00001264 PerFileData *F = SLocCursorForID(ID);
1265 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001266
Douglas Gregor258ae542009-04-27 06:38:32 +00001267 ++NumSLocEntriesRead;
Douglas Gregor258ae542009-04-27 06:38:32 +00001268 unsigned Code = SLocEntryCursor.ReadCode();
1269 if (Code == llvm::bitc::END_BLOCK ||
1270 Code == llvm::bitc::ENTER_SUBBLOCK ||
1271 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001272 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001273 return Failure;
1274 }
1275
Douglas Gregor258ae542009-04-27 06:38:32 +00001276 RecordData Record;
1277 const char *BlobStart;
1278 unsigned BlobLen;
1279 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1280 default:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001281 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001282 return Failure;
1283
Sebastian Redl539c5062010-08-18 23:57:32 +00001284 case SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001285 std::string Filename(BlobStart, BlobStart + BlobLen);
1286 MaybeAddSystemRootToFilename(Filename);
Chris Lattner5159f612010-11-23 08:35:12 +00001287 const FileEntry *File = FileMgr.getFile(Filename);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001288 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1289 OriginalDir != CurrentDir) {
1290 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1291 OriginalDir,
1292 CurrentDir);
1293 if (!resolved.empty())
1294 File = FileMgr.getFile(resolved);
1295 }
Axel Naumann63fbaed2011-01-27 10:55:51 +00001296 if (File == 0)
1297 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1298 (time_t)Record[5]);
Chris Lattnerd20dc872009-06-15 04:35:16 +00001299 if (File == 0) {
1300 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001301 ErrorStr += Filename;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001302 ErrorStr += "' referenced by AST file";
Chris Lattnerd20dc872009-06-15 04:35:16 +00001303 Error(ErrorStr.c_str());
1304 return Failure;
1305 }
Mike Stump11289f42009-09-09 15:08:12 +00001306
Douglas Gregor09b69892011-02-10 17:09:37 +00001307 if (Record.size() < 6) {
Ted Kremenekabb1ddd2010-03-18 21:23:05 +00001308 Error("source location entry is incorrect");
1309 return Failure;
1310 }
1311
Douglas Gregorce3a8292010-07-27 00:27:13 +00001312 if (!DisableValidation &&
1313 ((off_t)Record[4] != File->getSize()
Douglas Gregor08288f22010-04-09 15:54:22 +00001314#if !defined(LLVM_ON_WIN32)
1315 // In our regression testing, the Windows file system seems to
1316 // have inconsistent modification times that sometimes
1317 // erroneously trigger this error-handling path.
Douglas Gregorce3a8292010-07-27 00:27:13 +00001318 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor08288f22010-04-09 15:54:22 +00001319#endif
Douglas Gregorce3a8292010-07-27 00:27:13 +00001320 )) {
Argyrios Kyrtzidisdaa41f52011-04-25 22:23:56 +00001321 Error(diag::err_fe_pch_file_modified, Filename);
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001322 return Failure;
1323 }
1324
Chris Lattner26b5c192010-11-23 09:19:42 +00001325 FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]),
1326 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor258ae542009-04-27 06:38:32 +00001327 ID, Record[0]);
1328 if (Record[3])
1329 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1330 .setHasLineDirectives();
Douglas Gregor09b69892011-02-10 17:09:37 +00001331
Douglas Gregor258ae542009-04-27 06:38:32 +00001332 break;
1333 }
1334
Sebastian Redl539c5062010-08-18 23:57:32 +00001335 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor258ae542009-04-27 06:38:32 +00001336 const char *Name = BlobStart;
1337 unsigned Offset = Record[0];
1338 unsigned Code = SLocEntryCursor.ReadCode();
1339 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001340 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +00001341 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001342
Sebastian Redl539c5062010-08-18 23:57:32 +00001343 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001344 Error("AST record has invalid code");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001345 return Failure;
1346 }
1347
Douglas Gregor258ae542009-04-27 06:38:32 +00001348 llvm::MemoryBuffer *Buffer
Chris Lattner58c79342010-04-05 22:42:27 +00001349 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1350 Name);
Douglas Gregor258ae542009-04-27 06:38:32 +00001351 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump11289f42009-09-09 15:08:12 +00001352
Douglas Gregore6648fb2009-04-28 20:33:11 +00001353 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001354 PCHPredefinesBlock Block = {
1355 BufferID,
1356 llvm::StringRef(BlobStart, BlobLen - 1)
1357 };
1358 PCHPredefinesBuffers.push_back(Block);
Douglas Gregore6648fb2009-04-28 20:33:11 +00001359 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001360
1361 break;
1362 }
1363
Sebastian Redl539c5062010-08-18 23:57:32 +00001364 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001365 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001366 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001367 ReadSourceLocation(*F, Record[2]),
1368 ReadSourceLocation(*F, Record[3]),
Douglas Gregor258ae542009-04-27 06:38:32 +00001369 Record[4],
1370 ID,
1371 Record[0]);
1372 break;
Mike Stump11289f42009-09-09 15:08:12 +00001373 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001374 }
1375
1376 return Success;
1377}
1378
Chris Lattnere78a6be2009-04-27 01:05:14 +00001379/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1380/// specified cursor. Read the abbreviations that are at the top of the block
1381/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001382bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001383 unsigned BlockID) {
1384 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001385 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001386 return Failure;
1387 }
Mike Stump11289f42009-09-09 15:08:12 +00001388
Chris Lattnere78a6be2009-04-27 01:05:14 +00001389 while (true) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001390 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattnere78a6be2009-04-27 01:05:14 +00001391 unsigned Code = Cursor.ReadCode();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001392
Chris Lattnere78a6be2009-04-27 01:05:14 +00001393 // We expect all abbrevs to be at the start of the block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001394 if (Code != llvm::bitc::DEFINE_ABBREV) {
1395 Cursor.JumpToBit(Offset);
Chris Lattnere78a6be2009-04-27 01:05:14 +00001396 return false;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001397 }
Chris Lattnere78a6be2009-04-27 01:05:14 +00001398 Cursor.ReadAbbrevRecord();
1399 }
1400}
1401
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001402PreprocessedEntity *ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001403 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregor796d76a2010-10-20 22:00:55 +00001404 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump11289f42009-09-09 15:08:12 +00001405
Douglas Gregorc3366a52009-04-21 23:56:24 +00001406 // Keep track of where we are in the stream, then jump back there
1407 // after reading this macro.
1408 SavedStreamPosition SavedPosition(Stream);
1409
1410 Stream.JumpToBit(Offset);
1411 RecordData Record;
1412 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1413 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001414
Douglas Gregorc3366a52009-04-21 23:56:24 +00001415 while (true) {
1416 unsigned Code = Stream.ReadCode();
1417 switch (Code) {
1418 case llvm::bitc::END_BLOCK:
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001419 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001420
1421 case llvm::bitc::ENTER_SUBBLOCK:
1422 // No known subblocks, always skip them.
1423 Stream.ReadSubBlockID();
1424 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001425 Error("malformed block record in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001426 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001427 }
1428 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001429
Douglas Gregorc3366a52009-04-21 23:56:24 +00001430 case llvm::bitc::DEFINE_ABBREV:
1431 Stream.ReadAbbrevRecord();
1432 continue;
1433 default: break;
1434 }
1435
1436 // Read a record.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001437 const char *BlobStart = 0;
1438 unsigned BlobLen = 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001439 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001440 PreprocessorRecordTypes RecType =
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001441 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001442 BlobLen);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001443 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001444 case PP_MACRO_OBJECT_LIKE:
1445 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001446 // If we already have a macro, that means that we've hit the end
1447 // of the definition of the macro we were looking for. We're
1448 // done.
1449 if (Macro)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001450 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001451
1452 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1453 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001454 Error("macro must have a name in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001455 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001456 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00001457 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001458 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001459
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001460 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001461 MI->setIsUsed(isUsed);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001462 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001463
Douglas Gregoraae92242010-03-19 21:51:54 +00001464 unsigned NextIndex = 3;
Sebastian Redl539c5062010-08-18 23:57:32 +00001465 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001466 // Decode function-like macro info.
1467 bool isC99VarArgs = Record[3];
1468 bool isGNUVarArgs = Record[4];
1469 MacroArgs.clear();
1470 unsigned NumArgs = Record[5];
Douglas Gregoraae92242010-03-19 21:51:54 +00001471 NextIndex = 6 + NumArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001472 for (unsigned i = 0; i != NumArgs; ++i)
1473 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1474
1475 // Install function-like macro info.
1476 MI->setIsFunctionLike();
1477 if (isC99VarArgs) MI->setIsC99Varargs();
1478 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001479 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001480 PP->getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001481 }
1482
1483 // Finally, install the macro.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001484 PP->setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001485
1486 // Remember that we saw this macro last so that we add the tokens that
1487 // form its body to it.
1488 Macro = MI;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001489
Douglas Gregoraae92242010-03-19 21:51:54 +00001490 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1491 // We have a macro definition. Load it now.
1492 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1493 getMacroDefinition(Record[NextIndex]));
1494 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001495
Douglas Gregorc3366a52009-04-21 23:56:24 +00001496 ++NumMacrosRead;
1497 break;
1498 }
Mike Stump11289f42009-09-09 15:08:12 +00001499
Sebastian Redl539c5062010-08-18 23:57:32 +00001500 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001501 // If we see a TOKEN before a PP_MACRO_*, then the file is
1502 // erroneous, just pretend we didn't see this.
1503 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001504
Douglas Gregorc3366a52009-04-21 23:56:24 +00001505 Token Tok;
1506 Tok.startToken();
Sebastian Redl2c373b92010-10-05 15:59:54 +00001507 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001508 Tok.setLength(Record[1]);
1509 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1510 Tok.setIdentifierInfo(II);
1511 Tok.setKind((tok::TokenKind)Record[3]);
1512 Tok.setFlag((Token::TokenFlags)Record[4]);
1513 Macro->AddTokenToBody(Tok);
1514 break;
1515 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001516 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001517 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001518
1519 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001520}
1521
Douglas Gregor92a96f52011-02-08 21:58:10 +00001522PreprocessedEntity *ASTReader::LoadPreprocessedEntity(PerFileData &F) {
1523 assert(PP && "Forgot to set Preprocessor ?");
1524 unsigned Code = F.PreprocessorDetailCursor.ReadCode();
1525 switch (Code) {
1526 case llvm::bitc::END_BLOCK:
1527 return 0;
1528
1529 case llvm::bitc::ENTER_SUBBLOCK:
1530 Error("unexpected subblock record in preprocessor detail block");
1531 return 0;
1532
1533 case llvm::bitc::DEFINE_ABBREV:
1534 Error("unexpected abbrevation record in preprocessor detail block");
1535 return 0;
1536
1537 default:
1538 break;
1539 }
1540
1541 if (!PP->getPreprocessingRecord()) {
1542 Error("no preprocessing record");
1543 return 0;
1544 }
1545
1546 // Read the record.
1547 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1548 const char *BlobStart = 0;
1549 unsigned BlobLen = 0;
1550 RecordData Record;
1551 PreprocessorDetailRecordTypes RecType =
1552 (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord(
1553 Code, Record, BlobStart, BlobLen);
1554 switch (RecType) {
1555 case PPD_MACRO_INSTANTIATION: {
1556 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1557 return PE;
1558
1559 MacroInstantiation *MI
1560 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1561 SourceRange(ReadSourceLocation(F, Record[1]),
1562 ReadSourceLocation(F, Record[2])),
1563 getMacroDefinition(Record[4]));
1564 PPRec.SetPreallocatedEntity(Record[0], MI);
1565 return MI;
1566 }
1567
1568 case PPD_MACRO_DEFINITION: {
1569 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1570 return PE;
1571
1572 if (Record[1] > MacroDefinitionsLoaded.size()) {
1573 Error("out-of-bounds macro definition record");
1574 return 0;
1575 }
1576
1577 // Decode the identifier info and then check again; if the macro is
1578 // still defined and associated with the identifier,
1579 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1580 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1581 MacroDefinition *MD
1582 = new (PPRec) MacroDefinition(II,
1583 ReadSourceLocation(F, Record[5]),
1584 SourceRange(
1585 ReadSourceLocation(F, Record[2]),
1586 ReadSourceLocation(F, Record[3])));
1587
1588 PPRec.SetPreallocatedEntity(Record[0], MD);
1589 MacroDefinitionsLoaded[Record[1] - 1] = MD;
1590
1591 if (DeserializationListener)
1592 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1593 }
1594
1595 return MacroDefinitionsLoaded[Record[1] - 1];
1596 }
1597
1598 case PPD_INCLUSION_DIRECTIVE: {
1599 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1600 return PE;
1601
1602 const char *FullFileNameStart = BlobStart + Record[3];
1603 const FileEntry *File
1604 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
1605 BlobLen - Record[3]));
1606
1607 // FIXME: Stable encoding
1608 InclusionDirective::InclusionKind Kind
1609 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1610 InclusionDirective *ID
1611 = new (PPRec) InclusionDirective(PPRec, Kind,
1612 llvm::StringRef(BlobStart, Record[3]),
1613 Record[4],
1614 File,
1615 SourceRange(ReadSourceLocation(F, Record[1]),
1616 ReadSourceLocation(F, Record[2])));
1617 PPRec.SetPreallocatedEntity(Record[0], ID);
1618 return ID;
1619 }
1620 }
1621
1622 Error("invalid offset in preprocessor detail block");
1623 return 0;
1624}
1625
Douglas Gregor09b69892011-02-10 17:09:37 +00001626namespace {
1627 /// \brief Trait class used to search the on-disk hash table containing all of
1628 /// the header search information.
1629 ///
1630 /// The on-disk hash table contains a mapping from each header path to
1631 /// information about that header (how many times it has been included, its
1632 /// controlling macro, etc.). Note that we actually hash based on the
1633 /// filename, and support "deep" comparisons of file names based on current
1634 /// inode numbers, so that the search can cope with non-normalized path names
1635 /// and symlinks.
1636 class HeaderFileInfoTrait {
1637 const char *SearchPath;
1638 struct stat SearchPathStatBuf;
1639 llvm::Optional<int> SearchPathStatResult;
1640
1641 int StatSimpleCache(const char *Path, struct stat *StatBuf) {
1642 if (Path == SearchPath) {
1643 if (!SearchPathStatResult)
1644 SearchPathStatResult = stat(Path, &SearchPathStatBuf);
1645
1646 *StatBuf = SearchPathStatBuf;
1647 return *SearchPathStatResult;
1648 }
1649
1650 return stat(Path, StatBuf);
1651 }
1652
1653 public:
1654 typedef const char *external_key_type;
1655 typedef const char *internal_key_type;
1656
1657 typedef HeaderFileInfo data_type;
1658
1659 HeaderFileInfoTrait(const char *SearchPath = 0) : SearchPath(SearchPath) { }
1660
1661 static unsigned ComputeHash(const char *path) {
1662 return llvm::HashString(llvm::sys::path::filename(path));
1663 }
1664
1665 static internal_key_type GetInternalKey(const char *path) { return path; }
1666
1667 bool EqualKey(internal_key_type a, internal_key_type b) {
1668 if (strcmp(a, b) == 0)
1669 return true;
1670
1671 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1672 return false;
1673
1674 // The file names match, but the path names don't. stat() the files to
1675 // see if they are the same.
1676 struct stat StatBufA, StatBufB;
1677 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1678 return false;
1679
1680 return StatBufA.st_ino == StatBufB.st_ino;
1681 }
1682
1683 static std::pair<unsigned, unsigned>
1684 ReadKeyDataLength(const unsigned char*& d) {
1685 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1686 unsigned DataLen = (unsigned) *d++;
1687 return std::make_pair(KeyLen + 1, DataLen);
1688 }
1689
1690 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1691 return (const char *)d;
1692 }
1693
1694 static data_type ReadData(const internal_key_type, const unsigned char *d,
1695 unsigned DataLen) {
1696 const unsigned char *End = d + DataLen;
1697 using namespace clang::io;
1698 HeaderFileInfo HFI;
1699 unsigned Flags = *d++;
Douglas Gregor37aa4932011-05-04 00:14:37 +00001700 HFI.isImport = (Flags >> 4) & 0x01;
1701 HFI.isPragmaOnce = (Flags >> 3) & 0x01;
Douglas Gregor09b69892011-02-10 17:09:37 +00001702 HFI.DirInfo = (Flags >> 1) & 0x03;
1703 HFI.Resolved = Flags & 0x01;
1704 HFI.NumIncludes = ReadUnalignedLE16(d);
1705 HFI.ControllingMacroID = ReadUnalignedLE32(d);
1706 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1707 (void)End;
1708
1709 // This HeaderFileInfo was externally loaded.
1710 HFI.External = true;
1711 return HFI;
1712 }
1713 };
1714}
1715
1716/// \brief The on-disk hash table used for the global method pool.
1717typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
1718 HeaderFileInfoLookupTable;
1719
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001720void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1721 uint64_t Offset) {
1722 // Note that this identifier has a macro definition.
1723 II->setHasMacroDefinition(true);
1724
1725 // Adjust the offset based on our position in the chain.
1726 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1727 if (Chain[I] == &F)
1728 break;
1729
1730 Offset += Chain[I]->SizeInBits;
1731 }
1732
1733 UnreadMacroRecordOffsets[II] = Offset;
1734}
1735
Sebastian Redl2c499f62010-08-18 23:56:43 +00001736void ASTReader::ReadDefinedMacros() {
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001737 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001738 PerFileData &F = *Chain[N - I - 1];
1739 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001740
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001741 // If there was no preprocessor block, skip this file.
1742 if (!MacroCursor.getBitStreamReader())
1743 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001744
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001745 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001746 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001747
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001748 RecordData Record;
1749 while (true) {
1750 unsigned Code = Cursor.ReadCode();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001751 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001752 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001753
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001754 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1755 // No known subblocks, always skip them.
1756 Cursor.ReadSubBlockID();
1757 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001758 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001759 return;
1760 }
1761 continue;
1762 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001763
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001764 if (Code == llvm::bitc::DEFINE_ABBREV) {
1765 Cursor.ReadAbbrevRecord();
1766 continue;
1767 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001768
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001769 // Read a record.
1770 const char *BlobStart;
1771 unsigned BlobLen;
1772 Record.clear();
1773 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1774 default: // Default behavior: ignore.
1775 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001776
Sebastian Redl539c5062010-08-18 23:57:32 +00001777 case PP_MACRO_OBJECT_LIKE:
1778 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001779 DecodeIdentifierInfo(Record[0]);
1780 break;
1781
Sebastian Redl539c5062010-08-18 23:57:32 +00001782 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001783 // Ignore tokens.
1784 break;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001785 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001786 }
1787 }
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001788
1789 // Drain the unread macro-record offsets map.
1790 while (!UnreadMacroRecordOffsets.empty())
1791 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1792}
1793
1794void ASTReader::LoadMacroDefinition(
1795 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1796 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1797 PerFileData *F = 0;
1798 uint64_t Offset = Pos->second;
1799 UnreadMacroRecordOffsets.erase(Pos);
1800
1801 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1802 if (Offset < Chain[I]->SizeInBits) {
1803 F = Chain[I];
1804 break;
1805 }
1806
1807 Offset -= Chain[I]->SizeInBits;
1808 }
1809 if (!F) {
1810 Error("Malformed macro record offset");
1811 return;
1812 }
1813
1814 ReadMacroRecord(*F, Offset);
1815}
1816
1817void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1818 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1819 = UnreadMacroRecordOffsets.find(II);
1820 LoadMacroDefinition(Pos);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001821}
1822
Sebastian Redl50e26582010-09-15 19:54:06 +00001823MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor91096292010-10-02 19:29:26 +00001824 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregoraae92242010-03-19 21:51:54 +00001825 return 0;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001826
Douglas Gregor91096292010-10-02 19:29:26 +00001827 if (!MacroDefinitionsLoaded[ID - 1]) {
1828 unsigned Index = ID - 1;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001829 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1830 PerFileData &F = *Chain[N - I - 1];
1831 if (Index < F.LocalNumMacroDefinitions) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001832 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
1833 F.PreprocessorDetailCursor.JumpToBit(F.MacroDefinitionOffsets[Index]);
1834 LoadPreprocessedEntity(F);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001835 break;
1836 }
1837 Index -= F.LocalNumMacroDefinitions;
1838 }
Douglas Gregor91096292010-10-02 19:29:26 +00001839 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001840 }
1841
Douglas Gregor91096292010-10-02 19:29:26 +00001842 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001843}
1844
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00001845const FileEntry *ASTReader::getFileEntry(llvm::StringRef filenameStrRef) {
1846 std::string Filename = filenameStrRef;
1847 MaybeAddSystemRootToFilename(Filename);
1848 const FileEntry *File = FileMgr.getFile(Filename);
1849 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1850 OriginalDir != CurrentDir) {
1851 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1852 OriginalDir,
1853 CurrentDir);
1854 if (!resolved.empty())
1855 File = FileMgr.getFile(resolved);
1856 }
1857
1858 return File;
1859}
1860
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001861/// \brief If we are loading a relocatable PCH file, and the filename is
1862/// not an absolute path, add the system root to the beginning of the file
1863/// name.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001864void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001865 // If this is not a relocatable PCH file, there's nothing to do.
1866 if (!RelocatablePCH)
1867 return;
Mike Stump11289f42009-09-09 15:08:12 +00001868
Michael J. Spencerf28df4c2010-12-17 21:22:22 +00001869 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001870 return;
1871
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001872 if (isysroot == 0) {
1873 // If no system root was given, default to '/'
1874 Filename.insert(Filename.begin(), '/');
1875 return;
1876 }
Mike Stump11289f42009-09-09 15:08:12 +00001877
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001878 unsigned Length = strlen(isysroot);
1879 if (isysroot[Length - 1] != '/')
1880 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001881
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001882 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1883}
1884
Sebastian Redl2c499f62010-08-18 23:56:43 +00001885ASTReader::ASTReadResult
Sebastian Redl3e31c722010-08-18 23:56:56 +00001886ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001887 llvm::BitstreamCursor &Stream = F.Stream;
1888
Sebastian Redl539c5062010-08-18 23:57:32 +00001889 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001890 Error("malformed block record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001891 return Failure;
1892 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001893
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001894 // Read all of the records and blocks for the ASt file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001895 RecordData Record;
Sebastian Redl393f8b72010-07-19 20:52:06 +00001896 bool First = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001897 while (!Stream.AtEndOfStream()) {
1898 unsigned Code = Stream.ReadCode();
1899 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001900 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001901 Error("error at end of module block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001902 return Failure;
1903 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001904
Douglas Gregor55abb232009-04-10 20:39:37 +00001905 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001906 }
1907
1908 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1909 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001910 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001911 // We lazily load the decls block, but we want to set up the
1912 // DeclsCursor cursor to point into it. Clone our current bitcode
1913 // cursor to it, enter the block and read the abbrevs in that block.
1914 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001915 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001916 if (Stream.SkipBlock() || // Skip with the main cursor.
1917 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001918 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001919 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001920 return Failure;
1921 }
1922 break;
Mike Stump11289f42009-09-09 15:08:12 +00001923
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00001924 case DECL_UPDATES_BLOCK_ID:
1925 if (Stream.SkipBlock()) {
1926 Error("malformed block record in AST file");
1927 return Failure;
1928 }
1929 break;
1930
Sebastian Redl539c5062010-08-18 23:57:32 +00001931 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001932 F.MacroCursor = Stream;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001933 if (PP)
1934 PP->setExternalSource(this);
1935
Douglas Gregor796d76a2010-10-20 22:00:55 +00001936 if (Stream.SkipBlock() ||
1937 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001938 Error("malformed block record in AST file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001939 return Failure;
1940 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001941 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001942 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001943
Douglas Gregor92a96f52011-02-08 21:58:10 +00001944 case PREPROCESSOR_DETAIL_BLOCK_ID:
1945 F.PreprocessorDetailCursor = Stream;
1946 if (Stream.SkipBlock() ||
1947 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1948 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1949 Error("malformed preprocessor detail record in AST file");
1950 return Failure;
1951 }
1952 F.PreprocessorDetailStartOffset
1953 = F.PreprocessorDetailCursor.GetCurrentBitNo();
1954 break;
1955
Sebastian Redl539c5062010-08-18 23:57:32 +00001956 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001957 switch (ReadSourceManagerBlock(F)) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001958 case Success:
1959 break;
1960
1961 case Failure:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001962 Error("malformed source manager block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001963 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001964
1965 case IgnorePCH:
1966 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001967 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001968 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001969 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00001970 First = false;
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001971 continue;
1972 }
1973
1974 if (Code == llvm::bitc::DEFINE_ABBREV) {
1975 Stream.ReadAbbrevRecord();
1976 continue;
1977 }
1978
1979 // Read and process a record.
1980 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001981 const char *BlobStart = 0;
1982 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001983 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001984 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001985 default: // Default behavior: ignore.
1986 break;
1987
Sebastian Redl539c5062010-08-18 23:57:32 +00001988 case METADATA: {
1989 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1990 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001991 : diag::warn_pch_version_too_new);
1992 return IgnorePCH;
1993 }
1994
1995 RelocatablePCH = Record[4];
1996 if (Listener) {
1997 std::string TargetTriple(BlobStart, BlobLen);
1998 if (Listener->ReadTargetTriple(TargetTriple))
1999 return IgnorePCH;
2000 }
2001 break;
2002 }
2003
Sebastian Redl539c5062010-08-18 23:57:32 +00002004 case CHAINED_METADATA: {
Sebastian Redl393f8b72010-07-19 20:52:06 +00002005 if (!First) {
2006 Error("CHAINED_METADATA is not first record in block");
2007 return Failure;
2008 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002009 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2010 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002011 : diag::warn_pch_version_too_new);
2012 return IgnorePCH;
2013 }
2014
Sebastian Redl009e7f22010-10-05 16:15:19 +00002015 // Load the chained file, which is always a PCH file.
2016 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002017 case Failure: return Failure;
2018 // If we have to ignore the dependency, we'll have to ignore this too.
2019 case IgnorePCH: return IgnorePCH;
2020 case Success: break;
2021 }
2022 break;
2023 }
2024
Sebastian Redl539c5062010-08-18 23:57:32 +00002025 case TYPE_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00002026 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002027 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002028 return Failure;
2029 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002030 F.TypeOffsets = (const uint32_t *)BlobStart;
2031 F.LocalNumTypes = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002032 break;
2033
Sebastian Redl539c5062010-08-18 23:57:32 +00002034 case DECL_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00002035 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002036 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002037 return Failure;
2038 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002039 F.DeclOffsets = (const uint32_t *)BlobStart;
2040 F.LocalNumDecls = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002041 break;
Douglas Gregor55abb232009-04-10 20:39:37 +00002042
Sebastian Redl539c5062010-08-18 23:57:32 +00002043 case TU_UPDATE_LEXICAL: {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002044 DeclContextInfo Info = {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002045 /* No visible information */ 0,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002046 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
2047 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002048 };
Douglas Gregoraa433012010-10-01 01:18:02 +00002049 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
2050 .push_back(Info);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002051 break;
2052 }
2053
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002054 case UPDATE_VISIBLE: {
2055 serialization::DeclID ID = Record[0];
2056 void *Table = ASTDeclContextNameLookupTable::Create(
2057 (const unsigned char *)BlobStart + Record[1],
2058 (const unsigned char *)BlobStart,
2059 ASTDeclContextNameLookupTrait(*this));
Douglas Gregoraa433012010-10-01 01:18:02 +00002060 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002061 DeclContextInfo Info = {
2062 Table, /* No lexical inforamtion */ 0, 0
2063 };
2064 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
2065 } else
2066 PendingVisibleUpdates[ID].push_back(Table);
2067 break;
2068 }
2069
Sebastian Redl539c5062010-08-18 23:57:32 +00002070 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002071 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
2072 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002073 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002074 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
2075 Latest > FirstLatestDeclIDs[First]) &&
2076 "The new latest is supposed to come after the previous latest");
2077 FirstLatestDeclIDs[First] = Latest;
2078 }
2079 break;
2080 }
2081
Sebastian Redl539c5062010-08-18 23:57:32 +00002082 case LANGUAGE_OPTIONS:
Douglas Gregorce3a8292010-07-27 00:27:13 +00002083 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor55abb232009-04-10 20:39:37 +00002084 return IgnorePCH;
2085 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00002086
Sebastian Redl539c5062010-08-18 23:57:32 +00002087 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00002088 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00002089 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00002090 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002091 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00002092 (const unsigned char *)F.IdentifierTableData + Record[0],
2093 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl2c373b92010-10-05 15:59:54 +00002094 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002095 if (PP)
2096 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00002097 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002098 break;
2099
Sebastian Redl539c5062010-08-18 23:57:32 +00002100 case IDENTIFIER_OFFSET:
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002101 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002102 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002103 return Failure;
2104 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002105 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2106 F.LocalNumIdentifiers = Record[0];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002107 break;
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002108
Sebastian Redl539c5062010-08-18 23:57:32 +00002109 case EXTERNAL_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002110 // Optimization for the first block.
2111 if (ExternalDefinitions.empty())
2112 ExternalDefinitions.swap(Record);
2113 else
2114 ExternalDefinitions.insert(ExternalDefinitions.end(),
2115 Record.begin(), Record.end());
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002116 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00002117
Sebastian Redl539c5062010-08-18 23:57:32 +00002118 case SPECIAL_TYPES:
Sebastian Redlb293a452010-07-20 21:20:32 +00002119 // Optimization for the first block
2120 if (SpecialTypes.empty())
2121 SpecialTypes.swap(Record);
2122 else
2123 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002124 break;
2125
Sebastian Redl539c5062010-08-18 23:57:32 +00002126 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002127 TotalNumStatements += Record[0];
2128 TotalNumMacros += Record[1];
2129 TotalLexicalDeclContexts += Record[2];
2130 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00002131 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002132
Sebastian Redl539c5062010-08-18 23:57:32 +00002133 case UNUSED_FILESCOPED_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002134 // Optimization for the first block.
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002135 if (UnusedFileScopedDecls.empty())
2136 UnusedFileScopedDecls.swap(Record);
Sebastian Redlb293a452010-07-20 21:20:32 +00002137 else
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002138 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
2139 Record.begin(), Record.end());
Tanya Lattner90073802010-02-12 00:07:30 +00002140 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002141
Alexis Hunt27a761d2011-05-04 23:29:54 +00002142 case DELEGATING_CTORS:
2143 // Optimization for the first block.
2144 if (DelegatingCtorDecls.empty())
2145 DelegatingCtorDecls.swap(Record);
2146 else
2147 DelegatingCtorDecls.insert(DelegatingCtorDecls.end(),
2148 Record.begin(), Record.end());
2149 break;
2150
Sebastian Redl539c5062010-08-18 23:57:32 +00002151 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002152 // Later blocks overwrite earlier ones.
2153 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002154 break;
2155
Sebastian Redl539c5062010-08-18 23:57:32 +00002156 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002157 // Optimization for the first block.
2158 if (LocallyScopedExternalDecls.empty())
2159 LocallyScopedExternalDecls.swap(Record);
2160 else
2161 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
2162 Record.begin(), Record.end());
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002163 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002164
Sebastian Redl539c5062010-08-18 23:57:32 +00002165 case SELECTOR_OFFSETS:
Sebastian Redla19a67f2010-08-03 21:58:15 +00002166 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00002167 F.LocalNumSelectors = Record[0];
Douglas Gregor95c13f52009-04-25 17:48:32 +00002168 break;
2169
Sebastian Redl539c5062010-08-18 23:57:32 +00002170 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00002171 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002172 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00002173 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002174 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00002175 F.SelectorLookupTableData + Record[0],
2176 F.SelectorLookupTableData,
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002177 ASTSelectorLookupTrait(*this));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002178 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002179 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00002180
Sebastian Redl96371b42010-09-22 00:42:30 +00002181 case REFERENCED_SELECTOR_POOL:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002182 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002183 break;
2184
Sebastian Redl539c5062010-08-18 23:57:32 +00002185 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002186 if (!Record.empty() && Listener)
2187 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002188 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002189
Sebastian Redl539c5062010-08-18 23:57:32 +00002190 case SOURCE_LOCATION_OFFSETS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002191 F.SLocOffsets = (const uint32_t *)BlobStart;
2192 F.LocalNumSLocEntries = Record[0];
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002193 F.LocalSLocSize = Record[1];
Douglas Gregor258ae542009-04-27 06:38:32 +00002194 break;
2195
Sebastian Redl539c5062010-08-18 23:57:32 +00002196 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl96371b42010-09-22 00:42:30 +00002197 if (PreloadSLocEntries.empty())
2198 PreloadSLocEntries.swap(Record);
2199 else
2200 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2201 Record.begin(), Record.end());
Douglas Gregor258ae542009-04-27 06:38:32 +00002202 break;
Douglas Gregorc5046832009-04-27 18:38:38 +00002203
Sebastian Redl539c5062010-08-18 23:57:32 +00002204 case STAT_CACHE: {
Douglas Gregor606c4ac2011-02-05 19:42:43 +00002205 if (!DisableStatCache) {
2206 ASTStatCache *MyStatCache =
2207 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2208 (const unsigned char *)BlobStart,
2209 NumStatHits, NumStatMisses);
2210 FileMgr.addStatCache(MyStatCache);
2211 F.StatCache = MyStatCache;
2212 }
Douglas Gregorc5046832009-04-27 18:38:38 +00002213 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00002214 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002215
Sebastian Redl539c5062010-08-18 23:57:32 +00002216 case EXT_VECTOR_DECLS:
Sebastian Redl04f5c312010-07-28 21:38:49 +00002217 // Optimization for the first block.
2218 if (ExtVectorDecls.empty())
2219 ExtVectorDecls.swap(Record);
2220 else
2221 ExtVectorDecls.insert(ExtVectorDecls.end(),
2222 Record.begin(), Record.end());
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002223 break;
2224
Sebastian Redl539c5062010-08-18 23:57:32 +00002225 case VTABLE_USES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002226 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002227 VTableUses.swap(Record);
2228 break;
2229
Sebastian Redl539c5062010-08-18 23:57:32 +00002230 case DYNAMIC_CLASSES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002231 // Optimization for the first block.
2232 if (DynamicClasses.empty())
2233 DynamicClasses.swap(Record);
2234 else
2235 DynamicClasses.insert(DynamicClasses.end(),
2236 Record.begin(), Record.end());
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002237 break;
2238
Sebastian Redl539c5062010-08-18 23:57:32 +00002239 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002240 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002241 break;
2242
Sebastian Redl539c5062010-08-18 23:57:32 +00002243 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002244 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002245 SemaDeclRefs.swap(Record);
2246 break;
2247
Sebastian Redl539c5062010-08-18 23:57:32 +00002248 case ORIGINAL_FILE_NAME:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002249 // The primary AST will be the last to get here, so it will be the one
Sebastian Redlb293a452010-07-20 21:20:32 +00002250 // that's used.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00002251 ActualOriginalFileName.assign(BlobStart, BlobLen);
2252 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002253 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00002254 break;
Mike Stump11289f42009-09-09 15:08:12 +00002255
Douglas Gregora3b20262011-05-06 21:43:30 +00002256 case ORIGINAL_FILE_ID:
2257 OriginalFileID = FileID::get(Record[0]);
2258 break;
2259
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002260 case ORIGINAL_PCH_DIR:
2261 // The primary AST will be the last to get here, so it will be the one
2262 // that's used.
2263 OriginalDir.assign(BlobStart, BlobLen);
2264 break;
2265
Sebastian Redl539c5062010-08-18 23:57:32 +00002266 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00002267 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002268 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2269 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2270 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregord54f3a12009-10-05 21:07:28 +00002271 return IgnorePCH;
2272 }
2273 break;
2274 }
Sebastian Redlfa061442010-07-21 20:07:32 +00002275
Sebastian Redl539c5062010-08-18 23:57:32 +00002276 case MACRO_DEFINITION_OFFSETS:
Sebastian Redlfa061442010-07-21 20:07:32 +00002277 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2278 F.NumPreallocatedPreprocessingEntities = Record[0];
2279 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregoraae92242010-03-19 21:51:54 +00002280 break;
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002281
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002282 case DECL_UPDATE_OFFSETS: {
2283 if (Record.size() % 2 != 0) {
2284 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2285 return Failure;
2286 }
2287 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2288 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2289 .push_back(std::make_pair(&F, Record[I+1]));
2290 break;
2291 }
2292
Sebastian Redl539c5062010-08-18 23:57:32 +00002293 case DECL_REPLACEMENTS: {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002294 if (Record.size() % 2 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002295 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002296 return Failure;
2297 }
2298 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl539c5062010-08-18 23:57:32 +00002299 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002300 std::make_pair(&F, Record[I+1]);
2301 break;
2302 }
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002303
2304 case CXX_BASE_SPECIFIER_OFFSETS: {
2305 if (F.LocalNumCXXBaseSpecifiers != 0) {
2306 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2307 return Failure;
2308 }
2309
2310 F.LocalNumCXXBaseSpecifiers = Record[0];
2311 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2312 break;
2313 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002314
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002315 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002316 if (Record.size() % 2 != 0) {
2317 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2318 return Failure;
2319 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002320 if (PragmaDiagMappings.empty())
2321 PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002322 else
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002323 PragmaDiagMappings.insert(PragmaDiagMappings.end(),
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002324 Record.begin(), Record.end());
2325 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002326
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002327 case CUDA_SPECIAL_DECL_REFS:
2328 // Later tables overwrite earlier ones.
2329 CUDASpecialDeclRefs.swap(Record);
2330 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002331
2332 case HEADER_SEARCH_TABLE:
2333 F.HeaderFileInfoTableData = BlobStart;
2334 F.LocalNumHeaderFileInfos = Record[1];
2335 if (Record[0]) {
2336 F.HeaderFileInfoTable
2337 = HeaderFileInfoLookupTable::Create(
2338 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2339 (const unsigned char *)F.HeaderFileInfoTableData);
2340 if (PP)
2341 PP->getHeaderSearchInfo().SetExternalSource(this);
2342 }
2343 break;
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002344
2345 case FP_PRAGMA_OPTIONS:
2346 // Later tables overwrite earlier ones.
2347 FPPragmaOptions.swap(Record);
2348 break;
2349
2350 case OPENCL_EXTENSIONS:
2351 // Later tables overwrite earlier ones.
2352 OpenCLExtensions.swap(Record);
2353 break;
Alexis Hunt27a761d2011-05-04 23:29:54 +00002354
2355 case TENTATIVE_DEFINITIONS:
2356 // Optimization for the first block.
2357 if (TentativeDefinitions.empty())
2358 TentativeDefinitions.swap(Record);
2359 else
2360 TentativeDefinitions.insert(TentativeDefinitions.end(),
2361 Record.begin(), Record.end());
2362 break;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002363 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00002364 First = false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002365 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002366 Error("premature end of bitstream in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002367 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002368}
2369
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002370ASTReader::ASTReadResult ASTReader::validateFileEntries() {
2371 for (unsigned CI = 0, CN = Chain.size(); CI != CN; ++CI) {
2372 PerFileData *F = Chain[CI];
2373 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
2374
2375 for (unsigned i = 0, e = F->LocalNumSLocEntries; i != e; ++i) {
2376 SLocEntryCursor.JumpToBit(F->SLocOffsets[i]);
2377 unsigned Code = SLocEntryCursor.ReadCode();
2378 if (Code == llvm::bitc::END_BLOCK ||
2379 Code == llvm::bitc::ENTER_SUBBLOCK ||
2380 Code == llvm::bitc::DEFINE_ABBREV) {
2381 Error("incorrectly-formatted source location entry in AST file");
2382 return Failure;
2383 }
2384
2385 RecordData Record;
2386 const char *BlobStart;
2387 unsigned BlobLen;
2388 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2389 default:
2390 Error("incorrectly-formatted source location entry in AST file");
2391 return Failure;
2392
2393 case SM_SLOC_FILE_ENTRY: {
2394 llvm::StringRef Filename(BlobStart, BlobLen);
2395 const FileEntry *File = getFileEntry(Filename);
2396
2397 if (File == 0) {
2398 std::string ErrorStr = "could not find file '";
2399 ErrorStr += Filename;
2400 ErrorStr += "' referenced by AST file";
2401 Error(ErrorStr.c_str());
2402 return IgnorePCH;
2403 }
2404
2405 if (Record.size() < 6) {
2406 Error("source location entry is incorrect");
2407 return Failure;
2408 }
2409
2410 // The stat info from the FileEntry came from the cached stat
2411 // info of the PCH, so we cannot trust it.
2412 struct stat StatBuf;
2413 if (::stat(File->getName(), &StatBuf) != 0) {
2414 StatBuf.st_size = File->getSize();
2415 StatBuf.st_mtime = File->getModificationTime();
2416 }
2417
2418 if (((off_t)Record[4] != StatBuf.st_size
2419#if !defined(LLVM_ON_WIN32)
2420 // In our regression testing, the Windows file system seems to
2421 // have inconsistent modification times that sometimes
2422 // erroneously trigger this error-handling path.
2423 || (time_t)Record[5] != StatBuf.st_mtime
2424#endif
2425 )) {
2426 Error(diag::err_fe_pch_file_modified, Filename);
2427 return IgnorePCH;
2428 }
2429
2430 break;
2431 }
2432
2433 case SM_SLOC_BUFFER_ENTRY:
2434 case SM_SLOC_INSTANTIATION_ENTRY:
2435 break;
2436 }
2437 }
2438 }
2439
2440 return Success;
2441}
2442
Sebastian Redl009e7f22010-10-05 16:15:19 +00002443ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2444 ASTFileType Type) {
2445 switch(ReadASTCore(FileName, Type)) {
Sebastian Redl2abc0382010-07-16 20:41:52 +00002446 case Failure: return Failure;
2447 case IgnorePCH: return IgnorePCH;
2448 case Success: break;
2449 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002450
2451 // Here comes stuff that we only do once the entire chain is loaded.
2452
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002453 if (!DisableValidation) {
2454 switch(validateFileEntries()) {
2455 case Failure: return Failure;
2456 case IgnorePCH: return IgnorePCH;
2457 case Success: break;
2458 }
2459 }
2460
Sebastian Redl96371b42010-09-22 00:42:30 +00002461 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redlfa061442010-07-21 20:07:32 +00002462 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redlada023c2010-08-04 20:40:17 +00002463 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2464 TotalNumSelectors = 0;
Sebastian Redl9e687992010-07-19 22:06:55 +00002465 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl96371b42010-09-22 00:42:30 +00002466 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002467 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002468 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl9e687992010-07-19 22:06:55 +00002469 TotalNumTypes += Chain[I]->LocalNumTypes;
2470 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redlfa061442010-07-21 20:07:32 +00002471 TotalNumPreallocatedPreprocessingEntities +=
2472 Chain[I]->NumPreallocatedPreprocessingEntities;
2473 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redlada023c2010-08-04 20:40:17 +00002474 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl9e687992010-07-19 22:06:55 +00002475 }
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002476 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002477 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl9e687992010-07-19 22:06:55 +00002478 TypesLoaded.resize(TotalNumTypes);
2479 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redlfa061442010-07-21 20:07:32 +00002480 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2481 if (PP) {
2482 if (TotalNumIdentifiers > 0)
2483 PP->getHeaderSearchInfo().SetExternalLookup(this);
2484 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2485 if (!PP->getPreprocessingRecord())
Douglas Gregor998caea2011-05-06 16:33:08 +00002486 PP->createPreprocessingRecord(true);
Sebastian Redlfa061442010-07-21 20:07:32 +00002487 PP->getPreprocessingRecord()->SetExternalSource(*this,
2488 TotalNumPreallocatedPreprocessingEntities);
2489 }
2490 }
Sebastian Redlada023c2010-08-04 20:40:17 +00002491 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl96371b42010-09-22 00:42:30 +00002492 // Preload SLocEntries.
2493 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2494 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2495 if (Result != Success)
2496 return Result;
2497 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002498
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002499 // Check the predefines buffers.
Douglas Gregorce3a8292010-07-27 00:27:13 +00002500 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002501 return IgnorePCH;
2502
2503 if (PP) {
2504 // Initialization of keywords and pragmas occurs before the
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002505 // AST file is read, so there may be some identifiers that were
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002506 // loaded into the IdentifierTable before we intercepted the
2507 // creation of identifiers. Iterate through the list of known
2508 // identifiers and determine whether we have to establish
2509 // preprocessor definitions or top-level identifier declaration
2510 // chains for those identifiers.
2511 //
2512 // We copy the IdentifierInfo pointers to a small vector first,
2513 // since de-serializing declarations or macro definitions can add
2514 // new entries into the identifier table, invalidating the
2515 // iterators.
2516 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2517 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2518 IdEnd = PP->getIdentifierTable().end();
2519 Id != IdEnd; ++Id)
2520 Identifiers.push_back(Id->second);
Sebastian Redlfa061442010-07-21 20:07:32 +00002521 // We need to search the tables in all files.
Sebastian Redlfa061442010-07-21 20:07:32 +00002522 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002523 ASTIdentifierLookupTable *IdTable
2524 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2525 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl5c415f32010-07-22 17:01:13 +00002526 // ones.
2527 if (!IdTable)
2528 continue;
Sebastian Redlfa061442010-07-21 20:07:32 +00002529 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2530 IdentifierInfo *II = Identifiers[I];
2531 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00002532 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redlfa061442010-07-21 20:07:32 +00002533 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002534 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redlb293a452010-07-20 21:20:32 +00002535 if (Pos == IdTable->end())
2536 continue;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002537
Sebastian Redlb293a452010-07-20 21:20:32 +00002538 // Dereferencing the iterator has the effect of populating the
2539 // IdentifierInfo node with the various declarations it needs.
2540 (void)*Pos;
2541 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002542 }
2543 }
2544
2545 if (Context)
2546 InitializeContext(*Context);
2547
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002548 if (DeserializationListener)
2549 DeserializationListener->ReaderInitialized(this);
2550
Douglas Gregor936a5b42010-11-30 05:23:00 +00002551 // If this AST file is a precompiled preamble, then set the main file ID of
2552 // the source manager to the file source file from which the preamble was
2553 // built. This is the only valid way to use a precompiled preamble.
2554 if (Type == Preamble) {
Douglas Gregora3b20262011-05-06 21:43:30 +00002555 if (OriginalFileID.isInvalid()) {
2556 SourceLocation Loc
2557 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2558 if (Loc.isValid())
2559 OriginalFileID = SourceMgr.getDecomposedLoc(Loc).first;
Douglas Gregor936a5b42010-11-30 05:23:00 +00002560 }
Douglas Gregora3b20262011-05-06 21:43:30 +00002561
2562 if (!OriginalFileID.isInvalid())
2563 SourceMgr.SetPreambleFileID(OriginalFileID);
Douglas Gregor936a5b42010-11-30 05:23:00 +00002564 }
2565
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002566 return Success;
2567}
2568
Sebastian Redl009e7f22010-10-05 16:15:19 +00002569ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2570 ASTFileType Type) {
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002571 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl009e7f22010-10-05 16:15:19 +00002572 Chain.push_back(new PerFileData(Type));
Sebastian Redl34522812010-07-16 17:50:48 +00002573 PerFileData &F = *Chain.back();
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002574 if (Prev)
2575 Prev->NextInSource = &F;
2576 else
2577 FirstInSource = &F;
2578 F.Loaders.push_back(Prev);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002579
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002580 // Set the AST file name.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002581 F.FileName = FileName;
2582
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002583 if (FileName != "-") {
2584 CurrentDir = llvm::sys::path::parent_path(FileName);
2585 if (CurrentDir.empty()) CurrentDir = ".";
2586 }
2587
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +00002588 if (!ASTBuffers.empty()) {
Sebastian Redleb931c62011-04-14 14:07:41 +00002589 F.Buffer.reset(ASTBuffers.back());
2590 ASTBuffers.pop_back();
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +00002591 assert(F.Buffer && "Passed null buffer");
2592 } else {
2593 // Open the AST file.
2594 //
2595 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2596 std::string ErrStr;
2597 llvm::error_code ec;
2598 if (FileName == "-") {
2599 ec = llvm::MemoryBuffer::getSTDIN(F.Buffer);
2600 if (ec)
2601 ErrStr = ec.message();
2602 } else
2603 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
2604 if (!F.Buffer) {
2605 Error(ErrStr.c_str());
2606 return IgnorePCH;
2607 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002608 }
2609
2610 // Initialize the stream
2611 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2612 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl34522812010-07-16 17:50:48 +00002613 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002614 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002615 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002616
2617 // Sniff for the signature.
2618 if (Stream.Read(8) != 'C' ||
2619 Stream.Read(8) != 'P' ||
2620 Stream.Read(8) != 'C' ||
2621 Stream.Read(8) != 'H') {
2622 Diag(diag::err_not_a_pch_file) << FileName;
2623 return Failure;
2624 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002625
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002626 while (!Stream.AtEndOfStream()) {
2627 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002628
Douglas Gregor92863e42009-04-10 23:10:45 +00002629 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002630 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002631 return Failure;
2632 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002633
2634 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002635
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002636 // We only know the AST subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002637 switch (BlockID) {
2638 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002639 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002640 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002641 return Failure;
2642 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002643 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00002644 case AST_BLOCK_ID:
Sebastian Redl3e31c722010-08-18 23:56:56 +00002645 switch (ReadASTBlock(F)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002646 case Success:
2647 break;
2648
2649 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00002650 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00002651
2652 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00002653 // FIXME: We could consider reading through to the end of this
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002654 // AST block, skipping subblocks, to see if there are other
2655 // AST blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00002656
2657 // Clear out any preallocated source location entries, so that
2658 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002659 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00002660
2661 // Remove the stat cache.
Sebastian Redl34522812010-07-16 17:50:48 +00002662 if (F.StatCache)
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002663 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00002664
Douglas Gregor92863e42009-04-10 23:10:45 +00002665 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002666 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002667 break;
2668 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002669 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002670 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002671 return Failure;
2672 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002673 break;
2674 }
Mike Stump11289f42009-09-09 15:08:12 +00002675 }
2676
Sebastian Redl2abc0382010-07-16 20:41:52 +00002677 return Success;
2678}
2679
Sebastian Redl2c499f62010-08-18 23:56:43 +00002680void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002681 PP = &pp;
Sebastian Redlfa061442010-07-21 20:07:32 +00002682
2683 unsigned TotalNum = 0;
2684 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2685 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2686 if (TotalNum) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002687 if (!PP->getPreprocessingRecord())
Douglas Gregor998caea2011-05-06 16:33:08 +00002688 PP->createPreprocessingRecord(true);
Sebastian Redlfa061442010-07-21 20:07:32 +00002689 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregoraae92242010-03-19 21:51:54 +00002690 }
2691}
2692
Sebastian Redl2c499f62010-08-18 23:56:43 +00002693void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002694 Context = &Ctx;
2695 assert(Context && "Passed null context!");
2696
2697 assert(PP && "Forgot to set Preprocessor ?");
2698 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2699 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00002700 PP->setExternalSource(this);
Douglas Gregor09b69892011-02-10 17:09:37 +00002701 PP->getHeaderSearchInfo().SetExternalSource(this);
2702
Douglas Gregoraa433012010-10-01 01:18:02 +00002703 // If we have an update block for the TU waiting, we have to add it before
2704 // deserializing the decl.
2705 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2706 if (DCU != DeclContextOffsets.end()) {
2707 // Insertion could invalidate map, so grab vector.
2708 DeclContextInfos T;
2709 T.swap(DCU->second);
2710 DeclContextOffsets.erase(DCU);
2711 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2712 }
2713
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002714 // Load the translation unit declaration
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00002715 GetTranslationUnitDecl();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002716
2717 // Load the special types.
2718 Context->setBuiltinVaListType(
Sebastian Redl539c5062010-08-18 23:57:32 +00002719 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2720 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002721 Context->setObjCIdType(GetType(Id));
Sebastian Redl539c5062010-08-18 23:57:32 +00002722 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002723 Context->setObjCSelType(GetType(Sel));
Sebastian Redl539c5062010-08-18 23:57:32 +00002724 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002725 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl539c5062010-08-18 23:57:32 +00002726 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002727 Context->setObjCClassType(GetType(Class));
Steve Naroff7cae42b2009-07-10 23:34:53 +00002728
Sebastian Redl539c5062010-08-18 23:57:32 +00002729 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002730 Context->setCFConstantStringType(GetType(String));
Mike Stump11289f42009-09-09 15:08:12 +00002731 if (unsigned FastEnum
Sebastian Redl539c5062010-08-18 23:57:32 +00002732 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002733 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl539c5062010-08-18 23:57:32 +00002734 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregor27821ce2009-07-07 16:35:42 +00002735 QualType FileType = GetType(File);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002736 if (FileType.isNull()) {
2737 Error("FILE type is NULL");
2738 return;
2739 }
John McCall9dd450b2009-09-21 23:43:11 +00002740 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregor27821ce2009-07-07 16:35:42 +00002741 Context->setFILEDecl(Typedef->getDecl());
2742 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002743 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002744 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002745 Error("Invalid FILE type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002746 return;
2747 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002748 Context->setFILEDecl(Tag->getDecl());
2749 }
2750 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002751 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002752 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002753 if (Jmp_bufType.isNull()) {
2754 Error("jmp_bug type is NULL");
2755 return;
2756 }
John McCall9dd450b2009-09-21 23:43:11 +00002757 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002758 Context->setjmp_bufDecl(Typedef->getDecl());
2759 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002760 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002761 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002762 Error("Invalid jmp_buf type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002763 return;
2764 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002765 Context->setjmp_bufDecl(Tag->getDecl());
2766 }
2767 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002768 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002769 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002770 if (Sigjmp_bufType.isNull()) {
2771 Error("sigjmp_buf type is NULL");
2772 return;
2773 }
John McCall9dd450b2009-09-21 23:43:11 +00002774 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002775 Context->setsigjmp_bufDecl(Typedef->getDecl());
2776 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002777 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002778 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stumpa4de80b2009-07-28 02:25:19 +00002779 Context->setsigjmp_bufDecl(Tag->getDecl());
2780 }
2781 }
Mike Stump11289f42009-09-09 15:08:12 +00002782 if (unsigned ObjCIdRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002783 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002784 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump11289f42009-09-09 15:08:12 +00002785 if (unsigned ObjCClassRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002786 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002787 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002788 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpd0153282009-10-20 02:12:22 +00002789 Context->setBlockDescriptorType(GetType(String));
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002790 if (unsigned String
Sebastian Redl539c5062010-08-18 23:57:32 +00002791 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002792 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002793 if (unsigned ObjCSelRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002794 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002795 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002796 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002797 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002798
Sebastian Redl539c5062010-08-18 23:57:32 +00002799 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002800 Context->setInt128Installed();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002801
Richard Smith02e85f32011-04-14 22:09:26 +00002802 if (unsigned AutoDeduct = SpecialTypes[SPECIAL_TYPE_AUTO_DEDUCT])
2803 Context->AutoDeductTy = GetType(AutoDeduct);
2804 if (unsigned AutoRRefDeduct = SpecialTypes[SPECIAL_TYPE_AUTO_RREF_DEDUCT])
2805 Context->AutoRRefDeductTy = GetType(AutoRRefDeduct);
2806
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002807 ReadPragmaDiagnosticMappings(Context->getDiagnostics());
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002808
2809 // If there were any CUDA special declarations, deserialize them.
2810 if (!CUDASpecialDeclRefs.empty()) {
2811 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
2812 Context->setcudaConfigureCallDecl(
2813 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2814 }
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002815}
2816
Douglas Gregor45fe0362009-05-12 01:31:05 +00002817/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002818/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00002819/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002820std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00002821 FileManager &FileMgr,
Daniel Dunbar3b951482009-12-03 09:13:06 +00002822 Diagnostic &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002823 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002824 std::string ErrStr;
2825 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner5159f612010-11-23 08:35:12 +00002826 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00002827 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00002828 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002829 return std::string();
2830 }
2831
2832 // Initialize the stream
2833 llvm::BitstreamReader StreamFile;
2834 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00002835 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00002836 (const unsigned char *)Buffer->getBufferEnd());
2837 Stream.init(StreamFile);
2838
2839 // Sniff for the signature.
2840 if (Stream.Read(8) != 'C' ||
2841 Stream.Read(8) != 'P' ||
2842 Stream.Read(8) != 'C' ||
2843 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002844 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002845 return std::string();
2846 }
2847
2848 RecordData Record;
2849 while (!Stream.AtEndOfStream()) {
2850 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002851
Douglas Gregor45fe0362009-05-12 01:31:05 +00002852 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2853 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00002854
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002855 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002856 switch (BlockID) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002857 case AST_BLOCK_ID:
2858 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002859 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002860 return std::string();
2861 }
2862 break;
Mike Stump11289f42009-09-09 15:08:12 +00002863
Douglas Gregor45fe0362009-05-12 01:31:05 +00002864 default:
2865 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002866 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002867 return std::string();
2868 }
2869 break;
2870 }
2871 continue;
2872 }
2873
2874 if (Code == llvm::bitc::END_BLOCK) {
2875 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002876 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002877 return std::string();
2878 }
2879 continue;
2880 }
2881
2882 if (Code == llvm::bitc::DEFINE_ABBREV) {
2883 Stream.ReadAbbrevRecord();
2884 continue;
2885 }
2886
2887 Record.clear();
2888 const char *BlobStart = 0;
2889 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002890 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl539c5062010-08-18 23:57:32 +00002891 == ORIGINAL_FILE_NAME)
Douglas Gregor45fe0362009-05-12 01:31:05 +00002892 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00002893 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00002894
2895 return std::string();
2896}
2897
Douglas Gregor55abb232009-04-10 20:39:37 +00002898/// \brief Parse the record that corresponds to a LangOptions data
2899/// structure.
2900///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002901/// This routine parses the language options from the AST file and then gives
2902/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00002903///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002904/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002905bool ASTReader::ParseLanguageOptions(
Douglas Gregor55abb232009-04-10 20:39:37 +00002906 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002907 if (Listener) {
2908 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00002909
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002910 #define PARSE_LANGOPT(Option) \
2911 LangOpts.Option = Record[Idx]; \
2912 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00002913
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002914 unsigned Idx = 0;
2915 PARSE_LANGOPT(Trigraphs);
2916 PARSE_LANGOPT(BCPLComment);
2917 PARSE_LANGOPT(DollarIdents);
2918 PARSE_LANGOPT(AsmPreprocessor);
2919 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00002920 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002921 PARSE_LANGOPT(ImplicitInt);
2922 PARSE_LANGOPT(Digraphs);
2923 PARSE_LANGOPT(HexFloats);
2924 PARSE_LANGOPT(C99);
Peter Collingbournea686b5f2011-04-15 00:35:23 +00002925 PARSE_LANGOPT(C1X);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002926 PARSE_LANGOPT(Microsoft);
2927 PARSE_LANGOPT(CPlusPlus);
2928 PARSE_LANGOPT(CPlusPlus0x);
2929 PARSE_LANGOPT(CXXOperatorNames);
2930 PARSE_LANGOPT(ObjC1);
2931 PARSE_LANGOPT(ObjC2);
2932 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00002933 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00002934 PARSE_LANGOPT(AppleKext);
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00002935 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00002936 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002937 PARSE_LANGOPT(PascalStrings);
2938 PARSE_LANGOPT(WritableStrings);
2939 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00002940 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002941 PARSE_LANGOPT(Exceptions);
Anders Carlssonce8dd3a2011-02-19 23:53:54 +00002942 PARSE_LANGOPT(ObjCExceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +00002943 PARSE_LANGOPT(CXXExceptions);
2944 PARSE_LANGOPT(SjLjExceptions);
Douglas Gregordbe39272011-02-01 15:15:22 +00002945 PARSE_LANGOPT(MSBitfields);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002946 PARSE_LANGOPT(NeXTRuntime);
2947 PARSE_LANGOPT(Freestanding);
2948 PARSE_LANGOPT(NoBuiltin);
2949 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00002950 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002951 PARSE_LANGOPT(Blocks);
2952 PARSE_LANGOPT(EmitAllDecls);
2953 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00002954 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2955 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002956 PARSE_LANGOPT(HeinousExtensions);
2957 PARSE_LANGOPT(Optimize);
2958 PARSE_LANGOPT(OptimizeSize);
2959 PARSE_LANGOPT(Static);
2960 PARSE_LANGOPT(PICLevel);
2961 PARSE_LANGOPT(GNUInline);
2962 PARSE_LANGOPT(NoInline);
Chandler Carruth7ffce732011-04-23 20:05:38 +00002963 PARSE_LANGOPT(Deprecated);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002964 PARSE_LANGOPT(AccessControl);
2965 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00002966 PARSE_LANGOPT(ShortWChar);
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00002967 PARSE_LANGOPT(ShortEnums);
Chris Lattner51924e512010-06-26 21:25:03 +00002968 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall457a04e2010-10-22 21:05:15 +00002969 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00002970 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00002971 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002972 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00002973 PARSE_LANGOPT(OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00002974 PARSE_LANGOPT(CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00002975 PARSE_LANGOPT(CatchUndefined);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002976 PARSE_LANGOPT(DefaultFPContract);
Roman Divackydc1f68d2011-03-01 17:36:40 +00002977 PARSE_LANGOPT(ElideConstructors);
2978 PARSE_LANGOPT(SpellChecking);
Roman Divacky65b88cd2011-03-01 17:40:53 +00002979 PARSE_LANGOPT(MRTD);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002980 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00002981
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002982 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00002983 }
Douglas Gregor55abb232009-04-10 20:39:37 +00002984
2985 return false;
2986}
2987
Sebastian Redl2c499f62010-08-18 23:56:43 +00002988void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002989 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2990 PerFileData &F = *Chain[I];
2991 if (!F.PreprocessorDetailCursor.getBitStreamReader())
2992 continue;
2993
2994 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
2995 F.PreprocessorDetailCursor.JumpToBit(F.PreprocessorDetailStartOffset);
2996 while (LoadPreprocessedEntity(F)) { }
2997 }
Douglas Gregoraae92242010-03-19 21:51:54 +00002998}
2999
Douglas Gregor46c50012011-02-11 19:46:30 +00003000PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003001 PerFileData *F = 0;
3002 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3003 if (Offset < Chain[I]->SizeInBits) {
3004 F = Chain[I];
3005 break;
3006 }
3007
3008 Offset -= Chain[I]->SizeInBits;
3009 }
3010
3011 if (!F) {
3012 Error("Malformed preprocessed entity offset");
3013 return 0;
3014 }
3015
Douglas Gregor92a96f52011-02-08 21:58:10 +00003016 // Keep track of where we are in the stream, then jump back there
3017 // after reading this entity.
3018 SavedStreamPosition SavedPosition(F->PreprocessorDetailCursor);
3019 F->PreprocessorDetailCursor.JumpToBit(Offset);
3020 return LoadPreprocessedEntity(*F);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003021}
3022
Douglas Gregor09b69892011-02-10 17:09:37 +00003023HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
3024 HeaderFileInfoTrait Trait(FE->getName());
3025 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3026 PerFileData &F = *Chain[I];
3027 HeaderFileInfoLookupTable *Table
3028 = static_cast<HeaderFileInfoLookupTable *>(F.HeaderFileInfoTable);
3029 if (!Table)
3030 continue;
3031
3032 // Look in the on-disk hash table for an entry for this file name.
3033 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE->getName(),
3034 &Trait);
3035 if (Pos == Table->end())
3036 continue;
3037
3038 HeaderFileInfo HFI = *Pos;
3039 if (Listener)
3040 Listener->ReadHeaderFileInfo(HFI, FE->getUID());
3041
3042 return HFI;
3043 }
3044
3045 return HeaderFileInfo();
3046}
3047
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003048void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003049 unsigned Idx = 0;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003050 while (Idx < PragmaDiagMappings.size()) {
3051 SourceLocation
3052 Loc = SourceLocation::getFromRawEncoding(PragmaDiagMappings[Idx++]);
3053 while (1) {
3054 assert(Idx < PragmaDiagMappings.size() &&
3055 "Invalid data, didn't find '-1' marking end of diag/map pairs");
3056 if (Idx >= PragmaDiagMappings.size())
3057 break; // Something is messed up but at least avoid infinite loop in
3058 // release build.
3059 unsigned DiagID = PragmaDiagMappings[Idx++];
3060 if (DiagID == (unsigned)-1)
3061 break; // no more diag/map pairs for this location.
3062 diag::Mapping Map = (diag::Mapping)PragmaDiagMappings[Idx++];
3063 Diag.setDiagnosticMapping(DiagID, Map, Loc);
3064 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003065 }
3066}
3067
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003068/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003069ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003070 PerFileData *F = 0;
3071 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3072 F = Chain[N - I - 1];
3073 if (Index < F->LocalNumTypes)
3074 break;
3075 Index -= F->LocalNumTypes;
3076 }
3077 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redl2c373b92010-10-05 15:59:54 +00003078 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003079}
3080
3081/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003082///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003083/// The index is the type ID, shifted and minus the number of predefs. This
3084/// routine actually reads the record corresponding to the type at the given
3085/// location. It is a helper routine for GetType, which deals with reading type
3086/// IDs.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003087QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003088 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003089 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00003090
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003091 // Keep track of where we are in the stream, then jump back there
3092 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00003093 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003094
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003095 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00003096
Douglas Gregor1342e842009-07-06 18:54:52 +00003097 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003098 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00003099
Sebastian Redl2c373b92010-10-05 15:59:54 +00003100 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003101 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003102 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00003103 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3104 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003105 if (Record.size() != 2) {
3106 Error("Incorrect encoding of extended qualifier type");
3107 return QualType();
3108 }
Douglas Gregor455b8f42009-04-15 22:00:08 +00003109 QualType Base = GetType(Record[0]);
John McCall8ccfcb52009-09-24 19:53:00 +00003110 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
3111 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00003112 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003113
Sebastian Redl539c5062010-08-18 23:57:32 +00003114 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003115 if (Record.size() != 1) {
3116 Error("Incorrect encoding of complex type");
3117 return QualType();
3118 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003119 QualType ElemType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003120 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003121 }
3122
Sebastian Redl539c5062010-08-18 23:57:32 +00003123 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003124 if (Record.size() != 1) {
3125 Error("Incorrect encoding of pointer type");
3126 return QualType();
3127 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003128 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003129 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003130 }
3131
Sebastian Redl539c5062010-08-18 23:57:32 +00003132 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003133 if (Record.size() != 1) {
3134 Error("Incorrect encoding of block pointer type");
3135 return QualType();
3136 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003137 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003138 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003139 }
3140
Sebastian Redl539c5062010-08-18 23:57:32 +00003141 case TYPE_LVALUE_REFERENCE: {
Richard Smith0f538462011-04-12 10:38:03 +00003142 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003143 Error("Incorrect encoding of lvalue reference type");
3144 return QualType();
3145 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003146 QualType PointeeType = GetType(Record[0]);
Richard Smith0f538462011-04-12 10:38:03 +00003147 return Context->getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003148 }
3149
Sebastian Redl539c5062010-08-18 23:57:32 +00003150 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003151 if (Record.size() != 1) {
3152 Error("Incorrect encoding of rvalue reference type");
3153 return QualType();
3154 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003155 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003156 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003157 }
3158
Sebastian Redl539c5062010-08-18 23:57:32 +00003159 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00003160 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003161 Error("Incorrect encoding of member pointer type");
3162 return QualType();
3163 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003164 QualType PointeeType = GetType(Record[0]);
3165 QualType ClassType = GetType(Record[1]);
Douglas Gregor0cdc8322010-12-10 17:03:06 +00003166 if (PointeeType.isNull() || ClassType.isNull())
3167 return QualType();
3168
Chris Lattner8575daa2009-04-27 21:45:14 +00003169 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003170 }
3171
Sebastian Redl539c5062010-08-18 23:57:32 +00003172 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003173 QualType ElementType = GetType(Record[0]);
3174 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3175 unsigned IndexTypeQuals = Record[2];
3176 unsigned Idx = 3;
3177 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00003178 return Context->getConstantArrayType(ElementType, Size,
3179 ASM, IndexTypeQuals);
3180 }
3181
Sebastian Redl539c5062010-08-18 23:57:32 +00003182 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003183 QualType ElementType = GetType(Record[0]);
3184 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3185 unsigned IndexTypeQuals = Record[2];
Chris Lattner8575daa2009-04-27 21:45:14 +00003186 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003187 }
3188
Sebastian Redl539c5062010-08-18 23:57:32 +00003189 case TYPE_VARIABLE_ARRAY: {
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003190 QualType ElementType = GetType(Record[0]);
3191 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3192 unsigned IndexTypeQuals = Record[2];
Sebastian Redl2c373b92010-10-05 15:59:54 +00003193 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3194 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
3195 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor04318252009-07-06 15:59:29 +00003196 ASM, IndexTypeQuals,
3197 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003198 }
3199
Sebastian Redl539c5062010-08-18 23:57:32 +00003200 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00003201 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003202 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003203 return QualType();
3204 }
3205
3206 QualType ElementType = GetType(Record[0]);
3207 unsigned NumElements = Record[1];
Bob Wilsonaeb56442010-11-10 21:56:12 +00003208 unsigned VecKind = Record[2];
Chris Lattner37141f42010-06-23 06:00:24 +00003209 return Context->getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00003210 (VectorType::VectorKind)VecKind);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003211 }
3212
Sebastian Redl539c5062010-08-18 23:57:32 +00003213 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00003214 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003215 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003216 return QualType();
3217 }
3218
3219 QualType ElementType = GetType(Record[0]);
3220 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00003221 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003222 }
3223
Sebastian Redl539c5062010-08-18 23:57:32 +00003224 case TYPE_FUNCTION_NO_PROTO: {
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003225 if (Record.size() != 5) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00003226 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003227 return QualType();
3228 }
3229 QualType ResultType = GetType(Record[0]);
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003230 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3], (CallingConv)Record[4]);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00003231 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003232 }
3233
Sebastian Redl539c5062010-08-18 23:57:32 +00003234 case TYPE_FUNCTION_PROTO: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003235 QualType ResultType = GetType(Record[0]);
John McCalldb40c7f2010-12-14 08:05:40 +00003236
3237 FunctionProtoType::ExtProtoInfo EPI;
3238 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003239 /*hasregparm*/ Record[2],
3240 /*regparm*/ Record[3],
3241 static_cast<CallingConv>(Record[4]));
John McCalldb40c7f2010-12-14 08:05:40 +00003242
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003243 unsigned Idx = 5;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003244 unsigned NumParams = Record[Idx++];
3245 llvm::SmallVector<QualType, 16> ParamTypes;
3246 for (unsigned I = 0; I != NumParams; ++I)
3247 ParamTypes.push_back(GetType(Record[Idx++]));
John McCalldb40c7f2010-12-14 08:05:40 +00003248
3249 EPI.Variadic = Record[Idx++];
3250 EPI.TypeQuals = Record[Idx++];
Douglas Gregordb9d6642011-01-26 05:01:58 +00003251 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003252 ExceptionSpecificationType EST =
3253 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3254 EPI.ExceptionSpecType = EST;
3255 if (EST == EST_Dynamic) {
3256 EPI.NumExceptions = Record[Idx++];
3257 llvm::SmallVector<QualType, 2> Exceptions;
3258 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
3259 Exceptions.push_back(GetType(Record[Idx++]));
3260 EPI.Exceptions = Exceptions.data();
3261 } else if (EST == EST_ComputedNoexcept) {
3262 EPI.NoexceptExpr = ReadExpr(*Loc.F);
3263 }
Jay Foad7d0479f2009-05-21 09:52:38 +00003264 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalldb40c7f2010-12-14 08:05:40 +00003265 EPI);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003266 }
3267
Sebastian Redl539c5062010-08-18 23:57:32 +00003268 case TYPE_UNRESOLVED_USING:
John McCallb96ec562009-12-04 22:46:56 +00003269 return Context->getTypeDeclType(
3270 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
3271
Sebastian Redl539c5062010-08-18 23:57:32 +00003272 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003273 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003274 Error("incorrect encoding of typedef type");
3275 return QualType();
3276 }
Richard Smithdda56e42011-04-15 14:24:37 +00003277 TypedefNameDecl *Decl = cast<TypedefNameDecl>(GetDecl(Record[0]));
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003278 QualType Canonical = GetType(Record[1]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003279 if (!Canonical.isNull())
3280 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003281 return Context->getTypedefType(Decl, Canonical);
3282 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003283
Sebastian Redl539c5062010-08-18 23:57:32 +00003284 case TYPE_TYPEOF_EXPR:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003285 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003286
Sebastian Redl539c5062010-08-18 23:57:32 +00003287 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003288 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003289 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003290 return QualType();
3291 }
3292 QualType UnderlyingType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003293 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003294 }
Mike Stump11289f42009-09-09 15:08:12 +00003295
Sebastian Redl539c5062010-08-18 23:57:32 +00003296 case TYPE_DECLTYPE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003297 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson81df7b82009-06-24 19:06:50 +00003298
Alexis Hunte852b102011-05-24 22:41:36 +00003299 case TYPE_UNARY_TRANSFORM: {
3300 QualType BaseType = GetType(Record[0]);
3301 QualType UnderlyingType = GetType(Record[1]);
3302 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
3303 return Context->getUnaryTransformType(BaseType, UnderlyingType, UKind);
3304 }
3305
Richard Smith30482bc2011-02-20 03:19:35 +00003306 case TYPE_AUTO:
3307 return Context->getAutoType(GetType(Record[0]));
3308
Sebastian Redl539c5062010-08-18 23:57:32 +00003309 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003310 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003311 Error("incorrect encoding of record type");
3312 return QualType();
3313 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003314 bool IsDependent = Record[0];
3315 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCall424cec92011-01-19 06:33:43 +00003316 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003317 return T;
3318 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003319
Sebastian Redl539c5062010-08-18 23:57:32 +00003320 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003321 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003322 Error("incorrect encoding of enum type");
3323 return QualType();
3324 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003325 bool IsDependent = Record[0];
3326 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCall424cec92011-01-19 06:33:43 +00003327 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003328 return T;
3329 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00003330
John McCall81904512011-01-06 01:58:22 +00003331 case TYPE_ATTRIBUTED: {
3332 if (Record.size() != 3) {
3333 Error("incorrect encoding of attributed type");
3334 return QualType();
3335 }
3336 QualType modifiedType = GetType(Record[0]);
3337 QualType equivalentType = GetType(Record[1]);
3338 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3339 return Context->getAttributedType(kind, modifiedType, equivalentType);
3340 }
3341
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003342 case TYPE_PAREN: {
3343 if (Record.size() != 1) {
3344 Error("incorrect encoding of paren type");
3345 return QualType();
3346 }
3347 QualType InnerType = GetType(Record[0]);
3348 return Context->getParenType(InnerType);
3349 }
3350
Douglas Gregord2fa7662010-12-20 02:24:11 +00003351 case TYPE_PACK_EXPANSION: {
Douglas Gregor17328502011-02-01 15:24:58 +00003352 if (Record.size() != 2) {
Douglas Gregord2fa7662010-12-20 02:24:11 +00003353 Error("incorrect encoding of pack expansion type");
3354 return QualType();
3355 }
3356 QualType Pattern = GetType(Record[0]);
3357 if (Pattern.isNull())
3358 return QualType();
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00003359 llvm::Optional<unsigned> NumExpansions;
3360 if (Record[1])
3361 NumExpansions = Record[1] - 1;
3362 return Context->getPackExpansionType(Pattern, NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00003363 }
3364
Sebastian Redl539c5062010-08-18 23:57:32 +00003365 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003366 unsigned Idx = 0;
3367 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3368 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3369 QualType NamedType = GetType(Record[Idx++]);
3370 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00003371 }
3372
Sebastian Redl539c5062010-08-18 23:57:32 +00003373 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00003374 unsigned Idx = 0;
3375 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00003376 return Context->getObjCInterfaceType(ItfD);
3377 }
3378
Sebastian Redl539c5062010-08-18 23:57:32 +00003379 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00003380 unsigned Idx = 0;
3381 QualType Base = GetType(Record[Idx++]);
Chris Lattner587cbe12009-04-22 06:45:28 +00003382 unsigned NumProtos = Record[Idx++];
3383 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
3384 for (unsigned I = 0; I != NumProtos; ++I)
3385 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003386 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00003387 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003388
Sebastian Redl539c5062010-08-18 23:57:32 +00003389 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00003390 unsigned Idx = 0;
John McCall8b07ec22010-05-15 11:32:37 +00003391 QualType Pointee = GetType(Record[Idx++]);
3392 return Context->getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00003393 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00003394
Sebastian Redl539c5062010-08-18 23:57:32 +00003395 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00003396 unsigned Idx = 0;
3397 QualType Parm = GetType(Record[Idx++]);
3398 QualType Replacement = GetType(Record[Idx++]);
3399 return
3400 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3401 Replacement);
3402 }
John McCalle78aac42010-03-10 03:28:59 +00003403
Douglas Gregorada4b792011-01-14 02:55:32 +00003404 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3405 unsigned Idx = 0;
3406 QualType Parm = GetType(Record[Idx++]);
3407 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3408 return Context->getSubstTemplateTypeParmPackType(
3409 cast<TemplateTypeParmType>(Parm),
3410 ArgPack);
3411 }
3412
Sebastian Redl539c5062010-08-18 23:57:32 +00003413 case TYPE_INJECTED_CLASS_NAME: {
John McCalle78aac42010-03-10 03:28:59 +00003414 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
3415 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003416 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003417 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003418 return
3419 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00003420 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003421
Sebastian Redl539c5062010-08-18 23:57:32 +00003422 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003423 unsigned Idx = 0;
3424 unsigned Depth = Record[Idx++];
3425 unsigned Index = Record[Idx++];
3426 bool Pack = Record[Idx++];
Chandler Carruth08836322011-05-01 00:51:33 +00003427 TemplateTypeParmDecl *D =
3428 cast_or_null<TemplateTypeParmDecl>(GetDecl(Record[Idx++]));
3429 return Context->getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003430 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003431
Sebastian Redl539c5062010-08-18 23:57:32 +00003432 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003433 unsigned Idx = 0;
3434 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3435 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3436 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00003437 QualType Canon = GetType(Record[Idx++]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003438 if (!Canon.isNull())
3439 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00003440 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003441 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003442
Sebastian Redl539c5062010-08-18 23:57:32 +00003443 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003444 unsigned Idx = 0;
3445 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3446 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3447 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3448 unsigned NumArgs = Record[Idx++];
3449 llvm::SmallVector<TemplateArgument, 8> Args;
3450 Args.reserve(NumArgs);
3451 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003452 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003453 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3454 Args.size(), Args.data());
3455 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003456
Sebastian Redl539c5062010-08-18 23:57:32 +00003457 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003458 unsigned Idx = 0;
3459
3460 // ArrayType
3461 QualType ElementType = GetType(Record[Idx++]);
3462 ArrayType::ArraySizeModifier ASM
3463 = (ArrayType::ArraySizeModifier)Record[Idx++];
3464 unsigned IndexTypeQuals = Record[Idx++];
3465
3466 // DependentSizedArrayType
Sebastian Redl2c373b92010-10-05 15:59:54 +00003467 Expr *NumElts = ReadExpr(*Loc.F);
3468 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003469
3470 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3471 IndexTypeQuals, Brackets);
3472 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003473
Sebastian Redl539c5062010-08-18 23:57:32 +00003474 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003475 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003476 bool IsDependent = Record[Idx++];
Douglas Gregor5590be02011-01-15 06:45:20 +00003477 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003478 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003479 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Richard Smith3f1b5d02011-05-05 21:57:07 +00003480 QualType Underlying = GetType(Record[Idx++]);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003481 QualType T;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003482 if (Underlying.isNull())
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003483 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3484 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003485 else
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003486 T = Context->getTemplateSpecializationType(Name, Args.data(),
Richard Smith3f1b5d02011-05-05 21:57:07 +00003487 Args.size(), Underlying);
John McCall424cec92011-01-19 06:33:43 +00003488 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003489 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003490 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003491 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003492 // Suppress a GCC warning
3493 return QualType();
3494}
3495
Sebastian Redl2c373b92010-10-05 15:59:54 +00003496class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00003497 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003498 ASTReader::PerFileData &F;
Sebastian Redlc67764e2010-07-22 22:43:28 +00003499 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redl2c499f62010-08-18 23:56:43 +00003500 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00003501 unsigned &Idx;
3502
Sebastian Redl2c373b92010-10-05 15:59:54 +00003503 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3504 unsigned &I) {
3505 return Reader.ReadSourceLocation(F, R, I);
3506 }
3507
John McCall8f115c62009-10-16 21:56:05 +00003508public:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003509 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl2c499f62010-08-18 23:56:43 +00003510 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003511 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3512 { }
John McCall8f115c62009-10-16 21:56:05 +00003513
John McCall17001972009-10-18 01:05:36 +00003514 // We want compile-time assurance that we've enumerated all of
3515 // these, so unfortunately we have to declare them first, then
3516 // define them out-of-line.
3517#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00003518#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00003519 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00003520#include "clang/AST/TypeLocNodes.def"
3521
John McCall17001972009-10-18 01:05:36 +00003522 void VisitFunctionTypeLoc(FunctionTypeLoc);
3523 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00003524};
3525
John McCall17001972009-10-18 01:05:36 +00003526void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00003527 // nothing to do
3528}
John McCall17001972009-10-18 01:05:36 +00003529void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003530 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorc9b7a592010-01-18 18:04:31 +00003531 if (TL.needsExtraLocalData()) {
3532 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3533 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3534 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3535 TL.setModeAttr(Record[Idx++]);
3536 }
John McCall8f115c62009-10-16 21:56:05 +00003537}
John McCall17001972009-10-18 01:05:36 +00003538void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003539 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003540}
John McCall17001972009-10-18 01:05:36 +00003541void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003542 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003543}
John McCall17001972009-10-18 01:05:36 +00003544void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003545 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003546}
John McCall17001972009-10-18 01:05:36 +00003547void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003548 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003549}
John McCall17001972009-10-18 01:05:36 +00003550void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003551 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003552}
John McCall17001972009-10-18 01:05:36 +00003553void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003554 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara509357842011-03-05 14:42:21 +00003555 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003556}
John McCall17001972009-10-18 01:05:36 +00003557void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003558 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3559 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003560 if (Record[Idx++])
Sebastian Redl2c373b92010-10-05 15:59:54 +00003561 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003562 else
John McCall17001972009-10-18 01:05:36 +00003563 TL.setSizeExpr(0);
3564}
3565void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3566 VisitArrayTypeLoc(TL);
3567}
3568void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3569 VisitArrayTypeLoc(TL);
3570}
3571void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3572 VisitArrayTypeLoc(TL);
3573}
3574void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3575 DependentSizedArrayTypeLoc TL) {
3576 VisitArrayTypeLoc(TL);
3577}
3578void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3579 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003580 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003581}
3582void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003583 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003584}
3585void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003586 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003587}
3588void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003589 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
3590 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Douglas Gregor7fb25412010-10-01 18:44:50 +00003591 TL.setTrailingReturn(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00003592 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCalle6347002009-10-23 01:28:53 +00003593 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall17001972009-10-18 01:05:36 +00003594 }
3595}
3596void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3597 VisitFunctionTypeLoc(TL);
3598}
3599void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3600 VisitFunctionTypeLoc(TL);
3601}
John McCallb96ec562009-12-04 22:46:56 +00003602void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003603 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallb96ec562009-12-04 22:46:56 +00003604}
John McCall17001972009-10-18 01:05:36 +00003605void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003606 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003607}
3608void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003609 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3610 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3611 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003612}
3613void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003614 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3615 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3616 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3617 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003618}
3619void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003620 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003621}
Alexis Hunte852b102011-05-24 22:41:36 +00003622void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3623 TL.setKWLoc(ReadSourceLocation(Record, Idx));
3624 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3625 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3626 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3627}
Richard Smith30482bc2011-02-20 03:19:35 +00003628void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3629 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3630}
John McCall17001972009-10-18 01:05:36 +00003631void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003632 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003633}
3634void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003635 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003636}
John McCall81904512011-01-06 01:58:22 +00003637void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3638 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3639 if (TL.hasAttrOperand()) {
3640 SourceRange range;
3641 range.setBegin(ReadSourceLocation(Record, Idx));
3642 range.setEnd(ReadSourceLocation(Record, Idx));
3643 TL.setAttrOperandParensRange(range);
3644 }
3645 if (TL.hasAttrExprOperand()) {
3646 if (Record[Idx++])
3647 TL.setAttrExprOperand(Reader.ReadExpr(F));
3648 else
3649 TL.setAttrExprOperand(0);
3650 } else if (TL.hasAttrEnumOperand())
3651 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3652}
John McCall17001972009-10-18 01:05:36 +00003653void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003654 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003655}
John McCallcebee162009-10-18 09:09:24 +00003656void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3657 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003658 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallcebee162009-10-18 09:09:24 +00003659}
Douglas Gregorada4b792011-01-14 02:55:32 +00003660void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3661 SubstTemplateTypeParmPackTypeLoc TL) {
3662 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3663}
John McCall17001972009-10-18 01:05:36 +00003664void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3665 TemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003666 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3667 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3668 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall0ad16662009-10-29 08:12:44 +00003669 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3670 TL.setArgLocInfo(i,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003671 Reader.GetTemplateArgumentLocInfo(F,
3672 TL.getTypePtr()->getArg(i).getKind(),
3673 Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003674}
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003675void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3676 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3677 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3678}
Abramo Bagnara6150c882010-05-11 21:36:43 +00003679void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003680 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor844cb502011-03-01 18:12:44 +00003681 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003682}
John McCalle78aac42010-03-10 03:28:59 +00003683void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003684 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalle78aac42010-03-10 03:28:59 +00003685}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00003686void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003687 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor3d0da5f2011-03-01 01:34:45 +00003688 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00003689 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003690}
John McCallc392f372010-06-11 00:33:02 +00003691void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3692 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003693 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregora7a795b2011-03-01 20:11:18 +00003694 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00003695 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3696 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3697 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003698 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3699 TL.setArgLocInfo(I,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003700 Reader.GetTemplateArgumentLocInfo(F,
3701 TL.getTypePtr()->getArg(I).getKind(),
3702 Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003703}
Douglas Gregord2fa7662010-12-20 02:24:11 +00003704void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3705 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3706}
John McCall17001972009-10-18 01:05:36 +00003707void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003708 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8b07ec22010-05-15 11:32:37 +00003709}
3710void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3711 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003712 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3713 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003714 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003715 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003716}
John McCallfc93cf92009-10-22 22:37:11 +00003717void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003718 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCallfc93cf92009-10-22 22:37:11 +00003719}
John McCall8f115c62009-10-16 21:56:05 +00003720
Sebastian Redl2c373b92010-10-05 15:59:54 +00003721TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003722 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00003723 unsigned &Idx) {
3724 QualType InfoTy = GetType(Record[Idx++]);
3725 if (InfoTy.isNull())
3726 return 0;
3727
John McCallbcd03502009-12-07 02:54:59 +00003728 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003729 TypeLocReader TLR(*this, F, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00003730 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00003731 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00003732 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00003733}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003734
Sebastian Redl539c5062010-08-18 23:57:32 +00003735QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00003736 unsigned FastQuals = ID & Qualifiers::FastMask;
3737 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003738
Sebastian Redl539c5062010-08-18 23:57:32 +00003739 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003740 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00003741 switch ((PredefinedTypeIDs)Index) {
3742 case PREDEF_TYPE_NULL_ID: return QualType();
3743 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3744 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003745
Sebastian Redl539c5062010-08-18 23:57:32 +00003746 case PREDEF_TYPE_CHAR_U_ID:
3747 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003748 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00003749 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003750 break;
3751
Sebastian Redl539c5062010-08-18 23:57:32 +00003752 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3753 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3754 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3755 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3756 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3757 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3758 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3759 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3760 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3761 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3762 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3763 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3764 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3765 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3766 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3767 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3768 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
John McCall0009fcc2011-04-26 20:42:42 +00003769 case PREDEF_TYPE_BOUND_MEMBER: T = Context->BoundMemberTy; break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003770 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
John McCall31996342011-04-07 08:22:57 +00003771 case PREDEF_TYPE_UNKNOWN_ANY: T = Context->UnknownAnyTy; break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003772 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3773 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3774 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3775 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3776 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3777 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003778 }
3779
3780 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00003781 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003782 }
3783
Sebastian Redl539c5062010-08-18 23:57:32 +00003784 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003785 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00003786 if (TypesLoaded[Index].isNull()) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003787 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003788 if (TypesLoaded[Index].isNull())
3789 return QualType();
3790
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003791 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003792 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003793 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003794 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003795 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00003796 }
Mike Stump11289f42009-09-09 15:08:12 +00003797
John McCall8ccfcb52009-09-24 19:53:00 +00003798 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003799}
3800
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003801TypeID ASTReader::GetTypeID(QualType T) const {
3802 return MakeTypeID(T,
3803 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3804}
3805
3806TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3807 if (T.isNull())
3808 return TypeIdx();
3809 assert(!T.getLocalFastQualifiers());
3810
3811 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3812 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3813 // comparing keys of ASTDeclContextNameLookupTable.
3814 // If the type didn't come from the AST file use a specially marked index
3815 // so that any hash/key comparison fail since no such index is stored
3816 // in a AST file.
3817 if (I == TypeIdxs.end())
3818 return TypeIdx(-1);
3819 return I->second;
3820}
3821
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003822unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3823 unsigned Result = 0;
3824 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3825 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3826
3827 return Result;
3828}
3829
John McCall0ad16662009-10-29 08:12:44 +00003830TemplateArgumentLocInfo
Sebastian Redl2c373b92010-10-05 15:59:54 +00003831ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3832 TemplateArgument::ArgKind Kind,
John McCall0ad16662009-10-29 08:12:44 +00003833 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003834 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00003835 switch (Kind) {
3836 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003837 return ReadExpr(F);
John McCall0ad16662009-10-29 08:12:44 +00003838 case TemplateArgument::Type:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003839 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003840 case TemplateArgument::Template: {
Douglas Gregor9d802122011-03-02 17:09:35 +00003841 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3842 Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003843 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00003844 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003845 SourceLocation());
3846 }
3847 case TemplateArgument::TemplateExpansion: {
Douglas Gregor9d802122011-03-02 17:09:35 +00003848 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3849 Index);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003850 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003851 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00003852 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregoreb29d182011-01-05 17:40:24 +00003853 EllipsisLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003854 }
John McCall0ad16662009-10-29 08:12:44 +00003855 case TemplateArgument::Null:
3856 case TemplateArgument::Integral:
3857 case TemplateArgument::Declaration:
3858 case TemplateArgument::Pack:
3859 return TemplateArgumentLocInfo();
3860 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00003861 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00003862 return TemplateArgumentLocInfo();
3863}
3864
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003865TemplateArgumentLoc
Sebastian Redl2c373b92010-10-05 15:59:54 +00003866ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003867 const RecordData &Record, unsigned &Index) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003868 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003869
3870 if (Arg.getKind() == TemplateArgument::Expression) {
3871 if (Record[Index++]) // bool InfoHasSameExpr.
3872 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3873 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00003874 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003875 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003876}
3877
Sebastian Redl2c499f62010-08-18 23:56:43 +00003878Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00003879 return GetDecl(ID);
3880}
3881
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003882uint64_t
3883ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3884 if (ID == 0)
3885 return 0;
3886
3887 --ID;
3888 uint64_t Offset = 0;
3889 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003890 PerFileData &F = *Chain[N - I - 1];
3891
3892 if (ID < F.LocalNumCXXBaseSpecifiers)
3893 return Offset + F.CXXBaseSpecifiersOffsets[ID];
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003894
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003895 ID -= F.LocalNumCXXBaseSpecifiers;
3896 Offset += F.SizeInBits;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003897 }
3898
3899 assert(false && "CXXBaseSpecifiers not found");
3900 return 0;
3901}
3902
3903CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3904 // Figure out which AST file contains this offset.
3905 PerFileData *F = 0;
3906 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003907 if (Offset < Chain[N - I - 1]->SizeInBits) {
3908 F = Chain[N - I - 1];
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003909 break;
3910 }
3911
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003912 Offset -= Chain[N - I - 1]->SizeInBits;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003913 }
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003914
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003915 if (!F) {
3916 Error("Malformed AST file: C++ base specifiers at impossible offset");
3917 return 0;
3918 }
3919
3920 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3921 SavedStreamPosition SavedPosition(Cursor);
3922 Cursor.JumpToBit(Offset);
3923 ReadingKindTracker ReadingKind(Read_Decl, *this);
3924 RecordData Record;
3925 unsigned Code = Cursor.ReadCode();
3926 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3927 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3928 Error("Malformed AST file: missing C++ base specifiers");
3929 return 0;
3930 }
3931
3932 unsigned Idx = 0;
3933 unsigned NumBases = Record[Idx++];
3934 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3935 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3936 for (unsigned I = 0; I != NumBases; ++I)
3937 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3938 return Bases;
3939}
3940
Sebastian Redl2c499f62010-08-18 23:56:43 +00003941TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003942 if (!DeclsLoaded[0]) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00003943 ReadDeclRecord(0, 1);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003944 if (DeserializationListener)
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003945 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003946 }
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00003947
3948 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3949}
3950
Sebastian Redl539c5062010-08-18 23:57:32 +00003951Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003952 if (ID == 0)
3953 return 0;
3954
Douglas Gregor745ed142009-04-25 18:35:21 +00003955 if (ID > DeclsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003956 Error("declaration ID out-of-range for AST file");
Douglas Gregor745ed142009-04-25 18:35:21 +00003957 return 0;
3958 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003959
Douglas Gregor745ed142009-04-25 18:35:21 +00003960 unsigned Index = ID - 1;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003961 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003962 ReadDeclRecord(Index, ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003963 if (DeserializationListener)
3964 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3965 }
Douglas Gregor745ed142009-04-25 18:35:21 +00003966
3967 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003968}
3969
Chris Lattner9c28af02009-04-27 05:46:25 +00003970/// \brief Resolve the offset of a statement into a statement.
3971///
3972/// This operation will read a new statement from the external
3973/// source each time it is called, and is meant to be used via a
3974/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00003975Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00003976 // Switch case IDs are per Decl.
3977 ClearSwitchCaseIDs();
3978
Sebastian Redl5c415f32010-07-22 17:01:13 +00003979 // Offset here is a global offset across the entire chain.
3980 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3981 PerFileData &F = *Chain[N - I - 1];
3982 if (Offset < F.SizeInBits) {
3983 // Since we know that this statement is part of a decl, make sure to use
3984 // the decl cursor to read it.
3985 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003986 return ReadStmtFromStream(F);
Sebastian Redl5c415f32010-07-22 17:01:13 +00003987 }
3988 Offset -= F.SizeInBits;
3989 }
3990 llvm_unreachable("Broken chain");
Douglas Gregor3c3aa612009-04-18 00:07:54 +00003991}
3992
Sebastian Redl2c499f62010-08-18 23:56:43 +00003993bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003994 bool (*isKindWeWant)(Decl::Kind),
John McCall75b960e2010-06-01 09:23:16 +00003995 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump11289f42009-09-09 15:08:12 +00003996 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003997 "DeclContext has no lexical decls in storage");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003998
Sebastian Redl5c415f32010-07-22 17:01:13 +00003999 // There might be lexical decls in multiple parts of the chain, for the TU
4000 // at least.
Sebastian Redlda6a21c2010-09-28 02:24:44 +00004001 // DeclContextOffsets might reallocate as we load additional decls below,
4002 // so make a copy of the vector.
4003 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl5c415f32010-07-22 17:01:13 +00004004 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
4005 I != E; ++I) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004006 // IDs can be 0 if this context doesn't contain declarations.
4007 if (!I->LexicalDecls)
Sebastian Redl5c415f32010-07-22 17:01:13 +00004008 continue;
Sebastian Redl5c415f32010-07-22 17:01:13 +00004009
4010 // Load all of the declaration IDs
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00004011 for (const KindDeclIDPair *ID = I->LexicalDecls,
4012 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
4013 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
4014 continue;
4015
4016 Decl *D = GetDecl(ID->second);
Sebastian Redlda6a21c2010-09-28 02:24:44 +00004017 assert(D && "Null decl in lexical decls");
4018 Decls.push_back(D);
4019 }
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004020 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004021
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004022 ++NumLexicalDeclContextsRead;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004023 return false;
4024}
4025
John McCall75b960e2010-06-01 09:23:16 +00004026DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00004027ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00004028 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00004029 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004030 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004031 if (!Name)
4032 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
4033 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004034
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004035 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl471ac2f2010-08-24 00:49:55 +00004036 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl9617e7e2010-08-24 00:50:16 +00004037 // and namespaces. For any given name, the last available results replace
4038 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl5c415f32010-07-22 17:01:13 +00004039 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl9617e7e2010-08-24 00:50:16 +00004040 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl5c415f32010-07-22 17:01:13 +00004041 I != E; ++I) {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004042 if (!I->NameLookupTableData)
Sebastian Redl5c415f32010-07-22 17:01:13 +00004043 continue;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004044
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004045 ASTDeclContextNameLookupTable *LookupTable =
4046 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
4047 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
4048 if (Pos == LookupTable->end())
Sebastian Redl5c415f32010-07-22 17:01:13 +00004049 continue;
4050
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004051 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
4052 for (; Data.first != Data.second; ++Data.first)
4053 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl9617e7e2010-08-24 00:50:16 +00004054 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004055 }
4056
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004057 ++NumVisibleDeclContextsRead;
John McCall75b960e2010-06-01 09:23:16 +00004058
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004059 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00004060 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004061}
4062
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00004063void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
4064 assert(DC->hasExternalVisibleStorage() &&
4065 "DeclContext has no visible decls in storage");
4066
4067 llvm::SmallVector<NamedDecl *, 64> Decls;
4068 // There might be visible decls in multiple parts of the chain, for the TU
4069 // and namespaces.
4070 DeclContextInfos &Infos = DeclContextOffsets[DC];
4071 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
4072 I != E; ++I) {
4073 if (!I->NameLookupTableData)
4074 continue;
4075
4076 ASTDeclContextNameLookupTable *LookupTable =
4077 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
4078 for (ASTDeclContextNameLookupTable::item_iterator
4079 ItemI = LookupTable->item_begin(),
4080 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
4081 ASTDeclContextNameLookupTable::item_iterator::value_type Val
4082 = *ItemI;
4083 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
4084 Decls.clear();
4085 for (; Data.first != Data.second; ++Data.first)
4086 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
4087 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
4088 }
4089 }
4090}
4091
Sebastian Redl2c499f62010-08-18 23:56:43 +00004092void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004093 assert(Consumer);
4094 while (!InterestingDecls.empty()) {
4095 DeclGroupRef DG(InterestingDecls.front());
4096 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00004097 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004098 }
4099}
4100
Sebastian Redl2c499f62010-08-18 23:56:43 +00004101void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00004102 this->Consumer = Consumer;
4103
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004104 if (!Consumer)
4105 return;
4106
4107 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004108 // Force deserialization of this decl, which will cause it to be queued for
4109 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00004110 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004111 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00004112
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004113 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004114}
4115
Sebastian Redl2c499f62010-08-18 23:56:43 +00004116void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004117 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004118
Mike Stump11289f42009-09-09 15:08:12 +00004119 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00004120 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00004121 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00004122 unsigned NumDeclsLoaded
4123 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
4124 (Decl *)0);
4125 unsigned NumIdentifiersLoaded
4126 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
4127 IdentifiersLoaded.end(),
4128 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00004129 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00004130 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
4131 SelectorsLoaded.end(),
4132 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00004133
Douglas Gregorc5046832009-04-27 18:38:38 +00004134 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
4135 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor258ae542009-04-27 06:38:32 +00004136 if (TotalNumSLocEntries)
4137 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
4138 NumSLocEntriesRead, TotalNumSLocEntries,
4139 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00004140 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004141 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00004142 NumTypesLoaded, (unsigned)TypesLoaded.size(),
4143 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
4144 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004145 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00004146 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
4147 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00004148 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004149 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00004150 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
4151 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00004152 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004153 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00004154 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
4155 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00004156 if (TotalNumStatements)
4157 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
4158 NumStatementsRead, TotalNumStatements,
4159 ((float)NumStatementsRead/TotalNumStatements * 100));
4160 if (TotalNumMacros)
4161 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
4162 NumMacrosRead, TotalNumMacros,
4163 ((float)NumMacrosRead/TotalNumMacros * 100));
4164 if (TotalLexicalDeclContexts)
4165 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
4166 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4167 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4168 * 100));
4169 if (TotalVisibleDeclContexts)
4170 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
4171 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4172 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4173 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004174 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004175 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004176 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4177 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00004178 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004179 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00004180 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004181 std::fprintf(stderr, "\n");
4182}
4183
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00004184/// Return the amount of memory used by memory buffers, breaking down
4185/// by heap-backed versus mmap'ed memory.
4186void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
4187 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4188 if (llvm::MemoryBuffer *buf = Chain[i]->Buffer.get()) {
4189 size_t bytes = buf->getBufferSize();
4190 switch (buf->getBufferKind()) {
4191 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
4192 sizes.malloc_bytes += bytes;
4193 break;
4194 case llvm::MemoryBuffer::MemoryBuffer_MMap:
4195 sizes.mmap_bytes += bytes;
4196 break;
4197 }
4198 }
4199}
4200
Sebastian Redl2c499f62010-08-18 23:56:43 +00004201void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004202 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004203 S.ExternalSource = this;
4204
Douglas Gregor7cd60f72009-04-22 21:15:06 +00004205 // Makes sure any declarations that were deserialized "too early"
4206 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004207 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4208 if (SemaObj->TUScope)
John McCall48871652010-08-21 09:40:31 +00004209 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004210
4211 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004212 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00004213 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00004214
4215 // If there were any tentative definitions, deserialize them and add
Sebastian Redl35351a92010-01-31 22:27:38 +00004216 // them to Sema's list of tentative definitions.
Douglas Gregord4df8652009-04-22 22:02:47 +00004217 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4218 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redl35351a92010-01-31 22:27:38 +00004219 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregord4df8652009-04-22 22:02:47 +00004220 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00004221
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004222 // If there were any unused file scoped decls, deserialize them and add to
4223 // Sema's list of unused file scoped decls.
4224 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4225 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4226 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattner90073802010-02-12 00:07:30 +00004227 }
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004228
Alexis Hunt27a761d2011-05-04 23:29:54 +00004229 // If there were any delegating constructors, add them to Sema's list
4230 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
4231 CXXConstructorDecl *D
4232 = cast<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
4233 SemaObj->DelegatingCtorDecls.push_back(D);
4234 }
4235
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004236 // If there were any locally-scoped external declarations,
4237 // deserialize them and add them to Sema's table of locally-scoped
4238 // external declarations.
4239 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4240 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4241 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
4242 }
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004243
4244 // If there were any ext_vector type declarations, deserialize them
4245 // and add them to Sema's vector of such declarations.
4246 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
4247 SemaObj->ExtVectorDecls.push_back(
Richard Smithdda56e42011-04-15 14:24:37 +00004248 cast<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004249
4250 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
4251 // Can we cut them down before writing them ?
4252
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004253 // If there were any dynamic classes declarations, deserialize them
4254 // and add them to Sema's vector of such declarations.
4255 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
4256 SemaObj->DynamicClasses.push_back(
4257 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004258
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004259 // Load the offsets of the declarations that Sema references.
4260 // They will be lazily deserialized when needed.
4261 if (!SemaDeclRefs.empty()) {
4262 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
4263 SemaObj->StdNamespace = SemaDeclRefs[0];
4264 SemaObj->StdBadAlloc = SemaDeclRefs[1];
4265 }
4266
Sebastian Redl2c373b92010-10-05 15:59:54 +00004267 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
4268
4269 // If there are @selector references added them to its pool. This is for
4270 // implementation of -Wselector.
4271 if (!F->ReferencedSelectorsData.empty()) {
4272 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
4273 unsigned I = 0;
4274 while (I < DataSize) {
4275 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
4276 SourceLocation SelLoc = ReadSourceLocation(
4277 *F, F->ReferencedSelectorsData, I);
4278 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
4279 }
4280 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00004281 }
4282
Sebastian Redl14afaf02011-04-24 16:27:30 +00004283 // The special data sets below always come from the most recent PCH,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004284 // which is at the front of the chain.
4285 PerFileData &F = *Chain.front();
4286
Sebastian Redl14afaf02011-04-24 16:27:30 +00004287 // If there were any pending implicit instantiations, deserialize them
4288 // and add them to Sema's queue of such instantiations.
4289 assert(F.PendingInstantiations.size() % 2 == 0 &&
4290 "Expected pairs of entries");
4291 for (unsigned Idx = 0, N = F.PendingInstantiations.size(); Idx < N;) {
4292 ValueDecl *D=cast<ValueDecl>(GetDecl(F.PendingInstantiations[Idx++]));
4293 SourceLocation Loc = ReadSourceLocation(F, F.PendingInstantiations,Idx);
4294 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
4295 }
4296
Sebastian Redl2c373b92010-10-05 15:59:54 +00004297 // If there were any weak undeclared identifiers, deserialize them and add to
4298 // Sema's list of weak undeclared identifiers.
4299 if (!WeakUndeclaredIdentifiers.empty()) {
4300 unsigned Idx = 0;
4301 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
4302 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4303 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4304 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
4305 bool Used = WeakUndeclaredIdentifiers[Idx++];
4306 Sema::WeakInfo WI(AliasId, Loc);
4307 WI.setUsed(Used);
4308 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
4309 }
4310 }
4311
4312 // If there were any VTable uses, deserialize the information and add it
4313 // to Sema's vector and map of VTable uses.
4314 if (!VTableUses.empty()) {
4315 unsigned Idx = 0;
4316 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
4317 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4318 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
4319 bool DefinitionRequired = VTableUses[Idx++];
4320 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
4321 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004322 }
4323 }
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004324
4325 if (!FPPragmaOptions.empty()) {
4326 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4327 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4328 }
4329
4330 if (!OpenCLExtensions.empty()) {
4331 unsigned I = 0;
4332#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4333#include "clang/Basic/OpenCLExtensions.def"
4334
4335 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4336 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00004337}
4338
Sebastian Redl2c499f62010-08-18 23:56:43 +00004339IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redl78f51772010-08-02 18:30:12 +00004340 // Try to find this name within our on-disk hash tables. We start with the
4341 // most recent one, since that one contains the most up-to-date info.
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004342 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004343 ASTIdentifierLookupTable *IdTable
4344 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl5c415f32010-07-22 17:01:13 +00004345 if (!IdTable)
4346 continue;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004347 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004348 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004349 if (Pos == IdTable->end())
4350 continue;
Douglas Gregora868bbd2009-04-21 22:25:48 +00004351
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004352 // Dereferencing the iterator has the effect of building the
4353 // IdentifierInfo node and populating it with the various
4354 // declarations it needs.
Sebastian Redl78f51772010-08-02 18:30:12 +00004355 return *Pos;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004356 }
Sebastian Redl78f51772010-08-02 18:30:12 +00004357 return 0;
Douglas Gregora868bbd2009-04-21 22:25:48 +00004358}
4359
Douglas Gregor57756ea2010-10-14 22:11:03 +00004360namespace clang {
4361 /// \brief An identifier-lookup iterator that enumerates all of the
4362 /// identifiers stored within a set of AST files.
4363 class ASTIdentifierIterator : public IdentifierIterator {
4364 /// \brief The AST reader whose identifiers are being enumerated.
4365 const ASTReader &Reader;
4366
4367 /// \brief The current index into the chain of AST files stored in
4368 /// the AST reader.
4369 unsigned Index;
4370
4371 /// \brief The current position within the identifier lookup table
4372 /// of the current AST file.
4373 ASTIdentifierLookupTable::key_iterator Current;
4374
4375 /// \brief The end position within the identifier lookup table of
4376 /// the current AST file.
4377 ASTIdentifierLookupTable::key_iterator End;
4378
4379 public:
4380 explicit ASTIdentifierIterator(const ASTReader &Reader);
4381
4382 virtual llvm::StringRef Next();
4383 };
4384}
4385
4386ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
4387 : Reader(Reader), Index(Reader.Chain.size() - 1) {
4388 ASTIdentifierLookupTable *IdTable
4389 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4390 Current = IdTable->key_begin();
4391 End = IdTable->key_end();
4392}
4393
4394llvm::StringRef ASTIdentifierIterator::Next() {
4395 while (Current == End) {
4396 // If we have exhausted all of our AST files, we're done.
4397 if (Index == 0)
4398 return llvm::StringRef();
4399
4400 --Index;
4401 ASTIdentifierLookupTable *IdTable
4402 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4403 Current = IdTable->key_begin();
4404 End = IdTable->key_end();
4405 }
4406
4407 // We have any identifiers remaining in the current AST file; return
4408 // the next one.
4409 std::pair<const char*, unsigned> Key = *Current;
4410 ++Current;
4411 return llvm::StringRef(Key.first, Key.second);
4412}
4413
4414IdentifierIterator *ASTReader::getIdentifiers() const {
4415 return new ASTIdentifierIterator(*this);
4416}
4417
Mike Stump11289f42009-09-09 15:08:12 +00004418std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redl2c499f62010-08-18 23:56:43 +00004419ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redlada023c2010-08-04 20:40:17 +00004420 // Find this selector in a hash table. We want to find the most recent entry.
4421 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4422 PerFileData &F = *Chain[I];
4423 if (!F.SelectorLookupTable)
4424 continue;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004425
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004426 ASTSelectorLookupTable *PoolTable
4427 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
4428 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redlada023c2010-08-04 20:40:17 +00004429 if (Pos != PoolTable->end()) {
4430 ++NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004431 // FIXME: Not quite happy with the statistics here. We probably should
4432 // disable this tracking when called via LoadSelector.
4433 // Also, should entries without methods count as misses?
4434 ++NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004435 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redlada023c2010-08-04 20:40:17 +00004436 if (DeserializationListener)
4437 DeserializationListener->SelectorRead(Data.ID, Sel);
4438 return std::make_pair(Data.Instance, Data.Factory);
4439 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004440 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00004441
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004442 ++NumMethodPoolMisses;
Sebastian Redlada023c2010-08-04 20:40:17 +00004443 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorc78d3462009-04-24 21:10:55 +00004444}
4445
Sebastian Redl2c499f62010-08-18 23:56:43 +00004446void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004447 // It would be complicated to avoid reading the methods anyway. So don't.
4448 ReadMethodPool(Sel);
4449}
4450
Sebastian Redl2c499f62010-08-18 23:56:43 +00004451void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004452 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00004453 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00004454 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00004455 if (DeserializationListener)
4456 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004457}
4458
Douglas Gregor1342e842009-07-06 18:54:52 +00004459/// \brief Set the globally-visible declarations associated with the given
4460/// identifier.
4461///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004462/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00004463/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00004464/// them.
4465///
4466/// \param II an IdentifierInfo that refers to one or more globally-visible
4467/// declarations.
4468///
4469/// \param DeclIDs the set of declaration IDs with the name @p II that are
4470/// visible at global scope.
4471///
4472/// \param Nonrecursive should be true to indicate that the caller knows that
4473/// this call is non-recursive, and therefore the globally-visible declarations
4474/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00004475void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004476ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregor1342e842009-07-06 18:54:52 +00004477 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
4478 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004479 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004480 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4481 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4482 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004483 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00004484 return;
4485 }
Mike Stump11289f42009-09-09 15:08:12 +00004486
Douglas Gregor1342e842009-07-06 18:54:52 +00004487 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4488 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4489 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004490 if (SemaObj->TUScope) {
4491 // Introduce this declaration into the translation-unit scope
4492 // and add it to the declaration chain for this identifier, so
4493 // that (unqualified) name lookup will find it.
John McCall48871652010-08-21 09:40:31 +00004494 SemaObj->TUScope->AddDecl(D);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004495 }
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004496 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregor1342e842009-07-06 18:54:52 +00004497 } else {
4498 // Queue this declaration so that it will be added to the
4499 // translation unit scope and identifier's declaration chain
4500 // once a Sema object is known.
4501 PreloadedDecls.push_back(D);
4502 }
4503 }
4504}
4505
Sebastian Redl2c499f62010-08-18 23:56:43 +00004506IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004507 if (ID == 0)
4508 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00004509
Sebastian Redlc713b962010-07-21 00:46:22 +00004510 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004511 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004512 return 0;
4513 }
Mike Stump11289f42009-09-09 15:08:12 +00004514
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004515 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc713b962010-07-21 00:46:22 +00004516 ID -= 1;
4517 if (!IdentifiersLoaded[ID]) {
4518 unsigned Index = ID;
4519 const char *Str = 0;
4520 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4521 PerFileData *F = Chain[N - I - 1];
4522 if (Index < F->LocalNumIdentifiers) {
4523 uint32_t Offset = F->IdentifierOffsets[Index];
4524 Str = F->IdentifierTableData + Offset;
4525 break;
4526 }
4527 Index -= F->LocalNumIdentifiers;
4528 }
4529 assert(Str && "Broken Chain");
Douglas Gregor5287b4e2009-04-25 21:04:17 +00004530
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004531 // All of the strings in the AST file are preceded by a 16-bit length.
4532 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00004533 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4534 // unsigned integers. This is important to avoid integer overflow when
4535 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00004536 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00004537 unsigned StrLen = (((unsigned) StrLenPtr[0])
4538 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00004539 IdentifiersLoaded[ID]
Kovarththanan Rajaratnama3b09592010-03-12 10:32:27 +00004540 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlff4a2952010-07-23 23:49:55 +00004541 if (DeserializationListener)
4542 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004543 }
Mike Stump11289f42009-09-09 15:08:12 +00004544
Sebastian Redlc713b962010-07-21 00:46:22 +00004545 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004546}
4547
Douglas Gregor49f754f2011-04-20 00:21:03 +00004548bool ASTReader::ReadSLocEntry(unsigned ID) {
4549 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor258ae542009-04-27 06:38:32 +00004550}
4551
Sebastian Redl2c499f62010-08-18 23:56:43 +00004552Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00004553 if (ID == 0)
4554 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00004555
Sebastian Redlada023c2010-08-04 20:40:17 +00004556 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004557 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00004558 return Selector();
4559 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004560
Sebastian Redlada023c2010-08-04 20:40:17 +00004561 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004562 // Load this selector from the selector table.
Sebastian Redlada023c2010-08-04 20:40:17 +00004563 unsigned Idx = ID - 1;
4564 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4565 PerFileData &F = *Chain[N - I - 1];
4566 if (Idx < F.LocalNumSelectors) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004567 ASTSelectorLookupTrait Trait(*this);
Sebastian Redlada023c2010-08-04 20:40:17 +00004568 SelectorsLoaded[ID - 1] =
4569 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4570 if (DeserializationListener)
4571 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4572 break;
4573 }
4574 Idx -= F.LocalNumSelectors;
4575 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004576 }
4577
Sebastian Redlada023c2010-08-04 20:40:17 +00004578 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00004579}
4580
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004581Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00004582 return DecodeSelector(ID);
4583}
4584
Sebastian Redl2c499f62010-08-18 23:56:43 +00004585uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00004586 // ID 0 (the null selector) is considered an external selector.
4587 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00004588}
4589
Mike Stump11289f42009-09-09 15:08:12 +00004590DeclarationName
Sebastian Redl2c499f62010-08-18 23:56:43 +00004591ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004592 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4593 switch (Kind) {
4594 case DeclarationName::Identifier:
4595 return DeclarationName(GetIdentifierInfo(Record, Idx));
4596
4597 case DeclarationName::ObjCZeroArgSelector:
4598 case DeclarationName::ObjCOneArgSelector:
4599 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff3c301dc2009-04-23 15:15:40 +00004600 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004601
4602 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004603 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004604 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004605
4606 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004607 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004608 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004609
4610 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004611 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004612 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004613
4614 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004615 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004616 (OverloadedOperatorKind)Record[Idx++]);
4617
Alexis Hunt3d221f22009-11-29 07:34:05 +00004618 case DeclarationName::CXXLiteralOperatorName:
4619 return Context->DeclarationNames.getCXXLiteralOperatorName(
4620 GetIdentifierInfo(Record, Idx));
4621
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004622 case DeclarationName::CXXUsingDirective:
4623 return DeclarationName::getUsingDirectiveName();
4624 }
4625
4626 // Required to silence GCC warning
4627 return DeclarationName();
4628}
Douglas Gregor55abb232009-04-10 20:39:37 +00004629
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004630void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4631 DeclarationNameLoc &DNLoc,
4632 DeclarationName Name,
4633 const RecordData &Record, unsigned &Idx) {
4634 switch (Name.getNameKind()) {
4635 case DeclarationName::CXXConstructorName:
4636 case DeclarationName::CXXDestructorName:
4637 case DeclarationName::CXXConversionFunctionName:
4638 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4639 break;
4640
4641 case DeclarationName::CXXOperatorName:
4642 DNLoc.CXXOperatorName.BeginOpNameLoc
4643 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4644 DNLoc.CXXOperatorName.EndOpNameLoc
4645 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4646 break;
4647
4648 case DeclarationName::CXXLiteralOperatorName:
4649 DNLoc.CXXLiteralOperatorName.OpNameLoc
4650 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4651 break;
4652
4653 case DeclarationName::Identifier:
4654 case DeclarationName::ObjCZeroArgSelector:
4655 case DeclarationName::ObjCOneArgSelector:
4656 case DeclarationName::ObjCMultiArgSelector:
4657 case DeclarationName::CXXUsingDirective:
4658 break;
4659 }
4660}
4661
4662void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4663 DeclarationNameInfo &NameInfo,
4664 const RecordData &Record, unsigned &Idx) {
4665 NameInfo.setName(ReadDeclarationName(Record, Idx));
4666 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4667 DeclarationNameLoc DNLoc;
4668 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4669 NameInfo.setInfo(DNLoc);
4670}
4671
4672void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4673 const RecordData &Record, unsigned &Idx) {
Douglas Gregor14454802011-02-25 02:25:35 +00004674 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004675 unsigned NumTPLists = Record[Idx++];
4676 Info.NumTemplParamLists = NumTPLists;
4677 if (NumTPLists) {
4678 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4679 for (unsigned i=0; i != NumTPLists; ++i)
4680 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4681 }
4682}
4683
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004684TemplateName
Douglas Gregor5590be02011-01-15 06:45:20 +00004685ASTReader::ReadTemplateName(PerFileData &F, const RecordData &Record,
4686 unsigned &Idx) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004687 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004688 switch (Kind) {
4689 case TemplateName::Template:
4690 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4691
4692 case TemplateName::OverloadedTemplate: {
4693 unsigned size = Record[Idx++];
4694 UnresolvedSet<8> Decls;
4695 while (size--)
4696 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4697
4698 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4699 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004700
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004701 case TemplateName::QualifiedTemplate: {
4702 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4703 bool hasTemplKeyword = Record[Idx++];
4704 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4705 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4706 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004707
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004708 case TemplateName::DependentTemplate: {
4709 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4710 if (Record[Idx++]) // isIdentifier
4711 return Context->getDependentTemplateName(NNS,
4712 GetIdentifierInfo(Record, Idx));
4713 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004714 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004715 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004716
4717 case TemplateName::SubstTemplateTemplateParmPack: {
4718 TemplateTemplateParmDecl *Param
4719 = cast_or_null<TemplateTemplateParmDecl>(GetDecl(Record[Idx++]));
4720 if (!Param)
4721 return TemplateName();
4722
4723 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
4724 if (ArgPack.getKind() != TemplateArgument::Pack)
4725 return TemplateName();
4726
4727 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
4728 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004729 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004730
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004731 assert(0 && "Unhandled template name kind!");
4732 return TemplateName();
4733}
4734
4735TemplateArgument
Sebastian Redl2c373b92010-10-05 15:59:54 +00004736ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00004737 const RecordData &Record, unsigned &Idx) {
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004738 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
4739 switch (Kind) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004740 case TemplateArgument::Null:
4741 return TemplateArgument();
4742 case TemplateArgument::Type:
4743 return TemplateArgument(GetType(Record[Idx++]));
4744 case TemplateArgument::Declaration:
4745 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00004746 case TemplateArgument::Integral: {
4747 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4748 QualType T = GetType(Record[Idx++]);
4749 return TemplateArgument(Value, T);
4750 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004751 case TemplateArgument::Template:
Douglas Gregor5590be02011-01-15 06:45:20 +00004752 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004753 case TemplateArgument::TemplateExpansion: {
Douglas Gregor5590be02011-01-15 06:45:20 +00004754 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregore1d60df2011-01-14 23:41:42 +00004755 llvm::Optional<unsigned> NumTemplateExpansions;
4756 if (unsigned NumExpansions = Record[Idx++])
4757 NumTemplateExpansions = NumExpansions - 1;
4758 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004759 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004760 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004761 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004762 case TemplateArgument::Pack: {
4763 unsigned NumArgs = Record[Idx++];
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004764 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4765 for (unsigned I = 0; I != NumArgs; ++I)
4766 Args[I] = ReadTemplateArgument(F, Record, Idx);
4767 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004768 }
4769 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004770
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004771 assert(0 && "Unhandled template argument kind!");
4772 return TemplateArgument();
4773}
4774
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004775TemplateParameterList *
Sebastian Redl2c373b92010-10-05 15:59:54 +00004776ASTReader::ReadTemplateParameterList(PerFileData &F,
4777 const RecordData &Record, unsigned &Idx) {
4778 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4779 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4780 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004781
4782 unsigned NumParams = Record[Idx++];
4783 llvm::SmallVector<NamedDecl *, 16> Params;
4784 Params.reserve(NumParams);
4785 while (NumParams--)
4786 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004787
4788 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004789 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4790 Params.data(), Params.size(), RAngleLoc);
4791 return TemplateParams;
4792}
4793
4794void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004795ASTReader::
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004796ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004797 PerFileData &F, const RecordData &Record,
4798 unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004799 unsigned NumTemplateArgs = Record[Idx++];
4800 TemplArgs.reserve(NumTemplateArgs);
4801 while (NumTemplateArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004802 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004803}
4804
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004805/// \brief Read a UnresolvedSet structure.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004806void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004807 const RecordData &Record, unsigned &Idx) {
4808 unsigned NumDecls = Record[Idx++];
4809 while (NumDecls--) {
4810 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4811 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4812 Set.addDecl(D, AS);
4813 }
4814}
4815
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004816CXXBaseSpecifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00004817ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky19b9f952010-07-26 16:56:01 +00004818 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004819 bool isVirtual = static_cast<bool>(Record[Idx++]);
4820 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4821 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redl08905022011-02-05 19:23:19 +00004822 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004823 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4824 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor752a5952011-01-03 22:36:02 +00004825 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl08905022011-02-05 19:23:19 +00004826 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregor752a5952011-01-03 22:36:02 +00004827 EllipsisLoc);
Sebastian Redl08905022011-02-05 19:23:19 +00004828 Result.setInheritConstructors(inheritConstructors);
4829 return Result;
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004830}
4831
Alexis Hunt1d792652011-01-08 20:30:50 +00004832std::pair<CXXCtorInitializer **, unsigned>
4833ASTReader::ReadCXXCtorInitializers(PerFileData &F, const RecordData &Record,
4834 unsigned &Idx) {
4835 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004836 unsigned NumInitializers = Record[Idx++];
4837 if (NumInitializers) {
4838 ASTContext &C = *getContext();
4839
Alexis Hunt1d792652011-01-08 20:30:50 +00004840 CtorInitializers
4841 = new (C) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004842 for (unsigned i=0; i != NumInitializers; ++i) {
4843 TypeSourceInfo *BaseClassInfo = 0;
4844 bool IsBaseVirtual = false;
4845 FieldDecl *Member = 0;
Francois Pichetd583da02010-12-04 09:14:42 +00004846 IndirectFieldDecl *IndirectMember = 0;
Alexis Hunt37a477f2011-05-04 01:19:08 +00004847 CXXConstructorDecl *Target = 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004848
Alexis Hunt37a477f2011-05-04 01:19:08 +00004849 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
4850 switch (Type) {
4851 case CTOR_INITIALIZER_BASE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004852 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004853 IsBaseVirtual = Record[Idx++];
Alexis Hunt37a477f2011-05-04 01:19:08 +00004854 break;
4855
4856 case CTOR_INITIALIZER_DELEGATING:
4857 Target = cast<CXXConstructorDecl>(GetDecl(Record[Idx++]));
4858 break;
4859
4860 case CTOR_INITIALIZER_MEMBER:
4861 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
4862 break;
4863
4864 case CTOR_INITIALIZER_INDIRECT_MEMBER:
4865 IndirectMember = cast<IndirectFieldDecl>(GetDecl(Record[Idx++]));
4866 break;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004867 }
Alexis Hunt37a477f2011-05-04 01:19:08 +00004868
Douglas Gregor44e7df62011-01-04 00:32:56 +00004869 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004870 Expr *Init = ReadExpr(F);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004871 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4872 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004873 bool IsWritten = Record[Idx++];
4874 unsigned SourceOrderOrNumArrayIndices;
4875 llvm::SmallVector<VarDecl *, 8> Indices;
4876 if (IsWritten) {
4877 SourceOrderOrNumArrayIndices = Record[Idx++];
4878 } else {
4879 SourceOrderOrNumArrayIndices = Record[Idx++];
4880 Indices.reserve(SourceOrderOrNumArrayIndices);
4881 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4882 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4883 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004884
Alexis Hunt1d792652011-01-08 20:30:50 +00004885 CXXCtorInitializer *BOMInit;
Alexis Hunt37a477f2011-05-04 01:19:08 +00004886 if (Type == CTOR_INITIALIZER_BASE) {
Alexis Hunt1d792652011-01-08 20:30:50 +00004887 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
4888 LParenLoc, Init, RParenLoc,
4889 MemberOrEllipsisLoc);
Alexis Hunt37a477f2011-05-04 01:19:08 +00004890 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
4891 BOMInit = new (C) CXXCtorInitializer(C, MemberOrEllipsisLoc, LParenLoc,
4892 Target, Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004893 } else if (IsWritten) {
Francois Pichetd583da02010-12-04 09:14:42 +00004894 if (Member)
Alexis Hunt1d792652011-01-08 20:30:50 +00004895 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
4896 LParenLoc, Init, RParenLoc);
Francois Pichetd583da02010-12-04 09:14:42 +00004897 else
Alexis Hunt1d792652011-01-08 20:30:50 +00004898 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
4899 MemberOrEllipsisLoc, LParenLoc,
4900 Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004901 } else {
Alexis Hunt1d792652011-01-08 20:30:50 +00004902 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
4903 LParenLoc, Init, RParenLoc,
4904 Indices.data(), Indices.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004905 }
4906
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00004907 if (IsWritten)
4908 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Alexis Hunt1d792652011-01-08 20:30:50 +00004909 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004910 }
4911 }
4912
Alexis Hunt1d792652011-01-08 20:30:50 +00004913 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004914}
4915
Chris Lattnerca025db2010-05-07 21:43:38 +00004916NestedNameSpecifier *
Sebastian Redl2c499f62010-08-18 23:56:43 +00004917ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004918 unsigned N = Record[Idx++];
4919 NestedNameSpecifier *NNS = 0, *Prev = 0;
4920 for (unsigned I = 0; I != N; ++I) {
4921 NestedNameSpecifier::SpecifierKind Kind
4922 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4923 switch (Kind) {
4924 case NestedNameSpecifier::Identifier: {
4925 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4926 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4927 break;
4928 }
4929
4930 case NestedNameSpecifier::Namespace: {
4931 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4932 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4933 break;
4934 }
4935
Douglas Gregor7b26ff92011-02-24 02:36:08 +00004936 case NestedNameSpecifier::NamespaceAlias: {
4937 NamespaceAliasDecl *Alias
4938 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++]));
4939 NNS = NestedNameSpecifier::Create(*Context, Prev, Alias);
4940 break;
4941 }
4942
Chris Lattnerca025db2010-05-07 21:43:38 +00004943 case NestedNameSpecifier::TypeSpec:
4944 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall424cec92011-01-19 06:33:43 +00004945 const Type *T = GetType(Record[Idx++]).getTypePtrOrNull();
Douglas Gregor0cdc8322010-12-10 17:03:06 +00004946 if (!T)
4947 return 0;
4948
Chris Lattnerca025db2010-05-07 21:43:38 +00004949 bool Template = Record[Idx++];
4950 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4951 break;
4952 }
4953
4954 case NestedNameSpecifier::Global: {
4955 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4956 // No associated value, and there can't be a prefix.
4957 break;
4958 }
Chris Lattnerca025db2010-05-07 21:43:38 +00004959 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00004960 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00004961 }
4962 return NNS;
4963}
4964
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004965NestedNameSpecifierLoc
4966ASTReader::ReadNestedNameSpecifierLoc(PerFileData &F, const RecordData &Record,
4967 unsigned &Idx) {
4968 unsigned N = Record[Idx++];
Douglas Gregor9b272512011-02-28 23:58:31 +00004969 NestedNameSpecifierLocBuilder Builder;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004970 for (unsigned I = 0; I != N; ++I) {
4971 NestedNameSpecifier::SpecifierKind Kind
4972 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4973 switch (Kind) {
4974 case NestedNameSpecifier::Identifier: {
Douglas Gregor9b272512011-02-28 23:58:31 +00004975 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004976 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00004977 Builder.Extend(*Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004978 break;
4979 }
4980
4981 case NestedNameSpecifier::Namespace: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004982 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004983 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00004984 Builder.Extend(*Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004985 break;
4986 }
4987
4988 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004989 NamespaceAliasDecl *Alias
4990 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++]));
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004991 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00004992 Builder.Extend(*Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004993 break;
4994 }
4995
4996 case NestedNameSpecifier::TypeSpec:
4997 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004998 bool Template = Record[Idx++];
4999 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
5000 if (!T)
5001 return NestedNameSpecifierLoc();
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005002 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005003
5004 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
5005 Builder.Extend(*Context,
5006 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
5007 T->getTypeLoc(), ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005008 break;
5009 }
5010
5011 case NestedNameSpecifier::Global: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005012 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005013 Builder.MakeGlobal(*Context, ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005014 break;
5015 }
5016 }
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005017 }
5018
Douglas Gregor9b272512011-02-28 23:58:31 +00005019 return Builder.getWithLocInContext(*Context);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005020}
5021
Chris Lattnerca025db2010-05-07 21:43:38 +00005022SourceRange
Sebastian Redl2c373b92010-10-05 15:59:54 +00005023ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
5024 unsigned &Idx) {
5025 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
5026 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00005027 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00005028}
5029
Douglas Gregor1daeb692009-04-13 18:14:40 +00005030/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005031llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00005032 unsigned BitWidth = Record[Idx++];
5033 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
5034 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
5035 Idx += NumWords;
5036 return Result;
5037}
5038
5039/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005040llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00005041 bool isUnsigned = Record[Idx++];
5042 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
5043}
5044
Douglas Gregore0a3a512009-04-14 21:55:33 +00005045/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005046llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00005047 return llvm::APFloat(ReadAPInt(Record, Idx));
5048}
5049
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005050// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00005051std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005052 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00005053 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005054 Idx += Len;
5055 return Result;
5056}
5057
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005058VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
5059 unsigned &Idx) {
5060 unsigned Major = Record[Idx++];
5061 unsigned Minor = Record[Idx++];
5062 unsigned Subminor = Record[Idx++];
5063 if (Minor == 0)
5064 return VersionTuple(Major);
5065 if (Subminor == 0)
5066 return VersionTuple(Major, Minor - 1);
5067 return VersionTuple(Major, Minor - 1, Subminor - 1);
5068}
5069
Sebastian Redl2c499f62010-08-18 23:56:43 +00005070CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00005071 unsigned &Idx) {
5072 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
5073 return CXXTemporary::Create(*Context, Decl);
5074}
5075
Sebastian Redl2c499f62010-08-18 23:56:43 +00005076DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00005077 return Diag(SourceLocation(), DiagID);
5078}
5079
Sebastian Redl2c499f62010-08-18 23:56:43 +00005080DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005081 return Diags.Report(Loc, DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00005082}
Douglas Gregora9af1d12009-04-17 00:04:06 +00005083
Douglas Gregora868bbd2009-04-21 22:25:48 +00005084/// \brief Retrieve the identifier table associated with the
5085/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005086IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00005087 assert(PP && "Forgot to set Preprocessor ?");
5088 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00005089}
5090
Douglas Gregora9af1d12009-04-17 00:04:06 +00005091/// \brief Record that the given ID maps to the given switch-case
5092/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005093void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00005094 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
5095 SwitchCaseStmts[ID] = SC;
5096}
5097
5098/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005099SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00005100 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
5101 return SwitchCaseStmts[ID];
5102}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00005103
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00005104void ASTReader::ClearSwitchCaseIDs() {
5105 SwitchCaseStmts.clear();
5106}
5107
Sebastian Redl2c499f62010-08-18 23:56:43 +00005108void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005109 assert(NumCurrentElementsDeserializing &&
5110 "FinishedDeserializing not paired with StartedDeserializing");
5111 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00005112 // If any identifiers with corresponding top-level declarations have
5113 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005114 while (!PendingIdentifierInfos.empty()) {
5115 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
5116 PendingIdentifierInfos.front().DeclIDs, true);
5117 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00005118 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005119
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00005120 // Ready to load previous declarations of Decls that were delayed.
5121 while (!PendingPreviousDecls.empty()) {
5122 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
5123 PendingPreviousDecls.front().second);
5124 PendingPreviousDecls.pop_front();
5125 }
5126
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005127 // We are not in recursive loading, so it's safe to pass the "interesting"
5128 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005129 if (Consumer)
5130 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidisad5f95c2010-10-24 17:26:31 +00005131
5132 assert(PendingForwardRefs.size() == 0 &&
5133 "Some forward refs did not get linked to the definition!");
Douglas Gregor1342e842009-07-06 18:54:52 +00005134 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005135 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00005136}
Douglas Gregorb473b072010-08-19 00:28:17 +00005137
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005138ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005139 const char *isysroot, bool DisableValidation,
5140 bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005141 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
5142 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
5143 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
5144 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005145 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
5146 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NextSLocOffset(0),
5147 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
5148 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
5149 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
5150 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
5151 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
5152 NumCurrentElementsDeserializing(0)
5153{
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005154 RelocatablePCH = false;
5155}
5156
5157ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
5158 Diagnostic &Diags, const char *isysroot,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005159 bool DisableValidation, bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005160 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
5161 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005162 isysroot(isysroot), DisableValidation(DisableValidation),
5163 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
5164 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00005165 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
5166 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
5167 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
5168 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
5169 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
5170 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005171 RelocatablePCH = false;
5172}
5173
5174ASTReader::~ASTReader() {
5175 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
5176 delete Chain[e - i - 1];
5177 // Delete all visible decl lookup tables
5178 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
5179 E = DeclContextOffsets.end();
5180 I != E; ++I) {
5181 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
5182 J != F; ++J) {
5183 if (J->NameLookupTableData)
5184 delete static_cast<ASTDeclContextNameLookupTable*>(
5185 J->NameLookupTableData);
5186 }
5187 }
5188 for (DeclContextVisibleUpdatesPending::iterator
5189 I = PendingVisibleUpdates.begin(),
5190 E = PendingVisibleUpdates.end();
5191 I != E; ++I) {
5192 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5193 F = I->second.end();
5194 J != F; ++J)
5195 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
5196 }
5197}
5198
Sebastian Redl009e7f22010-10-05 16:15:19 +00005199ASTReader::PerFileData::PerFileData(ASTFileType Ty)
5200 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00005201 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
5202 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
Douglas Gregor09b69892011-02-10 17:09:37 +00005203 MacroDefinitionOffsets(0),
5204 LocalNumHeaderFileInfos(0), HeaderFileInfoTableData(0),
5205 HeaderFileInfoTable(0),
5206 LocalNumSelectors(0), SelectorOffsets(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00005207 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005208 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
5209 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redl3f6b7532010-10-01 19:59:12 +00005210 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregorb473b072010-08-19 00:28:17 +00005211{}
5212
5213ASTReader::PerFileData::~PerFileData() {
5214 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
Douglas Gregor09b69892011-02-10 17:09:37 +00005215 delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable);
Douglas Gregorb473b072010-08-19 00:28:17 +00005216 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
5217}