blob: a7c3fea7d60684cbf2205296e4d34e78eef55a18 [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);
82 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +000083 PARSE_LANGOPT_BENIGN(MSCVersion);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000084 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
85 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
86 PARSE_LANGOPT_BENIGN(CXXOperatorName);
87 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
88 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
89 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian45878032010-02-09 19:31:38 +000090 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +000091 PARSE_LANGOPT_IMPORTANT(AppleKext, diag::warn_pch_apple_kext);
Ted Kremenek1d56c9e2010-12-23 21:35:43 +000092 PARSE_LANGOPT_IMPORTANT(ObjCDefaultSynthProperties,
93 diag::warn_pch_objc_auto_properties);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +000094 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian62c56022010-04-22 21:01:59 +000095 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000096 PARSE_LANGOPT_BENIGN(PascalStrings);
97 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump11289f42009-09-09 15:08:12 +000098 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000099 diag::warn_pch_lax_vector_conversions);
Nate Begeman9d905792009-06-25 22:57:40 +0000100 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000101 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Anders Carlssonce8dd3a2011-02-19 23:53:54 +0000102 PARSE_LANGOPT_IMPORTANT(ObjCExceptions, diag::warn_pch_objc_exceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +0000103 PARSE_LANGOPT_IMPORTANT(CXXExceptions, diag::warn_pch_cxx_exceptions);
104 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Douglas Gregordbe39272011-02-01 15:15:22 +0000105 PARSE_LANGOPT_IMPORTANT(MSBitfields, diag::warn_pch_ms_bitfields);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000106 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
107 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
108 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump11289f42009-09-09 15:08:12 +0000109 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000110 diag::warn_pch_thread_safe_statics);
Daniel Dunbara77eaeb2009-09-03 04:54:28 +0000111 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000112 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
113 PARSE_LANGOPT_BENIGN(EmitAllDecls);
114 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattner51924e512010-06-26 21:25:03 +0000115 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump11289f42009-09-09 15:08:12 +0000116 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000117 diag::warn_pch_heinous_extensions);
118 // FIXME: Most of the options below are benign if the macro wasn't
119 // used. Unfortunately, this means that a PCH compiled without
120 // optimization can't be used with optimization turned on, even
121 // though the only thing that changes is whether __OPTIMIZE__ was
122 // defined... but if __OPTIMIZE__ never showed up in the header, it
123 // doesn't matter. We could consider making this some special kind
124 // of check.
125 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
126 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
127 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
128 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
129 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
130 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
131 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
132 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsoned4e2952009-11-05 20:14:16 +0000133 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +0000134 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000135 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump11289f42009-09-09 15:08:12 +0000136 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000137 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
138 return true;
139 }
140 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000141 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
142 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000143 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman9d905792009-06-25 22:57:40 +0000144 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Peter Collingbourne546d0792010-12-01 19:14:57 +0000145 PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda);
Mike Stumpd9546382009-12-12 01:27:46 +0000146 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbar143021e2009-09-21 04:16:19 +0000147 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000148 PARSE_LANGOPT_BENIGN(SpellChecking);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000149 PARSE_LANGOPT_BENIGN(DefaultFPContract);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +0000150#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000151#undef PARSE_LANGOPT_BENIGN
152
153 return false;
154}
155
Daniel Dunbar20a682d2009-11-11 00:52:11 +0000156bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
157 if (Triple == PP.getTargetInfo().getTriple().str())
158 return false;
159
160 Reader.Diag(diag::warn_pch_target_triple)
161 << Triple << PP.getTargetInfo().getTriple().str();
162 return true;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000163}
164
Benjamin Kramer90b5b682010-11-25 18:29:30 +0000165namespace {
166 struct EmptyStringRef {
167 bool operator ()(llvm::StringRef r) const { return r.empty(); }
168 };
169 struct EmptyBlock {
170 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
171 };
172}
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000173
174static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
175 PCHPredefinesBlocks R) {
176 // First, sum up the lengths.
177 unsigned LL = 0, RL = 0;
178 for (unsigned I = 0, N = L.size(); I != N; ++I) {
179 LL += L[I].size();
180 }
181 for (unsigned I = 0, N = R.size(); I != N; ++I) {
182 RL += R[I].Data.size();
183 }
184 if (LL != RL)
185 return false;
186 if (LL == 0 && RL == 0)
187 return true;
188
189 // Kick out empty parts, they confuse the algorithm below.
190 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
191 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
192
193 // Do it the hard way. At this point, both vectors must be non-empty.
194 llvm::StringRef LR = L[0], RR = R[0].Data;
195 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbar01ad0a72010-07-16 00:00:11 +0000196 (void) RN;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000197 for (;;) {
198 // Compare the current pieces.
199 if (LR.size() == RR.size()) {
200 // If they're the same length, it's pretty easy.
201 if (LR != RR)
202 return false;
203 // Both pieces are done, advance.
204 ++LI;
205 ++RI;
206 // If either string is done, they're both done, since they're the same
207 // length.
208 if (LI == LN) {
209 assert(RI == RN && "Strings not the same length after all?");
210 return true;
211 }
212 LR = L[LI];
213 RR = R[RI].Data;
214 } else if (LR.size() < RR.size()) {
215 // Right piece is longer.
216 if (!RR.startswith(LR))
217 return false;
218 ++LI;
219 assert(LI != LN && "Strings not the same length after all?");
220 RR = RR.substr(LR.size());
221 LR = L[LI];
222 } else {
223 // Left piece is longer.
224 if (!LR.startswith(RR))
225 return false;
226 ++RI;
227 assert(RI != RN && "Strings not the same length after all?");
228 LR = LR.substr(RR.size());
229 RR = R[RI].Data;
230 }
231 }
232}
233
234static std::pair<FileID, llvm::StringRef::size_type>
235FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
236 std::pair<FileID, llvm::StringRef::size_type> Res;
237 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
238 Res.second = Buffers[I].Data.find(MacroDef);
239 if (Res.second != llvm::StringRef::npos) {
240 Res.first = Buffers[I].BufferID;
241 break;
242 }
243 }
244 return Res;
245}
246
247bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000248 llvm::StringRef OriginalFileName,
Nick Lewycky36079892011-02-23 21:16:44 +0000249 std::string &SuggestedPredefines,
250 FileManager &FileMgr) {
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000251 // We are in the context of an implicit include, so the predefines buffer will
252 // have a #include entry for the PCH file itself (as normalized by the
253 // preprocessor initialization). Find it and skip over it in the checking
254 // below.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000255 llvm::SmallString<256> PCHInclude;
256 PCHInclude += "#include \"";
Nick Lewycky36079892011-02-23 21:16:44 +0000257 PCHInclude += NormalizeDashIncludePath(OriginalFileName, FileMgr);
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000258 PCHInclude += "\"\n";
259 std::pair<llvm::StringRef,llvm::StringRef> Split =
260 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
261 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000262 if (Left == PP.getPredefines()) {
263 Error("Missing PCH include entry!");
264 return true;
265 }
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000266
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000267 // If the concatenation of all the PCH buffers is equal to the adjusted
268 // command line, we're done.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000269 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
270 CommandLine.push_back(Left);
271 CommandLine.push_back(Right);
272 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000273 return false;
274
275 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000276
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000277 // The predefines buffers are different. Determine what the differences are,
278 // and whether they require us to reject the PCH file.
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000279 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000280 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
281 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000282
283 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
284 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000285
286 // Pick out implicit #includes after the PCH and don't consider them for
287 // validation; we will insert them into SuggestedPredefines so that the
288 // preprocessor includes them.
289 std::string IncludesAfterPCH;
290 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
291 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
292 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
293 if (AfterPCHLines[i].startswith("#include ")) {
294 IncludesAfterPCH += AfterPCHLines[i];
295 IncludesAfterPCH += '\n';
296 } else {
297 CmdLineLines.push_back(AfterPCHLines[i]);
298 }
299 }
300
301 // Make sure we add the includes last into SuggestedPredefines before we
302 // exit this function.
303 struct AddIncludesRAII {
304 std::string &SuggestedPredefines;
305 std::string &IncludesAfterPCH;
306
307 AddIncludesRAII(std::string &SuggestedPredefines,
308 std::string &IncludesAfterPCH)
309 : SuggestedPredefines(SuggestedPredefines),
310 IncludesAfterPCH(IncludesAfterPCH) { }
311 ~AddIncludesRAII() {
312 SuggestedPredefines += IncludesAfterPCH;
313 }
314 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000315
Daniel Dunbar499baed2009-11-11 05:26:28 +0000316 // Sort both sets of predefined buffer lines, since we allow some extra
317 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000318 std::sort(CmdLineLines.begin(), CmdLineLines.end());
319 std::sort(PCHLines.begin(), PCHLines.end());
320
Daniel Dunbar499baed2009-11-11 05:26:28 +0000321 // Determine which predefines that were used to build the PCH file are missing
322 // from the command line.
323 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000324 std::set_difference(PCHLines.begin(), PCHLines.end(),
325 CmdLineLines.begin(), CmdLineLines.end(),
326 std::back_inserter(MissingPredefines));
327
328 bool MissingDefines = false;
329 bool ConflictingDefines = false;
330 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000331 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000332 if (Missing.startswith("#include ")) {
333 // An -include was specified when generating the PCH; it is included in
334 // the PCH, just ignore it.
335 continue;
336 }
Daniel Dunbar499baed2009-11-11 05:26:28 +0000337 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000338 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
339 return true;
340 }
Mike Stump11289f42009-09-09 15:08:12 +0000341
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000342 // This is a macro definition. Determine the name of the macro we're
343 // defining.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000344 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000345 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000346 = Missing.find_first_of("( \n\r", StartOfMacroName);
347 assert(EndOfMacroName != std::string::npos &&
348 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000349 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000350
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000351 // Determine whether this macro was given a different definition on the
352 // command line.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000353 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000354 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000355 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000356 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
357 MacroDefStart);
358 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000359 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000360 // Different macro; we're done.
361 ConflictPos = CmdLineLines.end();
Mike Stump11289f42009-09-09 15:08:12 +0000362 break;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000363 }
Mike Stump11289f42009-09-09 15:08:12 +0000364
365 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000366 "Invalid #define in predefines buffer?");
Mike Stump11289f42009-09-09 15:08:12 +0000367 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000368 (*ConflictPos)[MacroDefLen] != '(')
369 continue; // Longer macro name; keep trying.
Mike Stump11289f42009-09-09 15:08:12 +0000370
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000371 // We found a conflicting macro definition.
372 break;
373 }
Mike Stump11289f42009-09-09 15:08:12 +0000374
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000375 if (ConflictPos != CmdLineLines.end()) {
376 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
377 << MacroName;
378
379 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000380 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
381 FindMacro(Buffers, Missing);
382 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
383 SourceLocation PCHMissingLoc =
384 SourceMgr.getLocForStartOfFile(MacroLoc.first)
385 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar499baed2009-11-11 05:26:28 +0000386 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000387
388 ConflictingDefines = true;
389 continue;
390 }
Mike Stump11289f42009-09-09 15:08:12 +0000391
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000392 // If the macro doesn't conflict, then we'll just pick up the macro
393 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000394 if (ConflictingDefines)
395 continue; // Don't complain if there are already conflicting defs
Mike Stump11289f42009-09-09 15:08:12 +0000396
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000397 if (!MissingDefines) {
398 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
399 MissingDefines = true;
400 }
401
402 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000403 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
404 FindMacro(Buffers, Missing);
405 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
406 SourceLocation PCHMissingLoc =
407 SourceMgr.getLocForStartOfFile(MacroLoc.first)
408 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000409 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
410 }
Mike Stump11289f42009-09-09 15:08:12 +0000411
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000412 if (ConflictingDefines)
413 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000414
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000415 // Determine what predefines were introduced based on command-line
416 // parameters that were not present when building the PCH
417 // file. Extra #defines are okay, so long as the identifiers being
418 // defined were not used within the precompiled header.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000419 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000420 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
421 PCHLines.begin(), PCHLines.end(),
Mike Stump11289f42009-09-09 15:08:12 +0000422 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000423 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000424 llvm::StringRef &Extra = ExtraPredefines[I];
425 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000426 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
427 return true;
428 }
429
430 // This is an extra macro definition. Determine the name of the
431 // macro we're defining.
432 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000433 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000434 = Extra.find_first_of("( \n\r", StartOfMacroName);
435 assert(EndOfMacroName != std::string::npos &&
436 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000437 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000438
439 // Check whether this name was used somewhere in the PCH file. If
440 // so, defining it as a macro could change behavior, so we reject
441 // the PCH file.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000442 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar045c92f2009-11-11 00:52:00 +0000443 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000444 return true;
445 }
446
447 // Add this definition to the suggested predefines buffer.
448 SuggestedPredefines += Extra;
449 SuggestedPredefines += '\n';
450 }
451
452 // If we get here, it's because the predefines buffer had compatible
453 // contents. Accept the PCH file.
454 return false;
455}
456
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000457void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
458 unsigned ID) {
459 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
460 ++NumHeaderInfos;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000461}
462
463void PCHValidator::ReadCounter(unsigned Value) {
464 PP.setCounterValue(Value);
465}
466
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000467//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +0000468// AST reader implementation
Douglas Gregora868bbd2009-04-21 22:25:48 +0000469//===----------------------------------------------------------------------===//
470
Sebastian Redl07a89a82010-07-30 00:29:29 +0000471void
Sebastian Redl3e31c722010-08-18 23:56:56 +0000472ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redl07a89a82010-07-30 00:29:29 +0000473 DeserializationListener = Listener;
Sebastian Redl07a89a82010-07-30 00:29:29 +0000474}
475
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000476
Douglas Gregora868bbd2009-04-21 22:25:48 +0000477namespace {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000478class ASTSelectorLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000479 ASTReader &Reader;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000480
481public:
Sebastian Redl834bb972010-08-04 17:20:04 +0000482 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +0000483 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +0000484 ObjCMethodList Instance, Factory;
485 };
Douglas Gregorc78d3462009-04-24 21:10:55 +0000486
487 typedef Selector external_key_type;
488 typedef external_key_type internal_key_type;
489
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000490 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump11289f42009-09-09 15:08:12 +0000491
Douglas Gregorc78d3462009-04-24 21:10:55 +0000492 static bool EqualKey(const internal_key_type& a,
493 const internal_key_type& b) {
494 return a == b;
495 }
Mike Stump11289f42009-09-09 15:08:12 +0000496
Douglas Gregorc78d3462009-04-24 21:10:55 +0000497 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +0000498 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000499 }
Mike Stump11289f42009-09-09 15:08:12 +0000500
Douglas Gregorc78d3462009-04-24 21:10:55 +0000501 // This hopefully will just get inlined and removed by the optimizer.
502 static const internal_key_type&
503 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000504
Douglas Gregorc78d3462009-04-24 21:10:55 +0000505 static std::pair<unsigned, unsigned>
506 ReadKeyDataLength(const unsigned char*& d) {
507 using namespace clang::io;
508 unsigned KeyLen = ReadUnalignedLE16(d);
509 unsigned DataLen = ReadUnalignedLE16(d);
510 return std::make_pair(KeyLen, DataLen);
511 }
Mike Stump11289f42009-09-09 15:08:12 +0000512
Douglas Gregor95c13f52009-04-25 17:48:32 +0000513 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000514 using namespace clang::io;
Chris Lattner8575daa2009-04-27 21:45:14 +0000515 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000516 unsigned N = ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000517 IdentifierInfo *FirstII
Douglas Gregorc78d3462009-04-24 21:10:55 +0000518 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
519 if (N == 0)
520 return SelTable.getNullarySelector(FirstII);
521 else if (N == 1)
522 return SelTable.getUnarySelector(FirstII);
523
524 llvm::SmallVector<IdentifierInfo *, 16> Args;
525 Args.push_back(FirstII);
526 for (unsigned I = 1; I != N; ++I)
527 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
528
Douglas Gregor038c3382009-05-22 22:45:36 +0000529 return SelTable.getSelector(N, Args.data());
Douglas Gregorc78d3462009-04-24 21:10:55 +0000530 }
Mike Stump11289f42009-09-09 15:08:12 +0000531
Douglas Gregorc78d3462009-04-24 21:10:55 +0000532 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
533 using namespace clang::io;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000534
535 data_type Result;
536
Sebastian Redl834bb972010-08-04 17:20:04 +0000537 Result.ID = ReadUnalignedLE32(d);
538 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
539 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
540
Douglas Gregorc78d3462009-04-24 21:10:55 +0000541 // Load instance methods
542 ObjCMethodList *Prev = 0;
543 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000544 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000545 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000546 if (!Result.Instance.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000547 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000548 Result.Instance.Method = Method;
549 Prev = &Result.Instance;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000550 continue;
551 }
552
Ted Kremenekda4abf12010-02-11 00:53:01 +0000553 ObjCMethodList *Mem =
554 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
555 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000556 Prev = Prev->Next;
557 }
558
559 // Load factory methods
560 Prev = 0;
561 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000562 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000563 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000564 if (!Result.Factory.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000565 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000566 Result.Factory.Method = Method;
567 Prev = &Result.Factory;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000568 continue;
569 }
570
Ted Kremenekda4abf12010-02-11 00:53:01 +0000571 ObjCMethodList *Mem =
572 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
573 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000574 Prev = Prev->Next;
575 }
576
577 return Result;
578 }
579};
Mike Stump11289f42009-09-09 15:08:12 +0000580
581} // end anonymous namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +0000582
583/// \brief The on-disk hash table used for the global method pool.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000584typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
585 ASTSelectorLookupTable;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000586
Sebastian Redl2c373b92010-10-05 15:59:54 +0000587namespace clang {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000588class ASTIdentifierLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000589 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +0000590 ASTReader::PerFileData &F;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000591
592 // If we know the IdentifierInfo in advance, it is here and we will
593 // not build a new one. Used when deserializing information about an
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000594 // identifier that was constructed before the AST file was read.
Douglas Gregora868bbd2009-04-21 22:25:48 +0000595 IdentifierInfo *KnownII;
596
597public:
598 typedef IdentifierInfo * data_type;
599
600 typedef const std::pair<const char*, unsigned> external_key_type;
601
602 typedef external_key_type internal_key_type;
603
Sebastian Redl2c373b92010-10-05 15:59:54 +0000604 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000605 IdentifierInfo *II = 0)
Sebastian Redl2c373b92010-10-05 15:59:54 +0000606 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump11289f42009-09-09 15:08:12 +0000607
Douglas Gregora868bbd2009-04-21 22:25:48 +0000608 static bool EqualKey(const internal_key_type& a,
609 const internal_key_type& b) {
610 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
611 : false;
612 }
Mike Stump11289f42009-09-09 15:08:12 +0000613
Douglas Gregora868bbd2009-04-21 22:25:48 +0000614 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000615 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregora868bbd2009-04-21 22:25:48 +0000616 }
Mike Stump11289f42009-09-09 15:08:12 +0000617
Douglas Gregora868bbd2009-04-21 22:25:48 +0000618 // This hopefully will just get inlined and removed by the optimizer.
619 static const internal_key_type&
620 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000621
Douglas Gregor57756ea2010-10-14 22:11:03 +0000622 // This hopefully will just get inlined and removed by the optimizer.
623 static const external_key_type&
624 GetExternalKey(const internal_key_type& x) { return x; }
625
Douglas Gregora868bbd2009-04-21 22:25:48 +0000626 static std::pair<unsigned, unsigned>
627 ReadKeyDataLength(const unsigned char*& d) {
628 using namespace clang::io;
Douglas Gregor6b7bf5a2009-04-25 20:26:24 +0000629 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor5287b4e2009-04-25 21:04:17 +0000630 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000631 return std::make_pair(KeyLen, DataLen);
632 }
Mike Stump11289f42009-09-09 15:08:12 +0000633
Douglas Gregora868bbd2009-04-21 22:25:48 +0000634 static std::pair<const char*, unsigned>
635 ReadKey(const unsigned char* d, unsigned n) {
636 assert(n >= 2 && d[n-1] == '\0');
637 return std::make_pair((const char*) d, n-1);
638 }
Mike Stump11289f42009-09-09 15:08:12 +0000639
640 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregora868bbd2009-04-21 22:25:48 +0000641 const unsigned char* d,
642 unsigned DataLen) {
643 using namespace clang::io;
Sebastian Redl539c5062010-08-18 23:57:32 +0000644 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000645 bool IsInteresting = ID & 0x01;
646
647 // Wipe out the "is interesting" bit.
648 ID = ID >> 1;
649
650 if (!IsInteresting) {
Sebastian Redl98912122010-07-27 23:01:28 +0000651 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregor1d583f22009-04-28 21:18:29 +0000652 // and associate it with the persistent ID.
653 IdentifierInfo *II = KnownII;
654 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000655 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000656 Reader.SetIdentifierInfo(ID, II);
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000657 II->setIsFromAST();
Douglas Gregor1d583f22009-04-28 21:18:29 +0000658 return II;
659 }
660
Douglas Gregorb9256522009-04-28 21:32:13 +0000661 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000662 bool CPlusPlusOperatorKeyword = Bits & 0x01;
663 Bits >>= 1;
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000664 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
665 Bits >>= 1;
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000666 bool Poisoned = Bits & 0x01;
667 Bits >>= 1;
668 bool ExtensionToken = Bits & 0x01;
669 Bits >>= 1;
670 bool hasMacroDefinition = Bits & 0x01;
671 Bits >>= 1;
672 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
673 Bits >>= 10;
Mike Stump11289f42009-09-09 15:08:12 +0000674
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000675 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorb9256522009-04-28 21:32:13 +0000676 DataLen -= 6;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000677
678 // Build the IdentifierInfo itself and link the identifier ID with
679 // the new IdentifierInfo.
680 IdentifierInfo *II = KnownII;
681 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000682 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000683 Reader.SetIdentifierInfo(ID, II);
684
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000685 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000686 // Token IDs are read-only.
687 if (HasRevertedTokenIDToIdentifier)
688 II->RevertTokenIDToIdentifier();
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000689 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump11289f42009-09-09 15:08:12 +0000690 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000691 "Incorrect extension token flag");
692 (void)ExtensionToken;
693 II->setIsPoisoned(Poisoned);
694 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
695 "Incorrect C++ operator keyword flag");
696 (void)CPlusPlusOperatorKeyword;
697
Douglas Gregorc3366a52009-04-21 23:56:24 +0000698 // If this identifier is a macro, deserialize the macro
699 // definition.
700 if (hasMacroDefinition) {
Douglas Gregorb9256522009-04-28 21:32:13 +0000701 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor5ef9e332010-10-30 00:23:06 +0000702 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregorb9256522009-04-28 21:32:13 +0000703 DataLen -= 4;
Douglas Gregorc3366a52009-04-21 23:56:24 +0000704 }
Douglas Gregora868bbd2009-04-21 22:25:48 +0000705
706 // Read all of the declarations visible at global scope with this
707 // name.
Chris Lattner1d728882009-04-27 22:17:41 +0000708 if (Reader.getContext() == 0) return II;
Douglas Gregor1342e842009-07-06 18:54:52 +0000709 if (DataLen > 0) {
710 llvm::SmallVector<uint32_t, 4> DeclIDs;
711 for (; DataLen > 0; DataLen -= 4)
712 DeclIDs.push_back(ReadUnalignedLE32(d));
713 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000714 }
Mike Stump11289f42009-09-09 15:08:12 +0000715
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000716 II->setIsFromAST();
Douglas Gregora868bbd2009-04-21 22:25:48 +0000717 return II;
718 }
719};
Mike Stump11289f42009-09-09 15:08:12 +0000720
721} // end anonymous namespace
Douglas Gregora868bbd2009-04-21 22:25:48 +0000722
723/// \brief The on-disk hash table used to contain information about
724/// all of the identifiers in the program.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000725typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
726 ASTIdentifierLookupTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000727
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000728namespace {
729class ASTDeclContextNameLookupTrait {
730 ASTReader &Reader;
731
732public:
733 /// \brief Pair of begin/end iterators for DeclIDs.
734 typedef std::pair<DeclID *, DeclID *> data_type;
735
736 /// \brief Special internal key for declaration names.
737 /// The hash table creates keys for comparison; we do not create
738 /// a DeclarationName for the internal key to avoid deserializing types.
739 struct DeclNameKey {
740 DeclarationName::NameKind Kind;
741 uint64_t Data;
742 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
743 };
744
745 typedef DeclarationName external_key_type;
746 typedef DeclNameKey internal_key_type;
747
748 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
749
750 static bool EqualKey(const internal_key_type& a,
751 const internal_key_type& b) {
752 return a.Kind == b.Kind && a.Data == b.Data;
753 }
754
755 unsigned ComputeHash(const DeclNameKey &Key) const {
756 llvm::FoldingSetNodeID ID;
757 ID.AddInteger(Key.Kind);
758
759 switch (Key.Kind) {
760 case DeclarationName::Identifier:
761 case DeclarationName::CXXLiteralOperatorName:
762 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
763 break;
764 case DeclarationName::ObjCZeroArgSelector:
765 case DeclarationName::ObjCOneArgSelector:
766 case DeclarationName::ObjCMultiArgSelector:
767 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
768 break;
769 case DeclarationName::CXXConstructorName:
770 case DeclarationName::CXXDestructorName:
771 case DeclarationName::CXXConversionFunctionName:
772 ID.AddInteger((TypeID)Key.Data);
773 break;
774 case DeclarationName::CXXOperatorName:
775 ID.AddInteger((OverloadedOperatorKind)Key.Data);
776 break;
777 case DeclarationName::CXXUsingDirective:
778 break;
779 }
780
781 return ID.ComputeHash();
782 }
783
784 internal_key_type GetInternalKey(const external_key_type& Name) const {
785 DeclNameKey Key;
786 Key.Kind = Name.getNameKind();
787 switch (Name.getNameKind()) {
788 case DeclarationName::Identifier:
789 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
790 break;
791 case DeclarationName::ObjCZeroArgSelector:
792 case DeclarationName::ObjCOneArgSelector:
793 case DeclarationName::ObjCMultiArgSelector:
794 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
795 break;
796 case DeclarationName::CXXConstructorName:
797 case DeclarationName::CXXDestructorName:
798 case DeclarationName::CXXConversionFunctionName:
799 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
800 break;
801 case DeclarationName::CXXOperatorName:
802 Key.Data = Name.getCXXOverloadedOperator();
803 break;
804 case DeclarationName::CXXLiteralOperatorName:
805 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
806 break;
807 case DeclarationName::CXXUsingDirective:
808 break;
809 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000810
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000811 return Key;
812 }
813
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000814 external_key_type GetExternalKey(const internal_key_type& Key) const {
815 ASTContext *Context = Reader.getContext();
816 switch (Key.Kind) {
817 case DeclarationName::Identifier:
818 return DeclarationName((IdentifierInfo*)Key.Data);
819
820 case DeclarationName::ObjCZeroArgSelector:
821 case DeclarationName::ObjCOneArgSelector:
822 case DeclarationName::ObjCMultiArgSelector:
823 return DeclarationName(Selector(Key.Data));
824
825 case DeclarationName::CXXConstructorName:
826 return Context->DeclarationNames.getCXXConstructorName(
827 Context->getCanonicalType(Reader.GetType(Key.Data)));
828
829 case DeclarationName::CXXDestructorName:
830 return Context->DeclarationNames.getCXXDestructorName(
831 Context->getCanonicalType(Reader.GetType(Key.Data)));
832
833 case DeclarationName::CXXConversionFunctionName:
834 return Context->DeclarationNames.getCXXConversionFunctionName(
835 Context->getCanonicalType(Reader.GetType(Key.Data)));
836
837 case DeclarationName::CXXOperatorName:
838 return Context->DeclarationNames.getCXXOperatorName(
839 (OverloadedOperatorKind)Key.Data);
840
841 case DeclarationName::CXXLiteralOperatorName:
842 return Context->DeclarationNames.getCXXLiteralOperatorName(
843 (IdentifierInfo*)Key.Data);
844
845 case DeclarationName::CXXUsingDirective:
846 return DeclarationName::getUsingDirectiveName();
847 }
848
849 llvm_unreachable("Invalid Name Kind ?");
850 }
851
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000852 static std::pair<unsigned, unsigned>
853 ReadKeyDataLength(const unsigned char*& d) {
854 using namespace clang::io;
855 unsigned KeyLen = ReadUnalignedLE16(d);
856 unsigned DataLen = ReadUnalignedLE16(d);
857 return std::make_pair(KeyLen, DataLen);
858 }
859
860 internal_key_type ReadKey(const unsigned char* d, unsigned) {
861 using namespace clang::io;
862
863 DeclNameKey Key;
864 Key.Kind = (DeclarationName::NameKind)*d++;
865 switch (Key.Kind) {
866 case DeclarationName::Identifier:
867 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
868 break;
869 case DeclarationName::ObjCZeroArgSelector:
870 case DeclarationName::ObjCOneArgSelector:
871 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000872 Key.Data =
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000873 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
874 break;
875 case DeclarationName::CXXConstructorName:
876 case DeclarationName::CXXDestructorName:
877 case DeclarationName::CXXConversionFunctionName:
878 Key.Data = ReadUnalignedLE32(d); // TypeID
879 break;
880 case DeclarationName::CXXOperatorName:
881 Key.Data = *d++; // OverloadedOperatorKind
882 break;
883 case DeclarationName::CXXLiteralOperatorName:
884 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
885 break;
886 case DeclarationName::CXXUsingDirective:
887 break;
888 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000889
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000890 return Key;
891 }
892
893 data_type ReadData(internal_key_type, const unsigned char* d,
894 unsigned DataLen) {
895 using namespace clang::io;
896 unsigned NumDecls = ReadUnalignedLE16(d);
897 DeclID *Start = (DeclID *)d;
898 return std::make_pair(Start, Start + NumDecls);
899 }
900};
901
902} // end anonymous namespace
903
904/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
905typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
906 ASTDeclContextNameLookupTable;
907
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000908bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
909 const std::pair<uint64_t, uint64_t> &Offsets,
910 DeclContextInfo &Info) {
911 SavedStreamPosition SavedPosition(Cursor);
912 // First the lexical decls.
913 if (Offsets.first != 0) {
914 Cursor.JumpToBit(Offsets.first);
915
916 RecordData Record;
917 const char *Blob;
918 unsigned BlobLen;
919 unsigned Code = Cursor.ReadCode();
920 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
921 if (RecCode != DECL_CONTEXT_LEXICAL) {
922 Error("Expected lexical block");
923 return true;
924 }
925
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000926 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
927 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000928 } else {
929 Info.LexicalDecls = 0;
930 Info.NumLexicalDecls = 0;
931 }
932
933 // Now the lookup table.
934 if (Offsets.second != 0) {
935 Cursor.JumpToBit(Offsets.second);
936
937 RecordData Record;
938 const char *Blob;
939 unsigned BlobLen;
940 unsigned Code = Cursor.ReadCode();
941 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
942 if (RecCode != DECL_CONTEXT_VISIBLE) {
943 Error("Expected visible lookup table block");
944 return true;
945 }
946 Info.NameLookupTableData
947 = ASTDeclContextNameLookupTable::Create(
948 (const unsigned char *)Blob + Record[0],
949 (const unsigned char *)Blob,
950 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl9d8f58b2010-08-24 00:50:00 +0000951 } else {
952 Info.NameLookupTableData = 0;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000953 }
954
955 return false;
956}
957
Sebastian Redl2c499f62010-08-18 23:56:43 +0000958void ASTReader::Error(const char *Msg) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000959 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000960}
961
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000962/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000963bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000964 if (Listener)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000965 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000966 ActualOriginalFileName,
Nick Lewycky36079892011-02-23 21:16:44 +0000967 SuggestedPredefines,
968 FileMgr);
Douglas Gregorc379c072009-04-28 18:58:38 +0000969 return false;
Douglas Gregor92863e42009-04-10 23:10:45 +0000970}
971
Douglas Gregorc5046832009-04-27 18:38:38 +0000972//===----------------------------------------------------------------------===//
973// Source Manager Deserialization
974//===----------------------------------------------------------------------===//
975
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000976/// \brief Read the line table in the source manager block.
Sebastian Redl2c373b92010-10-05 15:59:54 +0000977/// \returns true if there was an error.
978bool ASTReader::ParseLineTable(PerFileData &F,
979 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000980 unsigned Idx = 0;
981 LineTableInfo &LineTable = SourceMgr.getLineTable();
982
983 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +0000984 std::map<int, int> FileIDs;
985 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000986 // Extract the file name
987 unsigned FilenameLen = Record[Idx++];
988 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
989 Idx += FilenameLen;
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000990 MaybeAddSystemRootToFilename(Filename);
Mike Stump11289f42009-09-09 15:08:12 +0000991 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregora8854652009-04-13 17:12:42 +0000992 Filename.size());
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000993 }
994
995 // Parse the line entries
996 std::vector<LineEntry> Entries;
997 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000998 int FID = Record[Idx++];
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000999
1000 // Extract the line entries
1001 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +00001002 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001003 Entries.clear();
1004 Entries.reserve(NumEntries);
1005 for (unsigned I = 0; I != NumEntries; ++I) {
1006 unsigned FileOffset = Record[Idx++];
1007 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +00001008 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +00001009 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001010 = (SrcMgr::CharacteristicKind)Record[Idx++];
1011 unsigned IncludeOffset = Record[Idx++];
1012 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1013 FileKind, IncludeOffset));
1014 }
1015 LineTable.AddEntry(FID, Entries);
1016 }
1017
1018 return false;
1019}
1020
Douglas Gregorc5046832009-04-27 18:38:38 +00001021namespace {
1022
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001023class ASTStatData {
Douglas Gregorc5046832009-04-27 18:38:38 +00001024public:
Douglas Gregorc5046832009-04-27 18:38:38 +00001025 const ino_t ino;
1026 const dev_t dev;
1027 const mode_t mode;
1028 const time_t mtime;
1029 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +00001030
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001031 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner2a6fa472010-11-23 19:28:12 +00001032 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregorc5046832009-04-27 18:38:38 +00001033};
1034
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001035class ASTStatLookupTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001036 public:
1037 typedef const char *external_key_type;
1038 typedef const char *internal_key_type;
1039
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001040 typedef ASTStatData data_type;
Douglas Gregorc5046832009-04-27 18:38:38 +00001041
1042 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001043 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001044 }
1045
1046 static internal_key_type GetInternalKey(const char *path) { return path; }
1047
1048 static bool EqualKey(internal_key_type a, internal_key_type b) {
1049 return strcmp(a, b) == 0;
1050 }
1051
1052 static std::pair<unsigned, unsigned>
1053 ReadKeyDataLength(const unsigned char*& d) {
1054 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1055 unsigned DataLen = (unsigned) *d++;
1056 return std::make_pair(KeyLen + 1, DataLen);
1057 }
1058
1059 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1060 return (const char *)d;
1061 }
1062
1063 static data_type ReadData(const internal_key_type, const unsigned char *d,
1064 unsigned /*DataLen*/) {
1065 using namespace clang::io;
1066
Douglas Gregorc5046832009-04-27 18:38:38 +00001067 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1068 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1069 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +00001070 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +00001071 off_t size = (off_t) ReadUnalignedLE64(d);
1072 return data_type(ino, dev, mode, mtime, size);
1073 }
1074};
1075
1076/// \brief stat() cache for precompiled headers.
1077///
1078/// This cache is very similar to the stat cache used by pretokenized
1079/// headers.
Chris Lattner226efd32010-11-23 19:19:34 +00001080class ASTStatCache : public FileSystemStatCache {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001081 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregorc5046832009-04-27 18:38:38 +00001082 CacheTy *Cache;
1083
1084 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +00001085public:
Chris Lattner2a6fa472010-11-23 19:28:12 +00001086 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1087 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +00001088 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1089 Cache = CacheTy::Create(Buckets, Base);
1090 }
1091
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001092 ~ASTStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +00001093
Chris Lattnerdd278432010-11-23 21:17:56 +00001094 LookupResult getStat(const char *Path, struct stat &StatBuf,
1095 int *FileDescriptor) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001096 // Do the lookup for the file's data in the AST file.
Chris Lattner226efd32010-11-23 19:19:34 +00001097 CacheTy::iterator I = Cache->find(Path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001098
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001099 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregorc5046832009-04-27 18:38:38 +00001100 if (I == Cache->end()) {
1101 ++NumStatMisses;
Chris Lattnerdd278432010-11-23 21:17:56 +00001102 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregorc5046832009-04-27 18:38:38 +00001103 }
Mike Stump11289f42009-09-09 15:08:12 +00001104
Douglas Gregorc5046832009-04-27 18:38:38 +00001105 ++NumStatHits;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001106 ASTStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001107
Chris Lattner226efd32010-11-23 19:19:34 +00001108 StatBuf.st_ino = Data.ino;
1109 StatBuf.st_dev = Data.dev;
1110 StatBuf.st_mtime = Data.mtime;
1111 StatBuf.st_mode = Data.mode;
1112 StatBuf.st_size = Data.size;
Chris Lattner8f0583d2010-11-23 20:05:15 +00001113 return CacheExists;
Douglas Gregorc5046832009-04-27 18:38:38 +00001114 }
1115};
1116} // end anonymous namespace
1117
1118
Sebastian Redl393f8b72010-07-19 20:52:06 +00001119/// \brief Read a source manager block
Sebastian Redl2c499f62010-08-18 23:56:43 +00001120ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001121 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +00001122
Sebastian Redl393f8b72010-07-19 20:52:06 +00001123 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001124
Douglas Gregor258ae542009-04-27 06:38:32 +00001125 // Set the source-location entry cursor to the current position in
1126 // the stream. This cursor will be used to read the contents of the
1127 // source manager block initially, and then lazily read
1128 // source-location entries as needed.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001129 SLocEntryCursor = F.Stream;
Douglas Gregor258ae542009-04-27 06:38:32 +00001130
1131 // The stream itself is going to skip over the source manager block.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001132 if (F.Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001133 Error("malformed block record in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001134 return Failure;
1135 }
1136
1137 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001138 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001139 Error("malformed source manager block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001140 return Failure;
1141 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001142
Douglas Gregora7f71a92009-04-10 03:52:48 +00001143 RecordData Record;
1144 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001145 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001146 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001147 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001148 Error("error at end of Source Manager block in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001149 return Failure;
1150 }
Douglas Gregor92863e42009-04-10 23:10:45 +00001151 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001152 }
Mike Stump11289f42009-09-09 15:08:12 +00001153
Douglas Gregora7f71a92009-04-10 03:52:48 +00001154 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1155 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +00001156 SLocEntryCursor.ReadSubBlockID();
1157 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001158 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001159 return Failure;
1160 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001161 continue;
1162 }
Mike Stump11289f42009-09-09 15:08:12 +00001163
Douglas Gregora7f71a92009-04-10 03:52:48 +00001164 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001165 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001166 continue;
1167 }
Mike Stump11289f42009-09-09 15:08:12 +00001168
Douglas Gregora7f71a92009-04-10 03:52:48 +00001169 // Read a record.
1170 const char *BlobStart;
1171 unsigned BlobLen;
1172 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +00001173 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001174 default: // Default behavior: ignore.
1175 break;
1176
Sebastian Redl539c5062010-08-18 23:57:32 +00001177 case SM_LINE_TABLE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00001178 if (ParseLineTable(F, Record))
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001179 return Failure;
Chris Lattner184e65d2009-04-14 23:22:57 +00001180 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00001181
Sebastian Redl539c5062010-08-18 23:57:32 +00001182 case SM_SLOC_FILE_ENTRY:
1183 case SM_SLOC_BUFFER_ENTRY:
1184 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor258ae542009-04-27 06:38:32 +00001185 // Once we hit one of the source location entries, we're done.
1186 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001187 }
1188 }
1189}
1190
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001191/// \brief If a header file is not found at the path that we expect it to be
1192/// and the PCH file was moved from its original location, try to resolve the
1193/// file by assuming that header+PCH were moved together and the header is in
1194/// the same place relative to the PCH.
1195static std::string
1196resolveFileRelativeToOriginalDir(const std::string &Filename,
1197 const std::string &OriginalDir,
1198 const std::string &CurrDir) {
1199 assert(OriginalDir != CurrDir &&
1200 "No point trying to resolve the file if the PCH dir didn't change");
1201 using namespace llvm::sys;
1202 llvm::SmallString<128> filePath(Filename);
1203 fs::make_absolute(filePath);
1204 assert(path::is_absolute(OriginalDir));
1205 llvm::SmallString<128> currPCHPath(CurrDir);
1206
1207 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1208 fileDirE = path::end(path::parent_path(filePath));
1209 path::const_iterator origDirI = path::begin(OriginalDir),
1210 origDirE = path::end(OriginalDir);
1211 // Skip the common path components from filePath and OriginalDir.
1212 while (fileDirI != fileDirE && origDirI != origDirE &&
1213 *fileDirI == *origDirI) {
1214 ++fileDirI;
1215 ++origDirI;
1216 }
1217 for (; origDirI != origDirE; ++origDirI)
1218 path::append(currPCHPath, "..");
1219 path::append(currPCHPath, fileDirI, fileDirE);
1220 path::append(currPCHPath, path::filename(Filename));
1221 return currPCHPath.str();
1222}
1223
Sebastian Redl06750302010-07-20 21:50:20 +00001224/// \brief Get a cursor that's correctly positioned for reading the source
1225/// location entry with the given ID.
Sebastian Redl2c373b92010-10-05 15:59:54 +00001226ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl06750302010-07-20 21:50:20 +00001227 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1228 "SLocCursorForID should only be called for real IDs.");
1229
1230 ID -= 1;
1231 PerFileData *F = 0;
1232 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1233 F = Chain[N - I - 1];
1234 if (ID < F->LocalNumSLocEntries)
1235 break;
1236 ID -= F->LocalNumSLocEntries;
1237 }
1238 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1239
1240 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00001241 return F;
Sebastian Redl06750302010-07-20 21:50:20 +00001242}
1243
Douglas Gregor258ae542009-04-27 06:38:32 +00001244/// \brief Read in the source location entry with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001245ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001246 if (ID == 0)
1247 return Success;
1248
1249 if (ID > TotalNumSLocEntries) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001250 Error("source location entry ID out-of-range for AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001251 return Failure;
1252 }
1253
Sebastian Redl2c373b92010-10-05 15:59:54 +00001254 PerFileData *F = SLocCursorForID(ID);
1255 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001256
Douglas Gregor258ae542009-04-27 06:38:32 +00001257 ++NumSLocEntriesRead;
Douglas Gregor258ae542009-04-27 06:38:32 +00001258 unsigned Code = SLocEntryCursor.ReadCode();
1259 if (Code == llvm::bitc::END_BLOCK ||
1260 Code == llvm::bitc::ENTER_SUBBLOCK ||
1261 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001262 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001263 return Failure;
1264 }
1265
Douglas Gregor258ae542009-04-27 06:38:32 +00001266 RecordData Record;
1267 const char *BlobStart;
1268 unsigned BlobLen;
1269 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1270 default:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001271 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001272 return Failure;
1273
Sebastian Redl539c5062010-08-18 23:57:32 +00001274 case SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001275 std::string Filename(BlobStart, BlobStart + BlobLen);
1276 MaybeAddSystemRootToFilename(Filename);
Chris Lattner5159f612010-11-23 08:35:12 +00001277 const FileEntry *File = FileMgr.getFile(Filename);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001278 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1279 OriginalDir != CurrentDir) {
1280 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1281 OriginalDir,
1282 CurrentDir);
1283 if (!resolved.empty())
1284 File = FileMgr.getFile(resolved);
1285 }
Axel Naumann63fbaed2011-01-27 10:55:51 +00001286 if (File == 0)
1287 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1288 (time_t)Record[5]);
Chris Lattnerd20dc872009-06-15 04:35:16 +00001289 if (File == 0) {
1290 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001291 ErrorStr += Filename;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001292 ErrorStr += "' referenced by AST file";
Chris Lattnerd20dc872009-06-15 04:35:16 +00001293 Error(ErrorStr.c_str());
1294 return Failure;
1295 }
Mike Stump11289f42009-09-09 15:08:12 +00001296
Douglas Gregor09b69892011-02-10 17:09:37 +00001297 if (Record.size() < 6) {
Ted Kremenekabb1ddd2010-03-18 21:23:05 +00001298 Error("source location entry is incorrect");
1299 return Failure;
1300 }
1301
Douglas Gregorce3a8292010-07-27 00:27:13 +00001302 if (!DisableValidation &&
1303 ((off_t)Record[4] != File->getSize()
Douglas Gregor08288f22010-04-09 15:54:22 +00001304#if !defined(LLVM_ON_WIN32)
1305 // In our regression testing, the Windows file system seems to
1306 // have inconsistent modification times that sometimes
1307 // erroneously trigger this error-handling path.
Douglas Gregorce3a8292010-07-27 00:27:13 +00001308 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor08288f22010-04-09 15:54:22 +00001309#endif
Douglas Gregorce3a8292010-07-27 00:27:13 +00001310 )) {
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001311 Diag(diag::err_fe_pch_file_modified)
1312 << Filename;
1313 return Failure;
1314 }
1315
Chris Lattner26b5c192010-11-23 09:19:42 +00001316 FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]),
1317 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor258ae542009-04-27 06:38:32 +00001318 ID, Record[0]);
1319 if (Record[3])
1320 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1321 .setHasLineDirectives();
Douglas Gregor09b69892011-02-10 17:09:37 +00001322
Douglas Gregor258ae542009-04-27 06:38:32 +00001323 break;
1324 }
1325
Sebastian Redl539c5062010-08-18 23:57:32 +00001326 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor258ae542009-04-27 06:38:32 +00001327 const char *Name = BlobStart;
1328 unsigned Offset = Record[0];
1329 unsigned Code = SLocEntryCursor.ReadCode();
1330 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001331 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +00001332 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001333
Sebastian Redl539c5062010-08-18 23:57:32 +00001334 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001335 Error("AST record has invalid code");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001336 return Failure;
1337 }
1338
Douglas Gregor258ae542009-04-27 06:38:32 +00001339 llvm::MemoryBuffer *Buffer
Chris Lattner58c79342010-04-05 22:42:27 +00001340 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1341 Name);
Douglas Gregor258ae542009-04-27 06:38:32 +00001342 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump11289f42009-09-09 15:08:12 +00001343
Douglas Gregore6648fb2009-04-28 20:33:11 +00001344 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001345 PCHPredefinesBlock Block = {
1346 BufferID,
1347 llvm::StringRef(BlobStart, BlobLen - 1)
1348 };
1349 PCHPredefinesBuffers.push_back(Block);
Douglas Gregore6648fb2009-04-28 20:33:11 +00001350 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001351
1352 break;
1353 }
1354
Sebastian Redl539c5062010-08-18 23:57:32 +00001355 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001356 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001357 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001358 ReadSourceLocation(*F, Record[2]),
1359 ReadSourceLocation(*F, Record[3]),
Douglas Gregor258ae542009-04-27 06:38:32 +00001360 Record[4],
1361 ID,
1362 Record[0]);
1363 break;
Mike Stump11289f42009-09-09 15:08:12 +00001364 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001365 }
1366
1367 return Success;
1368}
1369
Chris Lattnere78a6be2009-04-27 01:05:14 +00001370/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1371/// specified cursor. Read the abbreviations that are at the top of the block
1372/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001373bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001374 unsigned BlockID) {
1375 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001376 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001377 return Failure;
1378 }
Mike Stump11289f42009-09-09 15:08:12 +00001379
Chris Lattnere78a6be2009-04-27 01:05:14 +00001380 while (true) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001381 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattnere78a6be2009-04-27 01:05:14 +00001382 unsigned Code = Cursor.ReadCode();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001383
Chris Lattnere78a6be2009-04-27 01:05:14 +00001384 // We expect all abbrevs to be at the start of the block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001385 if (Code != llvm::bitc::DEFINE_ABBREV) {
1386 Cursor.JumpToBit(Offset);
Chris Lattnere78a6be2009-04-27 01:05:14 +00001387 return false;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001388 }
Chris Lattnere78a6be2009-04-27 01:05:14 +00001389 Cursor.ReadAbbrevRecord();
1390 }
1391}
1392
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001393PreprocessedEntity *ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001394 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregor796d76a2010-10-20 22:00:55 +00001395 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump11289f42009-09-09 15:08:12 +00001396
Douglas Gregorc3366a52009-04-21 23:56:24 +00001397 // Keep track of where we are in the stream, then jump back there
1398 // after reading this macro.
1399 SavedStreamPosition SavedPosition(Stream);
1400
1401 Stream.JumpToBit(Offset);
1402 RecordData Record;
1403 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1404 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001405
Douglas Gregorc3366a52009-04-21 23:56:24 +00001406 while (true) {
1407 unsigned Code = Stream.ReadCode();
1408 switch (Code) {
1409 case llvm::bitc::END_BLOCK:
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001410 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001411
1412 case llvm::bitc::ENTER_SUBBLOCK:
1413 // No known subblocks, always skip them.
1414 Stream.ReadSubBlockID();
1415 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001416 Error("malformed block record in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001417 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001418 }
1419 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001420
Douglas Gregorc3366a52009-04-21 23:56:24 +00001421 case llvm::bitc::DEFINE_ABBREV:
1422 Stream.ReadAbbrevRecord();
1423 continue;
1424 default: break;
1425 }
1426
1427 // Read a record.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001428 const char *BlobStart = 0;
1429 unsigned BlobLen = 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001430 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001431 PreprocessorRecordTypes RecType =
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001432 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001433 BlobLen);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001434 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001435 case PP_MACRO_OBJECT_LIKE:
1436 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001437 // If we already have a macro, that means that we've hit the end
1438 // of the definition of the macro we were looking for. We're
1439 // done.
1440 if (Macro)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001441 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001442
1443 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1444 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001445 Error("macro must have a name in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001446 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001447 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00001448 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001449 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001450
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001451 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001452 MI->setIsUsed(isUsed);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001453 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001454
Douglas Gregoraae92242010-03-19 21:51:54 +00001455 unsigned NextIndex = 3;
Sebastian Redl539c5062010-08-18 23:57:32 +00001456 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001457 // Decode function-like macro info.
1458 bool isC99VarArgs = Record[3];
1459 bool isGNUVarArgs = Record[4];
1460 MacroArgs.clear();
1461 unsigned NumArgs = Record[5];
Douglas Gregoraae92242010-03-19 21:51:54 +00001462 NextIndex = 6 + NumArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001463 for (unsigned i = 0; i != NumArgs; ++i)
1464 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1465
1466 // Install function-like macro info.
1467 MI->setIsFunctionLike();
1468 if (isC99VarArgs) MI->setIsC99Varargs();
1469 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001470 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001471 PP->getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001472 }
1473
1474 // Finally, install the macro.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001475 PP->setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001476
1477 // Remember that we saw this macro last so that we add the tokens that
1478 // form its body to it.
1479 Macro = MI;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001480
Douglas Gregoraae92242010-03-19 21:51:54 +00001481 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1482 // We have a macro definition. Load it now.
1483 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1484 getMacroDefinition(Record[NextIndex]));
1485 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001486
Douglas Gregorc3366a52009-04-21 23:56:24 +00001487 ++NumMacrosRead;
1488 break;
1489 }
Mike Stump11289f42009-09-09 15:08:12 +00001490
Sebastian Redl539c5062010-08-18 23:57:32 +00001491 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001492 // If we see a TOKEN before a PP_MACRO_*, then the file is
1493 // erroneous, just pretend we didn't see this.
1494 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001495
Douglas Gregorc3366a52009-04-21 23:56:24 +00001496 Token Tok;
1497 Tok.startToken();
Sebastian Redl2c373b92010-10-05 15:59:54 +00001498 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001499 Tok.setLength(Record[1]);
1500 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1501 Tok.setIdentifierInfo(II);
1502 Tok.setKind((tok::TokenKind)Record[3]);
1503 Tok.setFlag((Token::TokenFlags)Record[4]);
1504 Macro->AddTokenToBody(Tok);
1505 break;
1506 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001507 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001508 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001509
1510 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001511}
1512
Douglas Gregor92a96f52011-02-08 21:58:10 +00001513PreprocessedEntity *ASTReader::LoadPreprocessedEntity(PerFileData &F) {
1514 assert(PP && "Forgot to set Preprocessor ?");
1515 unsigned Code = F.PreprocessorDetailCursor.ReadCode();
1516 switch (Code) {
1517 case llvm::bitc::END_BLOCK:
1518 return 0;
1519
1520 case llvm::bitc::ENTER_SUBBLOCK:
1521 Error("unexpected subblock record in preprocessor detail block");
1522 return 0;
1523
1524 case llvm::bitc::DEFINE_ABBREV:
1525 Error("unexpected abbrevation record in preprocessor detail block");
1526 return 0;
1527
1528 default:
1529 break;
1530 }
1531
1532 if (!PP->getPreprocessingRecord()) {
1533 Error("no preprocessing record");
1534 return 0;
1535 }
1536
1537 // Read the record.
1538 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1539 const char *BlobStart = 0;
1540 unsigned BlobLen = 0;
1541 RecordData Record;
1542 PreprocessorDetailRecordTypes RecType =
1543 (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord(
1544 Code, Record, BlobStart, BlobLen);
1545 switch (RecType) {
1546 case PPD_MACRO_INSTANTIATION: {
1547 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1548 return PE;
1549
1550 MacroInstantiation *MI
1551 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1552 SourceRange(ReadSourceLocation(F, Record[1]),
1553 ReadSourceLocation(F, Record[2])),
1554 getMacroDefinition(Record[4]));
1555 PPRec.SetPreallocatedEntity(Record[0], MI);
1556 return MI;
1557 }
1558
1559 case PPD_MACRO_DEFINITION: {
1560 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1561 return PE;
1562
1563 if (Record[1] > MacroDefinitionsLoaded.size()) {
1564 Error("out-of-bounds macro definition record");
1565 return 0;
1566 }
1567
1568 // Decode the identifier info and then check again; if the macro is
1569 // still defined and associated with the identifier,
1570 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1571 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1572 MacroDefinition *MD
1573 = new (PPRec) MacroDefinition(II,
1574 ReadSourceLocation(F, Record[5]),
1575 SourceRange(
1576 ReadSourceLocation(F, Record[2]),
1577 ReadSourceLocation(F, Record[3])));
1578
1579 PPRec.SetPreallocatedEntity(Record[0], MD);
1580 MacroDefinitionsLoaded[Record[1] - 1] = MD;
1581
1582 if (DeserializationListener)
1583 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1584 }
1585
1586 return MacroDefinitionsLoaded[Record[1] - 1];
1587 }
1588
1589 case PPD_INCLUSION_DIRECTIVE: {
1590 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1591 return PE;
1592
1593 const char *FullFileNameStart = BlobStart + Record[3];
1594 const FileEntry *File
1595 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
1596 BlobLen - Record[3]));
1597
1598 // FIXME: Stable encoding
1599 InclusionDirective::InclusionKind Kind
1600 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1601 InclusionDirective *ID
1602 = new (PPRec) InclusionDirective(PPRec, Kind,
1603 llvm::StringRef(BlobStart, Record[3]),
1604 Record[4],
1605 File,
1606 SourceRange(ReadSourceLocation(F, Record[1]),
1607 ReadSourceLocation(F, Record[2])));
1608 PPRec.SetPreallocatedEntity(Record[0], ID);
1609 return ID;
1610 }
1611 }
1612
1613 Error("invalid offset in preprocessor detail block");
1614 return 0;
1615}
1616
Douglas Gregor09b69892011-02-10 17:09:37 +00001617namespace {
1618 /// \brief Trait class used to search the on-disk hash table containing all of
1619 /// the header search information.
1620 ///
1621 /// The on-disk hash table contains a mapping from each header path to
1622 /// information about that header (how many times it has been included, its
1623 /// controlling macro, etc.). Note that we actually hash based on the
1624 /// filename, and support "deep" comparisons of file names based on current
1625 /// inode numbers, so that the search can cope with non-normalized path names
1626 /// and symlinks.
1627 class HeaderFileInfoTrait {
1628 const char *SearchPath;
1629 struct stat SearchPathStatBuf;
1630 llvm::Optional<int> SearchPathStatResult;
1631
1632 int StatSimpleCache(const char *Path, struct stat *StatBuf) {
1633 if (Path == SearchPath) {
1634 if (!SearchPathStatResult)
1635 SearchPathStatResult = stat(Path, &SearchPathStatBuf);
1636
1637 *StatBuf = SearchPathStatBuf;
1638 return *SearchPathStatResult;
1639 }
1640
1641 return stat(Path, StatBuf);
1642 }
1643
1644 public:
1645 typedef const char *external_key_type;
1646 typedef const char *internal_key_type;
1647
1648 typedef HeaderFileInfo data_type;
1649
1650 HeaderFileInfoTrait(const char *SearchPath = 0) : SearchPath(SearchPath) { }
1651
1652 static unsigned ComputeHash(const char *path) {
1653 return llvm::HashString(llvm::sys::path::filename(path));
1654 }
1655
1656 static internal_key_type GetInternalKey(const char *path) { return path; }
1657
1658 bool EqualKey(internal_key_type a, internal_key_type b) {
1659 if (strcmp(a, b) == 0)
1660 return true;
1661
1662 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1663 return false;
1664
1665 // The file names match, but the path names don't. stat() the files to
1666 // see if they are the same.
1667 struct stat StatBufA, StatBufB;
1668 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1669 return false;
1670
1671 return StatBufA.st_ino == StatBufB.st_ino;
1672 }
1673
1674 static std::pair<unsigned, unsigned>
1675 ReadKeyDataLength(const unsigned char*& d) {
1676 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1677 unsigned DataLen = (unsigned) *d++;
1678 return std::make_pair(KeyLen + 1, DataLen);
1679 }
1680
1681 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1682 return (const char *)d;
1683 }
1684
1685 static data_type ReadData(const internal_key_type, const unsigned char *d,
1686 unsigned DataLen) {
1687 const unsigned char *End = d + DataLen;
1688 using namespace clang::io;
1689 HeaderFileInfo HFI;
1690 unsigned Flags = *d++;
1691 HFI.isImport = (Flags >> 3) & 0x01;
1692 HFI.DirInfo = (Flags >> 1) & 0x03;
1693 HFI.Resolved = Flags & 0x01;
1694 HFI.NumIncludes = ReadUnalignedLE16(d);
1695 HFI.ControllingMacroID = ReadUnalignedLE32(d);
1696 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1697 (void)End;
1698
1699 // This HeaderFileInfo was externally loaded.
1700 HFI.External = true;
1701 return HFI;
1702 }
1703 };
1704}
1705
1706/// \brief The on-disk hash table used for the global method pool.
1707typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
1708 HeaderFileInfoLookupTable;
1709
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001710void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1711 uint64_t Offset) {
1712 // Note that this identifier has a macro definition.
1713 II->setHasMacroDefinition(true);
1714
1715 // Adjust the offset based on our position in the chain.
1716 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1717 if (Chain[I] == &F)
1718 break;
1719
1720 Offset += Chain[I]->SizeInBits;
1721 }
1722
1723 UnreadMacroRecordOffsets[II] = Offset;
1724}
1725
Sebastian Redl2c499f62010-08-18 23:56:43 +00001726void ASTReader::ReadDefinedMacros() {
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001727 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001728 PerFileData &F = *Chain[N - I - 1];
1729 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001730
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001731 // If there was no preprocessor block, skip this file.
1732 if (!MacroCursor.getBitStreamReader())
1733 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001734
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001735 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001736 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001737
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001738 RecordData Record;
1739 while (true) {
1740 unsigned Code = Cursor.ReadCode();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001741 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001742 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001743
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001744 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1745 // No known subblocks, always skip them.
1746 Cursor.ReadSubBlockID();
1747 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001748 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001749 return;
1750 }
1751 continue;
1752 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001753
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001754 if (Code == llvm::bitc::DEFINE_ABBREV) {
1755 Cursor.ReadAbbrevRecord();
1756 continue;
1757 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001758
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001759 // Read a record.
1760 const char *BlobStart;
1761 unsigned BlobLen;
1762 Record.clear();
1763 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1764 default: // Default behavior: ignore.
1765 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001766
Sebastian Redl539c5062010-08-18 23:57:32 +00001767 case PP_MACRO_OBJECT_LIKE:
1768 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001769 DecodeIdentifierInfo(Record[0]);
1770 break;
1771
Sebastian Redl539c5062010-08-18 23:57:32 +00001772 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001773 // Ignore tokens.
1774 break;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001775 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001776 }
1777 }
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001778
1779 // Drain the unread macro-record offsets map.
1780 while (!UnreadMacroRecordOffsets.empty())
1781 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1782}
1783
1784void ASTReader::LoadMacroDefinition(
1785 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1786 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1787 PerFileData *F = 0;
1788 uint64_t Offset = Pos->second;
1789 UnreadMacroRecordOffsets.erase(Pos);
1790
1791 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1792 if (Offset < Chain[I]->SizeInBits) {
1793 F = Chain[I];
1794 break;
1795 }
1796
1797 Offset -= Chain[I]->SizeInBits;
1798 }
1799 if (!F) {
1800 Error("Malformed macro record offset");
1801 return;
1802 }
1803
1804 ReadMacroRecord(*F, Offset);
1805}
1806
1807void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1808 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1809 = UnreadMacroRecordOffsets.find(II);
1810 LoadMacroDefinition(Pos);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001811}
1812
Sebastian Redl50e26582010-09-15 19:54:06 +00001813MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor91096292010-10-02 19:29:26 +00001814 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregoraae92242010-03-19 21:51:54 +00001815 return 0;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001816
Douglas Gregor91096292010-10-02 19:29:26 +00001817 if (!MacroDefinitionsLoaded[ID - 1]) {
1818 unsigned Index = ID - 1;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001819 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1820 PerFileData &F = *Chain[N - I - 1];
1821 if (Index < F.LocalNumMacroDefinitions) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001822 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
1823 F.PreprocessorDetailCursor.JumpToBit(F.MacroDefinitionOffsets[Index]);
1824 LoadPreprocessedEntity(F);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001825 break;
1826 }
1827 Index -= F.LocalNumMacroDefinitions;
1828 }
Douglas Gregor91096292010-10-02 19:29:26 +00001829 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001830 }
1831
Douglas Gregor91096292010-10-02 19:29:26 +00001832 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001833}
1834
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001835/// \brief If we are loading a relocatable PCH file, and the filename is
1836/// not an absolute path, add the system root to the beginning of the file
1837/// name.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001838void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001839 // If this is not a relocatable PCH file, there's nothing to do.
1840 if (!RelocatablePCH)
1841 return;
Mike Stump11289f42009-09-09 15:08:12 +00001842
Michael J. Spencerf28df4c2010-12-17 21:22:22 +00001843 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001844 return;
1845
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001846 if (isysroot == 0) {
1847 // If no system root was given, default to '/'
1848 Filename.insert(Filename.begin(), '/');
1849 return;
1850 }
Mike Stump11289f42009-09-09 15:08:12 +00001851
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001852 unsigned Length = strlen(isysroot);
1853 if (isysroot[Length - 1] != '/')
1854 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001855
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001856 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1857}
1858
Sebastian Redl2c499f62010-08-18 23:56:43 +00001859ASTReader::ASTReadResult
Sebastian Redl3e31c722010-08-18 23:56:56 +00001860ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001861 llvm::BitstreamCursor &Stream = F.Stream;
1862
Sebastian Redl539c5062010-08-18 23:57:32 +00001863 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001864 Error("malformed block record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001865 return Failure;
1866 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001867
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001868 // Read all of the records and blocks for the ASt file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001869 RecordData Record;
Sebastian Redl393f8b72010-07-19 20:52:06 +00001870 bool First = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001871 while (!Stream.AtEndOfStream()) {
1872 unsigned Code = Stream.ReadCode();
1873 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001874 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001875 Error("error at end of module block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001876 return Failure;
1877 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001878
Douglas Gregor55abb232009-04-10 20:39:37 +00001879 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001880 }
1881
1882 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1883 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001884 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001885 // We lazily load the decls block, but we want to set up the
1886 // DeclsCursor cursor to point into it. Clone our current bitcode
1887 // cursor to it, enter the block and read the abbrevs in that block.
1888 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001889 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001890 if (Stream.SkipBlock() || // Skip with the main cursor.
1891 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001892 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001893 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001894 return Failure;
1895 }
1896 break;
Mike Stump11289f42009-09-09 15:08:12 +00001897
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00001898 case DECL_UPDATES_BLOCK_ID:
1899 if (Stream.SkipBlock()) {
1900 Error("malformed block record in AST file");
1901 return Failure;
1902 }
1903 break;
1904
Sebastian Redl539c5062010-08-18 23:57:32 +00001905 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001906 F.MacroCursor = Stream;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001907 if (PP)
1908 PP->setExternalSource(this);
1909
Douglas Gregor796d76a2010-10-20 22:00:55 +00001910 if (Stream.SkipBlock() ||
1911 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001912 Error("malformed block record in AST file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001913 return Failure;
1914 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001915 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001916 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001917
Douglas Gregor92a96f52011-02-08 21:58:10 +00001918 case PREPROCESSOR_DETAIL_BLOCK_ID:
1919 F.PreprocessorDetailCursor = Stream;
1920 if (Stream.SkipBlock() ||
1921 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1922 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1923 Error("malformed preprocessor detail record in AST file");
1924 return Failure;
1925 }
1926 F.PreprocessorDetailStartOffset
1927 = F.PreprocessorDetailCursor.GetCurrentBitNo();
1928 break;
1929
Sebastian Redl539c5062010-08-18 23:57:32 +00001930 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001931 switch (ReadSourceManagerBlock(F)) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001932 case Success:
1933 break;
1934
1935 case Failure:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001936 Error("malformed source manager block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001937 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001938
1939 case IgnorePCH:
1940 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001941 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001942 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001943 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00001944 First = false;
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001945 continue;
1946 }
1947
1948 if (Code == llvm::bitc::DEFINE_ABBREV) {
1949 Stream.ReadAbbrevRecord();
1950 continue;
1951 }
1952
1953 // Read and process a record.
1954 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001955 const char *BlobStart = 0;
1956 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001957 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001958 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001959 default: // Default behavior: ignore.
1960 break;
1961
Sebastian Redl539c5062010-08-18 23:57:32 +00001962 case METADATA: {
1963 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1964 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001965 : diag::warn_pch_version_too_new);
1966 return IgnorePCH;
1967 }
1968
1969 RelocatablePCH = Record[4];
1970 if (Listener) {
1971 std::string TargetTriple(BlobStart, BlobLen);
1972 if (Listener->ReadTargetTriple(TargetTriple))
1973 return IgnorePCH;
1974 }
1975 break;
1976 }
1977
Sebastian Redl539c5062010-08-18 23:57:32 +00001978 case CHAINED_METADATA: {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001979 if (!First) {
1980 Error("CHAINED_METADATA is not first record in block");
1981 return Failure;
1982 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001983 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1984 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001985 : diag::warn_pch_version_too_new);
1986 return IgnorePCH;
1987 }
1988
Sebastian Redl009e7f22010-10-05 16:15:19 +00001989 // Load the chained file, which is always a PCH file.
1990 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001991 case Failure: return Failure;
1992 // If we have to ignore the dependency, we'll have to ignore this too.
1993 case IgnorePCH: return IgnorePCH;
1994 case Success: break;
1995 }
1996 break;
1997 }
1998
Sebastian Redl539c5062010-08-18 23:57:32 +00001999 case TYPE_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00002000 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002001 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002002 return Failure;
2003 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002004 F.TypeOffsets = (const uint32_t *)BlobStart;
2005 F.LocalNumTypes = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002006 break;
2007
Sebastian Redl539c5062010-08-18 23:57:32 +00002008 case DECL_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00002009 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002010 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002011 return Failure;
2012 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002013 F.DeclOffsets = (const uint32_t *)BlobStart;
2014 F.LocalNumDecls = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002015 break;
Douglas Gregor55abb232009-04-10 20:39:37 +00002016
Sebastian Redl539c5062010-08-18 23:57:32 +00002017 case TU_UPDATE_LEXICAL: {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002018 DeclContextInfo Info = {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002019 /* No visible information */ 0,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002020 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
2021 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002022 };
Douglas Gregoraa433012010-10-01 01:18:02 +00002023 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
2024 .push_back(Info);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002025 break;
2026 }
2027
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002028 case UPDATE_VISIBLE: {
2029 serialization::DeclID ID = Record[0];
2030 void *Table = ASTDeclContextNameLookupTable::Create(
2031 (const unsigned char *)BlobStart + Record[1],
2032 (const unsigned char *)BlobStart,
2033 ASTDeclContextNameLookupTrait(*this));
Douglas Gregoraa433012010-10-01 01:18:02 +00002034 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002035 DeclContextInfo Info = {
2036 Table, /* No lexical inforamtion */ 0, 0
2037 };
2038 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
2039 } else
2040 PendingVisibleUpdates[ID].push_back(Table);
2041 break;
2042 }
2043
Sebastian Redl539c5062010-08-18 23:57:32 +00002044 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002045 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
2046 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002047 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002048 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
2049 Latest > FirstLatestDeclIDs[First]) &&
2050 "The new latest is supposed to come after the previous latest");
2051 FirstLatestDeclIDs[First] = Latest;
2052 }
2053 break;
2054 }
2055
Sebastian Redl539c5062010-08-18 23:57:32 +00002056 case LANGUAGE_OPTIONS:
Douglas Gregorce3a8292010-07-27 00:27:13 +00002057 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor55abb232009-04-10 20:39:37 +00002058 return IgnorePCH;
2059 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00002060
Sebastian Redl539c5062010-08-18 23:57:32 +00002061 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00002062 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00002063 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00002064 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002065 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00002066 (const unsigned char *)F.IdentifierTableData + Record[0],
2067 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl2c373b92010-10-05 15:59:54 +00002068 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002069 if (PP)
2070 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00002071 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002072 break;
2073
Sebastian Redl539c5062010-08-18 23:57:32 +00002074 case IDENTIFIER_OFFSET:
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002075 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002076 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002077 return Failure;
2078 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002079 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2080 F.LocalNumIdentifiers = Record[0];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002081 break;
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002082
Sebastian Redl539c5062010-08-18 23:57:32 +00002083 case EXTERNAL_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002084 // Optimization for the first block.
2085 if (ExternalDefinitions.empty())
2086 ExternalDefinitions.swap(Record);
2087 else
2088 ExternalDefinitions.insert(ExternalDefinitions.end(),
2089 Record.begin(), Record.end());
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002090 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00002091
Sebastian Redl539c5062010-08-18 23:57:32 +00002092 case SPECIAL_TYPES:
Sebastian Redlb293a452010-07-20 21:20:32 +00002093 // Optimization for the first block
2094 if (SpecialTypes.empty())
2095 SpecialTypes.swap(Record);
2096 else
2097 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002098 break;
2099
Sebastian Redl539c5062010-08-18 23:57:32 +00002100 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002101 TotalNumStatements += Record[0];
2102 TotalNumMacros += Record[1];
2103 TotalLexicalDeclContexts += Record[2];
2104 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00002105 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002106
Sebastian Redl539c5062010-08-18 23:57:32 +00002107 case TENTATIVE_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002108 // Optimization for the first block.
2109 if (TentativeDefinitions.empty())
2110 TentativeDefinitions.swap(Record);
2111 else
2112 TentativeDefinitions.insert(TentativeDefinitions.end(),
2113 Record.begin(), Record.end());
Douglas Gregord4df8652009-04-22 22:02:47 +00002114 break;
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002115
Sebastian Redl539c5062010-08-18 23:57:32 +00002116 case UNUSED_FILESCOPED_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002117 // Optimization for the first block.
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002118 if (UnusedFileScopedDecls.empty())
2119 UnusedFileScopedDecls.swap(Record);
Sebastian Redlb293a452010-07-20 21:20:32 +00002120 else
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002121 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
2122 Record.begin(), Record.end());
Tanya Lattner90073802010-02-12 00:07:30 +00002123 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002124
Sebastian Redl539c5062010-08-18 23:57:32 +00002125 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002126 // Later blocks overwrite earlier ones.
2127 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002128 break;
2129
Sebastian Redl539c5062010-08-18 23:57:32 +00002130 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002131 // Optimization for the first block.
2132 if (LocallyScopedExternalDecls.empty())
2133 LocallyScopedExternalDecls.swap(Record);
2134 else
2135 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
2136 Record.begin(), Record.end());
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002137 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002138
Sebastian Redl539c5062010-08-18 23:57:32 +00002139 case SELECTOR_OFFSETS:
Sebastian Redla19a67f2010-08-03 21:58:15 +00002140 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00002141 F.LocalNumSelectors = Record[0];
Douglas Gregor95c13f52009-04-25 17:48:32 +00002142 break;
2143
Sebastian Redl539c5062010-08-18 23:57:32 +00002144 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00002145 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002146 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00002147 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002148 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00002149 F.SelectorLookupTableData + Record[0],
2150 F.SelectorLookupTableData,
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002151 ASTSelectorLookupTrait(*this));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002152 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002153 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00002154
Sebastian Redl96371b42010-09-22 00:42:30 +00002155 case REFERENCED_SELECTOR_POOL:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002156 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002157 break;
2158
Sebastian Redl539c5062010-08-18 23:57:32 +00002159 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002160 if (!Record.empty() && Listener)
2161 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002162 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002163
Sebastian Redl539c5062010-08-18 23:57:32 +00002164 case SOURCE_LOCATION_OFFSETS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002165 F.SLocOffsets = (const uint32_t *)BlobStart;
2166 F.LocalNumSLocEntries = Record[0];
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002167 F.LocalSLocSize = Record[1];
Douglas Gregor258ae542009-04-27 06:38:32 +00002168 break;
2169
Sebastian Redl539c5062010-08-18 23:57:32 +00002170 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl96371b42010-09-22 00:42:30 +00002171 if (PreloadSLocEntries.empty())
2172 PreloadSLocEntries.swap(Record);
2173 else
2174 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2175 Record.begin(), Record.end());
Douglas Gregor258ae542009-04-27 06:38:32 +00002176 break;
Douglas Gregorc5046832009-04-27 18:38:38 +00002177
Sebastian Redl539c5062010-08-18 23:57:32 +00002178 case STAT_CACHE: {
Douglas Gregor606c4ac2011-02-05 19:42:43 +00002179 if (!DisableStatCache) {
2180 ASTStatCache *MyStatCache =
2181 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2182 (const unsigned char *)BlobStart,
2183 NumStatHits, NumStatMisses);
2184 FileMgr.addStatCache(MyStatCache);
2185 F.StatCache = MyStatCache;
2186 }
Douglas Gregorc5046832009-04-27 18:38:38 +00002187 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00002188 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002189
Sebastian Redl539c5062010-08-18 23:57:32 +00002190 case EXT_VECTOR_DECLS:
Sebastian Redl04f5c312010-07-28 21:38:49 +00002191 // Optimization for the first block.
2192 if (ExtVectorDecls.empty())
2193 ExtVectorDecls.swap(Record);
2194 else
2195 ExtVectorDecls.insert(ExtVectorDecls.end(),
2196 Record.begin(), Record.end());
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002197 break;
2198
Sebastian Redl539c5062010-08-18 23:57:32 +00002199 case VTABLE_USES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002200 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002201 VTableUses.swap(Record);
2202 break;
2203
Sebastian Redl539c5062010-08-18 23:57:32 +00002204 case DYNAMIC_CLASSES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002205 // Optimization for the first block.
2206 if (DynamicClasses.empty())
2207 DynamicClasses.swap(Record);
2208 else
2209 DynamicClasses.insert(DynamicClasses.end(),
2210 Record.begin(), Record.end());
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002211 break;
2212
Sebastian Redl539c5062010-08-18 23:57:32 +00002213 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002214 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002215 break;
2216
Sebastian Redl539c5062010-08-18 23:57:32 +00002217 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002218 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002219 SemaDeclRefs.swap(Record);
2220 break;
2221
Sebastian Redl539c5062010-08-18 23:57:32 +00002222 case ORIGINAL_FILE_NAME:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002223 // The primary AST will be the last to get here, so it will be the one
Sebastian Redlb293a452010-07-20 21:20:32 +00002224 // that's used.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00002225 ActualOriginalFileName.assign(BlobStart, BlobLen);
2226 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002227 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00002228 break;
Mike Stump11289f42009-09-09 15:08:12 +00002229
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002230 case ORIGINAL_PCH_DIR:
2231 // The primary AST will be the last to get here, so it will be the one
2232 // that's used.
2233 OriginalDir.assign(BlobStart, BlobLen);
2234 break;
2235
Sebastian Redl539c5062010-08-18 23:57:32 +00002236 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00002237 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002238 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2239 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2240 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregord54f3a12009-10-05 21:07:28 +00002241 return IgnorePCH;
2242 }
2243 break;
2244 }
Sebastian Redlfa061442010-07-21 20:07:32 +00002245
Sebastian Redl539c5062010-08-18 23:57:32 +00002246 case MACRO_DEFINITION_OFFSETS:
Sebastian Redlfa061442010-07-21 20:07:32 +00002247 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2248 F.NumPreallocatedPreprocessingEntities = Record[0];
2249 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregoraae92242010-03-19 21:51:54 +00002250 break;
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002251
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002252 case DECL_UPDATE_OFFSETS: {
2253 if (Record.size() % 2 != 0) {
2254 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2255 return Failure;
2256 }
2257 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2258 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2259 .push_back(std::make_pair(&F, Record[I+1]));
2260 break;
2261 }
2262
Sebastian Redl539c5062010-08-18 23:57:32 +00002263 case DECL_REPLACEMENTS: {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002264 if (Record.size() % 2 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002265 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002266 return Failure;
2267 }
2268 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl539c5062010-08-18 23:57:32 +00002269 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002270 std::make_pair(&F, Record[I+1]);
2271 break;
2272 }
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002273
2274 case CXX_BASE_SPECIFIER_OFFSETS: {
2275 if (F.LocalNumCXXBaseSpecifiers != 0) {
2276 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2277 return Failure;
2278 }
2279
2280 F.LocalNumCXXBaseSpecifiers = Record[0];
2281 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2282 break;
2283 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002284
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002285 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002286 if (Record.size() % 2 != 0) {
2287 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2288 return Failure;
2289 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002290 if (PragmaDiagMappings.empty())
2291 PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002292 else
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002293 PragmaDiagMappings.insert(PragmaDiagMappings.end(),
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002294 Record.begin(), Record.end());
2295 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002296
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002297 case CUDA_SPECIAL_DECL_REFS:
2298 // Later tables overwrite earlier ones.
2299 CUDASpecialDeclRefs.swap(Record);
2300 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002301
2302 case HEADER_SEARCH_TABLE:
2303 F.HeaderFileInfoTableData = BlobStart;
2304 F.LocalNumHeaderFileInfos = Record[1];
2305 if (Record[0]) {
2306 F.HeaderFileInfoTable
2307 = HeaderFileInfoLookupTable::Create(
2308 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2309 (const unsigned char *)F.HeaderFileInfoTableData);
2310 if (PP)
2311 PP->getHeaderSearchInfo().SetExternalSource(this);
2312 }
2313 break;
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002314
2315 case FP_PRAGMA_OPTIONS:
2316 // Later tables overwrite earlier ones.
2317 FPPragmaOptions.swap(Record);
2318 break;
2319
2320 case OPENCL_EXTENSIONS:
2321 // Later tables overwrite earlier ones.
2322 OpenCLExtensions.swap(Record);
2323 break;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002324 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00002325 First = false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002326 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002327 Error("premature end of bitstream in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002328 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002329}
2330
Sebastian Redl009e7f22010-10-05 16:15:19 +00002331ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2332 ASTFileType Type) {
2333 switch(ReadASTCore(FileName, Type)) {
Sebastian Redl2abc0382010-07-16 20:41:52 +00002334 case Failure: return Failure;
2335 case IgnorePCH: return IgnorePCH;
2336 case Success: break;
2337 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002338
2339 // Here comes stuff that we only do once the entire chain is loaded.
2340
Sebastian Redl96371b42010-09-22 00:42:30 +00002341 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redlfa061442010-07-21 20:07:32 +00002342 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redlada023c2010-08-04 20:40:17 +00002343 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2344 TotalNumSelectors = 0;
Sebastian Redl9e687992010-07-19 22:06:55 +00002345 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl96371b42010-09-22 00:42:30 +00002346 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002347 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002348 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl9e687992010-07-19 22:06:55 +00002349 TotalNumTypes += Chain[I]->LocalNumTypes;
2350 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redlfa061442010-07-21 20:07:32 +00002351 TotalNumPreallocatedPreprocessingEntities +=
2352 Chain[I]->NumPreallocatedPreprocessingEntities;
2353 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redlada023c2010-08-04 20:40:17 +00002354 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl9e687992010-07-19 22:06:55 +00002355 }
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002356 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002357 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl9e687992010-07-19 22:06:55 +00002358 TypesLoaded.resize(TotalNumTypes);
2359 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redlfa061442010-07-21 20:07:32 +00002360 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2361 if (PP) {
2362 if (TotalNumIdentifiers > 0)
2363 PP->getHeaderSearchInfo().SetExternalLookup(this);
2364 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2365 if (!PP->getPreprocessingRecord())
2366 PP->createPreprocessingRecord();
2367 PP->getPreprocessingRecord()->SetExternalSource(*this,
2368 TotalNumPreallocatedPreprocessingEntities);
2369 }
2370 }
Sebastian Redlada023c2010-08-04 20:40:17 +00002371 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl96371b42010-09-22 00:42:30 +00002372 // Preload SLocEntries.
2373 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2374 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2375 if (Result != Success)
2376 return Result;
2377 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002378
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002379 // Check the predefines buffers.
Douglas Gregorce3a8292010-07-27 00:27:13 +00002380 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002381 return IgnorePCH;
2382
2383 if (PP) {
2384 // Initialization of keywords and pragmas occurs before the
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002385 // AST file is read, so there may be some identifiers that were
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002386 // loaded into the IdentifierTable before we intercepted the
2387 // creation of identifiers. Iterate through the list of known
2388 // identifiers and determine whether we have to establish
2389 // preprocessor definitions or top-level identifier declaration
2390 // chains for those identifiers.
2391 //
2392 // We copy the IdentifierInfo pointers to a small vector first,
2393 // since de-serializing declarations or macro definitions can add
2394 // new entries into the identifier table, invalidating the
2395 // iterators.
2396 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2397 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2398 IdEnd = PP->getIdentifierTable().end();
2399 Id != IdEnd; ++Id)
2400 Identifiers.push_back(Id->second);
Sebastian Redlfa061442010-07-21 20:07:32 +00002401 // We need to search the tables in all files.
Sebastian Redlfa061442010-07-21 20:07:32 +00002402 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002403 ASTIdentifierLookupTable *IdTable
2404 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2405 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl5c415f32010-07-22 17:01:13 +00002406 // ones.
2407 if (!IdTable)
2408 continue;
Sebastian Redlfa061442010-07-21 20:07:32 +00002409 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2410 IdentifierInfo *II = Identifiers[I];
2411 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00002412 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redlfa061442010-07-21 20:07:32 +00002413 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002414 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redlb293a452010-07-20 21:20:32 +00002415 if (Pos == IdTable->end())
2416 continue;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002417
Sebastian Redlb293a452010-07-20 21:20:32 +00002418 // Dereferencing the iterator has the effect of populating the
2419 // IdentifierInfo node with the various declarations it needs.
2420 (void)*Pos;
2421 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002422 }
2423 }
2424
2425 if (Context)
2426 InitializeContext(*Context);
2427
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002428 if (DeserializationListener)
2429 DeserializationListener->ReaderInitialized(this);
2430
Douglas Gregor936a5b42010-11-30 05:23:00 +00002431 // If this AST file is a precompiled preamble, then set the main file ID of
2432 // the source manager to the file source file from which the preamble was
2433 // built. This is the only valid way to use a precompiled preamble.
2434 if (Type == Preamble) {
2435 SourceLocation Loc
2436 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2437 if (Loc.isValid()) {
2438 std::pair<FileID, unsigned> Decomposed = SourceMgr.getDecomposedLoc(Loc);
2439 SourceMgr.SetPreambleFileID(Decomposed.first);
2440 }
2441 }
2442
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002443 return Success;
2444}
2445
Sebastian Redl009e7f22010-10-05 16:15:19 +00002446ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2447 ASTFileType Type) {
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002448 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl009e7f22010-10-05 16:15:19 +00002449 Chain.push_back(new PerFileData(Type));
Sebastian Redl34522812010-07-16 17:50:48 +00002450 PerFileData &F = *Chain.back();
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002451 if (Prev)
2452 Prev->NextInSource = &F;
2453 else
2454 FirstInSource = &F;
2455 F.Loaders.push_back(Prev);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002456
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002457 // Set the AST file name.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002458 F.FileName = FileName;
2459
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002460 if (FileName != "-") {
2461 CurrentDir = llvm::sys::path::parent_path(FileName);
2462 if (CurrentDir.empty()) CurrentDir = ".";
2463 }
2464
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +00002465 if (!ASTBuffers.empty()) {
Sebastian Redleb931c62011-04-14 14:07:41 +00002466 F.Buffer.reset(ASTBuffers.back());
2467 ASTBuffers.pop_back();
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +00002468 assert(F.Buffer && "Passed null buffer");
2469 } else {
2470 // Open the AST file.
2471 //
2472 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2473 std::string ErrStr;
2474 llvm::error_code ec;
2475 if (FileName == "-") {
2476 ec = llvm::MemoryBuffer::getSTDIN(F.Buffer);
2477 if (ec)
2478 ErrStr = ec.message();
2479 } else
2480 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
2481 if (!F.Buffer) {
2482 Error(ErrStr.c_str());
2483 return IgnorePCH;
2484 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002485 }
2486
2487 // Initialize the stream
2488 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2489 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl34522812010-07-16 17:50:48 +00002490 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002491 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002492 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002493
2494 // Sniff for the signature.
2495 if (Stream.Read(8) != 'C' ||
2496 Stream.Read(8) != 'P' ||
2497 Stream.Read(8) != 'C' ||
2498 Stream.Read(8) != 'H') {
2499 Diag(diag::err_not_a_pch_file) << FileName;
2500 return Failure;
2501 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002502
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002503 while (!Stream.AtEndOfStream()) {
2504 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002505
Douglas Gregor92863e42009-04-10 23:10:45 +00002506 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002507 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002508 return Failure;
2509 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002510
2511 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002512
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002513 // We only know the AST subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002514 switch (BlockID) {
2515 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002516 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002517 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002518 return Failure;
2519 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002520 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00002521 case AST_BLOCK_ID:
Sebastian Redl3e31c722010-08-18 23:56:56 +00002522 switch (ReadASTBlock(F)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002523 case Success:
2524 break;
2525
2526 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00002527 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00002528
2529 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00002530 // FIXME: We could consider reading through to the end of this
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002531 // AST block, skipping subblocks, to see if there are other
2532 // AST blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00002533
2534 // Clear out any preallocated source location entries, so that
2535 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002536 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00002537
2538 // Remove the stat cache.
Sebastian Redl34522812010-07-16 17:50:48 +00002539 if (F.StatCache)
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002540 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00002541
Douglas Gregor92863e42009-04-10 23:10:45 +00002542 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002543 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002544 break;
2545 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002546 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002547 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002548 return Failure;
2549 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002550 break;
2551 }
Mike Stump11289f42009-09-09 15:08:12 +00002552 }
2553
Sebastian Redl2abc0382010-07-16 20:41:52 +00002554 return Success;
2555}
2556
Sebastian Redl2c499f62010-08-18 23:56:43 +00002557void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002558 PP = &pp;
Sebastian Redlfa061442010-07-21 20:07:32 +00002559
2560 unsigned TotalNum = 0;
2561 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2562 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2563 if (TotalNum) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002564 if (!PP->getPreprocessingRecord())
2565 PP->createPreprocessingRecord();
Sebastian Redlfa061442010-07-21 20:07:32 +00002566 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregoraae92242010-03-19 21:51:54 +00002567 }
2568}
2569
Sebastian Redl2c499f62010-08-18 23:56:43 +00002570void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002571 Context = &Ctx;
2572 assert(Context && "Passed null context!");
2573
2574 assert(PP && "Forgot to set Preprocessor ?");
2575 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2576 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00002577 PP->setExternalSource(this);
Douglas Gregor09b69892011-02-10 17:09:37 +00002578 PP->getHeaderSearchInfo().SetExternalSource(this);
2579
Douglas Gregoraa433012010-10-01 01:18:02 +00002580 // If we have an update block for the TU waiting, we have to add it before
2581 // deserializing the decl.
2582 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2583 if (DCU != DeclContextOffsets.end()) {
2584 // Insertion could invalidate map, so grab vector.
2585 DeclContextInfos T;
2586 T.swap(DCU->second);
2587 DeclContextOffsets.erase(DCU);
2588 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2589 }
2590
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002591 // Load the translation unit declaration
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00002592 GetTranslationUnitDecl();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002593
2594 // Load the special types.
2595 Context->setBuiltinVaListType(
Sebastian Redl539c5062010-08-18 23:57:32 +00002596 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2597 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002598 Context->setObjCIdType(GetType(Id));
Sebastian Redl539c5062010-08-18 23:57:32 +00002599 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002600 Context->setObjCSelType(GetType(Sel));
Sebastian Redl539c5062010-08-18 23:57:32 +00002601 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002602 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl539c5062010-08-18 23:57:32 +00002603 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002604 Context->setObjCClassType(GetType(Class));
Steve Naroff7cae42b2009-07-10 23:34:53 +00002605
Sebastian Redl539c5062010-08-18 23:57:32 +00002606 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002607 Context->setCFConstantStringType(GetType(String));
Mike Stump11289f42009-09-09 15:08:12 +00002608 if (unsigned FastEnum
Sebastian Redl539c5062010-08-18 23:57:32 +00002609 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002610 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl539c5062010-08-18 23:57:32 +00002611 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregor27821ce2009-07-07 16:35:42 +00002612 QualType FileType = GetType(File);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002613 if (FileType.isNull()) {
2614 Error("FILE type is NULL");
2615 return;
2616 }
John McCall9dd450b2009-09-21 23:43:11 +00002617 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregor27821ce2009-07-07 16:35:42 +00002618 Context->setFILEDecl(Typedef->getDecl());
2619 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002620 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002621 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002622 Error("Invalid FILE type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002623 return;
2624 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002625 Context->setFILEDecl(Tag->getDecl());
2626 }
2627 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002628 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002629 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002630 if (Jmp_bufType.isNull()) {
2631 Error("jmp_bug type is NULL");
2632 return;
2633 }
John McCall9dd450b2009-09-21 23:43:11 +00002634 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002635 Context->setjmp_bufDecl(Typedef->getDecl());
2636 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002637 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002638 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002639 Error("Invalid jmp_buf type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002640 return;
2641 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002642 Context->setjmp_bufDecl(Tag->getDecl());
2643 }
2644 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002645 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002646 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002647 if (Sigjmp_bufType.isNull()) {
2648 Error("sigjmp_buf type is NULL");
2649 return;
2650 }
John McCall9dd450b2009-09-21 23:43:11 +00002651 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002652 Context->setsigjmp_bufDecl(Typedef->getDecl());
2653 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002654 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002655 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stumpa4de80b2009-07-28 02:25:19 +00002656 Context->setsigjmp_bufDecl(Tag->getDecl());
2657 }
2658 }
Mike Stump11289f42009-09-09 15:08:12 +00002659 if (unsigned ObjCIdRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002660 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002661 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump11289f42009-09-09 15:08:12 +00002662 if (unsigned ObjCClassRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002663 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002664 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002665 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpd0153282009-10-20 02:12:22 +00002666 Context->setBlockDescriptorType(GetType(String));
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002667 if (unsigned String
Sebastian Redl539c5062010-08-18 23:57:32 +00002668 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002669 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002670 if (unsigned ObjCSelRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002671 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002672 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002673 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002674 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002675
Sebastian Redl539c5062010-08-18 23:57:32 +00002676 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002677 Context->setInt128Installed();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002678
Richard Smith02e85f32011-04-14 22:09:26 +00002679 if (unsigned AutoDeduct = SpecialTypes[SPECIAL_TYPE_AUTO_DEDUCT])
2680 Context->AutoDeductTy = GetType(AutoDeduct);
2681 if (unsigned AutoRRefDeduct = SpecialTypes[SPECIAL_TYPE_AUTO_RREF_DEDUCT])
2682 Context->AutoRRefDeductTy = GetType(AutoRRefDeduct);
2683
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002684 ReadPragmaDiagnosticMappings(Context->getDiagnostics());
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002685
2686 // If there were any CUDA special declarations, deserialize them.
2687 if (!CUDASpecialDeclRefs.empty()) {
2688 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
2689 Context->setcudaConfigureCallDecl(
2690 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2691 }
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002692}
2693
Douglas Gregor45fe0362009-05-12 01:31:05 +00002694/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002695/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00002696/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002697std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00002698 FileManager &FileMgr,
Daniel Dunbar3b951482009-12-03 09:13:06 +00002699 Diagnostic &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002700 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002701 std::string ErrStr;
2702 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner5159f612010-11-23 08:35:12 +00002703 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00002704 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00002705 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002706 return std::string();
2707 }
2708
2709 // Initialize the stream
2710 llvm::BitstreamReader StreamFile;
2711 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00002712 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00002713 (const unsigned char *)Buffer->getBufferEnd());
2714 Stream.init(StreamFile);
2715
2716 // Sniff for the signature.
2717 if (Stream.Read(8) != 'C' ||
2718 Stream.Read(8) != 'P' ||
2719 Stream.Read(8) != 'C' ||
2720 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002721 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002722 return std::string();
2723 }
2724
2725 RecordData Record;
2726 while (!Stream.AtEndOfStream()) {
2727 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002728
Douglas Gregor45fe0362009-05-12 01:31:05 +00002729 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2730 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00002731
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002732 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002733 switch (BlockID) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002734 case AST_BLOCK_ID:
2735 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002736 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002737 return std::string();
2738 }
2739 break;
Mike Stump11289f42009-09-09 15:08:12 +00002740
Douglas Gregor45fe0362009-05-12 01:31:05 +00002741 default:
2742 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002743 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002744 return std::string();
2745 }
2746 break;
2747 }
2748 continue;
2749 }
2750
2751 if (Code == llvm::bitc::END_BLOCK) {
2752 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002753 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002754 return std::string();
2755 }
2756 continue;
2757 }
2758
2759 if (Code == llvm::bitc::DEFINE_ABBREV) {
2760 Stream.ReadAbbrevRecord();
2761 continue;
2762 }
2763
2764 Record.clear();
2765 const char *BlobStart = 0;
2766 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002767 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl539c5062010-08-18 23:57:32 +00002768 == ORIGINAL_FILE_NAME)
Douglas Gregor45fe0362009-05-12 01:31:05 +00002769 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00002770 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00002771
2772 return std::string();
2773}
2774
Douglas Gregor55abb232009-04-10 20:39:37 +00002775/// \brief Parse the record that corresponds to a LangOptions data
2776/// structure.
2777///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002778/// This routine parses the language options from the AST file and then gives
2779/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00002780///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002781/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002782bool ASTReader::ParseLanguageOptions(
Douglas Gregor55abb232009-04-10 20:39:37 +00002783 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002784 if (Listener) {
2785 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00002786
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002787 #define PARSE_LANGOPT(Option) \
2788 LangOpts.Option = Record[Idx]; \
2789 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00002790
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002791 unsigned Idx = 0;
2792 PARSE_LANGOPT(Trigraphs);
2793 PARSE_LANGOPT(BCPLComment);
2794 PARSE_LANGOPT(DollarIdents);
2795 PARSE_LANGOPT(AsmPreprocessor);
2796 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00002797 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002798 PARSE_LANGOPT(ImplicitInt);
2799 PARSE_LANGOPT(Digraphs);
2800 PARSE_LANGOPT(HexFloats);
2801 PARSE_LANGOPT(C99);
2802 PARSE_LANGOPT(Microsoft);
2803 PARSE_LANGOPT(CPlusPlus);
2804 PARSE_LANGOPT(CPlusPlus0x);
2805 PARSE_LANGOPT(CXXOperatorNames);
2806 PARSE_LANGOPT(ObjC1);
2807 PARSE_LANGOPT(ObjC2);
2808 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00002809 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00002810 PARSE_LANGOPT(AppleKext);
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00002811 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00002812 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002813 PARSE_LANGOPT(PascalStrings);
2814 PARSE_LANGOPT(WritableStrings);
2815 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00002816 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002817 PARSE_LANGOPT(Exceptions);
Anders Carlssonce8dd3a2011-02-19 23:53:54 +00002818 PARSE_LANGOPT(ObjCExceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +00002819 PARSE_LANGOPT(CXXExceptions);
2820 PARSE_LANGOPT(SjLjExceptions);
Douglas Gregordbe39272011-02-01 15:15:22 +00002821 PARSE_LANGOPT(MSBitfields);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002822 PARSE_LANGOPT(NeXTRuntime);
2823 PARSE_LANGOPT(Freestanding);
2824 PARSE_LANGOPT(NoBuiltin);
2825 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00002826 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002827 PARSE_LANGOPT(Blocks);
2828 PARSE_LANGOPT(EmitAllDecls);
2829 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00002830 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2831 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002832 PARSE_LANGOPT(HeinousExtensions);
2833 PARSE_LANGOPT(Optimize);
2834 PARSE_LANGOPT(OptimizeSize);
2835 PARSE_LANGOPT(Static);
2836 PARSE_LANGOPT(PICLevel);
2837 PARSE_LANGOPT(GNUInline);
2838 PARSE_LANGOPT(NoInline);
2839 PARSE_LANGOPT(AccessControl);
2840 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00002841 PARSE_LANGOPT(ShortWChar);
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00002842 PARSE_LANGOPT(ShortEnums);
Chris Lattner51924e512010-06-26 21:25:03 +00002843 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall457a04e2010-10-22 21:05:15 +00002844 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00002845 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00002846 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002847 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00002848 PARSE_LANGOPT(OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00002849 PARSE_LANGOPT(CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00002850 PARSE_LANGOPT(CatchUndefined);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002851 PARSE_LANGOPT(DefaultFPContract);
Roman Divackydc1f68d2011-03-01 17:36:40 +00002852 PARSE_LANGOPT(ElideConstructors);
2853 PARSE_LANGOPT(SpellChecking);
Roman Divacky65b88cd2011-03-01 17:40:53 +00002854 PARSE_LANGOPT(MRTD);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002855 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00002856
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002857 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00002858 }
Douglas Gregor55abb232009-04-10 20:39:37 +00002859
2860 return false;
2861}
2862
Sebastian Redl2c499f62010-08-18 23:56:43 +00002863void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002864 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2865 PerFileData &F = *Chain[I];
2866 if (!F.PreprocessorDetailCursor.getBitStreamReader())
2867 continue;
2868
2869 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
2870 F.PreprocessorDetailCursor.JumpToBit(F.PreprocessorDetailStartOffset);
2871 while (LoadPreprocessedEntity(F)) { }
2872 }
Douglas Gregoraae92242010-03-19 21:51:54 +00002873}
2874
Douglas Gregor46c50012011-02-11 19:46:30 +00002875PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002876 PerFileData *F = 0;
2877 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2878 if (Offset < Chain[I]->SizeInBits) {
2879 F = Chain[I];
2880 break;
2881 }
2882
2883 Offset -= Chain[I]->SizeInBits;
2884 }
2885
2886 if (!F) {
2887 Error("Malformed preprocessed entity offset");
2888 return 0;
2889 }
2890
Douglas Gregor92a96f52011-02-08 21:58:10 +00002891 // Keep track of where we are in the stream, then jump back there
2892 // after reading this entity.
2893 SavedStreamPosition SavedPosition(F->PreprocessorDetailCursor);
2894 F->PreprocessorDetailCursor.JumpToBit(Offset);
2895 return LoadPreprocessedEntity(*F);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002896}
2897
Douglas Gregor09b69892011-02-10 17:09:37 +00002898HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
2899 HeaderFileInfoTrait Trait(FE->getName());
2900 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2901 PerFileData &F = *Chain[I];
2902 HeaderFileInfoLookupTable *Table
2903 = static_cast<HeaderFileInfoLookupTable *>(F.HeaderFileInfoTable);
2904 if (!Table)
2905 continue;
2906
2907 // Look in the on-disk hash table for an entry for this file name.
2908 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE->getName(),
2909 &Trait);
2910 if (Pos == Table->end())
2911 continue;
2912
2913 HeaderFileInfo HFI = *Pos;
2914 if (Listener)
2915 Listener->ReadHeaderFileInfo(HFI, FE->getUID());
2916
2917 return HFI;
2918 }
2919
2920 return HeaderFileInfo();
2921}
2922
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002923void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002924 unsigned Idx = 0;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002925 while (Idx < PragmaDiagMappings.size()) {
2926 SourceLocation
2927 Loc = SourceLocation::getFromRawEncoding(PragmaDiagMappings[Idx++]);
2928 while (1) {
2929 assert(Idx < PragmaDiagMappings.size() &&
2930 "Invalid data, didn't find '-1' marking end of diag/map pairs");
2931 if (Idx >= PragmaDiagMappings.size())
2932 break; // Something is messed up but at least avoid infinite loop in
2933 // release build.
2934 unsigned DiagID = PragmaDiagMappings[Idx++];
2935 if (DiagID == (unsigned)-1)
2936 break; // no more diag/map pairs for this location.
2937 diag::Mapping Map = (diag::Mapping)PragmaDiagMappings[Idx++];
2938 Diag.setDiagnosticMapping(DiagID, Map, Loc);
2939 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002940 }
2941}
2942
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002943/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002944ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002945 PerFileData *F = 0;
2946 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2947 F = Chain[N - I - 1];
2948 if (Index < F->LocalNumTypes)
2949 break;
2950 Index -= F->LocalNumTypes;
2951 }
2952 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redl2c373b92010-10-05 15:59:54 +00002953 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002954}
2955
2956/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002957///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002958/// The index is the type ID, shifted and minus the number of predefs. This
2959/// routine actually reads the record corresponding to the type at the given
2960/// location. It is a helper routine for GetType, which deals with reading type
2961/// IDs.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002962QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002963 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00002964 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00002965
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002966 // Keep track of where we are in the stream, then jump back there
2967 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002968 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002969
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002970 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00002971
Douglas Gregor1342e842009-07-06 18:54:52 +00002972 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00002973 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00002974
Sebastian Redl2c373b92010-10-05 15:59:54 +00002975 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002976 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002977 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00002978 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2979 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002980 if (Record.size() != 2) {
2981 Error("Incorrect encoding of extended qualifier type");
2982 return QualType();
2983 }
Douglas Gregor455b8f42009-04-15 22:00:08 +00002984 QualType Base = GetType(Record[0]);
John McCall8ccfcb52009-09-24 19:53:00 +00002985 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2986 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00002987 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002988
Sebastian Redl539c5062010-08-18 23:57:32 +00002989 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002990 if (Record.size() != 1) {
2991 Error("Incorrect encoding of complex type");
2992 return QualType();
2993 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002994 QualType ElemType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002995 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002996 }
2997
Sebastian Redl539c5062010-08-18 23:57:32 +00002998 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002999 if (Record.size() != 1) {
3000 Error("Incorrect encoding of pointer type");
3001 return QualType();
3002 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003003 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003004 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003005 }
3006
Sebastian Redl539c5062010-08-18 23:57:32 +00003007 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003008 if (Record.size() != 1) {
3009 Error("Incorrect encoding of block pointer type");
3010 return QualType();
3011 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003012 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003013 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003014 }
3015
Sebastian Redl539c5062010-08-18 23:57:32 +00003016 case TYPE_LVALUE_REFERENCE: {
Richard Smith0f538462011-04-12 10:38:03 +00003017 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003018 Error("Incorrect encoding of lvalue reference type");
3019 return QualType();
3020 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003021 QualType PointeeType = GetType(Record[0]);
Richard Smith0f538462011-04-12 10:38:03 +00003022 return Context->getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003023 }
3024
Sebastian Redl539c5062010-08-18 23:57:32 +00003025 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003026 if (Record.size() != 1) {
3027 Error("Incorrect encoding of rvalue reference type");
3028 return QualType();
3029 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003030 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003031 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003032 }
3033
Sebastian Redl539c5062010-08-18 23:57:32 +00003034 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00003035 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003036 Error("Incorrect encoding of member pointer type");
3037 return QualType();
3038 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003039 QualType PointeeType = GetType(Record[0]);
3040 QualType ClassType = GetType(Record[1]);
Douglas Gregor0cdc8322010-12-10 17:03:06 +00003041 if (PointeeType.isNull() || ClassType.isNull())
3042 return QualType();
3043
Chris Lattner8575daa2009-04-27 21:45:14 +00003044 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003045 }
3046
Sebastian Redl539c5062010-08-18 23:57:32 +00003047 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003048 QualType ElementType = GetType(Record[0]);
3049 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3050 unsigned IndexTypeQuals = Record[2];
3051 unsigned Idx = 3;
3052 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00003053 return Context->getConstantArrayType(ElementType, Size,
3054 ASM, IndexTypeQuals);
3055 }
3056
Sebastian Redl539c5062010-08-18 23:57:32 +00003057 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003058 QualType ElementType = GetType(Record[0]);
3059 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3060 unsigned IndexTypeQuals = Record[2];
Chris Lattner8575daa2009-04-27 21:45:14 +00003061 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003062 }
3063
Sebastian Redl539c5062010-08-18 23:57:32 +00003064 case TYPE_VARIABLE_ARRAY: {
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003065 QualType ElementType = GetType(Record[0]);
3066 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3067 unsigned IndexTypeQuals = Record[2];
Sebastian Redl2c373b92010-10-05 15:59:54 +00003068 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3069 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
3070 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor04318252009-07-06 15:59:29 +00003071 ASM, IndexTypeQuals,
3072 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003073 }
3074
Sebastian Redl539c5062010-08-18 23:57:32 +00003075 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00003076 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003077 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003078 return QualType();
3079 }
3080
3081 QualType ElementType = GetType(Record[0]);
3082 unsigned NumElements = Record[1];
Bob Wilsonaeb56442010-11-10 21:56:12 +00003083 unsigned VecKind = Record[2];
Chris Lattner37141f42010-06-23 06:00:24 +00003084 return Context->getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00003085 (VectorType::VectorKind)VecKind);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003086 }
3087
Sebastian Redl539c5062010-08-18 23:57:32 +00003088 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00003089 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003090 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003091 return QualType();
3092 }
3093
3094 QualType ElementType = GetType(Record[0]);
3095 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00003096 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003097 }
3098
Sebastian Redl539c5062010-08-18 23:57:32 +00003099 case TYPE_FUNCTION_NO_PROTO: {
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003100 if (Record.size() != 5) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00003101 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003102 return QualType();
3103 }
3104 QualType ResultType = GetType(Record[0]);
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003105 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3], (CallingConv)Record[4]);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00003106 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003107 }
3108
Sebastian Redl539c5062010-08-18 23:57:32 +00003109 case TYPE_FUNCTION_PROTO: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003110 QualType ResultType = GetType(Record[0]);
John McCalldb40c7f2010-12-14 08:05:40 +00003111
3112 FunctionProtoType::ExtProtoInfo EPI;
3113 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003114 /*hasregparm*/ Record[2],
3115 /*regparm*/ Record[3],
3116 static_cast<CallingConv>(Record[4]));
John McCalldb40c7f2010-12-14 08:05:40 +00003117
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003118 unsigned Idx = 5;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003119 unsigned NumParams = Record[Idx++];
3120 llvm::SmallVector<QualType, 16> ParamTypes;
3121 for (unsigned I = 0; I != NumParams; ++I)
3122 ParamTypes.push_back(GetType(Record[Idx++]));
John McCalldb40c7f2010-12-14 08:05:40 +00003123
3124 EPI.Variadic = Record[Idx++];
3125 EPI.TypeQuals = Record[Idx++];
Douglas Gregordb9d6642011-01-26 05:01:58 +00003126 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003127 ExceptionSpecificationType EST =
3128 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3129 EPI.ExceptionSpecType = EST;
3130 if (EST == EST_Dynamic) {
3131 EPI.NumExceptions = Record[Idx++];
3132 llvm::SmallVector<QualType, 2> Exceptions;
3133 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
3134 Exceptions.push_back(GetType(Record[Idx++]));
3135 EPI.Exceptions = Exceptions.data();
3136 } else if (EST == EST_ComputedNoexcept) {
3137 EPI.NoexceptExpr = ReadExpr(*Loc.F);
3138 }
Jay Foad7d0479f2009-05-21 09:52:38 +00003139 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalldb40c7f2010-12-14 08:05:40 +00003140 EPI);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003141 }
3142
Sebastian Redl539c5062010-08-18 23:57:32 +00003143 case TYPE_UNRESOLVED_USING:
John McCallb96ec562009-12-04 22:46:56 +00003144 return Context->getTypeDeclType(
3145 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
3146
Sebastian Redl539c5062010-08-18 23:57:32 +00003147 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003148 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003149 Error("incorrect encoding of typedef type");
3150 return QualType();
3151 }
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003152 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
3153 QualType Canonical = GetType(Record[1]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003154 if (!Canonical.isNull())
3155 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003156 return Context->getTypedefType(Decl, Canonical);
3157 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003158
Sebastian Redl539c5062010-08-18 23:57:32 +00003159 case TYPE_TYPEOF_EXPR:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003160 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003161
Sebastian Redl539c5062010-08-18 23:57:32 +00003162 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003163 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003164 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003165 return QualType();
3166 }
3167 QualType UnderlyingType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003168 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003169 }
Mike Stump11289f42009-09-09 15:08:12 +00003170
Sebastian Redl539c5062010-08-18 23:57:32 +00003171 case TYPE_DECLTYPE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003172 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson81df7b82009-06-24 19:06:50 +00003173
Richard Smith30482bc2011-02-20 03:19:35 +00003174 case TYPE_AUTO:
3175 return Context->getAutoType(GetType(Record[0]));
3176
Sebastian Redl539c5062010-08-18 23:57:32 +00003177 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003178 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003179 Error("incorrect encoding of record type");
3180 return QualType();
3181 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003182 bool IsDependent = Record[0];
3183 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCall424cec92011-01-19 06:33:43 +00003184 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003185 return T;
3186 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003187
Sebastian Redl539c5062010-08-18 23:57:32 +00003188 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003189 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003190 Error("incorrect encoding of enum type");
3191 return QualType();
3192 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003193 bool IsDependent = Record[0];
3194 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCall424cec92011-01-19 06:33:43 +00003195 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003196 return T;
3197 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00003198
John McCall81904512011-01-06 01:58:22 +00003199 case TYPE_ATTRIBUTED: {
3200 if (Record.size() != 3) {
3201 Error("incorrect encoding of attributed type");
3202 return QualType();
3203 }
3204 QualType modifiedType = GetType(Record[0]);
3205 QualType equivalentType = GetType(Record[1]);
3206 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3207 return Context->getAttributedType(kind, modifiedType, equivalentType);
3208 }
3209
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003210 case TYPE_PAREN: {
3211 if (Record.size() != 1) {
3212 Error("incorrect encoding of paren type");
3213 return QualType();
3214 }
3215 QualType InnerType = GetType(Record[0]);
3216 return Context->getParenType(InnerType);
3217 }
3218
Douglas Gregord2fa7662010-12-20 02:24:11 +00003219 case TYPE_PACK_EXPANSION: {
Douglas Gregor17328502011-02-01 15:24:58 +00003220 if (Record.size() != 2) {
Douglas Gregord2fa7662010-12-20 02:24:11 +00003221 Error("incorrect encoding of pack expansion type");
3222 return QualType();
3223 }
3224 QualType Pattern = GetType(Record[0]);
3225 if (Pattern.isNull())
3226 return QualType();
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00003227 llvm::Optional<unsigned> NumExpansions;
3228 if (Record[1])
3229 NumExpansions = Record[1] - 1;
3230 return Context->getPackExpansionType(Pattern, NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00003231 }
3232
Sebastian Redl539c5062010-08-18 23:57:32 +00003233 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003234 unsigned Idx = 0;
3235 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3236 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3237 QualType NamedType = GetType(Record[Idx++]);
3238 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00003239 }
3240
Sebastian Redl539c5062010-08-18 23:57:32 +00003241 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00003242 unsigned Idx = 0;
3243 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00003244 return Context->getObjCInterfaceType(ItfD);
3245 }
3246
Sebastian Redl539c5062010-08-18 23:57:32 +00003247 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00003248 unsigned Idx = 0;
3249 QualType Base = GetType(Record[Idx++]);
Chris Lattner587cbe12009-04-22 06:45:28 +00003250 unsigned NumProtos = Record[Idx++];
3251 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
3252 for (unsigned I = 0; I != NumProtos; ++I)
3253 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003254 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00003255 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003256
Sebastian Redl539c5062010-08-18 23:57:32 +00003257 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00003258 unsigned Idx = 0;
John McCall8b07ec22010-05-15 11:32:37 +00003259 QualType Pointee = GetType(Record[Idx++]);
3260 return Context->getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00003261 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00003262
Sebastian Redl539c5062010-08-18 23:57:32 +00003263 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00003264 unsigned Idx = 0;
3265 QualType Parm = GetType(Record[Idx++]);
3266 QualType Replacement = GetType(Record[Idx++]);
3267 return
3268 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3269 Replacement);
3270 }
John McCalle78aac42010-03-10 03:28:59 +00003271
Douglas Gregorada4b792011-01-14 02:55:32 +00003272 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3273 unsigned Idx = 0;
3274 QualType Parm = GetType(Record[Idx++]);
3275 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3276 return Context->getSubstTemplateTypeParmPackType(
3277 cast<TemplateTypeParmType>(Parm),
3278 ArgPack);
3279 }
3280
Sebastian Redl539c5062010-08-18 23:57:32 +00003281 case TYPE_INJECTED_CLASS_NAME: {
John McCalle78aac42010-03-10 03:28:59 +00003282 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
3283 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003284 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003285 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003286 return
3287 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00003288 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003289
Sebastian Redl539c5062010-08-18 23:57:32 +00003290 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003291 unsigned Idx = 0;
3292 unsigned Depth = Record[Idx++];
3293 unsigned Index = Record[Idx++];
3294 bool Pack = Record[Idx++];
3295 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
3296 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
3297 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003298
Sebastian Redl539c5062010-08-18 23:57:32 +00003299 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003300 unsigned Idx = 0;
3301 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3302 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3303 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00003304 QualType Canon = GetType(Record[Idx++]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003305 if (!Canon.isNull())
3306 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00003307 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003308 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003309
Sebastian Redl539c5062010-08-18 23:57:32 +00003310 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003311 unsigned Idx = 0;
3312 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3313 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3314 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3315 unsigned NumArgs = Record[Idx++];
3316 llvm::SmallVector<TemplateArgument, 8> Args;
3317 Args.reserve(NumArgs);
3318 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003319 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003320 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3321 Args.size(), Args.data());
3322 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003323
Sebastian Redl539c5062010-08-18 23:57:32 +00003324 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003325 unsigned Idx = 0;
3326
3327 // ArrayType
3328 QualType ElementType = GetType(Record[Idx++]);
3329 ArrayType::ArraySizeModifier ASM
3330 = (ArrayType::ArraySizeModifier)Record[Idx++];
3331 unsigned IndexTypeQuals = Record[Idx++];
3332
3333 // DependentSizedArrayType
Sebastian Redl2c373b92010-10-05 15:59:54 +00003334 Expr *NumElts = ReadExpr(*Loc.F);
3335 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003336
3337 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3338 IndexTypeQuals, Brackets);
3339 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003340
Sebastian Redl539c5062010-08-18 23:57:32 +00003341 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003342 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003343 bool IsDependent = Record[Idx++];
Douglas Gregor5590be02011-01-15 06:45:20 +00003344 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003345 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003346 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003347 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003348 QualType T;
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003349 if (Canon.isNull())
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003350 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3351 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003352 else
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003353 T = Context->getTemplateSpecializationType(Name, Args.data(),
3354 Args.size(), Canon);
John McCall424cec92011-01-19 06:33:43 +00003355 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003356 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003357 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003358 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003359 // Suppress a GCC warning
3360 return QualType();
3361}
3362
Sebastian Redl2c373b92010-10-05 15:59:54 +00003363class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00003364 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003365 ASTReader::PerFileData &F;
Sebastian Redlc67764e2010-07-22 22:43:28 +00003366 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redl2c499f62010-08-18 23:56:43 +00003367 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00003368 unsigned &Idx;
3369
Sebastian Redl2c373b92010-10-05 15:59:54 +00003370 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3371 unsigned &I) {
3372 return Reader.ReadSourceLocation(F, R, I);
3373 }
3374
John McCall8f115c62009-10-16 21:56:05 +00003375public:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003376 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl2c499f62010-08-18 23:56:43 +00003377 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003378 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3379 { }
John McCall8f115c62009-10-16 21:56:05 +00003380
John McCall17001972009-10-18 01:05:36 +00003381 // We want compile-time assurance that we've enumerated all of
3382 // these, so unfortunately we have to declare them first, then
3383 // define them out-of-line.
3384#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00003385#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00003386 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00003387#include "clang/AST/TypeLocNodes.def"
3388
John McCall17001972009-10-18 01:05:36 +00003389 void VisitFunctionTypeLoc(FunctionTypeLoc);
3390 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00003391};
3392
John McCall17001972009-10-18 01:05:36 +00003393void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00003394 // nothing to do
3395}
John McCall17001972009-10-18 01:05:36 +00003396void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003397 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorc9b7a592010-01-18 18:04:31 +00003398 if (TL.needsExtraLocalData()) {
3399 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3400 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3401 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3402 TL.setModeAttr(Record[Idx++]);
3403 }
John McCall8f115c62009-10-16 21:56:05 +00003404}
John McCall17001972009-10-18 01:05:36 +00003405void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003406 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003407}
John McCall17001972009-10-18 01:05:36 +00003408void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003409 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003410}
John McCall17001972009-10-18 01:05:36 +00003411void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003412 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003413}
John McCall17001972009-10-18 01:05:36 +00003414void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003415 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003416}
John McCall17001972009-10-18 01:05:36 +00003417void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003418 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003419}
John McCall17001972009-10-18 01:05:36 +00003420void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003421 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara509357842011-03-05 14:42:21 +00003422 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003423}
John McCall17001972009-10-18 01:05:36 +00003424void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003425 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3426 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003427 if (Record[Idx++])
Sebastian Redl2c373b92010-10-05 15:59:54 +00003428 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003429 else
John McCall17001972009-10-18 01:05:36 +00003430 TL.setSizeExpr(0);
3431}
3432void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3433 VisitArrayTypeLoc(TL);
3434}
3435void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3436 VisitArrayTypeLoc(TL);
3437}
3438void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3439 VisitArrayTypeLoc(TL);
3440}
3441void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3442 DependentSizedArrayTypeLoc TL) {
3443 VisitArrayTypeLoc(TL);
3444}
3445void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3446 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003447 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003448}
3449void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003450 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003451}
3452void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003453 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003454}
3455void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003456 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
3457 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Douglas Gregor7fb25412010-10-01 18:44:50 +00003458 TL.setTrailingReturn(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00003459 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCalle6347002009-10-23 01:28:53 +00003460 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall17001972009-10-18 01:05:36 +00003461 }
3462}
3463void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3464 VisitFunctionTypeLoc(TL);
3465}
3466void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3467 VisitFunctionTypeLoc(TL);
3468}
John McCallb96ec562009-12-04 22:46:56 +00003469void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003470 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallb96ec562009-12-04 22:46:56 +00003471}
John McCall17001972009-10-18 01:05:36 +00003472void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003473 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003474}
3475void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003476 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3477 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3478 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003479}
3480void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003481 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3482 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3483 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3484 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003485}
3486void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003487 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003488}
Richard Smith30482bc2011-02-20 03:19:35 +00003489void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3490 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3491}
John McCall17001972009-10-18 01:05:36 +00003492void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003493 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003494}
3495void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003496 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003497}
John McCall81904512011-01-06 01:58:22 +00003498void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3499 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3500 if (TL.hasAttrOperand()) {
3501 SourceRange range;
3502 range.setBegin(ReadSourceLocation(Record, Idx));
3503 range.setEnd(ReadSourceLocation(Record, Idx));
3504 TL.setAttrOperandParensRange(range);
3505 }
3506 if (TL.hasAttrExprOperand()) {
3507 if (Record[Idx++])
3508 TL.setAttrExprOperand(Reader.ReadExpr(F));
3509 else
3510 TL.setAttrExprOperand(0);
3511 } else if (TL.hasAttrEnumOperand())
3512 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3513}
John McCall17001972009-10-18 01:05:36 +00003514void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003515 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003516}
John McCallcebee162009-10-18 09:09:24 +00003517void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3518 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003519 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallcebee162009-10-18 09:09:24 +00003520}
Douglas Gregorada4b792011-01-14 02:55:32 +00003521void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3522 SubstTemplateTypeParmPackTypeLoc TL) {
3523 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3524}
John McCall17001972009-10-18 01:05:36 +00003525void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3526 TemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003527 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3528 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3529 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall0ad16662009-10-29 08:12:44 +00003530 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3531 TL.setArgLocInfo(i,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003532 Reader.GetTemplateArgumentLocInfo(F,
3533 TL.getTypePtr()->getArg(i).getKind(),
3534 Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003535}
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003536void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3537 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3538 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3539}
Abramo Bagnara6150c882010-05-11 21:36:43 +00003540void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003541 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor844cb502011-03-01 18:12:44 +00003542 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003543}
John McCalle78aac42010-03-10 03:28:59 +00003544void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003545 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalle78aac42010-03-10 03:28:59 +00003546}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00003547void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003548 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor3d0da5f2011-03-01 01:34:45 +00003549 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00003550 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003551}
John McCallc392f372010-06-11 00:33:02 +00003552void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3553 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003554 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregora7a795b2011-03-01 20:11:18 +00003555 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00003556 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3557 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3558 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003559 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3560 TL.setArgLocInfo(I,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003561 Reader.GetTemplateArgumentLocInfo(F,
3562 TL.getTypePtr()->getArg(I).getKind(),
3563 Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003564}
Douglas Gregord2fa7662010-12-20 02:24:11 +00003565void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3566 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3567}
John McCall17001972009-10-18 01:05:36 +00003568void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003569 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8b07ec22010-05-15 11:32:37 +00003570}
3571void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3572 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003573 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3574 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003575 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003576 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003577}
John McCallfc93cf92009-10-22 22:37:11 +00003578void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003579 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCallfc93cf92009-10-22 22:37:11 +00003580}
John McCall8f115c62009-10-16 21:56:05 +00003581
Sebastian Redl2c373b92010-10-05 15:59:54 +00003582TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003583 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00003584 unsigned &Idx) {
3585 QualType InfoTy = GetType(Record[Idx++]);
3586 if (InfoTy.isNull())
3587 return 0;
3588
John McCallbcd03502009-12-07 02:54:59 +00003589 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003590 TypeLocReader TLR(*this, F, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00003591 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00003592 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00003593 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00003594}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003595
Sebastian Redl539c5062010-08-18 23:57:32 +00003596QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00003597 unsigned FastQuals = ID & Qualifiers::FastMask;
3598 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003599
Sebastian Redl539c5062010-08-18 23:57:32 +00003600 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003601 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00003602 switch ((PredefinedTypeIDs)Index) {
3603 case PREDEF_TYPE_NULL_ID: return QualType();
3604 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3605 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003606
Sebastian Redl539c5062010-08-18 23:57:32 +00003607 case PREDEF_TYPE_CHAR_U_ID:
3608 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003609 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00003610 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003611 break;
3612
Sebastian Redl539c5062010-08-18 23:57:32 +00003613 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3614 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3615 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3616 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3617 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3618 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3619 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3620 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3621 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3622 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3623 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3624 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3625 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3626 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3627 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3628 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3629 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3630 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
John McCall31996342011-04-07 08:22:57 +00003631 case PREDEF_TYPE_UNKNOWN_ANY: T = Context->UnknownAnyTy; break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003632 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3633 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3634 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3635 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3636 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3637 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003638 }
3639
3640 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00003641 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003642 }
3643
Sebastian Redl539c5062010-08-18 23:57:32 +00003644 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003645 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00003646 if (TypesLoaded[Index].isNull()) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003647 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003648 if (TypesLoaded[Index].isNull())
3649 return QualType();
3650
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003651 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003652 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003653 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003654 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003655 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00003656 }
Mike Stump11289f42009-09-09 15:08:12 +00003657
John McCall8ccfcb52009-09-24 19:53:00 +00003658 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003659}
3660
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003661TypeID ASTReader::GetTypeID(QualType T) const {
3662 return MakeTypeID(T,
3663 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3664}
3665
3666TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3667 if (T.isNull())
3668 return TypeIdx();
3669 assert(!T.getLocalFastQualifiers());
3670
3671 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3672 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3673 // comparing keys of ASTDeclContextNameLookupTable.
3674 // If the type didn't come from the AST file use a specially marked index
3675 // so that any hash/key comparison fail since no such index is stored
3676 // in a AST file.
3677 if (I == TypeIdxs.end())
3678 return TypeIdx(-1);
3679 return I->second;
3680}
3681
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003682unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3683 unsigned Result = 0;
3684 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3685 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3686
3687 return Result;
3688}
3689
John McCall0ad16662009-10-29 08:12:44 +00003690TemplateArgumentLocInfo
Sebastian Redl2c373b92010-10-05 15:59:54 +00003691ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3692 TemplateArgument::ArgKind Kind,
John McCall0ad16662009-10-29 08:12:44 +00003693 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003694 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00003695 switch (Kind) {
3696 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003697 return ReadExpr(F);
John McCall0ad16662009-10-29 08:12:44 +00003698 case TemplateArgument::Type:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003699 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003700 case TemplateArgument::Template: {
Douglas Gregor9d802122011-03-02 17:09:35 +00003701 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3702 Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003703 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00003704 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003705 SourceLocation());
3706 }
3707 case TemplateArgument::TemplateExpansion: {
Douglas Gregor9d802122011-03-02 17:09:35 +00003708 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3709 Index);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003710 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003711 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00003712 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregoreb29d182011-01-05 17:40:24 +00003713 EllipsisLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003714 }
John McCall0ad16662009-10-29 08:12:44 +00003715 case TemplateArgument::Null:
3716 case TemplateArgument::Integral:
3717 case TemplateArgument::Declaration:
3718 case TemplateArgument::Pack:
3719 return TemplateArgumentLocInfo();
3720 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00003721 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00003722 return TemplateArgumentLocInfo();
3723}
3724
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003725TemplateArgumentLoc
Sebastian Redl2c373b92010-10-05 15:59:54 +00003726ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003727 const RecordData &Record, unsigned &Index) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003728 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003729
3730 if (Arg.getKind() == TemplateArgument::Expression) {
3731 if (Record[Index++]) // bool InfoHasSameExpr.
3732 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3733 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00003734 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003735 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003736}
3737
Sebastian Redl2c499f62010-08-18 23:56:43 +00003738Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00003739 return GetDecl(ID);
3740}
3741
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003742uint64_t
3743ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3744 if (ID == 0)
3745 return 0;
3746
3747 --ID;
3748 uint64_t Offset = 0;
3749 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003750 PerFileData &F = *Chain[N - I - 1];
3751
3752 if (ID < F.LocalNumCXXBaseSpecifiers)
3753 return Offset + F.CXXBaseSpecifiersOffsets[ID];
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003754
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003755 ID -= F.LocalNumCXXBaseSpecifiers;
3756 Offset += F.SizeInBits;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003757 }
3758
3759 assert(false && "CXXBaseSpecifiers not found");
3760 return 0;
3761}
3762
3763CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3764 // Figure out which AST file contains this offset.
3765 PerFileData *F = 0;
3766 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003767 if (Offset < Chain[N - I - 1]->SizeInBits) {
3768 F = Chain[N - I - 1];
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003769 break;
3770 }
3771
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003772 Offset -= Chain[N - I - 1]->SizeInBits;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003773 }
Anders Carlsson1f13bb52011-03-09 05:09:32 +00003774
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003775 if (!F) {
3776 Error("Malformed AST file: C++ base specifiers at impossible offset");
3777 return 0;
3778 }
3779
3780 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3781 SavedStreamPosition SavedPosition(Cursor);
3782 Cursor.JumpToBit(Offset);
3783 ReadingKindTracker ReadingKind(Read_Decl, *this);
3784 RecordData Record;
3785 unsigned Code = Cursor.ReadCode();
3786 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3787 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3788 Error("Malformed AST file: missing C++ base specifiers");
3789 return 0;
3790 }
3791
3792 unsigned Idx = 0;
3793 unsigned NumBases = Record[Idx++];
3794 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3795 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3796 for (unsigned I = 0; I != NumBases; ++I)
3797 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3798 return Bases;
3799}
3800
Sebastian Redl2c499f62010-08-18 23:56:43 +00003801TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003802 if (!DeclsLoaded[0]) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00003803 ReadDeclRecord(0, 1);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003804 if (DeserializationListener)
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003805 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003806 }
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00003807
3808 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3809}
3810
Sebastian Redl539c5062010-08-18 23:57:32 +00003811Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003812 if (ID == 0)
3813 return 0;
3814
Douglas Gregor745ed142009-04-25 18:35:21 +00003815 if (ID > DeclsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003816 Error("declaration ID out-of-range for AST file");
Douglas Gregor745ed142009-04-25 18:35:21 +00003817 return 0;
3818 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003819
Douglas Gregor745ed142009-04-25 18:35:21 +00003820 unsigned Index = ID - 1;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003821 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003822 ReadDeclRecord(Index, ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003823 if (DeserializationListener)
3824 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3825 }
Douglas Gregor745ed142009-04-25 18:35:21 +00003826
3827 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003828}
3829
Chris Lattner9c28af02009-04-27 05:46:25 +00003830/// \brief Resolve the offset of a statement into a statement.
3831///
3832/// This operation will read a new statement from the external
3833/// source each time it is called, and is meant to be used via a
3834/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00003835Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00003836 // Switch case IDs are per Decl.
3837 ClearSwitchCaseIDs();
3838
Sebastian Redl5c415f32010-07-22 17:01:13 +00003839 // Offset here is a global offset across the entire chain.
3840 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3841 PerFileData &F = *Chain[N - I - 1];
3842 if (Offset < F.SizeInBits) {
3843 // Since we know that this statement is part of a decl, make sure to use
3844 // the decl cursor to read it.
3845 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003846 return ReadStmtFromStream(F);
Sebastian Redl5c415f32010-07-22 17:01:13 +00003847 }
3848 Offset -= F.SizeInBits;
3849 }
3850 llvm_unreachable("Broken chain");
Douglas Gregor3c3aa612009-04-18 00:07:54 +00003851}
3852
Sebastian Redl2c499f62010-08-18 23:56:43 +00003853bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003854 bool (*isKindWeWant)(Decl::Kind),
John McCall75b960e2010-06-01 09:23:16 +00003855 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump11289f42009-09-09 15:08:12 +00003856 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003857 "DeclContext has no lexical decls in storage");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003858
Sebastian Redl5c415f32010-07-22 17:01:13 +00003859 // There might be lexical decls in multiple parts of the chain, for the TU
3860 // at least.
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003861 // DeclContextOffsets might reallocate as we load additional decls below,
3862 // so make a copy of the vector.
3863 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl5c415f32010-07-22 17:01:13 +00003864 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3865 I != E; ++I) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003866 // IDs can be 0 if this context doesn't contain declarations.
3867 if (!I->LexicalDecls)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003868 continue;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003869
3870 // Load all of the declaration IDs
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003871 for (const KindDeclIDPair *ID = I->LexicalDecls,
3872 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3873 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3874 continue;
3875
3876 Decl *D = GetDecl(ID->second);
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003877 assert(D && "Null decl in lexical decls");
3878 Decls.push_back(D);
3879 }
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003880 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003881
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003882 ++NumLexicalDeclContextsRead;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003883 return false;
3884}
3885
John McCall75b960e2010-06-01 09:23:16 +00003886DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00003887ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00003888 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003889 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003890 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003891 if (!Name)
3892 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3893 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003894
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003895 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl471ac2f2010-08-24 00:49:55 +00003896 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003897 // and namespaces. For any given name, the last available results replace
3898 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl5c415f32010-07-22 17:01:13 +00003899 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003900 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl5c415f32010-07-22 17:01:13 +00003901 I != E; ++I) {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003902 if (!I->NameLookupTableData)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003903 continue;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003904
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003905 ASTDeclContextNameLookupTable *LookupTable =
3906 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3907 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3908 if (Pos == LookupTable->end())
Sebastian Redl5c415f32010-07-22 17:01:13 +00003909 continue;
3910
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003911 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3912 for (; Data.first != Data.second; ++Data.first)
3913 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003914 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003915 }
3916
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003917 ++NumVisibleDeclContextsRead;
John McCall75b960e2010-06-01 09:23:16 +00003918
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003919 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00003920 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003921}
3922
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00003923void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3924 assert(DC->hasExternalVisibleStorage() &&
3925 "DeclContext has no visible decls in storage");
3926
3927 llvm::SmallVector<NamedDecl *, 64> Decls;
3928 // There might be visible decls in multiple parts of the chain, for the TU
3929 // and namespaces.
3930 DeclContextInfos &Infos = DeclContextOffsets[DC];
3931 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3932 I != E; ++I) {
3933 if (!I->NameLookupTableData)
3934 continue;
3935
3936 ASTDeclContextNameLookupTable *LookupTable =
3937 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3938 for (ASTDeclContextNameLookupTable::item_iterator
3939 ItemI = LookupTable->item_begin(),
3940 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3941 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3942 = *ItemI;
3943 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3944 Decls.clear();
3945 for (; Data.first != Data.second; ++Data.first)
3946 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3947 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3948 }
3949 }
3950}
3951
Sebastian Redl2c499f62010-08-18 23:56:43 +00003952void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003953 assert(Consumer);
3954 while (!InterestingDecls.empty()) {
3955 DeclGroupRef DG(InterestingDecls.front());
3956 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00003957 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003958 }
3959}
3960
Sebastian Redl2c499f62010-08-18 23:56:43 +00003961void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00003962 this->Consumer = Consumer;
3963
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003964 if (!Consumer)
3965 return;
3966
3967 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003968 // Force deserialization of this decl, which will cause it to be queued for
3969 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00003970 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003971 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00003972
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003973 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003974}
3975
Sebastian Redl2c499f62010-08-18 23:56:43 +00003976void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003977 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003978
Mike Stump11289f42009-09-09 15:08:12 +00003979 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003980 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00003981 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00003982 unsigned NumDeclsLoaded
3983 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3984 (Decl *)0);
3985 unsigned NumIdentifiersLoaded
3986 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3987 IdentifiersLoaded.end(),
3988 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00003989 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003990 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3991 SelectorsLoaded.end(),
3992 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00003993
Douglas Gregorc5046832009-04-27 18:38:38 +00003994 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3995 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor258ae542009-04-27 06:38:32 +00003996 if (TotalNumSLocEntries)
3997 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3998 NumSLocEntriesRead, TotalNumSLocEntries,
3999 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00004000 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004001 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00004002 NumTypesLoaded, (unsigned)TypesLoaded.size(),
4003 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
4004 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004005 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00004006 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
4007 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00004008 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004009 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00004010 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
4011 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00004012 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004013 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00004014 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
4015 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00004016 if (TotalNumStatements)
4017 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
4018 NumStatementsRead, TotalNumStatements,
4019 ((float)NumStatementsRead/TotalNumStatements * 100));
4020 if (TotalNumMacros)
4021 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
4022 NumMacrosRead, TotalNumMacros,
4023 ((float)NumMacrosRead/TotalNumMacros * 100));
4024 if (TotalLexicalDeclContexts)
4025 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
4026 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4027 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4028 * 100));
4029 if (TotalVisibleDeclContexts)
4030 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
4031 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4032 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4033 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004034 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004035 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004036 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4037 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00004038 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004039 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00004040 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004041 std::fprintf(stderr, "\n");
4042}
4043
Sebastian Redl2c499f62010-08-18 23:56:43 +00004044void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004045 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004046 S.ExternalSource = this;
4047
Douglas Gregor7cd60f72009-04-22 21:15:06 +00004048 // Makes sure any declarations that were deserialized "too early"
4049 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004050 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4051 if (SemaObj->TUScope)
John McCall48871652010-08-21 09:40:31 +00004052 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004053
4054 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004055 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00004056 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00004057
4058 // If there were any tentative definitions, deserialize them and add
Sebastian Redl35351a92010-01-31 22:27:38 +00004059 // them to Sema's list of tentative definitions.
Douglas Gregord4df8652009-04-22 22:02:47 +00004060 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4061 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redl35351a92010-01-31 22:27:38 +00004062 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregord4df8652009-04-22 22:02:47 +00004063 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00004064
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004065 // If there were any unused file scoped decls, deserialize them and add to
4066 // Sema's list of unused file scoped decls.
4067 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4068 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4069 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattner90073802010-02-12 00:07:30 +00004070 }
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004071
4072 // If there were any locally-scoped external declarations,
4073 // deserialize them and add them to Sema's table of locally-scoped
4074 // external declarations.
4075 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4076 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4077 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
4078 }
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004079
4080 // If there were any ext_vector type declarations, deserialize them
4081 // and add them to Sema's vector of such declarations.
4082 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
4083 SemaObj->ExtVectorDecls.push_back(
4084 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004085
4086 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
4087 // Can we cut them down before writing them ?
4088
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004089 // If there were any dynamic classes declarations, deserialize them
4090 // and add them to Sema's vector of such declarations.
4091 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
4092 SemaObj->DynamicClasses.push_back(
4093 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004094
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004095 // Load the offsets of the declarations that Sema references.
4096 // They will be lazily deserialized when needed.
4097 if (!SemaDeclRefs.empty()) {
4098 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
4099 SemaObj->StdNamespace = SemaDeclRefs[0];
4100 SemaObj->StdBadAlloc = SemaDeclRefs[1];
4101 }
4102
Sebastian Redl2c373b92010-10-05 15:59:54 +00004103 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
4104
4105 // If there are @selector references added them to its pool. This is for
4106 // implementation of -Wselector.
4107 if (!F->ReferencedSelectorsData.empty()) {
4108 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
4109 unsigned I = 0;
4110 while (I < DataSize) {
4111 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
4112 SourceLocation SelLoc = ReadSourceLocation(
4113 *F, F->ReferencedSelectorsData, I);
4114 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
4115 }
4116 }
4117
4118 // If there were any pending implicit instantiations, deserialize them
4119 // and add them to Sema's queue of such instantiations.
4120 assert(F->PendingInstantiations.size() % 2 == 0 &&
4121 "Expected pairs of entries");
4122 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
4123 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
4124 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
4125 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
4126 }
4127 }
4128
4129 // The two special data sets below always come from the most recent PCH,
4130 // which is at the front of the chain.
4131 PerFileData &F = *Chain.front();
4132
4133 // If there were any weak undeclared identifiers, deserialize them and add to
4134 // Sema's list of weak undeclared identifiers.
4135 if (!WeakUndeclaredIdentifiers.empty()) {
4136 unsigned Idx = 0;
4137 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
4138 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4139 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4140 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
4141 bool Used = WeakUndeclaredIdentifiers[Idx++];
4142 Sema::WeakInfo WI(AliasId, Loc);
4143 WI.setUsed(Used);
4144 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
4145 }
4146 }
4147
4148 // If there were any VTable uses, deserialize the information and add it
4149 // to Sema's vector and map of VTable uses.
4150 if (!VTableUses.empty()) {
4151 unsigned Idx = 0;
4152 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
4153 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4154 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
4155 bool DefinitionRequired = VTableUses[Idx++];
4156 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
4157 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004158 }
4159 }
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004160
4161 if (!FPPragmaOptions.empty()) {
4162 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4163 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4164 }
4165
4166 if (!OpenCLExtensions.empty()) {
4167 unsigned I = 0;
4168#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4169#include "clang/Basic/OpenCLExtensions.def"
4170
4171 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4172 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00004173}
4174
Sebastian Redl2c499f62010-08-18 23:56:43 +00004175IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redl78f51772010-08-02 18:30:12 +00004176 // Try to find this name within our on-disk hash tables. We start with the
4177 // most recent one, since that one contains the most up-to-date info.
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004178 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004179 ASTIdentifierLookupTable *IdTable
4180 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl5c415f32010-07-22 17:01:13 +00004181 if (!IdTable)
4182 continue;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004183 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004184 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004185 if (Pos == IdTable->end())
4186 continue;
Douglas Gregora868bbd2009-04-21 22:25:48 +00004187
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004188 // Dereferencing the iterator has the effect of building the
4189 // IdentifierInfo node and populating it with the various
4190 // declarations it needs.
Sebastian Redl78f51772010-08-02 18:30:12 +00004191 return *Pos;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004192 }
Sebastian Redl78f51772010-08-02 18:30:12 +00004193 return 0;
Douglas Gregora868bbd2009-04-21 22:25:48 +00004194}
4195
Douglas Gregor57756ea2010-10-14 22:11:03 +00004196namespace clang {
4197 /// \brief An identifier-lookup iterator that enumerates all of the
4198 /// identifiers stored within a set of AST files.
4199 class ASTIdentifierIterator : public IdentifierIterator {
4200 /// \brief The AST reader whose identifiers are being enumerated.
4201 const ASTReader &Reader;
4202
4203 /// \brief The current index into the chain of AST files stored in
4204 /// the AST reader.
4205 unsigned Index;
4206
4207 /// \brief The current position within the identifier lookup table
4208 /// of the current AST file.
4209 ASTIdentifierLookupTable::key_iterator Current;
4210
4211 /// \brief The end position within the identifier lookup table of
4212 /// the current AST file.
4213 ASTIdentifierLookupTable::key_iterator End;
4214
4215 public:
4216 explicit ASTIdentifierIterator(const ASTReader &Reader);
4217
4218 virtual llvm::StringRef Next();
4219 };
4220}
4221
4222ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
4223 : Reader(Reader), Index(Reader.Chain.size() - 1) {
4224 ASTIdentifierLookupTable *IdTable
4225 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4226 Current = IdTable->key_begin();
4227 End = IdTable->key_end();
4228}
4229
4230llvm::StringRef ASTIdentifierIterator::Next() {
4231 while (Current == End) {
4232 // If we have exhausted all of our AST files, we're done.
4233 if (Index == 0)
4234 return llvm::StringRef();
4235
4236 --Index;
4237 ASTIdentifierLookupTable *IdTable
4238 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4239 Current = IdTable->key_begin();
4240 End = IdTable->key_end();
4241 }
4242
4243 // We have any identifiers remaining in the current AST file; return
4244 // the next one.
4245 std::pair<const char*, unsigned> Key = *Current;
4246 ++Current;
4247 return llvm::StringRef(Key.first, Key.second);
4248}
4249
4250IdentifierIterator *ASTReader::getIdentifiers() const {
4251 return new ASTIdentifierIterator(*this);
4252}
4253
Mike Stump11289f42009-09-09 15:08:12 +00004254std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redl2c499f62010-08-18 23:56:43 +00004255ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redlada023c2010-08-04 20:40:17 +00004256 // Find this selector in a hash table. We want to find the most recent entry.
4257 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4258 PerFileData &F = *Chain[I];
4259 if (!F.SelectorLookupTable)
4260 continue;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004261
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004262 ASTSelectorLookupTable *PoolTable
4263 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
4264 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redlada023c2010-08-04 20:40:17 +00004265 if (Pos != PoolTable->end()) {
4266 ++NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004267 // FIXME: Not quite happy with the statistics here. We probably should
4268 // disable this tracking when called via LoadSelector.
4269 // Also, should entries without methods count as misses?
4270 ++NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004271 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redlada023c2010-08-04 20:40:17 +00004272 if (DeserializationListener)
4273 DeserializationListener->SelectorRead(Data.ID, Sel);
4274 return std::make_pair(Data.Instance, Data.Factory);
4275 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004276 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00004277
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004278 ++NumMethodPoolMisses;
Sebastian Redlada023c2010-08-04 20:40:17 +00004279 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorc78d3462009-04-24 21:10:55 +00004280}
4281
Sebastian Redl2c499f62010-08-18 23:56:43 +00004282void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004283 // It would be complicated to avoid reading the methods anyway. So don't.
4284 ReadMethodPool(Sel);
4285}
4286
Sebastian Redl2c499f62010-08-18 23:56:43 +00004287void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004288 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00004289 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00004290 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00004291 if (DeserializationListener)
4292 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004293}
4294
Douglas Gregor1342e842009-07-06 18:54:52 +00004295/// \brief Set the globally-visible declarations associated with the given
4296/// identifier.
4297///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004298/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00004299/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00004300/// them.
4301///
4302/// \param II an IdentifierInfo that refers to one or more globally-visible
4303/// declarations.
4304///
4305/// \param DeclIDs the set of declaration IDs with the name @p II that are
4306/// visible at global scope.
4307///
4308/// \param Nonrecursive should be true to indicate that the caller knows that
4309/// this call is non-recursive, and therefore the globally-visible declarations
4310/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00004311void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004312ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregor1342e842009-07-06 18:54:52 +00004313 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
4314 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004315 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004316 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4317 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4318 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004319 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00004320 return;
4321 }
Mike Stump11289f42009-09-09 15:08:12 +00004322
Douglas Gregor1342e842009-07-06 18:54:52 +00004323 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4324 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4325 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004326 if (SemaObj->TUScope) {
4327 // Introduce this declaration into the translation-unit scope
4328 // and add it to the declaration chain for this identifier, so
4329 // that (unqualified) name lookup will find it.
John McCall48871652010-08-21 09:40:31 +00004330 SemaObj->TUScope->AddDecl(D);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004331 }
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004332 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregor1342e842009-07-06 18:54:52 +00004333 } else {
4334 // Queue this declaration so that it will be added to the
4335 // translation unit scope and identifier's declaration chain
4336 // once a Sema object is known.
4337 PreloadedDecls.push_back(D);
4338 }
4339 }
4340}
4341
Sebastian Redl2c499f62010-08-18 23:56:43 +00004342IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004343 if (ID == 0)
4344 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00004345
Sebastian Redlc713b962010-07-21 00:46:22 +00004346 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004347 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004348 return 0;
4349 }
Mike Stump11289f42009-09-09 15:08:12 +00004350
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004351 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc713b962010-07-21 00:46:22 +00004352 ID -= 1;
4353 if (!IdentifiersLoaded[ID]) {
4354 unsigned Index = ID;
4355 const char *Str = 0;
4356 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4357 PerFileData *F = Chain[N - I - 1];
4358 if (Index < F->LocalNumIdentifiers) {
4359 uint32_t Offset = F->IdentifierOffsets[Index];
4360 Str = F->IdentifierTableData + Offset;
4361 break;
4362 }
4363 Index -= F->LocalNumIdentifiers;
4364 }
4365 assert(Str && "Broken Chain");
Douglas Gregor5287b4e2009-04-25 21:04:17 +00004366
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004367 // All of the strings in the AST file are preceded by a 16-bit length.
4368 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00004369 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4370 // unsigned integers. This is important to avoid integer overflow when
4371 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00004372 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00004373 unsigned StrLen = (((unsigned) StrLenPtr[0])
4374 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00004375 IdentifiersLoaded[ID]
Kovarththanan Rajaratnama3b09592010-03-12 10:32:27 +00004376 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlff4a2952010-07-23 23:49:55 +00004377 if (DeserializationListener)
4378 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004379 }
Mike Stump11289f42009-09-09 15:08:12 +00004380
Sebastian Redlc713b962010-07-21 00:46:22 +00004381 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004382}
4383
Sebastian Redl2c499f62010-08-18 23:56:43 +00004384void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00004385 ReadSLocEntryRecord(ID);
4386}
4387
Sebastian Redl2c499f62010-08-18 23:56:43 +00004388Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00004389 if (ID == 0)
4390 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00004391
Sebastian Redlada023c2010-08-04 20:40:17 +00004392 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004393 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00004394 return Selector();
4395 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004396
Sebastian Redlada023c2010-08-04 20:40:17 +00004397 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004398 // Load this selector from the selector table.
Sebastian Redlada023c2010-08-04 20:40:17 +00004399 unsigned Idx = ID - 1;
4400 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4401 PerFileData &F = *Chain[N - I - 1];
4402 if (Idx < F.LocalNumSelectors) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004403 ASTSelectorLookupTrait Trait(*this);
Sebastian Redlada023c2010-08-04 20:40:17 +00004404 SelectorsLoaded[ID - 1] =
4405 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4406 if (DeserializationListener)
4407 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4408 break;
4409 }
4410 Idx -= F.LocalNumSelectors;
4411 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004412 }
4413
Sebastian Redlada023c2010-08-04 20:40:17 +00004414 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00004415}
4416
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004417Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00004418 return DecodeSelector(ID);
4419}
4420
Sebastian Redl2c499f62010-08-18 23:56:43 +00004421uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00004422 // ID 0 (the null selector) is considered an external selector.
4423 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00004424}
4425
Mike Stump11289f42009-09-09 15:08:12 +00004426DeclarationName
Sebastian Redl2c499f62010-08-18 23:56:43 +00004427ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004428 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4429 switch (Kind) {
4430 case DeclarationName::Identifier:
4431 return DeclarationName(GetIdentifierInfo(Record, Idx));
4432
4433 case DeclarationName::ObjCZeroArgSelector:
4434 case DeclarationName::ObjCOneArgSelector:
4435 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff3c301dc2009-04-23 15:15:40 +00004436 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004437
4438 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004439 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004440 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004441
4442 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004443 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004444 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004445
4446 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004447 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004448 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004449
4450 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004451 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004452 (OverloadedOperatorKind)Record[Idx++]);
4453
Alexis Hunt3d221f22009-11-29 07:34:05 +00004454 case DeclarationName::CXXLiteralOperatorName:
4455 return Context->DeclarationNames.getCXXLiteralOperatorName(
4456 GetIdentifierInfo(Record, Idx));
4457
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004458 case DeclarationName::CXXUsingDirective:
4459 return DeclarationName::getUsingDirectiveName();
4460 }
4461
4462 // Required to silence GCC warning
4463 return DeclarationName();
4464}
Douglas Gregor55abb232009-04-10 20:39:37 +00004465
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004466void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4467 DeclarationNameLoc &DNLoc,
4468 DeclarationName Name,
4469 const RecordData &Record, unsigned &Idx) {
4470 switch (Name.getNameKind()) {
4471 case DeclarationName::CXXConstructorName:
4472 case DeclarationName::CXXDestructorName:
4473 case DeclarationName::CXXConversionFunctionName:
4474 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4475 break;
4476
4477 case DeclarationName::CXXOperatorName:
4478 DNLoc.CXXOperatorName.BeginOpNameLoc
4479 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4480 DNLoc.CXXOperatorName.EndOpNameLoc
4481 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4482 break;
4483
4484 case DeclarationName::CXXLiteralOperatorName:
4485 DNLoc.CXXLiteralOperatorName.OpNameLoc
4486 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4487 break;
4488
4489 case DeclarationName::Identifier:
4490 case DeclarationName::ObjCZeroArgSelector:
4491 case DeclarationName::ObjCOneArgSelector:
4492 case DeclarationName::ObjCMultiArgSelector:
4493 case DeclarationName::CXXUsingDirective:
4494 break;
4495 }
4496}
4497
4498void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4499 DeclarationNameInfo &NameInfo,
4500 const RecordData &Record, unsigned &Idx) {
4501 NameInfo.setName(ReadDeclarationName(Record, Idx));
4502 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4503 DeclarationNameLoc DNLoc;
4504 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4505 NameInfo.setInfo(DNLoc);
4506}
4507
4508void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4509 const RecordData &Record, unsigned &Idx) {
Douglas Gregor14454802011-02-25 02:25:35 +00004510 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004511 unsigned NumTPLists = Record[Idx++];
4512 Info.NumTemplParamLists = NumTPLists;
4513 if (NumTPLists) {
4514 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4515 for (unsigned i=0; i != NumTPLists; ++i)
4516 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4517 }
4518}
4519
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004520TemplateName
Douglas Gregor5590be02011-01-15 06:45:20 +00004521ASTReader::ReadTemplateName(PerFileData &F, const RecordData &Record,
4522 unsigned &Idx) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004523 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004524 switch (Kind) {
4525 case TemplateName::Template:
4526 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4527
4528 case TemplateName::OverloadedTemplate: {
4529 unsigned size = Record[Idx++];
4530 UnresolvedSet<8> Decls;
4531 while (size--)
4532 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4533
4534 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4535 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004536
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004537 case TemplateName::QualifiedTemplate: {
4538 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4539 bool hasTemplKeyword = Record[Idx++];
4540 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4541 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4542 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004543
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004544 case TemplateName::DependentTemplate: {
4545 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4546 if (Record[Idx++]) // isIdentifier
4547 return Context->getDependentTemplateName(NNS,
4548 GetIdentifierInfo(Record, Idx));
4549 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004550 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004551 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004552
4553 case TemplateName::SubstTemplateTemplateParmPack: {
4554 TemplateTemplateParmDecl *Param
4555 = cast_or_null<TemplateTemplateParmDecl>(GetDecl(Record[Idx++]));
4556 if (!Param)
4557 return TemplateName();
4558
4559 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
4560 if (ArgPack.getKind() != TemplateArgument::Pack)
4561 return TemplateName();
4562
4563 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
4564 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004565 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004566
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004567 assert(0 && "Unhandled template name kind!");
4568 return TemplateName();
4569}
4570
4571TemplateArgument
Sebastian Redl2c373b92010-10-05 15:59:54 +00004572ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00004573 const RecordData &Record, unsigned &Idx) {
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004574 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
4575 switch (Kind) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004576 case TemplateArgument::Null:
4577 return TemplateArgument();
4578 case TemplateArgument::Type:
4579 return TemplateArgument(GetType(Record[Idx++]));
4580 case TemplateArgument::Declaration:
4581 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00004582 case TemplateArgument::Integral: {
4583 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4584 QualType T = GetType(Record[Idx++]);
4585 return TemplateArgument(Value, T);
4586 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004587 case TemplateArgument::Template:
Douglas Gregor5590be02011-01-15 06:45:20 +00004588 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004589 case TemplateArgument::TemplateExpansion: {
Douglas Gregor5590be02011-01-15 06:45:20 +00004590 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregore1d60df2011-01-14 23:41:42 +00004591 llvm::Optional<unsigned> NumTemplateExpansions;
4592 if (unsigned NumExpansions = Record[Idx++])
4593 NumTemplateExpansions = NumExpansions - 1;
4594 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004595 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004596 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004597 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004598 case TemplateArgument::Pack: {
4599 unsigned NumArgs = Record[Idx++];
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004600 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4601 for (unsigned I = 0; I != NumArgs; ++I)
4602 Args[I] = ReadTemplateArgument(F, Record, Idx);
4603 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004604 }
4605 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004606
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004607 assert(0 && "Unhandled template argument kind!");
4608 return TemplateArgument();
4609}
4610
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004611TemplateParameterList *
Sebastian Redl2c373b92010-10-05 15:59:54 +00004612ASTReader::ReadTemplateParameterList(PerFileData &F,
4613 const RecordData &Record, unsigned &Idx) {
4614 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4615 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4616 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004617
4618 unsigned NumParams = Record[Idx++];
4619 llvm::SmallVector<NamedDecl *, 16> Params;
4620 Params.reserve(NumParams);
4621 while (NumParams--)
4622 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004623
4624 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004625 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4626 Params.data(), Params.size(), RAngleLoc);
4627 return TemplateParams;
4628}
4629
4630void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004631ASTReader::
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004632ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004633 PerFileData &F, const RecordData &Record,
4634 unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004635 unsigned NumTemplateArgs = Record[Idx++];
4636 TemplArgs.reserve(NumTemplateArgs);
4637 while (NumTemplateArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004638 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004639}
4640
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004641/// \brief Read a UnresolvedSet structure.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004642void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004643 const RecordData &Record, unsigned &Idx) {
4644 unsigned NumDecls = Record[Idx++];
4645 while (NumDecls--) {
4646 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4647 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4648 Set.addDecl(D, AS);
4649 }
4650}
4651
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004652CXXBaseSpecifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00004653ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky19b9f952010-07-26 16:56:01 +00004654 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004655 bool isVirtual = static_cast<bool>(Record[Idx++]);
4656 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4657 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redl08905022011-02-05 19:23:19 +00004658 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004659 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4660 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor752a5952011-01-03 22:36:02 +00004661 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl08905022011-02-05 19:23:19 +00004662 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregor752a5952011-01-03 22:36:02 +00004663 EllipsisLoc);
Sebastian Redl08905022011-02-05 19:23:19 +00004664 Result.setInheritConstructors(inheritConstructors);
4665 return Result;
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004666}
4667
Alexis Hunt1d792652011-01-08 20:30:50 +00004668std::pair<CXXCtorInitializer **, unsigned>
4669ASTReader::ReadCXXCtorInitializers(PerFileData &F, const RecordData &Record,
4670 unsigned &Idx) {
4671 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004672 unsigned NumInitializers = Record[Idx++];
4673 if (NumInitializers) {
4674 ASTContext &C = *getContext();
4675
Alexis Hunt1d792652011-01-08 20:30:50 +00004676 CtorInitializers
4677 = new (C) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004678 for (unsigned i=0; i != NumInitializers; ++i) {
4679 TypeSourceInfo *BaseClassInfo = 0;
4680 bool IsBaseVirtual = false;
4681 FieldDecl *Member = 0;
Francois Pichetd583da02010-12-04 09:14:42 +00004682 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004683
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004684 bool IsBaseInitializer = Record[Idx++];
4685 if (IsBaseInitializer) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004686 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004687 IsBaseVirtual = Record[Idx++];
4688 } else {
Francois Pichetd583da02010-12-04 09:14:42 +00004689 bool IsIndirectMemberInitializer = Record[Idx++];
4690 if (IsIndirectMemberInitializer)
4691 IndirectMember = cast<IndirectFieldDecl>(GetDecl(Record[Idx++]));
4692 else
4693 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004694 }
Douglas Gregor44e7df62011-01-04 00:32:56 +00004695 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004696 Expr *Init = ReadExpr(F);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004697 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4698 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004699 bool IsWritten = Record[Idx++];
4700 unsigned SourceOrderOrNumArrayIndices;
4701 llvm::SmallVector<VarDecl *, 8> Indices;
4702 if (IsWritten) {
4703 SourceOrderOrNumArrayIndices = Record[Idx++];
4704 } else {
4705 SourceOrderOrNumArrayIndices = Record[Idx++];
4706 Indices.reserve(SourceOrderOrNumArrayIndices);
4707 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4708 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4709 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004710
Alexis Hunt1d792652011-01-08 20:30:50 +00004711 CXXCtorInitializer *BOMInit;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004712 if (IsBaseInitializer) {
Alexis Hunt1d792652011-01-08 20:30:50 +00004713 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
4714 LParenLoc, Init, RParenLoc,
4715 MemberOrEllipsisLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004716 } else if (IsWritten) {
Francois Pichetd583da02010-12-04 09:14:42 +00004717 if (Member)
Alexis Hunt1d792652011-01-08 20:30:50 +00004718 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
4719 LParenLoc, Init, RParenLoc);
Francois Pichetd583da02010-12-04 09:14:42 +00004720 else
Alexis Hunt1d792652011-01-08 20:30:50 +00004721 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
4722 MemberOrEllipsisLoc, LParenLoc,
4723 Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004724 } else {
Alexis Hunt1d792652011-01-08 20:30:50 +00004725 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
4726 LParenLoc, Init, RParenLoc,
4727 Indices.data(), Indices.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004728 }
4729
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00004730 if (IsWritten)
4731 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Alexis Hunt1d792652011-01-08 20:30:50 +00004732 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004733 }
4734 }
4735
Alexis Hunt1d792652011-01-08 20:30:50 +00004736 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004737}
4738
Chris Lattnerca025db2010-05-07 21:43:38 +00004739NestedNameSpecifier *
Sebastian Redl2c499f62010-08-18 23:56:43 +00004740ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004741 unsigned N = Record[Idx++];
4742 NestedNameSpecifier *NNS = 0, *Prev = 0;
4743 for (unsigned I = 0; I != N; ++I) {
4744 NestedNameSpecifier::SpecifierKind Kind
4745 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4746 switch (Kind) {
4747 case NestedNameSpecifier::Identifier: {
4748 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4749 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4750 break;
4751 }
4752
4753 case NestedNameSpecifier::Namespace: {
4754 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4755 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4756 break;
4757 }
4758
Douglas Gregor7b26ff92011-02-24 02:36:08 +00004759 case NestedNameSpecifier::NamespaceAlias: {
4760 NamespaceAliasDecl *Alias
4761 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++]));
4762 NNS = NestedNameSpecifier::Create(*Context, Prev, Alias);
4763 break;
4764 }
4765
Chris Lattnerca025db2010-05-07 21:43:38 +00004766 case NestedNameSpecifier::TypeSpec:
4767 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall424cec92011-01-19 06:33:43 +00004768 const Type *T = GetType(Record[Idx++]).getTypePtrOrNull();
Douglas Gregor0cdc8322010-12-10 17:03:06 +00004769 if (!T)
4770 return 0;
4771
Chris Lattnerca025db2010-05-07 21:43:38 +00004772 bool Template = Record[Idx++];
4773 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4774 break;
4775 }
4776
4777 case NestedNameSpecifier::Global: {
4778 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4779 // No associated value, and there can't be a prefix.
4780 break;
4781 }
Chris Lattnerca025db2010-05-07 21:43:38 +00004782 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00004783 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00004784 }
4785 return NNS;
4786}
4787
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004788NestedNameSpecifierLoc
4789ASTReader::ReadNestedNameSpecifierLoc(PerFileData &F, const RecordData &Record,
4790 unsigned &Idx) {
4791 unsigned N = Record[Idx++];
Douglas Gregor9b272512011-02-28 23:58:31 +00004792 NestedNameSpecifierLocBuilder Builder;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004793 for (unsigned I = 0; I != N; ++I) {
4794 NestedNameSpecifier::SpecifierKind Kind
4795 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4796 switch (Kind) {
4797 case NestedNameSpecifier::Identifier: {
Douglas Gregor9b272512011-02-28 23:58:31 +00004798 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004799 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00004800 Builder.Extend(*Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004801 break;
4802 }
4803
4804 case NestedNameSpecifier::Namespace: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004805 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004806 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00004807 Builder.Extend(*Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004808 break;
4809 }
4810
4811 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004812 NamespaceAliasDecl *Alias
4813 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++]));
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004814 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00004815 Builder.Extend(*Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004816 break;
4817 }
4818
4819 case NestedNameSpecifier::TypeSpec:
4820 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004821 bool Template = Record[Idx++];
4822 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
4823 if (!T)
4824 return NestedNameSpecifierLoc();
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004825 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00004826
4827 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
4828 Builder.Extend(*Context,
4829 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
4830 T->getTypeLoc(), ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004831 break;
4832 }
4833
4834 case NestedNameSpecifier::Global: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004835 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00004836 Builder.MakeGlobal(*Context, ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004837 break;
4838 }
4839 }
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004840 }
4841
Douglas Gregor9b272512011-02-28 23:58:31 +00004842 return Builder.getWithLocInContext(*Context);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004843}
4844
Chris Lattnerca025db2010-05-07 21:43:38 +00004845SourceRange
Sebastian Redl2c373b92010-10-05 15:59:54 +00004846ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4847 unsigned &Idx) {
4848 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4849 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00004850 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00004851}
4852
Douglas Gregor1daeb692009-04-13 18:14:40 +00004853/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004854llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004855 unsigned BitWidth = Record[Idx++];
4856 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4857 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4858 Idx += NumWords;
4859 return Result;
4860}
4861
4862/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004863llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004864 bool isUnsigned = Record[Idx++];
4865 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4866}
4867
Douglas Gregore0a3a512009-04-14 21:55:33 +00004868/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004869llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004870 return llvm::APFloat(ReadAPInt(Record, Idx));
4871}
4872
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004873// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00004874std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004875 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00004876 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004877 Idx += Len;
4878 return Result;
4879}
4880
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004881VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
4882 unsigned &Idx) {
4883 unsigned Major = Record[Idx++];
4884 unsigned Minor = Record[Idx++];
4885 unsigned Subminor = Record[Idx++];
4886 if (Minor == 0)
4887 return VersionTuple(Major);
4888 if (Subminor == 0)
4889 return VersionTuple(Major, Minor - 1);
4890 return VersionTuple(Major, Minor - 1, Subminor - 1);
4891}
4892
Sebastian Redl2c499f62010-08-18 23:56:43 +00004893CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00004894 unsigned &Idx) {
4895 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4896 return CXXTemporary::Create(*Context, Decl);
4897}
4898
Sebastian Redl2c499f62010-08-18 23:56:43 +00004899DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00004900 return Diag(SourceLocation(), DiagID);
4901}
4902
Sebastian Redl2c499f62010-08-18 23:56:43 +00004903DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004904 return Diags.Report(Loc, DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00004905}
Douglas Gregora9af1d12009-04-17 00:04:06 +00004906
Douglas Gregora868bbd2009-04-21 22:25:48 +00004907/// \brief Retrieve the identifier table associated with the
4908/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004909IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004910 assert(PP && "Forgot to set Preprocessor ?");
4911 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00004912}
4913
Douglas Gregora9af1d12009-04-17 00:04:06 +00004914/// \brief Record that the given ID maps to the given switch-case
4915/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004916void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004917 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4918 SwitchCaseStmts[ID] = SC;
4919}
4920
4921/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004922SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004923 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4924 return SwitchCaseStmts[ID];
4925}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004926
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00004927void ASTReader::ClearSwitchCaseIDs() {
4928 SwitchCaseStmts.clear();
4929}
4930
Sebastian Redl2c499f62010-08-18 23:56:43 +00004931void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004932 assert(NumCurrentElementsDeserializing &&
4933 "FinishedDeserializing not paired with StartedDeserializing");
4934 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004935 // If any identifiers with corresponding top-level declarations have
4936 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004937 while (!PendingIdentifierInfos.empty()) {
4938 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4939 PendingIdentifierInfos.front().DeclIDs, true);
4940 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00004941 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004942
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00004943 // Ready to load previous declarations of Decls that were delayed.
4944 while (!PendingPreviousDecls.empty()) {
4945 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
4946 PendingPreviousDecls.front().second);
4947 PendingPreviousDecls.pop_front();
4948 }
4949
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004950 // We are not in recursive loading, so it's safe to pass the "interesting"
4951 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004952 if (Consumer)
4953 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidisad5f95c2010-10-24 17:26:31 +00004954
4955 assert(PendingForwardRefs.size() == 0 &&
4956 "Some forward refs did not get linked to the definition!");
Douglas Gregor1342e842009-07-06 18:54:52 +00004957 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004958 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00004959}
Douglas Gregorb473b072010-08-19 00:28:17 +00004960
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004961ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00004962 const char *isysroot, bool DisableValidation,
4963 bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004964 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4965 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4966 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4967 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00004968 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
4969 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NextSLocOffset(0),
4970 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
4971 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
4972 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
4973 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
4974 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
4975 NumCurrentElementsDeserializing(0)
4976{
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004977 RelocatablePCH = false;
4978}
4979
4980ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4981 Diagnostic &Diags, const char *isysroot,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00004982 bool DisableValidation, bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004983 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4984 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00004985 isysroot(isysroot), DisableValidation(DisableValidation),
4986 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
4987 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00004988 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4989 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4990 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4991 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4992 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4993 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004994 RelocatablePCH = false;
4995}
4996
4997ASTReader::~ASTReader() {
4998 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4999 delete Chain[e - i - 1];
5000 // Delete all visible decl lookup tables
5001 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
5002 E = DeclContextOffsets.end();
5003 I != E; ++I) {
5004 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
5005 J != F; ++J) {
5006 if (J->NameLookupTableData)
5007 delete static_cast<ASTDeclContextNameLookupTable*>(
5008 J->NameLookupTableData);
5009 }
5010 }
5011 for (DeclContextVisibleUpdatesPending::iterator
5012 I = PendingVisibleUpdates.begin(),
5013 E = PendingVisibleUpdates.end();
5014 I != E; ++I) {
5015 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5016 F = I->second.end();
5017 J != F; ++J)
5018 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
5019 }
5020}
5021
Sebastian Redl009e7f22010-10-05 16:15:19 +00005022ASTReader::PerFileData::PerFileData(ASTFileType Ty)
5023 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00005024 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
5025 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
Douglas Gregor09b69892011-02-10 17:09:37 +00005026 MacroDefinitionOffsets(0),
5027 LocalNumHeaderFileInfos(0), HeaderFileInfoTableData(0),
5028 HeaderFileInfoTable(0),
5029 LocalNumSelectors(0), SelectorOffsets(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00005030 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005031 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
5032 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redl3f6b7532010-10-01 19:59:12 +00005033 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregorb473b072010-08-19 00:28:17 +00005034{}
5035
5036ASTReader::PerFileData::~PerFileData() {
5037 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
Douglas Gregor09b69892011-02-10 17:09:37 +00005038 delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable);
Douglas Gregorb473b072010-08-19 00:28:17 +00005039 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
5040}