blob: d2d20bfdbdf84b575baa35992314b69469ac92ff [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 Gregoref84c4b2009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Chris Lattner34321bc2009-04-10 21:41:48 +000028#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000029#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000030#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000031#include "clang/Lex/HeaderSearch.h"
Douglas Gregora868bbd2009-04-21 22:25:48 +000032#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000033#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000034#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000035#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000036#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000037#include "clang/Basic/TargetInfo.h"
Douglas Gregord54f3a12009-10-05 21:07:28 +000038#include "clang/Basic/Version.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000039#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000040#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000041#include "llvm/Support/MemoryBuffer.h"
John McCall0ad16662009-10-29 08:12:44 +000042#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000043#include "llvm/Support/Path.h"
Michael J. Spencerf25faaa2010-12-09 17:36:38 +000044#include "llvm/Support/system_error.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000045#include <algorithm>
Douglas Gregorc379c072009-04-28 18:58:38 +000046#include <iterator>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000047#include <cstdio>
Douglas Gregorc5046832009-04-27 18:38:38 +000048#include <sys/stat.h>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000049using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000050using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000051
52//===----------------------------------------------------------------------===//
Sebastian Redld44cd6a2010-08-18 23:57:06 +000053// PCH validator implementation
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000054//===----------------------------------------------------------------------===//
55
Sebastian Redl3e31c722010-08-18 23:56:56 +000056ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000057
58bool
59PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
60 const LangOptions &PPLangOpts = PP.getLangOptions();
61#define PARSE_LANGOPT_BENIGN(Option)
62#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
63 if (PPLangOpts.Option != LangOpts.Option) { \
64 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
65 return true; \
66 }
67
68 PARSE_LANGOPT_BENIGN(Trigraphs);
69 PARSE_LANGOPT_BENIGN(BCPLComment);
70 PARSE_LANGOPT_BENIGN(DollarIdents);
71 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
72 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carruthe03aa552010-04-17 20:17:31 +000073 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000074 PARSE_LANGOPT_BENIGN(ImplicitInt);
75 PARSE_LANGOPT_BENIGN(Digraphs);
76 PARSE_LANGOPT_BENIGN(HexFloats);
77 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
78 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +000079 PARSE_LANGOPT_BENIGN(MSCVersion);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000080 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
81 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
82 PARSE_LANGOPT_BENIGN(CXXOperatorName);
83 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
84 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
85 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian45878032010-02-09 19:31:38 +000086 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +000087 PARSE_LANGOPT_IMPORTANT(AppleKext, diag::warn_pch_apple_kext);
Ted Kremenek1d56c9e2010-12-23 21:35:43 +000088 PARSE_LANGOPT_IMPORTANT(ObjCDefaultSynthProperties,
89 diag::warn_pch_objc_auto_properties);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +000090 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian62c56022010-04-22 21:01:59 +000091 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000092 PARSE_LANGOPT_BENIGN(PascalStrings);
93 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump11289f42009-09-09 15:08:12 +000094 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000095 diag::warn_pch_lax_vector_conversions);
Nate Begeman9d905792009-06-25 22:57:40 +000096 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000097 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +000098 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Douglas Gregordbe39272011-02-01 15:15:22 +000099 PARSE_LANGOPT_IMPORTANT(MSBitfields, diag::warn_pch_ms_bitfields);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000100 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
101 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
102 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump11289f42009-09-09 15:08:12 +0000103 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000104 diag::warn_pch_thread_safe_statics);
Daniel Dunbara77eaeb2009-09-03 04:54:28 +0000105 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000106 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
107 PARSE_LANGOPT_BENIGN(EmitAllDecls);
108 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattner51924e512010-06-26 21:25:03 +0000109 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump11289f42009-09-09 15:08:12 +0000110 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000111 diag::warn_pch_heinous_extensions);
112 // FIXME: Most of the options below are benign if the macro wasn't
113 // used. Unfortunately, this means that a PCH compiled without
114 // optimization can't be used with optimization turned on, even
115 // though the only thing that changes is whether __OPTIMIZE__ was
116 // defined... but if __OPTIMIZE__ never showed up in the header, it
117 // doesn't matter. We could consider making this some special kind
118 // of check.
119 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
120 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
121 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
122 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
123 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
124 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
125 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
126 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsoned4e2952009-11-05 20:14:16 +0000127 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +0000128 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000129 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump11289f42009-09-09 15:08:12 +0000130 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000131 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
132 return true;
133 }
134 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000135 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
136 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000137 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman9d905792009-06-25 22:57:40 +0000138 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Peter Collingbourne546d0792010-12-01 19:14:57 +0000139 PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda);
Mike Stumpd9546382009-12-12 01:27:46 +0000140 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbar143021e2009-09-21 04:16:19 +0000141 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000142 PARSE_LANGOPT_BENIGN(SpellChecking);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +0000143#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000144#undef PARSE_LANGOPT_BENIGN
145
146 return false;
147}
148
Daniel Dunbar20a682d2009-11-11 00:52:11 +0000149bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
150 if (Triple == PP.getTargetInfo().getTriple().str())
151 return false;
152
153 Reader.Diag(diag::warn_pch_target_triple)
154 << Triple << PP.getTargetInfo().getTriple().str();
155 return true;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000156}
157
Benjamin Kramer90b5b682010-11-25 18:29:30 +0000158namespace {
159 struct EmptyStringRef {
160 bool operator ()(llvm::StringRef r) const { return r.empty(); }
161 };
162 struct EmptyBlock {
163 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
164 };
165}
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000166
167static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
168 PCHPredefinesBlocks R) {
169 // First, sum up the lengths.
170 unsigned LL = 0, RL = 0;
171 for (unsigned I = 0, N = L.size(); I != N; ++I) {
172 LL += L[I].size();
173 }
174 for (unsigned I = 0, N = R.size(); I != N; ++I) {
175 RL += R[I].Data.size();
176 }
177 if (LL != RL)
178 return false;
179 if (LL == 0 && RL == 0)
180 return true;
181
182 // Kick out empty parts, they confuse the algorithm below.
183 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
184 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
185
186 // Do it the hard way. At this point, both vectors must be non-empty.
187 llvm::StringRef LR = L[0], RR = R[0].Data;
188 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbar01ad0a72010-07-16 00:00:11 +0000189 (void) RN;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000190 for (;;) {
191 // Compare the current pieces.
192 if (LR.size() == RR.size()) {
193 // If they're the same length, it's pretty easy.
194 if (LR != RR)
195 return false;
196 // Both pieces are done, advance.
197 ++LI;
198 ++RI;
199 // If either string is done, they're both done, since they're the same
200 // length.
201 if (LI == LN) {
202 assert(RI == RN && "Strings not the same length after all?");
203 return true;
204 }
205 LR = L[LI];
206 RR = R[RI].Data;
207 } else if (LR.size() < RR.size()) {
208 // Right piece is longer.
209 if (!RR.startswith(LR))
210 return false;
211 ++LI;
212 assert(LI != LN && "Strings not the same length after all?");
213 RR = RR.substr(LR.size());
214 LR = L[LI];
215 } else {
216 // Left piece is longer.
217 if (!LR.startswith(RR))
218 return false;
219 ++RI;
220 assert(RI != RN && "Strings not the same length after all?");
221 LR = LR.substr(RR.size());
222 RR = R[RI].Data;
223 }
224 }
225}
226
227static std::pair<FileID, llvm::StringRef::size_type>
228FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
229 std::pair<FileID, llvm::StringRef::size_type> Res;
230 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
231 Res.second = Buffers[I].Data.find(MacroDef);
232 if (Res.second != llvm::StringRef::npos) {
233 Res.first = Buffers[I].BufferID;
234 break;
235 }
236 }
237 return Res;
238}
239
240bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000241 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000242 std::string &SuggestedPredefines) {
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000243 // We are in the context of an implicit include, so the predefines buffer will
244 // have a #include entry for the PCH file itself (as normalized by the
245 // preprocessor initialization). Find it and skip over it in the checking
246 // below.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000247 llvm::SmallString<256> PCHInclude;
248 PCHInclude += "#include \"";
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000249 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000250 PCHInclude += "\"\n";
251 std::pair<llvm::StringRef,llvm::StringRef> Split =
252 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
253 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000254 if (Left == PP.getPredefines()) {
255 Error("Missing PCH include entry!");
256 return true;
257 }
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000258
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000259 // If the concatenation of all the PCH buffers is equal to the adjusted
260 // command line, we're done.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000261 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
262 CommandLine.push_back(Left);
263 CommandLine.push_back(Right);
264 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000265 return false;
266
267 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000268
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000269 // The predefines buffers are different. Determine what the differences are,
270 // and whether they require us to reject the PCH file.
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000271 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000272 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
273 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000274
275 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
276 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000277
278 // Pick out implicit #includes after the PCH and don't consider them for
279 // validation; we will insert them into SuggestedPredefines so that the
280 // preprocessor includes them.
281 std::string IncludesAfterPCH;
282 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
283 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
284 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
285 if (AfterPCHLines[i].startswith("#include ")) {
286 IncludesAfterPCH += AfterPCHLines[i];
287 IncludesAfterPCH += '\n';
288 } else {
289 CmdLineLines.push_back(AfterPCHLines[i]);
290 }
291 }
292
293 // Make sure we add the includes last into SuggestedPredefines before we
294 // exit this function.
295 struct AddIncludesRAII {
296 std::string &SuggestedPredefines;
297 std::string &IncludesAfterPCH;
298
299 AddIncludesRAII(std::string &SuggestedPredefines,
300 std::string &IncludesAfterPCH)
301 : SuggestedPredefines(SuggestedPredefines),
302 IncludesAfterPCH(IncludesAfterPCH) { }
303 ~AddIncludesRAII() {
304 SuggestedPredefines += IncludesAfterPCH;
305 }
306 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000307
Daniel Dunbar499baed2009-11-11 05:26:28 +0000308 // Sort both sets of predefined buffer lines, since we allow some extra
309 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000310 std::sort(CmdLineLines.begin(), CmdLineLines.end());
311 std::sort(PCHLines.begin(), PCHLines.end());
312
Daniel Dunbar499baed2009-11-11 05:26:28 +0000313 // Determine which predefines that were used to build the PCH file are missing
314 // from the command line.
315 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000316 std::set_difference(PCHLines.begin(), PCHLines.end(),
317 CmdLineLines.begin(), CmdLineLines.end(),
318 std::back_inserter(MissingPredefines));
319
320 bool MissingDefines = false;
321 bool ConflictingDefines = false;
322 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000323 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000324 if (Missing.startswith("#include ")) {
325 // An -include was specified when generating the PCH; it is included in
326 // the PCH, just ignore it.
327 continue;
328 }
Daniel Dunbar499baed2009-11-11 05:26:28 +0000329 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000330 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
331 return true;
332 }
Mike Stump11289f42009-09-09 15:08:12 +0000333
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000334 // This is a macro definition. Determine the name of the macro we're
335 // defining.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000336 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000337 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000338 = Missing.find_first_of("( \n\r", StartOfMacroName);
339 assert(EndOfMacroName != std::string::npos &&
340 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000341 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000342
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000343 // Determine whether this macro was given a different definition on the
344 // command line.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000345 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000346 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000347 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000348 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
349 MacroDefStart);
350 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000351 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000352 // Different macro; we're done.
353 ConflictPos = CmdLineLines.end();
Mike Stump11289f42009-09-09 15:08:12 +0000354 break;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000355 }
Mike Stump11289f42009-09-09 15:08:12 +0000356
357 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000358 "Invalid #define in predefines buffer?");
Mike Stump11289f42009-09-09 15:08:12 +0000359 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000360 (*ConflictPos)[MacroDefLen] != '(')
361 continue; // Longer macro name; keep trying.
Mike Stump11289f42009-09-09 15:08:12 +0000362
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000363 // We found a conflicting macro definition.
364 break;
365 }
Mike Stump11289f42009-09-09 15:08:12 +0000366
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000367 if (ConflictPos != CmdLineLines.end()) {
368 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
369 << MacroName;
370
371 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000372 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
373 FindMacro(Buffers, Missing);
374 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
375 SourceLocation PCHMissingLoc =
376 SourceMgr.getLocForStartOfFile(MacroLoc.first)
377 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar499baed2009-11-11 05:26:28 +0000378 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000379
380 ConflictingDefines = true;
381 continue;
382 }
Mike Stump11289f42009-09-09 15:08:12 +0000383
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000384 // If the macro doesn't conflict, then we'll just pick up the macro
385 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000386 if (ConflictingDefines)
387 continue; // Don't complain if there are already conflicting defs
Mike Stump11289f42009-09-09 15:08:12 +0000388
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000389 if (!MissingDefines) {
390 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
391 MissingDefines = true;
392 }
393
394 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000395 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
396 FindMacro(Buffers, Missing);
397 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
398 SourceLocation PCHMissingLoc =
399 SourceMgr.getLocForStartOfFile(MacroLoc.first)
400 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000401 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
402 }
Mike Stump11289f42009-09-09 15:08:12 +0000403
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000404 if (ConflictingDefines)
405 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000406
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000407 // Determine what predefines were introduced based on command-line
408 // parameters that were not present when building the PCH
409 // file. Extra #defines are okay, so long as the identifiers being
410 // defined were not used within the precompiled header.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000411 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000412 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
413 PCHLines.begin(), PCHLines.end(),
Mike Stump11289f42009-09-09 15:08:12 +0000414 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000415 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000416 llvm::StringRef &Extra = ExtraPredefines[I];
417 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000418 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
419 return true;
420 }
421
422 // This is an extra macro definition. Determine the name of the
423 // macro we're defining.
424 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000425 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000426 = Extra.find_first_of("( \n\r", StartOfMacroName);
427 assert(EndOfMacroName != std::string::npos &&
428 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000429 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000430
431 // Check whether this name was used somewhere in the PCH file. If
432 // so, defining it as a macro could change behavior, so we reject
433 // the PCH file.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000434 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar045c92f2009-11-11 00:52:00 +0000435 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000436 return true;
437 }
438
439 // Add this definition to the suggested predefines buffer.
440 SuggestedPredefines += Extra;
441 SuggestedPredefines += '\n';
442 }
443
444 // If we get here, it's because the predefines buffer had compatible
445 // contents. Accept the PCH file.
446 return false;
447}
448
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000449void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
450 unsigned ID) {
451 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
452 ++NumHeaderInfos;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000453}
454
455void PCHValidator::ReadCounter(unsigned Value) {
456 PP.setCounterValue(Value);
457}
458
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000459//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +0000460// AST reader implementation
Douglas Gregora868bbd2009-04-21 22:25:48 +0000461//===----------------------------------------------------------------------===//
462
Sebastian Redl07a89a82010-07-30 00:29:29 +0000463void
Sebastian Redl3e31c722010-08-18 23:56:56 +0000464ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redl07a89a82010-07-30 00:29:29 +0000465 DeserializationListener = Listener;
Sebastian Redl07a89a82010-07-30 00:29:29 +0000466}
467
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000468
Douglas Gregora868bbd2009-04-21 22:25:48 +0000469namespace {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000470class ASTSelectorLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000471 ASTReader &Reader;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000472
473public:
Sebastian Redl834bb972010-08-04 17:20:04 +0000474 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +0000475 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +0000476 ObjCMethodList Instance, Factory;
477 };
Douglas Gregorc78d3462009-04-24 21:10:55 +0000478
479 typedef Selector external_key_type;
480 typedef external_key_type internal_key_type;
481
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000482 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump11289f42009-09-09 15:08:12 +0000483
Douglas Gregorc78d3462009-04-24 21:10:55 +0000484 static bool EqualKey(const internal_key_type& a,
485 const internal_key_type& b) {
486 return a == b;
487 }
Mike Stump11289f42009-09-09 15:08:12 +0000488
Douglas Gregorc78d3462009-04-24 21:10:55 +0000489 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +0000490 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000491 }
Mike Stump11289f42009-09-09 15:08:12 +0000492
Douglas Gregorc78d3462009-04-24 21:10:55 +0000493 // This hopefully will just get inlined and removed by the optimizer.
494 static const internal_key_type&
495 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000496
Douglas Gregorc78d3462009-04-24 21:10:55 +0000497 static std::pair<unsigned, unsigned>
498 ReadKeyDataLength(const unsigned char*& d) {
499 using namespace clang::io;
500 unsigned KeyLen = ReadUnalignedLE16(d);
501 unsigned DataLen = ReadUnalignedLE16(d);
502 return std::make_pair(KeyLen, DataLen);
503 }
Mike Stump11289f42009-09-09 15:08:12 +0000504
Douglas Gregor95c13f52009-04-25 17:48:32 +0000505 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000506 using namespace clang::io;
Chris Lattner8575daa2009-04-27 21:45:14 +0000507 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000508 unsigned N = ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000509 IdentifierInfo *FirstII
Douglas Gregorc78d3462009-04-24 21:10:55 +0000510 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
511 if (N == 0)
512 return SelTable.getNullarySelector(FirstII);
513 else if (N == 1)
514 return SelTable.getUnarySelector(FirstII);
515
516 llvm::SmallVector<IdentifierInfo *, 16> Args;
517 Args.push_back(FirstII);
518 for (unsigned I = 1; I != N; ++I)
519 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
520
Douglas Gregor038c3382009-05-22 22:45:36 +0000521 return SelTable.getSelector(N, Args.data());
Douglas Gregorc78d3462009-04-24 21:10:55 +0000522 }
Mike Stump11289f42009-09-09 15:08:12 +0000523
Douglas Gregorc78d3462009-04-24 21:10:55 +0000524 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
525 using namespace clang::io;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000526
527 data_type Result;
528
Sebastian Redl834bb972010-08-04 17:20:04 +0000529 Result.ID = ReadUnalignedLE32(d);
530 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
531 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
532
Douglas Gregorc78d3462009-04-24 21:10:55 +0000533 // Load instance methods
534 ObjCMethodList *Prev = 0;
535 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000536 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000537 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000538 if (!Result.Instance.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000539 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000540 Result.Instance.Method = Method;
541 Prev = &Result.Instance;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000542 continue;
543 }
544
Ted Kremenekda4abf12010-02-11 00:53:01 +0000545 ObjCMethodList *Mem =
546 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
547 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000548 Prev = Prev->Next;
549 }
550
551 // Load factory methods
552 Prev = 0;
553 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000554 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000555 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000556 if (!Result.Factory.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000557 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000558 Result.Factory.Method = Method;
559 Prev = &Result.Factory;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000560 continue;
561 }
562
Ted Kremenekda4abf12010-02-11 00:53:01 +0000563 ObjCMethodList *Mem =
564 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
565 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000566 Prev = Prev->Next;
567 }
568
569 return Result;
570 }
571};
Mike Stump11289f42009-09-09 15:08:12 +0000572
573} // end anonymous namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +0000574
575/// \brief The on-disk hash table used for the global method pool.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000576typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
577 ASTSelectorLookupTable;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000578
Sebastian Redl2c373b92010-10-05 15:59:54 +0000579namespace clang {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000580class ASTIdentifierLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000581 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +0000582 ASTReader::PerFileData &F;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000583
584 // If we know the IdentifierInfo in advance, it is here and we will
585 // not build a new one. Used when deserializing information about an
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000586 // identifier that was constructed before the AST file was read.
Douglas Gregora868bbd2009-04-21 22:25:48 +0000587 IdentifierInfo *KnownII;
588
589public:
590 typedef IdentifierInfo * data_type;
591
592 typedef const std::pair<const char*, unsigned> external_key_type;
593
594 typedef external_key_type internal_key_type;
595
Sebastian Redl2c373b92010-10-05 15:59:54 +0000596 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000597 IdentifierInfo *II = 0)
Sebastian Redl2c373b92010-10-05 15:59:54 +0000598 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump11289f42009-09-09 15:08:12 +0000599
Douglas Gregora868bbd2009-04-21 22:25:48 +0000600 static bool EqualKey(const internal_key_type& a,
601 const internal_key_type& b) {
602 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
603 : false;
604 }
Mike Stump11289f42009-09-09 15:08:12 +0000605
Douglas Gregora868bbd2009-04-21 22:25:48 +0000606 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000607 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregora868bbd2009-04-21 22:25:48 +0000608 }
Mike Stump11289f42009-09-09 15:08:12 +0000609
Douglas Gregora868bbd2009-04-21 22:25:48 +0000610 // This hopefully will just get inlined and removed by the optimizer.
611 static const internal_key_type&
612 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000613
Douglas Gregor57756ea2010-10-14 22:11:03 +0000614 // This hopefully will just get inlined and removed by the optimizer.
615 static const external_key_type&
616 GetExternalKey(const internal_key_type& x) { return x; }
617
Douglas Gregora868bbd2009-04-21 22:25:48 +0000618 static std::pair<unsigned, unsigned>
619 ReadKeyDataLength(const unsigned char*& d) {
620 using namespace clang::io;
Douglas Gregor6b7bf5a2009-04-25 20:26:24 +0000621 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor5287b4e2009-04-25 21:04:17 +0000622 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000623 return std::make_pair(KeyLen, DataLen);
624 }
Mike Stump11289f42009-09-09 15:08:12 +0000625
Douglas Gregora868bbd2009-04-21 22:25:48 +0000626 static std::pair<const char*, unsigned>
627 ReadKey(const unsigned char* d, unsigned n) {
628 assert(n >= 2 && d[n-1] == '\0');
629 return std::make_pair((const char*) d, n-1);
630 }
Mike Stump11289f42009-09-09 15:08:12 +0000631
632 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregora868bbd2009-04-21 22:25:48 +0000633 const unsigned char* d,
634 unsigned DataLen) {
635 using namespace clang::io;
Sebastian Redl539c5062010-08-18 23:57:32 +0000636 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000637 bool IsInteresting = ID & 0x01;
638
639 // Wipe out the "is interesting" bit.
640 ID = ID >> 1;
641
642 if (!IsInteresting) {
Sebastian Redl98912122010-07-27 23:01:28 +0000643 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregor1d583f22009-04-28 21:18:29 +0000644 // and associate it with the persistent ID.
645 IdentifierInfo *II = KnownII;
646 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000647 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000648 Reader.SetIdentifierInfo(ID, II);
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000649 II->setIsFromAST();
Douglas Gregor1d583f22009-04-28 21:18:29 +0000650 return II;
651 }
652
Douglas Gregorb9256522009-04-28 21:32:13 +0000653 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000654 bool CPlusPlusOperatorKeyword = Bits & 0x01;
655 Bits >>= 1;
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000656 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
657 Bits >>= 1;
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000658 bool Poisoned = Bits & 0x01;
659 Bits >>= 1;
660 bool ExtensionToken = Bits & 0x01;
661 Bits >>= 1;
662 bool hasMacroDefinition = Bits & 0x01;
663 Bits >>= 1;
664 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
665 Bits >>= 10;
Mike Stump11289f42009-09-09 15:08:12 +0000666
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000667 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorb9256522009-04-28 21:32:13 +0000668 DataLen -= 6;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000669
670 // Build the IdentifierInfo itself and link the identifier ID with
671 // the new IdentifierInfo.
672 IdentifierInfo *II = KnownII;
673 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000674 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000675 Reader.SetIdentifierInfo(ID, II);
676
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000677 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000678 // Token IDs are read-only.
679 if (HasRevertedTokenIDToIdentifier)
680 II->RevertTokenIDToIdentifier();
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000681 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump11289f42009-09-09 15:08:12 +0000682 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000683 "Incorrect extension token flag");
684 (void)ExtensionToken;
685 II->setIsPoisoned(Poisoned);
686 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
687 "Incorrect C++ operator keyword flag");
688 (void)CPlusPlusOperatorKeyword;
689
Douglas Gregorc3366a52009-04-21 23:56:24 +0000690 // If this identifier is a macro, deserialize the macro
691 // definition.
692 if (hasMacroDefinition) {
Douglas Gregorb9256522009-04-28 21:32:13 +0000693 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor5ef9e332010-10-30 00:23:06 +0000694 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregorb9256522009-04-28 21:32:13 +0000695 DataLen -= 4;
Douglas Gregorc3366a52009-04-21 23:56:24 +0000696 }
Douglas Gregora868bbd2009-04-21 22:25:48 +0000697
698 // Read all of the declarations visible at global scope with this
699 // name.
Chris Lattner1d728882009-04-27 22:17:41 +0000700 if (Reader.getContext() == 0) return II;
Douglas Gregor1342e842009-07-06 18:54:52 +0000701 if (DataLen > 0) {
702 llvm::SmallVector<uint32_t, 4> DeclIDs;
703 for (; DataLen > 0; DataLen -= 4)
704 DeclIDs.push_back(ReadUnalignedLE32(d));
705 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000706 }
Mike Stump11289f42009-09-09 15:08:12 +0000707
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000708 II->setIsFromAST();
Douglas Gregora868bbd2009-04-21 22:25:48 +0000709 return II;
710 }
711};
Mike Stump11289f42009-09-09 15:08:12 +0000712
713} // end anonymous namespace
Douglas Gregora868bbd2009-04-21 22:25:48 +0000714
715/// \brief The on-disk hash table used to contain information about
716/// all of the identifiers in the program.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000717typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
718 ASTIdentifierLookupTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000719
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000720namespace {
721class ASTDeclContextNameLookupTrait {
722 ASTReader &Reader;
723
724public:
725 /// \brief Pair of begin/end iterators for DeclIDs.
726 typedef std::pair<DeclID *, DeclID *> data_type;
727
728 /// \brief Special internal key for declaration names.
729 /// The hash table creates keys for comparison; we do not create
730 /// a DeclarationName for the internal key to avoid deserializing types.
731 struct DeclNameKey {
732 DeclarationName::NameKind Kind;
733 uint64_t Data;
734 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
735 };
736
737 typedef DeclarationName external_key_type;
738 typedef DeclNameKey internal_key_type;
739
740 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
741
742 static bool EqualKey(const internal_key_type& a,
743 const internal_key_type& b) {
744 return a.Kind == b.Kind && a.Data == b.Data;
745 }
746
747 unsigned ComputeHash(const DeclNameKey &Key) const {
748 llvm::FoldingSetNodeID ID;
749 ID.AddInteger(Key.Kind);
750
751 switch (Key.Kind) {
752 case DeclarationName::Identifier:
753 case DeclarationName::CXXLiteralOperatorName:
754 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
755 break;
756 case DeclarationName::ObjCZeroArgSelector:
757 case DeclarationName::ObjCOneArgSelector:
758 case DeclarationName::ObjCMultiArgSelector:
759 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
760 break;
761 case DeclarationName::CXXConstructorName:
762 case DeclarationName::CXXDestructorName:
763 case DeclarationName::CXXConversionFunctionName:
764 ID.AddInteger((TypeID)Key.Data);
765 break;
766 case DeclarationName::CXXOperatorName:
767 ID.AddInteger((OverloadedOperatorKind)Key.Data);
768 break;
769 case DeclarationName::CXXUsingDirective:
770 break;
771 }
772
773 return ID.ComputeHash();
774 }
775
776 internal_key_type GetInternalKey(const external_key_type& Name) const {
777 DeclNameKey Key;
778 Key.Kind = Name.getNameKind();
779 switch (Name.getNameKind()) {
780 case DeclarationName::Identifier:
781 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
782 break;
783 case DeclarationName::ObjCZeroArgSelector:
784 case DeclarationName::ObjCOneArgSelector:
785 case DeclarationName::ObjCMultiArgSelector:
786 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
787 break;
788 case DeclarationName::CXXConstructorName:
789 case DeclarationName::CXXDestructorName:
790 case DeclarationName::CXXConversionFunctionName:
791 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
792 break;
793 case DeclarationName::CXXOperatorName:
794 Key.Data = Name.getCXXOverloadedOperator();
795 break;
796 case DeclarationName::CXXLiteralOperatorName:
797 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
798 break;
799 case DeclarationName::CXXUsingDirective:
800 break;
801 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000802
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000803 return Key;
804 }
805
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000806 external_key_type GetExternalKey(const internal_key_type& Key) const {
807 ASTContext *Context = Reader.getContext();
808 switch (Key.Kind) {
809 case DeclarationName::Identifier:
810 return DeclarationName((IdentifierInfo*)Key.Data);
811
812 case DeclarationName::ObjCZeroArgSelector:
813 case DeclarationName::ObjCOneArgSelector:
814 case DeclarationName::ObjCMultiArgSelector:
815 return DeclarationName(Selector(Key.Data));
816
817 case DeclarationName::CXXConstructorName:
818 return Context->DeclarationNames.getCXXConstructorName(
819 Context->getCanonicalType(Reader.GetType(Key.Data)));
820
821 case DeclarationName::CXXDestructorName:
822 return Context->DeclarationNames.getCXXDestructorName(
823 Context->getCanonicalType(Reader.GetType(Key.Data)));
824
825 case DeclarationName::CXXConversionFunctionName:
826 return Context->DeclarationNames.getCXXConversionFunctionName(
827 Context->getCanonicalType(Reader.GetType(Key.Data)));
828
829 case DeclarationName::CXXOperatorName:
830 return Context->DeclarationNames.getCXXOperatorName(
831 (OverloadedOperatorKind)Key.Data);
832
833 case DeclarationName::CXXLiteralOperatorName:
834 return Context->DeclarationNames.getCXXLiteralOperatorName(
835 (IdentifierInfo*)Key.Data);
836
837 case DeclarationName::CXXUsingDirective:
838 return DeclarationName::getUsingDirectiveName();
839 }
840
841 llvm_unreachable("Invalid Name Kind ?");
842 }
843
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000844 static std::pair<unsigned, unsigned>
845 ReadKeyDataLength(const unsigned char*& d) {
846 using namespace clang::io;
847 unsigned KeyLen = ReadUnalignedLE16(d);
848 unsigned DataLen = ReadUnalignedLE16(d);
849 return std::make_pair(KeyLen, DataLen);
850 }
851
852 internal_key_type ReadKey(const unsigned char* d, unsigned) {
853 using namespace clang::io;
854
855 DeclNameKey Key;
856 Key.Kind = (DeclarationName::NameKind)*d++;
857 switch (Key.Kind) {
858 case DeclarationName::Identifier:
859 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
860 break;
861 case DeclarationName::ObjCZeroArgSelector:
862 case DeclarationName::ObjCOneArgSelector:
863 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000864 Key.Data =
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000865 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
866 break;
867 case DeclarationName::CXXConstructorName:
868 case DeclarationName::CXXDestructorName:
869 case DeclarationName::CXXConversionFunctionName:
870 Key.Data = ReadUnalignedLE32(d); // TypeID
871 break;
872 case DeclarationName::CXXOperatorName:
873 Key.Data = *d++; // OverloadedOperatorKind
874 break;
875 case DeclarationName::CXXLiteralOperatorName:
876 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
877 break;
878 case DeclarationName::CXXUsingDirective:
879 break;
880 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000881
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000882 return Key;
883 }
884
885 data_type ReadData(internal_key_type, const unsigned char* d,
886 unsigned DataLen) {
887 using namespace clang::io;
888 unsigned NumDecls = ReadUnalignedLE16(d);
889 DeclID *Start = (DeclID *)d;
890 return std::make_pair(Start, Start + NumDecls);
891 }
892};
893
894} // end anonymous namespace
895
896/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
897typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
898 ASTDeclContextNameLookupTable;
899
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000900bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
901 const std::pair<uint64_t, uint64_t> &Offsets,
902 DeclContextInfo &Info) {
903 SavedStreamPosition SavedPosition(Cursor);
904 // First the lexical decls.
905 if (Offsets.first != 0) {
906 Cursor.JumpToBit(Offsets.first);
907
908 RecordData Record;
909 const char *Blob;
910 unsigned BlobLen;
911 unsigned Code = Cursor.ReadCode();
912 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
913 if (RecCode != DECL_CONTEXT_LEXICAL) {
914 Error("Expected lexical block");
915 return true;
916 }
917
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000918 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
919 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000920 } else {
921 Info.LexicalDecls = 0;
922 Info.NumLexicalDecls = 0;
923 }
924
925 // Now the lookup table.
926 if (Offsets.second != 0) {
927 Cursor.JumpToBit(Offsets.second);
928
929 RecordData Record;
930 const char *Blob;
931 unsigned BlobLen;
932 unsigned Code = Cursor.ReadCode();
933 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
934 if (RecCode != DECL_CONTEXT_VISIBLE) {
935 Error("Expected visible lookup table block");
936 return true;
937 }
938 Info.NameLookupTableData
939 = ASTDeclContextNameLookupTable::Create(
940 (const unsigned char *)Blob + Record[0],
941 (const unsigned char *)Blob,
942 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl9d8f58b2010-08-24 00:50:00 +0000943 } else {
944 Info.NameLookupTableData = 0;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000945 }
946
947 return false;
948}
949
Sebastian Redl2c499f62010-08-18 23:56:43 +0000950void ASTReader::Error(const char *Msg) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000951 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000952}
953
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000954/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000955bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000956 if (Listener)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000957 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000958 ActualOriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000959 SuggestedPredefines);
Douglas Gregorc379c072009-04-28 18:58:38 +0000960 return false;
Douglas Gregor92863e42009-04-10 23:10:45 +0000961}
962
Douglas Gregorc5046832009-04-27 18:38:38 +0000963//===----------------------------------------------------------------------===//
964// Source Manager Deserialization
965//===----------------------------------------------------------------------===//
966
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000967/// \brief Read the line table in the source manager block.
Sebastian Redl2c373b92010-10-05 15:59:54 +0000968/// \returns true if there was an error.
969bool ASTReader::ParseLineTable(PerFileData &F,
970 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000971 unsigned Idx = 0;
972 LineTableInfo &LineTable = SourceMgr.getLineTable();
973
974 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +0000975 std::map<int, int> FileIDs;
976 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000977 // Extract the file name
978 unsigned FilenameLen = Record[Idx++];
979 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
980 Idx += FilenameLen;
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000981 MaybeAddSystemRootToFilename(Filename);
Mike Stump11289f42009-09-09 15:08:12 +0000982 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregora8854652009-04-13 17:12:42 +0000983 Filename.size());
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000984 }
985
986 // Parse the line entries
987 std::vector<LineEntry> Entries;
988 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000989 int FID = Record[Idx++];
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000990
991 // Extract the line entries
992 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000993 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000994 Entries.clear();
995 Entries.reserve(NumEntries);
996 for (unsigned I = 0; I != NumEntries; ++I) {
997 unsigned FileOffset = Record[Idx++];
998 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000999 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +00001000 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001001 = (SrcMgr::CharacteristicKind)Record[Idx++];
1002 unsigned IncludeOffset = Record[Idx++];
1003 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1004 FileKind, IncludeOffset));
1005 }
1006 LineTable.AddEntry(FID, Entries);
1007 }
1008
1009 return false;
1010}
1011
Douglas Gregorc5046832009-04-27 18:38:38 +00001012namespace {
1013
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001014class ASTStatData {
Douglas Gregorc5046832009-04-27 18:38:38 +00001015public:
Douglas Gregorc5046832009-04-27 18:38:38 +00001016 const ino_t ino;
1017 const dev_t dev;
1018 const mode_t mode;
1019 const time_t mtime;
1020 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +00001021
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001022 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner2a6fa472010-11-23 19:28:12 +00001023 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregorc5046832009-04-27 18:38:38 +00001024};
1025
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001026class ASTStatLookupTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001027 public:
1028 typedef const char *external_key_type;
1029 typedef const char *internal_key_type;
1030
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001031 typedef ASTStatData data_type;
Douglas Gregorc5046832009-04-27 18:38:38 +00001032
1033 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001034 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001035 }
1036
1037 static internal_key_type GetInternalKey(const char *path) { return path; }
1038
1039 static bool EqualKey(internal_key_type a, internal_key_type b) {
1040 return strcmp(a, b) == 0;
1041 }
1042
1043 static std::pair<unsigned, unsigned>
1044 ReadKeyDataLength(const unsigned char*& d) {
1045 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1046 unsigned DataLen = (unsigned) *d++;
1047 return std::make_pair(KeyLen + 1, DataLen);
1048 }
1049
1050 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1051 return (const char *)d;
1052 }
1053
1054 static data_type ReadData(const internal_key_type, const unsigned char *d,
1055 unsigned /*DataLen*/) {
1056 using namespace clang::io;
1057
Douglas Gregorc5046832009-04-27 18:38:38 +00001058 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1059 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1060 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +00001061 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +00001062 off_t size = (off_t) ReadUnalignedLE64(d);
1063 return data_type(ino, dev, mode, mtime, size);
1064 }
1065};
1066
1067/// \brief stat() cache for precompiled headers.
1068///
1069/// This cache is very similar to the stat cache used by pretokenized
1070/// headers.
Chris Lattner226efd32010-11-23 19:19:34 +00001071class ASTStatCache : public FileSystemStatCache {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001072 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregorc5046832009-04-27 18:38:38 +00001073 CacheTy *Cache;
1074
1075 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +00001076public:
Chris Lattner2a6fa472010-11-23 19:28:12 +00001077 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1078 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +00001079 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1080 Cache = CacheTy::Create(Buckets, Base);
1081 }
1082
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001083 ~ASTStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +00001084
Chris Lattnerdd278432010-11-23 21:17:56 +00001085 LookupResult getStat(const char *Path, struct stat &StatBuf,
1086 int *FileDescriptor) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001087 // Do the lookup for the file's data in the AST file.
Chris Lattner226efd32010-11-23 19:19:34 +00001088 CacheTy::iterator I = Cache->find(Path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001089
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001090 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregorc5046832009-04-27 18:38:38 +00001091 if (I == Cache->end()) {
1092 ++NumStatMisses;
Chris Lattnerdd278432010-11-23 21:17:56 +00001093 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregorc5046832009-04-27 18:38:38 +00001094 }
Mike Stump11289f42009-09-09 15:08:12 +00001095
Douglas Gregorc5046832009-04-27 18:38:38 +00001096 ++NumStatHits;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001097 ASTStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001098
Chris Lattner226efd32010-11-23 19:19:34 +00001099 StatBuf.st_ino = Data.ino;
1100 StatBuf.st_dev = Data.dev;
1101 StatBuf.st_mtime = Data.mtime;
1102 StatBuf.st_mode = Data.mode;
1103 StatBuf.st_size = Data.size;
Chris Lattner8f0583d2010-11-23 20:05:15 +00001104 return CacheExists;
Douglas Gregorc5046832009-04-27 18:38:38 +00001105 }
1106};
1107} // end anonymous namespace
1108
1109
Sebastian Redl393f8b72010-07-19 20:52:06 +00001110/// \brief Read a source manager block
Sebastian Redl2c499f62010-08-18 23:56:43 +00001111ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001112 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +00001113
Sebastian Redl393f8b72010-07-19 20:52:06 +00001114 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001115
Douglas Gregor258ae542009-04-27 06:38:32 +00001116 // Set the source-location entry cursor to the current position in
1117 // the stream. This cursor will be used to read the contents of the
1118 // source manager block initially, and then lazily read
1119 // source-location entries as needed.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001120 SLocEntryCursor = F.Stream;
Douglas Gregor258ae542009-04-27 06:38:32 +00001121
1122 // The stream itself is going to skip over the source manager block.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001123 if (F.Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001124 Error("malformed block record in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001125 return Failure;
1126 }
1127
1128 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001129 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001130 Error("malformed source manager block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001131 return Failure;
1132 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001133
Douglas Gregora7f71a92009-04-10 03:52:48 +00001134 RecordData Record;
1135 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001136 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001137 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001138 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001139 Error("error at end of Source Manager block in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001140 return Failure;
1141 }
Douglas Gregor92863e42009-04-10 23:10:45 +00001142 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001143 }
Mike Stump11289f42009-09-09 15:08:12 +00001144
Douglas Gregora7f71a92009-04-10 03:52:48 +00001145 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1146 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +00001147 SLocEntryCursor.ReadSubBlockID();
1148 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001149 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001150 return Failure;
1151 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001152 continue;
1153 }
Mike Stump11289f42009-09-09 15:08:12 +00001154
Douglas Gregora7f71a92009-04-10 03:52:48 +00001155 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001156 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001157 continue;
1158 }
Mike Stump11289f42009-09-09 15:08:12 +00001159
Douglas Gregora7f71a92009-04-10 03:52:48 +00001160 // Read a record.
1161 const char *BlobStart;
1162 unsigned BlobLen;
1163 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +00001164 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001165 default: // Default behavior: ignore.
1166 break;
1167
Sebastian Redl539c5062010-08-18 23:57:32 +00001168 case SM_LINE_TABLE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00001169 if (ParseLineTable(F, Record))
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001170 return Failure;
Chris Lattner184e65d2009-04-14 23:22:57 +00001171 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00001172
Sebastian Redl539c5062010-08-18 23:57:32 +00001173 case SM_SLOC_FILE_ENTRY:
1174 case SM_SLOC_BUFFER_ENTRY:
1175 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor258ae542009-04-27 06:38:32 +00001176 // Once we hit one of the source location entries, we're done.
1177 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001178 }
1179 }
1180}
1181
Sebastian Redl06750302010-07-20 21:50:20 +00001182/// \brief Get a cursor that's correctly positioned for reading the source
1183/// location entry with the given ID.
Sebastian Redl2c373b92010-10-05 15:59:54 +00001184ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl06750302010-07-20 21:50:20 +00001185 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1186 "SLocCursorForID should only be called for real IDs.");
1187
1188 ID -= 1;
1189 PerFileData *F = 0;
1190 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1191 F = Chain[N - I - 1];
1192 if (ID < F->LocalNumSLocEntries)
1193 break;
1194 ID -= F->LocalNumSLocEntries;
1195 }
1196 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1197
1198 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00001199 return F;
Sebastian Redl06750302010-07-20 21:50:20 +00001200}
1201
Douglas Gregor258ae542009-04-27 06:38:32 +00001202/// \brief Read in the source location entry with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001203ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001204 if (ID == 0)
1205 return Success;
1206
1207 if (ID > TotalNumSLocEntries) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001208 Error("source location entry ID out-of-range for AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001209 return Failure;
1210 }
1211
Sebastian Redl2c373b92010-10-05 15:59:54 +00001212 PerFileData *F = SLocCursorForID(ID);
1213 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001214
Douglas Gregor258ae542009-04-27 06:38:32 +00001215 ++NumSLocEntriesRead;
Douglas Gregor258ae542009-04-27 06:38:32 +00001216 unsigned Code = SLocEntryCursor.ReadCode();
1217 if (Code == llvm::bitc::END_BLOCK ||
1218 Code == llvm::bitc::ENTER_SUBBLOCK ||
1219 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001220 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001221 return Failure;
1222 }
1223
Douglas Gregor258ae542009-04-27 06:38:32 +00001224 RecordData Record;
1225 const char *BlobStart;
1226 unsigned BlobLen;
1227 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1228 default:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001229 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001230 return Failure;
1231
Sebastian Redl539c5062010-08-18 23:57:32 +00001232 case SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001233 std::string Filename(BlobStart, BlobStart + BlobLen);
1234 MaybeAddSystemRootToFilename(Filename);
Chris Lattner5159f612010-11-23 08:35:12 +00001235 const FileEntry *File = FileMgr.getFile(Filename);
Axel Naumann63fbaed2011-01-27 10:55:51 +00001236 if (File == 0)
1237 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1238 (time_t)Record[5]);
Chris Lattnerd20dc872009-06-15 04:35:16 +00001239 if (File == 0) {
1240 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001241 ErrorStr += Filename;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001242 ErrorStr += "' referenced by AST file";
Chris Lattnerd20dc872009-06-15 04:35:16 +00001243 Error(ErrorStr.c_str());
1244 return Failure;
1245 }
Mike Stump11289f42009-09-09 15:08:12 +00001246
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001247 if (Record.size() < 10) {
Ted Kremenekabb1ddd2010-03-18 21:23:05 +00001248 Error("source location entry is incorrect");
1249 return Failure;
1250 }
1251
Douglas Gregorce3a8292010-07-27 00:27:13 +00001252 if (!DisableValidation &&
1253 ((off_t)Record[4] != File->getSize()
Douglas Gregor08288f22010-04-09 15:54:22 +00001254#if !defined(LLVM_ON_WIN32)
1255 // In our regression testing, the Windows file system seems to
1256 // have inconsistent modification times that sometimes
1257 // erroneously trigger this error-handling path.
Douglas Gregorce3a8292010-07-27 00:27:13 +00001258 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor08288f22010-04-09 15:54:22 +00001259#endif
Douglas Gregorce3a8292010-07-27 00:27:13 +00001260 )) {
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001261 Diag(diag::err_fe_pch_file_modified)
1262 << Filename;
1263 return Failure;
1264 }
1265
Chris Lattner26b5c192010-11-23 09:19:42 +00001266 FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]),
1267 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor258ae542009-04-27 06:38:32 +00001268 ID, Record[0]);
1269 if (Record[3])
1270 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1271 .setHasLineDirectives();
1272
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001273 // Reconstruct header-search information for this file.
1274 HeaderFileInfo HFI;
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001275 HFI.isImport = Record[6];
1276 HFI.DirInfo = Record[7];
1277 HFI.NumIncludes = Record[8];
1278 HFI.ControllingMacroID = Record[9];
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001279 if (Listener)
1280 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor258ae542009-04-27 06:38:32 +00001281 break;
1282 }
1283
Sebastian Redl539c5062010-08-18 23:57:32 +00001284 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor258ae542009-04-27 06:38:32 +00001285 const char *Name = BlobStart;
1286 unsigned Offset = Record[0];
1287 unsigned Code = SLocEntryCursor.ReadCode();
1288 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001289 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +00001290 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001291
Sebastian Redl539c5062010-08-18 23:57:32 +00001292 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001293 Error("AST record has invalid code");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001294 return Failure;
1295 }
1296
Douglas Gregor258ae542009-04-27 06:38:32 +00001297 llvm::MemoryBuffer *Buffer
Chris Lattner58c79342010-04-05 22:42:27 +00001298 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1299 Name);
Douglas Gregor258ae542009-04-27 06:38:32 +00001300 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump11289f42009-09-09 15:08:12 +00001301
Douglas Gregore6648fb2009-04-28 20:33:11 +00001302 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001303 PCHPredefinesBlock Block = {
1304 BufferID,
1305 llvm::StringRef(BlobStart, BlobLen - 1)
1306 };
1307 PCHPredefinesBuffers.push_back(Block);
Douglas Gregore6648fb2009-04-28 20:33:11 +00001308 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001309
1310 break;
1311 }
1312
Sebastian Redl539c5062010-08-18 23:57:32 +00001313 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001314 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001315 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001316 ReadSourceLocation(*F, Record[2]),
1317 ReadSourceLocation(*F, Record[3]),
Douglas Gregor258ae542009-04-27 06:38:32 +00001318 Record[4],
1319 ID,
1320 Record[0]);
1321 break;
Mike Stump11289f42009-09-09 15:08:12 +00001322 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001323 }
1324
1325 return Success;
1326}
1327
Chris Lattnere78a6be2009-04-27 01:05:14 +00001328/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1329/// specified cursor. Read the abbreviations that are at the top of the block
1330/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001331bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001332 unsigned BlockID) {
1333 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001334 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001335 return Failure;
1336 }
Mike Stump11289f42009-09-09 15:08:12 +00001337
Chris Lattnere78a6be2009-04-27 01:05:14 +00001338 while (true) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001339 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattnere78a6be2009-04-27 01:05:14 +00001340 unsigned Code = Cursor.ReadCode();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001341
Chris Lattnere78a6be2009-04-27 01:05:14 +00001342 // We expect all abbrevs to be at the start of the block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001343 if (Code != llvm::bitc::DEFINE_ABBREV) {
1344 Cursor.JumpToBit(Offset);
Chris Lattnere78a6be2009-04-27 01:05:14 +00001345 return false;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001346 }
Chris Lattnere78a6be2009-04-27 01:05:14 +00001347 Cursor.ReadAbbrevRecord();
1348 }
1349}
1350
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001351PreprocessedEntity *ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001352 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregor796d76a2010-10-20 22:00:55 +00001353 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump11289f42009-09-09 15:08:12 +00001354
Douglas Gregorc3366a52009-04-21 23:56:24 +00001355 // Keep track of where we are in the stream, then jump back there
1356 // after reading this macro.
1357 SavedStreamPosition SavedPosition(Stream);
1358
1359 Stream.JumpToBit(Offset);
1360 RecordData Record;
1361 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1362 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001363
Douglas Gregorc3366a52009-04-21 23:56:24 +00001364 while (true) {
1365 unsigned Code = Stream.ReadCode();
1366 switch (Code) {
1367 case llvm::bitc::END_BLOCK:
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001368 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001369
1370 case llvm::bitc::ENTER_SUBBLOCK:
1371 // No known subblocks, always skip them.
1372 Stream.ReadSubBlockID();
1373 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001374 Error("malformed block record in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001375 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001376 }
1377 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001378
Douglas Gregorc3366a52009-04-21 23:56:24 +00001379 case llvm::bitc::DEFINE_ABBREV:
1380 Stream.ReadAbbrevRecord();
1381 continue;
1382 default: break;
1383 }
1384
1385 // Read a record.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001386 const char *BlobStart = 0;
1387 unsigned BlobLen = 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001388 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001389 PreprocessorRecordTypes RecType =
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001390 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001391 BlobLen);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001392 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001393 case PP_MACRO_OBJECT_LIKE:
1394 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001395 // If we already have a macro, that means that we've hit the end
1396 // of the definition of the macro we were looking for. We're
1397 // done.
1398 if (Macro)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001399 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001400
1401 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1402 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001403 Error("macro must have a name in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001404 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001405 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00001406 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001407 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001408
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001409 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001410 MI->setIsUsed(isUsed);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001411 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001412
Douglas Gregoraae92242010-03-19 21:51:54 +00001413 unsigned NextIndex = 3;
Sebastian Redl539c5062010-08-18 23:57:32 +00001414 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001415 // Decode function-like macro info.
1416 bool isC99VarArgs = Record[3];
1417 bool isGNUVarArgs = Record[4];
1418 MacroArgs.clear();
1419 unsigned NumArgs = Record[5];
Douglas Gregoraae92242010-03-19 21:51:54 +00001420 NextIndex = 6 + NumArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001421 for (unsigned i = 0; i != NumArgs; ++i)
1422 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1423
1424 // Install function-like macro info.
1425 MI->setIsFunctionLike();
1426 if (isC99VarArgs) MI->setIsC99Varargs();
1427 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001428 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001429 PP->getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001430 }
1431
1432 // Finally, install the macro.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001433 PP->setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001434
1435 // Remember that we saw this macro last so that we add the tokens that
1436 // form its body to it.
1437 Macro = MI;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001438
Douglas Gregoraae92242010-03-19 21:51:54 +00001439 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1440 // We have a macro definition. Load it now.
1441 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1442 getMacroDefinition(Record[NextIndex]));
1443 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001444
Douglas Gregorc3366a52009-04-21 23:56:24 +00001445 ++NumMacrosRead;
1446 break;
1447 }
Mike Stump11289f42009-09-09 15:08:12 +00001448
Sebastian Redl539c5062010-08-18 23:57:32 +00001449 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001450 // If we see a TOKEN before a PP_MACRO_*, then the file is
1451 // erroneous, just pretend we didn't see this.
1452 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001453
Douglas Gregorc3366a52009-04-21 23:56:24 +00001454 Token Tok;
1455 Tok.startToken();
Sebastian Redl2c373b92010-10-05 15:59:54 +00001456 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001457 Tok.setLength(Record[1]);
1458 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1459 Tok.setIdentifierInfo(II);
1460 Tok.setKind((tok::TokenKind)Record[3]);
1461 Tok.setFlag((Token::TokenFlags)Record[4]);
1462 Macro->AddTokenToBody(Tok);
1463 break;
1464 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001465
Sebastian Redl539c5062010-08-18 23:57:32 +00001466 case PP_MACRO_INSTANTIATION: {
Douglas Gregoraae92242010-03-19 21:51:54 +00001467 // If we already have a macro, that means that we've hit the end
1468 // of the definition of the macro we were looking for. We're
1469 // done.
1470 if (Macro)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001471 return 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001472
Douglas Gregoraae92242010-03-19 21:51:54 +00001473 if (!PP->getPreprocessingRecord()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001474 Error("missing preprocessing record in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001475 return 0;
Douglas Gregoraae92242010-03-19 21:51:54 +00001476 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001477
Douglas Gregoraae92242010-03-19 21:51:54 +00001478 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001479 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1480 return PE;
Douglas Gregoraae92242010-03-19 21:51:54 +00001481
1482 MacroInstantiation *MI
1483 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
Sebastian Redl2c373b92010-10-05 15:59:54 +00001484 SourceRange(ReadSourceLocation(F, Record[1]),
1485 ReadSourceLocation(F, Record[2])),
Douglas Gregoraae92242010-03-19 21:51:54 +00001486 getMacroDefinition(Record[4]));
1487 PPRec.SetPreallocatedEntity(Record[0], MI);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001488 return MI;
Douglas Gregoraae92242010-03-19 21:51:54 +00001489 }
1490
Sebastian Redl539c5062010-08-18 23:57:32 +00001491 case PP_MACRO_DEFINITION: {
Douglas Gregoraae92242010-03-19 21:51:54 +00001492 // If we already have a macro, that means that we've hit the end
1493 // of the definition of the macro we were looking for. We're
1494 // done.
1495 if (Macro)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001496 return 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001497
Douglas Gregoraae92242010-03-19 21:51:54 +00001498 if (!PP->getPreprocessingRecord()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001499 Error("missing preprocessing record in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001500 return 0;
Douglas Gregoraae92242010-03-19 21:51:54 +00001501 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001502
Douglas Gregoraae92242010-03-19 21:51:54 +00001503 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001504 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1505 return PE;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001506
Douglas Gregor91096292010-10-02 19:29:26 +00001507 if (Record[1] > MacroDefinitionsLoaded.size()) {
Douglas Gregoraae92242010-03-19 21:51:54 +00001508 Error("out-of-bounds macro definition record");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001509 return 0;
Douglas Gregoraae92242010-03-19 21:51:54 +00001510 }
1511
Douglas Gregor91096292010-10-02 19:29:26 +00001512 // Decode the identifier info and then check again; if the macro is
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001513 // still defined and associated with the identifier,
Douglas Gregor91096292010-10-02 19:29:26 +00001514 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1515 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1516 MacroDefinition *MD
1517 = new (PPRec) MacroDefinition(II,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001518 ReadSourceLocation(F, Record[5]),
Douglas Gregor36ea4d42010-10-01 20:33:34 +00001519 SourceRange(
Sebastian Redl2c373b92010-10-05 15:59:54 +00001520 ReadSourceLocation(F, Record[2]),
1521 ReadSourceLocation(F, Record[3])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001522
Douglas Gregor91096292010-10-02 19:29:26 +00001523 PPRec.SetPreallocatedEntity(Record[0], MD);
1524 MacroDefinitionsLoaded[Record[1] - 1] = MD;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001525
Douglas Gregor91096292010-10-02 19:29:26 +00001526 if (DeserializationListener)
1527 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1528 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001529
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001530 return MacroDefinitionsLoaded[Record[1] - 1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001531 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001532
Douglas Gregor796d76a2010-10-20 22:00:55 +00001533 case PP_INCLUSION_DIRECTIVE: {
1534 // If we already have a macro, that means that we've hit the end
1535 // of the definition of the macro we were looking for. We're
1536 // done.
1537 if (Macro)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001538 return 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001539
Douglas Gregor796d76a2010-10-20 22:00:55 +00001540 if (!PP->getPreprocessingRecord()) {
1541 Error("missing preprocessing record in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001542 return 0;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001543 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001544
Douglas Gregor796d76a2010-10-20 22:00:55 +00001545 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001546 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1547 return PE;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001548
1549 const char *FullFileNameStart = BlobStart + Record[3];
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001550 const FileEntry *File
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001551 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
1552 BlobLen - Record[3]));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001553
Douglas Gregor796d76a2010-10-20 22:00:55 +00001554 // FIXME: Stable encoding
1555 InclusionDirective::InclusionKind Kind
1556 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1557 InclusionDirective *ID
Douglas Gregorf09b6c92010-11-01 15:03:47 +00001558 = new (PPRec) InclusionDirective(PPRec, Kind,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001559 llvm::StringRef(BlobStart, Record[3]),
1560 Record[4],
1561 File,
1562 SourceRange(ReadSourceLocation(F, Record[1]),
1563 ReadSourceLocation(F, Record[2])));
1564 PPRec.SetPreallocatedEntity(Record[0], ID);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001565 return ID;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001566 }
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001567 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001568 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001569
1570 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001571}
1572
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001573void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1574 uint64_t Offset) {
1575 // Note that this identifier has a macro definition.
1576 II->setHasMacroDefinition(true);
1577
1578 // Adjust the offset based on our position in the chain.
1579 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1580 if (Chain[I] == &F)
1581 break;
1582
1583 Offset += Chain[I]->SizeInBits;
1584 }
1585
1586 UnreadMacroRecordOffsets[II] = Offset;
1587}
1588
Sebastian Redl2c499f62010-08-18 23:56:43 +00001589void ASTReader::ReadDefinedMacros() {
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001590 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001591 PerFileData &F = *Chain[N - I - 1];
1592 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001593
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001594 // If there was no preprocessor block, skip this file.
1595 if (!MacroCursor.getBitStreamReader())
1596 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001597
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001598 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001599 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001600
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001601 RecordData Record;
1602 while (true) {
Sebastian Redl4102dd52010-09-28 02:55:49 +00001603 uint64_t Offset = Cursor.GetCurrentBitNo();
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001604 unsigned Code = Cursor.ReadCode();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001605 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001606 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001607
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001608 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1609 // No known subblocks, always skip them.
1610 Cursor.ReadSubBlockID();
1611 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001612 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001613 return;
1614 }
1615 continue;
1616 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001617
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001618 if (Code == llvm::bitc::DEFINE_ABBREV) {
1619 Cursor.ReadAbbrevRecord();
1620 continue;
1621 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001622
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001623 // Read a record.
1624 const char *BlobStart;
1625 unsigned BlobLen;
1626 Record.clear();
1627 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1628 default: // Default behavior: ignore.
1629 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001630
Sebastian Redl539c5062010-08-18 23:57:32 +00001631 case PP_MACRO_OBJECT_LIKE:
1632 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001633 DecodeIdentifierInfo(Record[0]);
1634 break;
1635
Sebastian Redl539c5062010-08-18 23:57:32 +00001636 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001637 // Ignore tokens.
1638 break;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001639
Sebastian Redl539c5062010-08-18 23:57:32 +00001640 case PP_MACRO_INSTANTIATION:
1641 case PP_MACRO_DEFINITION:
Douglas Gregor796d76a2010-10-20 22:00:55 +00001642 case PP_INCLUSION_DIRECTIVE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001643 // Read the macro record.
Sebastian Redl4102dd52010-09-28 02:55:49 +00001644 // FIXME: That's a stupid way to do this. We should reuse this cursor.
Sebastian Redl2c373b92010-10-05 15:59:54 +00001645 ReadMacroRecord(F, Offset);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001646 break;
1647 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001648 }
1649 }
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001650
1651 // Drain the unread macro-record offsets map.
1652 while (!UnreadMacroRecordOffsets.empty())
1653 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1654}
1655
1656void ASTReader::LoadMacroDefinition(
1657 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1658 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1659 PerFileData *F = 0;
1660 uint64_t Offset = Pos->second;
1661 UnreadMacroRecordOffsets.erase(Pos);
1662
1663 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1664 if (Offset < Chain[I]->SizeInBits) {
1665 F = Chain[I];
1666 break;
1667 }
1668
1669 Offset -= Chain[I]->SizeInBits;
1670 }
1671 if (!F) {
1672 Error("Malformed macro record offset");
1673 return;
1674 }
1675
1676 ReadMacroRecord(*F, Offset);
1677}
1678
1679void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1680 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1681 = UnreadMacroRecordOffsets.find(II);
1682 LoadMacroDefinition(Pos);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001683}
1684
Sebastian Redl50e26582010-09-15 19:54:06 +00001685MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor91096292010-10-02 19:29:26 +00001686 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregoraae92242010-03-19 21:51:54 +00001687 return 0;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001688
Douglas Gregor91096292010-10-02 19:29:26 +00001689 if (!MacroDefinitionsLoaded[ID - 1]) {
1690 unsigned Index = ID - 1;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001691 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1692 PerFileData &F = *Chain[N - I - 1];
1693 if (Index < F.LocalNumMacroDefinitions) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001694 ReadMacroRecord(F, F.MacroDefinitionOffsets[Index]);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001695 break;
1696 }
1697 Index -= F.LocalNumMacroDefinitions;
1698 }
Douglas Gregor91096292010-10-02 19:29:26 +00001699 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001700 }
1701
Douglas Gregor91096292010-10-02 19:29:26 +00001702 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001703}
1704
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001705/// \brief If we are loading a relocatable PCH file, and the filename is
1706/// not an absolute path, add the system root to the beginning of the file
1707/// name.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001708void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001709 // If this is not a relocatable PCH file, there's nothing to do.
1710 if (!RelocatablePCH)
1711 return;
Mike Stump11289f42009-09-09 15:08:12 +00001712
Michael J. Spencerf28df4c2010-12-17 21:22:22 +00001713 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001714 return;
1715
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001716 if (isysroot == 0) {
1717 // If no system root was given, default to '/'
1718 Filename.insert(Filename.begin(), '/');
1719 return;
1720 }
Mike Stump11289f42009-09-09 15:08:12 +00001721
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001722 unsigned Length = strlen(isysroot);
1723 if (isysroot[Length - 1] != '/')
1724 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001725
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001726 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1727}
1728
Sebastian Redl2c499f62010-08-18 23:56:43 +00001729ASTReader::ASTReadResult
Sebastian Redl3e31c722010-08-18 23:56:56 +00001730ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001731 llvm::BitstreamCursor &Stream = F.Stream;
1732
Sebastian Redl539c5062010-08-18 23:57:32 +00001733 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001734 Error("malformed block record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001735 return Failure;
1736 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001737
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001738 // Read all of the records and blocks for the ASt file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001739 RecordData Record;
Sebastian Redl393f8b72010-07-19 20:52:06 +00001740 bool First = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001741 while (!Stream.AtEndOfStream()) {
1742 unsigned Code = Stream.ReadCode();
1743 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001744 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001745 Error("error at end of module block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001746 return Failure;
1747 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001748
Douglas Gregor55abb232009-04-10 20:39:37 +00001749 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001750 }
1751
1752 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1753 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001754 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001755 // We lazily load the decls block, but we want to set up the
1756 // DeclsCursor cursor to point into it. Clone our current bitcode
1757 // cursor to it, enter the block and read the abbrevs in that block.
1758 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001759 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001760 if (Stream.SkipBlock() || // Skip with the main cursor.
1761 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001762 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001763 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001764 return Failure;
1765 }
1766 break;
Mike Stump11289f42009-09-09 15:08:12 +00001767
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00001768 case DECL_UPDATES_BLOCK_ID:
1769 if (Stream.SkipBlock()) {
1770 Error("malformed block record in AST file");
1771 return Failure;
1772 }
1773 break;
1774
Sebastian Redl539c5062010-08-18 23:57:32 +00001775 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001776 F.MacroCursor = Stream;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001777 if (PP)
1778 PP->setExternalSource(this);
1779
Douglas Gregor796d76a2010-10-20 22:00:55 +00001780 if (Stream.SkipBlock() ||
1781 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001782 Error("malformed block record in AST file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001783 return Failure;
1784 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001785 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001786 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001787
Sebastian Redl539c5062010-08-18 23:57:32 +00001788 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001789 switch (ReadSourceManagerBlock(F)) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001790 case Success:
1791 break;
1792
1793 case Failure:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001794 Error("malformed source manager block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001795 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001796
1797 case IgnorePCH:
1798 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001799 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001800 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001801 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00001802 First = false;
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001803 continue;
1804 }
1805
1806 if (Code == llvm::bitc::DEFINE_ABBREV) {
1807 Stream.ReadAbbrevRecord();
1808 continue;
1809 }
1810
1811 // Read and process a record.
1812 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001813 const char *BlobStart = 0;
1814 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001815 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001816 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001817 default: // Default behavior: ignore.
1818 break;
1819
Sebastian Redl539c5062010-08-18 23:57:32 +00001820 case METADATA: {
1821 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1822 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001823 : diag::warn_pch_version_too_new);
1824 return IgnorePCH;
1825 }
1826
1827 RelocatablePCH = Record[4];
1828 if (Listener) {
1829 std::string TargetTriple(BlobStart, BlobLen);
1830 if (Listener->ReadTargetTriple(TargetTriple))
1831 return IgnorePCH;
1832 }
1833 break;
1834 }
1835
Sebastian Redl539c5062010-08-18 23:57:32 +00001836 case CHAINED_METADATA: {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001837 if (!First) {
1838 Error("CHAINED_METADATA is not first record in block");
1839 return Failure;
1840 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001841 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1842 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001843 : diag::warn_pch_version_too_new);
1844 return IgnorePCH;
1845 }
1846
Sebastian Redl009e7f22010-10-05 16:15:19 +00001847 // Load the chained file, which is always a PCH file.
1848 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001849 case Failure: return Failure;
1850 // If we have to ignore the dependency, we'll have to ignore this too.
1851 case IgnorePCH: return IgnorePCH;
1852 case Success: break;
1853 }
1854 break;
1855 }
1856
Sebastian Redl539c5062010-08-18 23:57:32 +00001857 case TYPE_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001858 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001859 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001860 return Failure;
1861 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001862 F.TypeOffsets = (const uint32_t *)BlobStart;
1863 F.LocalNumTypes = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001864 break;
1865
Sebastian Redl539c5062010-08-18 23:57:32 +00001866 case DECL_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001867 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001868 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001869 return Failure;
1870 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001871 F.DeclOffsets = (const uint32_t *)BlobStart;
1872 F.LocalNumDecls = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001873 break;
Douglas Gregor55abb232009-04-10 20:39:37 +00001874
Sebastian Redl539c5062010-08-18 23:57:32 +00001875 case TU_UPDATE_LEXICAL: {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001876 DeclContextInfo Info = {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001877 /* No visible information */ 0,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001878 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
1879 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001880 };
Douglas Gregoraa433012010-10-01 01:18:02 +00001881 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
1882 .push_back(Info);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001883 break;
1884 }
1885
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001886 case UPDATE_VISIBLE: {
1887 serialization::DeclID ID = Record[0];
1888 void *Table = ASTDeclContextNameLookupTable::Create(
1889 (const unsigned char *)BlobStart + Record[1],
1890 (const unsigned char *)BlobStart,
1891 ASTDeclContextNameLookupTrait(*this));
Douglas Gregoraa433012010-10-01 01:18:02 +00001892 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001893 DeclContextInfo Info = {
1894 Table, /* No lexical inforamtion */ 0, 0
1895 };
1896 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1897 } else
1898 PendingVisibleUpdates[ID].push_back(Table);
1899 break;
1900 }
1901
Sebastian Redl539c5062010-08-18 23:57:32 +00001902 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001903 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1904 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001905 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001906 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1907 Latest > FirstLatestDeclIDs[First]) &&
1908 "The new latest is supposed to come after the previous latest");
1909 FirstLatestDeclIDs[First] = Latest;
1910 }
1911 break;
1912 }
1913
Sebastian Redl539c5062010-08-18 23:57:32 +00001914 case LANGUAGE_OPTIONS:
Douglas Gregorce3a8292010-07-27 00:27:13 +00001915 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor55abb232009-04-10 20:39:37 +00001916 return IgnorePCH;
1917 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00001918
Sebastian Redl539c5062010-08-18 23:57:32 +00001919 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001920 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00001921 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001922 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001923 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00001924 (const unsigned char *)F.IdentifierTableData + Record[0],
1925 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001926 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001927 if (PP)
1928 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00001929 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001930 break;
1931
Sebastian Redl539c5062010-08-18 23:57:32 +00001932 case IDENTIFIER_OFFSET:
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001933 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001934 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001935 return Failure;
1936 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001937 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1938 F.LocalNumIdentifiers = Record[0];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001939 break;
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001940
Sebastian Redl539c5062010-08-18 23:57:32 +00001941 case EXTERNAL_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001942 // Optimization for the first block.
1943 if (ExternalDefinitions.empty())
1944 ExternalDefinitions.swap(Record);
1945 else
1946 ExternalDefinitions.insert(ExternalDefinitions.end(),
1947 Record.begin(), Record.end());
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001948 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00001949
Sebastian Redl539c5062010-08-18 23:57:32 +00001950 case SPECIAL_TYPES:
Sebastian Redlb293a452010-07-20 21:20:32 +00001951 // Optimization for the first block
1952 if (SpecialTypes.empty())
1953 SpecialTypes.swap(Record);
1954 else
1955 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregor652d82a2009-04-18 05:55:16 +00001956 break;
1957
Sebastian Redl539c5062010-08-18 23:57:32 +00001958 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001959 TotalNumStatements += Record[0];
1960 TotalNumMacros += Record[1];
1961 TotalLexicalDeclContexts += Record[2];
1962 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00001963 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00001964
Sebastian Redl539c5062010-08-18 23:57:32 +00001965 case TENTATIVE_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001966 // Optimization for the first block.
1967 if (TentativeDefinitions.empty())
1968 TentativeDefinitions.swap(Record);
1969 else
1970 TentativeDefinitions.insert(TentativeDefinitions.end(),
1971 Record.begin(), Record.end());
Douglas Gregord4df8652009-04-22 22:02:47 +00001972 break;
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001973
Sebastian Redl539c5062010-08-18 23:57:32 +00001974 case UNUSED_FILESCOPED_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001975 // Optimization for the first block.
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00001976 if (UnusedFileScopedDecls.empty())
1977 UnusedFileScopedDecls.swap(Record);
Sebastian Redlb293a452010-07-20 21:20:32 +00001978 else
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00001979 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1980 Record.begin(), Record.end());
Tanya Lattner90073802010-02-12 00:07:30 +00001981 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001982
Sebastian Redl539c5062010-08-18 23:57:32 +00001983 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001984 // Later blocks overwrite earlier ones.
1985 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00001986 break;
1987
Sebastian Redl539c5062010-08-18 23:57:32 +00001988 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001989 // Optimization for the first block.
1990 if (LocallyScopedExternalDecls.empty())
1991 LocallyScopedExternalDecls.swap(Record);
1992 else
1993 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1994 Record.begin(), Record.end());
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001995 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001996
Sebastian Redl539c5062010-08-18 23:57:32 +00001997 case SELECTOR_OFFSETS:
Sebastian Redla19a67f2010-08-03 21:58:15 +00001998 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00001999 F.LocalNumSelectors = Record[0];
Douglas Gregor95c13f52009-04-25 17:48:32 +00002000 break;
2001
Sebastian Redl539c5062010-08-18 23:57:32 +00002002 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00002003 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002004 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00002005 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002006 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00002007 F.SelectorLookupTableData + Record[0],
2008 F.SelectorLookupTableData,
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002009 ASTSelectorLookupTrait(*this));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002010 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002011 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00002012
Sebastian Redl96371b42010-09-22 00:42:30 +00002013 case REFERENCED_SELECTOR_POOL:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002014 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002015 break;
2016
Sebastian Redl539c5062010-08-18 23:57:32 +00002017 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002018 if (!Record.empty() && Listener)
2019 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002020 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002021
Sebastian Redl539c5062010-08-18 23:57:32 +00002022 case SOURCE_LOCATION_OFFSETS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002023 F.SLocOffsets = (const uint32_t *)BlobStart;
2024 F.LocalNumSLocEntries = Record[0];
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002025 F.LocalSLocSize = Record[1];
Douglas Gregor258ae542009-04-27 06:38:32 +00002026 break;
2027
Sebastian Redl539c5062010-08-18 23:57:32 +00002028 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl96371b42010-09-22 00:42:30 +00002029 if (PreloadSLocEntries.empty())
2030 PreloadSLocEntries.swap(Record);
2031 else
2032 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2033 Record.begin(), Record.end());
Douglas Gregor258ae542009-04-27 06:38:32 +00002034 break;
Douglas Gregorc5046832009-04-27 18:38:38 +00002035
Sebastian Redl539c5062010-08-18 23:57:32 +00002036 case STAT_CACHE: {
Douglas Gregor606c4ac2011-02-05 19:42:43 +00002037 if (!DisableStatCache) {
2038 ASTStatCache *MyStatCache =
2039 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2040 (const unsigned char *)BlobStart,
2041 NumStatHits, NumStatMisses);
2042 FileMgr.addStatCache(MyStatCache);
2043 F.StatCache = MyStatCache;
2044 }
Douglas Gregorc5046832009-04-27 18:38:38 +00002045 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00002046 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002047
Sebastian Redl539c5062010-08-18 23:57:32 +00002048 case EXT_VECTOR_DECLS:
Sebastian Redl04f5c312010-07-28 21:38:49 +00002049 // Optimization for the first block.
2050 if (ExtVectorDecls.empty())
2051 ExtVectorDecls.swap(Record);
2052 else
2053 ExtVectorDecls.insert(ExtVectorDecls.end(),
2054 Record.begin(), Record.end());
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002055 break;
2056
Sebastian Redl539c5062010-08-18 23:57:32 +00002057 case VTABLE_USES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002058 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002059 VTableUses.swap(Record);
2060 break;
2061
Sebastian Redl539c5062010-08-18 23:57:32 +00002062 case DYNAMIC_CLASSES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002063 // Optimization for the first block.
2064 if (DynamicClasses.empty())
2065 DynamicClasses.swap(Record);
2066 else
2067 DynamicClasses.insert(DynamicClasses.end(),
2068 Record.begin(), Record.end());
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002069 break;
2070
Sebastian Redl539c5062010-08-18 23:57:32 +00002071 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002072 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002073 break;
2074
Sebastian Redl539c5062010-08-18 23:57:32 +00002075 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002076 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002077 SemaDeclRefs.swap(Record);
2078 break;
2079
Sebastian Redl539c5062010-08-18 23:57:32 +00002080 case ORIGINAL_FILE_NAME:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002081 // The primary AST will be the last to get here, so it will be the one
Sebastian Redlb293a452010-07-20 21:20:32 +00002082 // that's used.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00002083 ActualOriginalFileName.assign(BlobStart, BlobLen);
2084 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002085 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00002086 break;
Mike Stump11289f42009-09-09 15:08:12 +00002087
Sebastian Redl539c5062010-08-18 23:57:32 +00002088 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00002089 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002090 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2091 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2092 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregord54f3a12009-10-05 21:07:28 +00002093 return IgnorePCH;
2094 }
2095 break;
2096 }
Sebastian Redlfa061442010-07-21 20:07:32 +00002097
Sebastian Redl539c5062010-08-18 23:57:32 +00002098 case MACRO_DEFINITION_OFFSETS:
Sebastian Redlfa061442010-07-21 20:07:32 +00002099 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2100 F.NumPreallocatedPreprocessingEntities = Record[0];
2101 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregoraae92242010-03-19 21:51:54 +00002102 break;
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002103
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002104 case DECL_UPDATE_OFFSETS: {
2105 if (Record.size() % 2 != 0) {
2106 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2107 return Failure;
2108 }
2109 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2110 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2111 .push_back(std::make_pair(&F, Record[I+1]));
2112 break;
2113 }
2114
Sebastian Redl539c5062010-08-18 23:57:32 +00002115 case DECL_REPLACEMENTS: {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002116 if (Record.size() % 2 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002117 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002118 return Failure;
2119 }
2120 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl539c5062010-08-18 23:57:32 +00002121 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002122 std::make_pair(&F, Record[I+1]);
2123 break;
2124 }
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002125
2126 case CXX_BASE_SPECIFIER_OFFSETS: {
2127 if (F.LocalNumCXXBaseSpecifiers != 0) {
2128 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2129 return Failure;
2130 }
2131
2132 F.LocalNumCXXBaseSpecifiers = Record[0];
2133 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2134 break;
2135 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002136
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002137 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002138 if (Record.size() % 2 != 0) {
2139 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2140 return Failure;
2141 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002142 if (PragmaDiagMappings.empty())
2143 PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002144 else
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002145 PragmaDiagMappings.insert(PragmaDiagMappings.end(),
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002146 Record.begin(), Record.end());
2147 break;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002148 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00002149 First = false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002150 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002151 Error("premature end of bitstream in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002152 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002153}
2154
Sebastian Redl009e7f22010-10-05 16:15:19 +00002155ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2156 ASTFileType Type) {
2157 switch(ReadASTCore(FileName, Type)) {
Sebastian Redl2abc0382010-07-16 20:41:52 +00002158 case Failure: return Failure;
2159 case IgnorePCH: return IgnorePCH;
2160 case Success: break;
2161 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002162
2163 // Here comes stuff that we only do once the entire chain is loaded.
2164
Sebastian Redl96371b42010-09-22 00:42:30 +00002165 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redlfa061442010-07-21 20:07:32 +00002166 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redlada023c2010-08-04 20:40:17 +00002167 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2168 TotalNumSelectors = 0;
Sebastian Redl9e687992010-07-19 22:06:55 +00002169 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl96371b42010-09-22 00:42:30 +00002170 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002171 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002172 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl9e687992010-07-19 22:06:55 +00002173 TotalNumTypes += Chain[I]->LocalNumTypes;
2174 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redlfa061442010-07-21 20:07:32 +00002175 TotalNumPreallocatedPreprocessingEntities +=
2176 Chain[I]->NumPreallocatedPreprocessingEntities;
2177 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redlada023c2010-08-04 20:40:17 +00002178 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl9e687992010-07-19 22:06:55 +00002179 }
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002180 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002181 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl9e687992010-07-19 22:06:55 +00002182 TypesLoaded.resize(TotalNumTypes);
2183 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redlfa061442010-07-21 20:07:32 +00002184 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2185 if (PP) {
2186 if (TotalNumIdentifiers > 0)
2187 PP->getHeaderSearchInfo().SetExternalLookup(this);
2188 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2189 if (!PP->getPreprocessingRecord())
2190 PP->createPreprocessingRecord();
2191 PP->getPreprocessingRecord()->SetExternalSource(*this,
2192 TotalNumPreallocatedPreprocessingEntities);
2193 }
2194 }
Sebastian Redlada023c2010-08-04 20:40:17 +00002195 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl96371b42010-09-22 00:42:30 +00002196 // Preload SLocEntries.
2197 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2198 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2199 if (Result != Success)
2200 return Result;
2201 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002202
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002203 // Check the predefines buffers.
Douglas Gregorce3a8292010-07-27 00:27:13 +00002204 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002205 return IgnorePCH;
2206
2207 if (PP) {
2208 // Initialization of keywords and pragmas occurs before the
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002209 // AST file is read, so there may be some identifiers that were
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002210 // loaded into the IdentifierTable before we intercepted the
2211 // creation of identifiers. Iterate through the list of known
2212 // identifiers and determine whether we have to establish
2213 // preprocessor definitions or top-level identifier declaration
2214 // chains for those identifiers.
2215 //
2216 // We copy the IdentifierInfo pointers to a small vector first,
2217 // since de-serializing declarations or macro definitions can add
2218 // new entries into the identifier table, invalidating the
2219 // iterators.
2220 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2221 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2222 IdEnd = PP->getIdentifierTable().end();
2223 Id != IdEnd; ++Id)
2224 Identifiers.push_back(Id->second);
Sebastian Redlfa061442010-07-21 20:07:32 +00002225 // We need to search the tables in all files.
Sebastian Redlfa061442010-07-21 20:07:32 +00002226 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002227 ASTIdentifierLookupTable *IdTable
2228 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2229 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl5c415f32010-07-22 17:01:13 +00002230 // ones.
2231 if (!IdTable)
2232 continue;
Sebastian Redlfa061442010-07-21 20:07:32 +00002233 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2234 IdentifierInfo *II = Identifiers[I];
2235 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00002236 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redlfa061442010-07-21 20:07:32 +00002237 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002238 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redlb293a452010-07-20 21:20:32 +00002239 if (Pos == IdTable->end())
2240 continue;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002241
Sebastian Redlb293a452010-07-20 21:20:32 +00002242 // Dereferencing the iterator has the effect of populating the
2243 // IdentifierInfo node with the various declarations it needs.
2244 (void)*Pos;
2245 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002246 }
2247 }
2248
2249 if (Context)
2250 InitializeContext(*Context);
2251
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002252 if (DeserializationListener)
2253 DeserializationListener->ReaderInitialized(this);
2254
Douglas Gregor936a5b42010-11-30 05:23:00 +00002255 // If this AST file is a precompiled preamble, then set the main file ID of
2256 // the source manager to the file source file from which the preamble was
2257 // built. This is the only valid way to use a precompiled preamble.
2258 if (Type == Preamble) {
2259 SourceLocation Loc
2260 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2261 if (Loc.isValid()) {
2262 std::pair<FileID, unsigned> Decomposed = SourceMgr.getDecomposedLoc(Loc);
2263 SourceMgr.SetPreambleFileID(Decomposed.first);
2264 }
2265 }
2266
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002267 return Success;
2268}
2269
Sebastian Redl009e7f22010-10-05 16:15:19 +00002270ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2271 ASTFileType Type) {
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002272 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl009e7f22010-10-05 16:15:19 +00002273 Chain.push_back(new PerFileData(Type));
Sebastian Redl34522812010-07-16 17:50:48 +00002274 PerFileData &F = *Chain.back();
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002275 if (Prev)
2276 Prev->NextInSource = &F;
2277 else
2278 FirstInSource = &F;
2279 F.Loaders.push_back(Prev);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002280
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002281 // Set the AST file name.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002282 F.FileName = FileName;
2283
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002284 // Open the AST file.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002285 //
2286 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2287 std::string ErrStr;
Michael J. Spencerf25faaa2010-12-09 17:36:38 +00002288 llvm::error_code ec;
2289 if (FileName == "-") {
Michael J. Spencerd9da7a12010-12-16 03:28:14 +00002290 ec = llvm::MemoryBuffer::getSTDIN(F.Buffer);
Michael J. Spencerf25faaa2010-12-09 17:36:38 +00002291 if (ec)
2292 ErrStr = ec.message();
2293 } else
Chris Lattner5159f612010-11-23 08:35:12 +00002294 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002295 if (!F.Buffer) {
2296 Error(ErrStr.c_str());
2297 return IgnorePCH;
2298 }
2299
2300 // Initialize the stream
2301 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2302 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl34522812010-07-16 17:50:48 +00002303 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002304 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002305 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002306
2307 // Sniff for the signature.
2308 if (Stream.Read(8) != 'C' ||
2309 Stream.Read(8) != 'P' ||
2310 Stream.Read(8) != 'C' ||
2311 Stream.Read(8) != 'H') {
2312 Diag(diag::err_not_a_pch_file) << FileName;
2313 return Failure;
2314 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002315
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002316 while (!Stream.AtEndOfStream()) {
2317 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002318
Douglas Gregor92863e42009-04-10 23:10:45 +00002319 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002320 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002321 return Failure;
2322 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002323
2324 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002325
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002326 // We only know the AST subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002327 switch (BlockID) {
2328 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002329 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002330 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002331 return Failure;
2332 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002333 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00002334 case AST_BLOCK_ID:
Sebastian Redl3e31c722010-08-18 23:56:56 +00002335 switch (ReadASTBlock(F)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002336 case Success:
2337 break;
2338
2339 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00002340 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00002341
2342 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00002343 // FIXME: We could consider reading through to the end of this
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002344 // AST block, skipping subblocks, to see if there are other
2345 // AST blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00002346
2347 // Clear out any preallocated source location entries, so that
2348 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002349 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00002350
2351 // Remove the stat cache.
Sebastian Redl34522812010-07-16 17:50:48 +00002352 if (F.StatCache)
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002353 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00002354
Douglas Gregor92863e42009-04-10 23:10:45 +00002355 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002356 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002357 break;
2358 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002359 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002360 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002361 return Failure;
2362 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002363 break;
2364 }
Mike Stump11289f42009-09-09 15:08:12 +00002365 }
2366
Sebastian Redl2abc0382010-07-16 20:41:52 +00002367 return Success;
2368}
2369
Sebastian Redl2c499f62010-08-18 23:56:43 +00002370void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002371 PP = &pp;
Sebastian Redlfa061442010-07-21 20:07:32 +00002372
2373 unsigned TotalNum = 0;
2374 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2375 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2376 if (TotalNum) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002377 if (!PP->getPreprocessingRecord())
2378 PP->createPreprocessingRecord();
Sebastian Redlfa061442010-07-21 20:07:32 +00002379 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregoraae92242010-03-19 21:51:54 +00002380 }
2381}
2382
Sebastian Redl2c499f62010-08-18 23:56:43 +00002383void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002384 Context = &Ctx;
2385 assert(Context && "Passed null context!");
2386
2387 assert(PP && "Forgot to set Preprocessor ?");
2388 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2389 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00002390 PP->setExternalSource(this);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002391
Douglas Gregoraa433012010-10-01 01:18:02 +00002392 // If we have an update block for the TU waiting, we have to add it before
2393 // deserializing the decl.
2394 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2395 if (DCU != DeclContextOffsets.end()) {
2396 // Insertion could invalidate map, so grab vector.
2397 DeclContextInfos T;
2398 T.swap(DCU->second);
2399 DeclContextOffsets.erase(DCU);
2400 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2401 }
2402
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002403 // Load the translation unit declaration
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00002404 GetTranslationUnitDecl();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002405
2406 // Load the special types.
2407 Context->setBuiltinVaListType(
Sebastian Redl539c5062010-08-18 23:57:32 +00002408 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2409 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002410 Context->setObjCIdType(GetType(Id));
Sebastian Redl539c5062010-08-18 23:57:32 +00002411 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002412 Context->setObjCSelType(GetType(Sel));
Sebastian Redl539c5062010-08-18 23:57:32 +00002413 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002414 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl539c5062010-08-18 23:57:32 +00002415 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002416 Context->setObjCClassType(GetType(Class));
Steve Naroff7cae42b2009-07-10 23:34:53 +00002417
Sebastian Redl539c5062010-08-18 23:57:32 +00002418 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002419 Context->setCFConstantStringType(GetType(String));
Mike Stump11289f42009-09-09 15:08:12 +00002420 if (unsigned FastEnum
Sebastian Redl539c5062010-08-18 23:57:32 +00002421 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002422 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl539c5062010-08-18 23:57:32 +00002423 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregor27821ce2009-07-07 16:35:42 +00002424 QualType FileType = GetType(File);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002425 if (FileType.isNull()) {
2426 Error("FILE type is NULL");
2427 return;
2428 }
John McCall9dd450b2009-09-21 23:43:11 +00002429 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregor27821ce2009-07-07 16:35:42 +00002430 Context->setFILEDecl(Typedef->getDecl());
2431 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002432 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002433 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002434 Error("Invalid FILE type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002435 return;
2436 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002437 Context->setFILEDecl(Tag->getDecl());
2438 }
2439 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002440 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002441 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002442 if (Jmp_bufType.isNull()) {
2443 Error("jmp_bug type is NULL");
2444 return;
2445 }
John McCall9dd450b2009-09-21 23:43:11 +00002446 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002447 Context->setjmp_bufDecl(Typedef->getDecl());
2448 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002449 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002450 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002451 Error("Invalid jmp_buf type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002452 return;
2453 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002454 Context->setjmp_bufDecl(Tag->getDecl());
2455 }
2456 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002457 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002458 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002459 if (Sigjmp_bufType.isNull()) {
2460 Error("sigjmp_buf type is NULL");
2461 return;
2462 }
John McCall9dd450b2009-09-21 23:43:11 +00002463 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002464 Context->setsigjmp_bufDecl(Typedef->getDecl());
2465 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002466 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002467 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stumpa4de80b2009-07-28 02:25:19 +00002468 Context->setsigjmp_bufDecl(Tag->getDecl());
2469 }
2470 }
Mike Stump11289f42009-09-09 15:08:12 +00002471 if (unsigned ObjCIdRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002472 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002473 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump11289f42009-09-09 15:08:12 +00002474 if (unsigned ObjCClassRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002475 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002476 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002477 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpd0153282009-10-20 02:12:22 +00002478 Context->setBlockDescriptorType(GetType(String));
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002479 if (unsigned String
Sebastian Redl539c5062010-08-18 23:57:32 +00002480 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002481 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002482 if (unsigned ObjCSelRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002483 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002484 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002485 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002486 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002487
Sebastian Redl539c5062010-08-18 23:57:32 +00002488 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002489 Context->setInt128Installed();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002490
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002491 ReadPragmaDiagnosticMappings(Context->getDiagnostics());
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002492}
2493
Douglas Gregor45fe0362009-05-12 01:31:05 +00002494/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002495/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00002496/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002497std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00002498 FileManager &FileMgr,
Daniel Dunbar3b951482009-12-03 09:13:06 +00002499 Diagnostic &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002500 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002501 std::string ErrStr;
2502 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner5159f612010-11-23 08:35:12 +00002503 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00002504 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00002505 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002506 return std::string();
2507 }
2508
2509 // Initialize the stream
2510 llvm::BitstreamReader StreamFile;
2511 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00002512 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00002513 (const unsigned char *)Buffer->getBufferEnd());
2514 Stream.init(StreamFile);
2515
2516 // Sniff for the signature.
2517 if (Stream.Read(8) != 'C' ||
2518 Stream.Read(8) != 'P' ||
2519 Stream.Read(8) != 'C' ||
2520 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002521 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002522 return std::string();
2523 }
2524
2525 RecordData Record;
2526 while (!Stream.AtEndOfStream()) {
2527 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002528
Douglas Gregor45fe0362009-05-12 01:31:05 +00002529 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2530 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00002531
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002532 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002533 switch (BlockID) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002534 case AST_BLOCK_ID:
2535 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002536 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002537 return std::string();
2538 }
2539 break;
Mike Stump11289f42009-09-09 15:08:12 +00002540
Douglas Gregor45fe0362009-05-12 01:31:05 +00002541 default:
2542 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002543 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002544 return std::string();
2545 }
2546 break;
2547 }
2548 continue;
2549 }
2550
2551 if (Code == llvm::bitc::END_BLOCK) {
2552 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002553 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002554 return std::string();
2555 }
2556 continue;
2557 }
2558
2559 if (Code == llvm::bitc::DEFINE_ABBREV) {
2560 Stream.ReadAbbrevRecord();
2561 continue;
2562 }
2563
2564 Record.clear();
2565 const char *BlobStart = 0;
2566 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002567 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl539c5062010-08-18 23:57:32 +00002568 == ORIGINAL_FILE_NAME)
Douglas Gregor45fe0362009-05-12 01:31:05 +00002569 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00002570 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00002571
2572 return std::string();
2573}
2574
Douglas Gregor55abb232009-04-10 20:39:37 +00002575/// \brief Parse the record that corresponds to a LangOptions data
2576/// structure.
2577///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002578/// This routine parses the language options from the AST file and then gives
2579/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00002580///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002581/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002582bool ASTReader::ParseLanguageOptions(
Douglas Gregor55abb232009-04-10 20:39:37 +00002583 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002584 if (Listener) {
2585 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00002586
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002587 #define PARSE_LANGOPT(Option) \
2588 LangOpts.Option = Record[Idx]; \
2589 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00002590
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002591 unsigned Idx = 0;
2592 PARSE_LANGOPT(Trigraphs);
2593 PARSE_LANGOPT(BCPLComment);
2594 PARSE_LANGOPT(DollarIdents);
2595 PARSE_LANGOPT(AsmPreprocessor);
2596 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00002597 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002598 PARSE_LANGOPT(ImplicitInt);
2599 PARSE_LANGOPT(Digraphs);
2600 PARSE_LANGOPT(HexFloats);
2601 PARSE_LANGOPT(C99);
2602 PARSE_LANGOPT(Microsoft);
2603 PARSE_LANGOPT(CPlusPlus);
2604 PARSE_LANGOPT(CPlusPlus0x);
2605 PARSE_LANGOPT(CXXOperatorNames);
2606 PARSE_LANGOPT(ObjC1);
2607 PARSE_LANGOPT(ObjC2);
2608 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00002609 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00002610 PARSE_LANGOPT(AppleKext);
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00002611 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00002612 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002613 PARSE_LANGOPT(PascalStrings);
2614 PARSE_LANGOPT(WritableStrings);
2615 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00002616 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002617 PARSE_LANGOPT(Exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +00002618 PARSE_LANGOPT(SjLjExceptions);
Douglas Gregordbe39272011-02-01 15:15:22 +00002619 PARSE_LANGOPT(MSBitfields);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002620 PARSE_LANGOPT(NeXTRuntime);
2621 PARSE_LANGOPT(Freestanding);
2622 PARSE_LANGOPT(NoBuiltin);
2623 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00002624 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002625 PARSE_LANGOPT(Blocks);
2626 PARSE_LANGOPT(EmitAllDecls);
2627 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00002628 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2629 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002630 PARSE_LANGOPT(HeinousExtensions);
2631 PARSE_LANGOPT(Optimize);
2632 PARSE_LANGOPT(OptimizeSize);
2633 PARSE_LANGOPT(Static);
2634 PARSE_LANGOPT(PICLevel);
2635 PARSE_LANGOPT(GNUInline);
2636 PARSE_LANGOPT(NoInline);
2637 PARSE_LANGOPT(AccessControl);
2638 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00002639 PARSE_LANGOPT(ShortWChar);
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00002640 PARSE_LANGOPT(ShortEnums);
Chris Lattner51924e512010-06-26 21:25:03 +00002641 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall457a04e2010-10-22 21:05:15 +00002642 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00002643 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00002644 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002645 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00002646 PARSE_LANGOPT(OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00002647 PARSE_LANGOPT(CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00002648 PARSE_LANGOPT(CatchUndefined);
2649 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002650 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00002651
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002652 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00002653 }
Douglas Gregor55abb232009-04-10 20:39:37 +00002654
2655 return false;
2656}
2657
Sebastian Redl2c499f62010-08-18 23:56:43 +00002658void ASTReader::ReadPreprocessedEntities() {
Douglas Gregoraae92242010-03-19 21:51:54 +00002659 ReadDefinedMacros();
2660}
2661
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002662PreprocessedEntity *ASTReader::ReadPreprocessedEntity(uint64_t Offset) {
2663 PerFileData *F = 0;
2664 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2665 if (Offset < Chain[I]->SizeInBits) {
2666 F = Chain[I];
2667 break;
2668 }
2669
2670 Offset -= Chain[I]->SizeInBits;
2671 }
2672
2673 if (!F) {
2674 Error("Malformed preprocessed entity offset");
2675 return 0;
2676 }
2677
2678 return ReadMacroRecord(*F, Offset);
2679}
2680
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002681void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002682 unsigned Idx = 0;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002683 while (Idx < PragmaDiagMappings.size()) {
2684 SourceLocation
2685 Loc = SourceLocation::getFromRawEncoding(PragmaDiagMappings[Idx++]);
2686 while (1) {
2687 assert(Idx < PragmaDiagMappings.size() &&
2688 "Invalid data, didn't find '-1' marking end of diag/map pairs");
2689 if (Idx >= PragmaDiagMappings.size())
2690 break; // Something is messed up but at least avoid infinite loop in
2691 // release build.
2692 unsigned DiagID = PragmaDiagMappings[Idx++];
2693 if (DiagID == (unsigned)-1)
2694 break; // no more diag/map pairs for this location.
2695 diag::Mapping Map = (diag::Mapping)PragmaDiagMappings[Idx++];
2696 Diag.setDiagnosticMapping(DiagID, Map, Loc);
2697 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002698 }
2699}
2700
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002701/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002702ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002703 PerFileData *F = 0;
2704 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2705 F = Chain[N - I - 1];
2706 if (Index < F->LocalNumTypes)
2707 break;
2708 Index -= F->LocalNumTypes;
2709 }
2710 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redl2c373b92010-10-05 15:59:54 +00002711 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002712}
2713
2714/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002715///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002716/// The index is the type ID, shifted and minus the number of predefs. This
2717/// routine actually reads the record corresponding to the type at the given
2718/// location. It is a helper routine for GetType, which deals with reading type
2719/// IDs.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002720QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002721 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00002722 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00002723
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002724 // Keep track of where we are in the stream, then jump back there
2725 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002726 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002727
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002728 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00002729
Douglas Gregor1342e842009-07-06 18:54:52 +00002730 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00002731 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00002732
Sebastian Redl2c373b92010-10-05 15:59:54 +00002733 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002734 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002735 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00002736 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2737 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002738 if (Record.size() != 2) {
2739 Error("Incorrect encoding of extended qualifier type");
2740 return QualType();
2741 }
Douglas Gregor455b8f42009-04-15 22:00:08 +00002742 QualType Base = GetType(Record[0]);
John McCall8ccfcb52009-09-24 19:53:00 +00002743 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2744 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00002745 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002746
Sebastian Redl539c5062010-08-18 23:57:32 +00002747 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002748 if (Record.size() != 1) {
2749 Error("Incorrect encoding of complex type");
2750 return QualType();
2751 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002752 QualType ElemType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002753 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002754 }
2755
Sebastian Redl539c5062010-08-18 23:57:32 +00002756 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002757 if (Record.size() != 1) {
2758 Error("Incorrect encoding of pointer type");
2759 return QualType();
2760 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002761 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002762 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002763 }
2764
Sebastian Redl539c5062010-08-18 23:57:32 +00002765 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002766 if (Record.size() != 1) {
2767 Error("Incorrect encoding of block pointer type");
2768 return QualType();
2769 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002770 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002771 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002772 }
2773
Sebastian Redl539c5062010-08-18 23:57:32 +00002774 case TYPE_LVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002775 if (Record.size() != 1) {
2776 Error("Incorrect encoding of lvalue reference type");
2777 return QualType();
2778 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002779 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002780 return Context->getLValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002781 }
2782
Sebastian Redl539c5062010-08-18 23:57:32 +00002783 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002784 if (Record.size() != 1) {
2785 Error("Incorrect encoding of rvalue reference type");
2786 return QualType();
2787 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002788 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002789 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002790 }
2791
Sebastian Redl539c5062010-08-18 23:57:32 +00002792 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00002793 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002794 Error("Incorrect encoding of member pointer type");
2795 return QualType();
2796 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002797 QualType PointeeType = GetType(Record[0]);
2798 QualType ClassType = GetType(Record[1]);
Douglas Gregor0cdc8322010-12-10 17:03:06 +00002799 if (PointeeType.isNull() || ClassType.isNull())
2800 return QualType();
2801
Chris Lattner8575daa2009-04-27 21:45:14 +00002802 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002803 }
2804
Sebastian Redl539c5062010-08-18 23:57:32 +00002805 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002806 QualType ElementType = GetType(Record[0]);
2807 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2808 unsigned IndexTypeQuals = Record[2];
2809 unsigned Idx = 3;
2810 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00002811 return Context->getConstantArrayType(ElementType, Size,
2812 ASM, IndexTypeQuals);
2813 }
2814
Sebastian Redl539c5062010-08-18 23:57:32 +00002815 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002816 QualType ElementType = GetType(Record[0]);
2817 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2818 unsigned IndexTypeQuals = Record[2];
Chris Lattner8575daa2009-04-27 21:45:14 +00002819 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002820 }
2821
Sebastian Redl539c5062010-08-18 23:57:32 +00002822 case TYPE_VARIABLE_ARRAY: {
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002823 QualType ElementType = GetType(Record[0]);
2824 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2825 unsigned IndexTypeQuals = Record[2];
Sebastian Redl2c373b92010-10-05 15:59:54 +00002826 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
2827 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
2828 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor04318252009-07-06 15:59:29 +00002829 ASM, IndexTypeQuals,
2830 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002831 }
2832
Sebastian Redl539c5062010-08-18 23:57:32 +00002833 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002834 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002835 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002836 return QualType();
2837 }
2838
2839 QualType ElementType = GetType(Record[0]);
2840 unsigned NumElements = Record[1];
Bob Wilsonaeb56442010-11-10 21:56:12 +00002841 unsigned VecKind = Record[2];
Chris Lattner37141f42010-06-23 06:00:24 +00002842 return Context->getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00002843 (VectorType::VectorKind)VecKind);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002844 }
2845
Sebastian Redl539c5062010-08-18 23:57:32 +00002846 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002847 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002848 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002849 return QualType();
2850 }
2851
2852 QualType ElementType = GetType(Record[0]);
2853 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00002854 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002855 }
2856
Sebastian Redl539c5062010-08-18 23:57:32 +00002857 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002858 if (Record.size() != 4) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002859 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002860 return QualType();
2861 }
2862 QualType ResultType = GetType(Record[0]);
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002863 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00002864 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002865 }
2866
Sebastian Redl539c5062010-08-18 23:57:32 +00002867 case TYPE_FUNCTION_PROTO: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002868 QualType ResultType = GetType(Record[0]);
John McCalldb40c7f2010-12-14 08:05:40 +00002869
2870 FunctionProtoType::ExtProtoInfo EPI;
2871 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
2872 /*regparm*/ Record[2],
2873 static_cast<CallingConv>(Record[3]));
2874
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002875 unsigned Idx = 4;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002876 unsigned NumParams = Record[Idx++];
2877 llvm::SmallVector<QualType, 16> ParamTypes;
2878 for (unsigned I = 0; I != NumParams; ++I)
2879 ParamTypes.push_back(GetType(Record[Idx++]));
John McCalldb40c7f2010-12-14 08:05:40 +00002880
2881 EPI.Variadic = Record[Idx++];
2882 EPI.TypeQuals = Record[Idx++];
Douglas Gregordb9d6642011-01-26 05:01:58 +00002883 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
John McCalldb40c7f2010-12-14 08:05:40 +00002884 EPI.HasExceptionSpec = Record[Idx++];
2885 EPI.HasAnyExceptionSpec = Record[Idx++];
2886 EPI.NumExceptions = Record[Idx++];
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002887 llvm::SmallVector<QualType, 2> Exceptions;
John McCalldb40c7f2010-12-14 08:05:40 +00002888 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002889 Exceptions.push_back(GetType(Record[Idx++]));
John McCalldb40c7f2010-12-14 08:05:40 +00002890 EPI.Exceptions = Exceptions.data();
Jay Foad7d0479f2009-05-21 09:52:38 +00002891 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalldb40c7f2010-12-14 08:05:40 +00002892 EPI);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002893 }
2894
Sebastian Redl539c5062010-08-18 23:57:32 +00002895 case TYPE_UNRESOLVED_USING:
John McCallb96ec562009-12-04 22:46:56 +00002896 return Context->getTypeDeclType(
2897 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2898
Sebastian Redl539c5062010-08-18 23:57:32 +00002899 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002900 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002901 Error("incorrect encoding of typedef type");
2902 return QualType();
2903 }
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002904 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2905 QualType Canonical = GetType(Record[1]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00002906 if (!Canonical.isNull())
2907 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002908 return Context->getTypedefType(Decl, Canonical);
2909 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002910
Sebastian Redl539c5062010-08-18 23:57:32 +00002911 case TYPE_TYPEOF_EXPR:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002912 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002913
Sebastian Redl539c5062010-08-18 23:57:32 +00002914 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002915 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002916 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002917 return QualType();
2918 }
2919 QualType UnderlyingType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002920 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002921 }
Mike Stump11289f42009-09-09 15:08:12 +00002922
Sebastian Redl539c5062010-08-18 23:57:32 +00002923 case TYPE_DECLTYPE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002924 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson81df7b82009-06-24 19:06:50 +00002925
Sebastian Redl539c5062010-08-18 23:57:32 +00002926 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002927 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002928 Error("incorrect encoding of record type");
2929 return QualType();
2930 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002931 bool IsDependent = Record[0];
2932 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCall424cec92011-01-19 06:33:43 +00002933 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002934 return T;
2935 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002936
Sebastian Redl539c5062010-08-18 23:57:32 +00002937 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002938 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002939 Error("incorrect encoding of enum type");
2940 return QualType();
2941 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002942 bool IsDependent = Record[0];
2943 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCall424cec92011-01-19 06:33:43 +00002944 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002945 return T;
2946 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00002947
John McCall81904512011-01-06 01:58:22 +00002948 case TYPE_ATTRIBUTED: {
2949 if (Record.size() != 3) {
2950 Error("incorrect encoding of attributed type");
2951 return QualType();
2952 }
2953 QualType modifiedType = GetType(Record[0]);
2954 QualType equivalentType = GetType(Record[1]);
2955 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
2956 return Context->getAttributedType(kind, modifiedType, equivalentType);
2957 }
2958
Abramo Bagnara924a8f32010-12-10 16:29:40 +00002959 case TYPE_PAREN: {
2960 if (Record.size() != 1) {
2961 Error("incorrect encoding of paren type");
2962 return QualType();
2963 }
2964 QualType InnerType = GetType(Record[0]);
2965 return Context->getParenType(InnerType);
2966 }
2967
Douglas Gregord2fa7662010-12-20 02:24:11 +00002968 case TYPE_PACK_EXPANSION: {
Douglas Gregor17328502011-02-01 15:24:58 +00002969 if (Record.size() != 2) {
Douglas Gregord2fa7662010-12-20 02:24:11 +00002970 Error("incorrect encoding of pack expansion type");
2971 return QualType();
2972 }
2973 QualType Pattern = GetType(Record[0]);
2974 if (Pattern.isNull())
2975 return QualType();
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00002976 llvm::Optional<unsigned> NumExpansions;
2977 if (Record[1])
2978 NumExpansions = Record[1] - 1;
2979 return Context->getPackExpansionType(Pattern, NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00002980 }
2981
Sebastian Redl539c5062010-08-18 23:57:32 +00002982 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002983 unsigned Idx = 0;
2984 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2985 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2986 QualType NamedType = GetType(Record[Idx++]);
2987 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00002988 }
2989
Sebastian Redl539c5062010-08-18 23:57:32 +00002990 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00002991 unsigned Idx = 0;
2992 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00002993 return Context->getObjCInterfaceType(ItfD);
2994 }
2995
Sebastian Redl539c5062010-08-18 23:57:32 +00002996 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00002997 unsigned Idx = 0;
2998 QualType Base = GetType(Record[Idx++]);
Chris Lattner587cbe12009-04-22 06:45:28 +00002999 unsigned NumProtos = Record[Idx++];
3000 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
3001 for (unsigned I = 0; I != NumProtos; ++I)
3002 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003003 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00003004 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003005
Sebastian Redl539c5062010-08-18 23:57:32 +00003006 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00003007 unsigned Idx = 0;
John McCall8b07ec22010-05-15 11:32:37 +00003008 QualType Pointee = GetType(Record[Idx++]);
3009 return Context->getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00003010 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00003011
Sebastian Redl539c5062010-08-18 23:57:32 +00003012 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00003013 unsigned Idx = 0;
3014 QualType Parm = GetType(Record[Idx++]);
3015 QualType Replacement = GetType(Record[Idx++]);
3016 return
3017 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3018 Replacement);
3019 }
John McCalle78aac42010-03-10 03:28:59 +00003020
Douglas Gregorada4b792011-01-14 02:55:32 +00003021 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3022 unsigned Idx = 0;
3023 QualType Parm = GetType(Record[Idx++]);
3024 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3025 return Context->getSubstTemplateTypeParmPackType(
3026 cast<TemplateTypeParmType>(Parm),
3027 ArgPack);
3028 }
3029
Sebastian Redl539c5062010-08-18 23:57:32 +00003030 case TYPE_INJECTED_CLASS_NAME: {
John McCalle78aac42010-03-10 03:28:59 +00003031 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
3032 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003033 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003034 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003035 return
3036 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00003037 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003038
Sebastian Redl539c5062010-08-18 23:57:32 +00003039 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003040 unsigned Idx = 0;
3041 unsigned Depth = Record[Idx++];
3042 unsigned Index = Record[Idx++];
3043 bool Pack = Record[Idx++];
3044 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
3045 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
3046 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003047
Sebastian Redl539c5062010-08-18 23:57:32 +00003048 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003049 unsigned Idx = 0;
3050 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3051 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3052 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00003053 QualType Canon = GetType(Record[Idx++]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003054 if (!Canon.isNull())
3055 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00003056 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003057 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003058
Sebastian Redl539c5062010-08-18 23:57:32 +00003059 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003060 unsigned Idx = 0;
3061 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3062 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3063 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3064 unsigned NumArgs = Record[Idx++];
3065 llvm::SmallVector<TemplateArgument, 8> Args;
3066 Args.reserve(NumArgs);
3067 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003068 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003069 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3070 Args.size(), Args.data());
3071 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003072
Sebastian Redl539c5062010-08-18 23:57:32 +00003073 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003074 unsigned Idx = 0;
3075
3076 // ArrayType
3077 QualType ElementType = GetType(Record[Idx++]);
3078 ArrayType::ArraySizeModifier ASM
3079 = (ArrayType::ArraySizeModifier)Record[Idx++];
3080 unsigned IndexTypeQuals = Record[Idx++];
3081
3082 // DependentSizedArrayType
Sebastian Redl2c373b92010-10-05 15:59:54 +00003083 Expr *NumElts = ReadExpr(*Loc.F);
3084 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003085
3086 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3087 IndexTypeQuals, Brackets);
3088 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003089
Sebastian Redl539c5062010-08-18 23:57:32 +00003090 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003091 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003092 bool IsDependent = Record[Idx++];
Douglas Gregor5590be02011-01-15 06:45:20 +00003093 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003094 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003095 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003096 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003097 QualType T;
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003098 if (Canon.isNull())
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003099 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3100 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003101 else
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003102 T = Context->getTemplateSpecializationType(Name, Args.data(),
3103 Args.size(), Canon);
John McCall424cec92011-01-19 06:33:43 +00003104 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003105 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003106 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003107 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003108 // Suppress a GCC warning
3109 return QualType();
3110}
3111
Sebastian Redl2c373b92010-10-05 15:59:54 +00003112class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00003113 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003114 ASTReader::PerFileData &F;
Sebastian Redlc67764e2010-07-22 22:43:28 +00003115 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redl2c499f62010-08-18 23:56:43 +00003116 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00003117 unsigned &Idx;
3118
Sebastian Redl2c373b92010-10-05 15:59:54 +00003119 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3120 unsigned &I) {
3121 return Reader.ReadSourceLocation(F, R, I);
3122 }
3123
John McCall8f115c62009-10-16 21:56:05 +00003124public:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003125 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl2c499f62010-08-18 23:56:43 +00003126 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003127 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3128 { }
John McCall8f115c62009-10-16 21:56:05 +00003129
John McCall17001972009-10-18 01:05:36 +00003130 // We want compile-time assurance that we've enumerated all of
3131 // these, so unfortunately we have to declare them first, then
3132 // define them out-of-line.
3133#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00003134#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00003135 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00003136#include "clang/AST/TypeLocNodes.def"
3137
John McCall17001972009-10-18 01:05:36 +00003138 void VisitFunctionTypeLoc(FunctionTypeLoc);
3139 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00003140};
3141
John McCall17001972009-10-18 01:05:36 +00003142void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00003143 // nothing to do
3144}
John McCall17001972009-10-18 01:05:36 +00003145void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003146 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorc9b7a592010-01-18 18:04:31 +00003147 if (TL.needsExtraLocalData()) {
3148 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3149 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3150 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3151 TL.setModeAttr(Record[Idx++]);
3152 }
John McCall8f115c62009-10-16 21:56:05 +00003153}
John McCall17001972009-10-18 01:05:36 +00003154void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003155 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003156}
John McCall17001972009-10-18 01:05:36 +00003157void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003158 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003159}
John McCall17001972009-10-18 01:05:36 +00003160void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003161 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003162}
John McCall17001972009-10-18 01:05:36 +00003163void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003164 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003165}
John McCall17001972009-10-18 01:05:36 +00003166void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003167 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003168}
John McCall17001972009-10-18 01:05:36 +00003169void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003170 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003171}
John McCall17001972009-10-18 01:05:36 +00003172void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003173 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3174 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003175 if (Record[Idx++])
Sebastian Redl2c373b92010-10-05 15:59:54 +00003176 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003177 else
John McCall17001972009-10-18 01:05:36 +00003178 TL.setSizeExpr(0);
3179}
3180void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3181 VisitArrayTypeLoc(TL);
3182}
3183void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3184 VisitArrayTypeLoc(TL);
3185}
3186void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3187 VisitArrayTypeLoc(TL);
3188}
3189void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3190 DependentSizedArrayTypeLoc TL) {
3191 VisitArrayTypeLoc(TL);
3192}
3193void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3194 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003195 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003196}
3197void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003198 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003199}
3200void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003201 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003202}
3203void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003204 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3205 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor7fb25412010-10-01 18:44:50 +00003206 TL.setTrailingReturn(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00003207 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCalle6347002009-10-23 01:28:53 +00003208 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall17001972009-10-18 01:05:36 +00003209 }
3210}
3211void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3212 VisitFunctionTypeLoc(TL);
3213}
3214void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3215 VisitFunctionTypeLoc(TL);
3216}
John McCallb96ec562009-12-04 22:46:56 +00003217void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003218 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallb96ec562009-12-04 22:46:56 +00003219}
John McCall17001972009-10-18 01:05:36 +00003220void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003221 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003222}
3223void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003224 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3225 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3226 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003227}
3228void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003229 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3230 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3231 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3232 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003233}
3234void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003235 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003236}
3237void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003238 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003239}
3240void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003241 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003242}
John McCall81904512011-01-06 01:58:22 +00003243void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3244 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3245 if (TL.hasAttrOperand()) {
3246 SourceRange range;
3247 range.setBegin(ReadSourceLocation(Record, Idx));
3248 range.setEnd(ReadSourceLocation(Record, Idx));
3249 TL.setAttrOperandParensRange(range);
3250 }
3251 if (TL.hasAttrExprOperand()) {
3252 if (Record[Idx++])
3253 TL.setAttrExprOperand(Reader.ReadExpr(F));
3254 else
3255 TL.setAttrExprOperand(0);
3256 } else if (TL.hasAttrEnumOperand())
3257 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3258}
John McCall17001972009-10-18 01:05:36 +00003259void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003260 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003261}
John McCallcebee162009-10-18 09:09:24 +00003262void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3263 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003264 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallcebee162009-10-18 09:09:24 +00003265}
Douglas Gregorada4b792011-01-14 02:55:32 +00003266void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3267 SubstTemplateTypeParmPackTypeLoc TL) {
3268 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3269}
John McCall17001972009-10-18 01:05:36 +00003270void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3271 TemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003272 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3273 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3274 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall0ad16662009-10-29 08:12:44 +00003275 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3276 TL.setArgLocInfo(i,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003277 Reader.GetTemplateArgumentLocInfo(F,
3278 TL.getTypePtr()->getArg(i).getKind(),
3279 Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003280}
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003281void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3282 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3283 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3284}
Abramo Bagnara6150c882010-05-11 21:36:43 +00003285void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003286 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3287 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003288}
John McCalle78aac42010-03-10 03:28:59 +00003289void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003290 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalle78aac42010-03-10 03:28:59 +00003291}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00003292void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003293 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3294 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3295 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003296}
John McCallc392f372010-06-11 00:33:02 +00003297void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3298 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003299 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3300 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3301 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3302 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3303 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003304 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3305 TL.setArgLocInfo(I,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003306 Reader.GetTemplateArgumentLocInfo(F,
3307 TL.getTypePtr()->getArg(I).getKind(),
3308 Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003309}
Douglas Gregord2fa7662010-12-20 02:24:11 +00003310void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3311 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3312}
John McCall17001972009-10-18 01:05:36 +00003313void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003314 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8b07ec22010-05-15 11:32:37 +00003315}
3316void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3317 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003318 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3319 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003320 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003321 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003322}
John McCallfc93cf92009-10-22 22:37:11 +00003323void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003324 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCallfc93cf92009-10-22 22:37:11 +00003325}
John McCall8f115c62009-10-16 21:56:05 +00003326
Sebastian Redl2c373b92010-10-05 15:59:54 +00003327TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003328 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00003329 unsigned &Idx) {
3330 QualType InfoTy = GetType(Record[Idx++]);
3331 if (InfoTy.isNull())
3332 return 0;
3333
John McCallbcd03502009-12-07 02:54:59 +00003334 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003335 TypeLocReader TLR(*this, F, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00003336 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00003337 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00003338 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00003339}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003340
Sebastian Redl539c5062010-08-18 23:57:32 +00003341QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00003342 unsigned FastQuals = ID & Qualifiers::FastMask;
3343 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003344
Sebastian Redl539c5062010-08-18 23:57:32 +00003345 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003346 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00003347 switch ((PredefinedTypeIDs)Index) {
3348 case PREDEF_TYPE_NULL_ID: return QualType();
3349 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3350 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003351
Sebastian Redl539c5062010-08-18 23:57:32 +00003352 case PREDEF_TYPE_CHAR_U_ID:
3353 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003354 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00003355 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003356 break;
3357
Sebastian Redl539c5062010-08-18 23:57:32 +00003358 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3359 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3360 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3361 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3362 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3363 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3364 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3365 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3366 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3367 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3368 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3369 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3370 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3371 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3372 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3373 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3374 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3375 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3376 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3377 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3378 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3379 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3380 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3381 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003382 }
3383
3384 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00003385 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003386 }
3387
Sebastian Redl539c5062010-08-18 23:57:32 +00003388 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003389 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00003390 if (TypesLoaded[Index].isNull()) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003391 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003392 if (TypesLoaded[Index].isNull())
3393 return QualType();
3394
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003395 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003396 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003397 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003398 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003399 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00003400 }
Mike Stump11289f42009-09-09 15:08:12 +00003401
John McCall8ccfcb52009-09-24 19:53:00 +00003402 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003403}
3404
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003405TypeID ASTReader::GetTypeID(QualType T) const {
3406 return MakeTypeID(T,
3407 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3408}
3409
3410TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3411 if (T.isNull())
3412 return TypeIdx();
3413 assert(!T.getLocalFastQualifiers());
3414
3415 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3416 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3417 // comparing keys of ASTDeclContextNameLookupTable.
3418 // If the type didn't come from the AST file use a specially marked index
3419 // so that any hash/key comparison fail since no such index is stored
3420 // in a AST file.
3421 if (I == TypeIdxs.end())
3422 return TypeIdx(-1);
3423 return I->second;
3424}
3425
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003426unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3427 unsigned Result = 0;
3428 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3429 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3430
3431 return Result;
3432}
3433
John McCall0ad16662009-10-29 08:12:44 +00003434TemplateArgumentLocInfo
Sebastian Redl2c373b92010-10-05 15:59:54 +00003435ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3436 TemplateArgument::ArgKind Kind,
John McCall0ad16662009-10-29 08:12:44 +00003437 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003438 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00003439 switch (Kind) {
3440 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003441 return ReadExpr(F);
John McCall0ad16662009-10-29 08:12:44 +00003442 case TemplateArgument::Type:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003443 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003444 case TemplateArgument::Template: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003445 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3446 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003447 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc,
3448 SourceLocation());
3449 }
3450 case TemplateArgument::TemplateExpansion: {
3451 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3452 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003453 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
3454 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc,
3455 EllipsisLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003456 }
John McCall0ad16662009-10-29 08:12:44 +00003457 case TemplateArgument::Null:
3458 case TemplateArgument::Integral:
3459 case TemplateArgument::Declaration:
3460 case TemplateArgument::Pack:
3461 return TemplateArgumentLocInfo();
3462 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00003463 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00003464 return TemplateArgumentLocInfo();
3465}
3466
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003467TemplateArgumentLoc
Sebastian Redl2c373b92010-10-05 15:59:54 +00003468ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003469 const RecordData &Record, unsigned &Index) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003470 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003471
3472 if (Arg.getKind() == TemplateArgument::Expression) {
3473 if (Record[Index++]) // bool InfoHasSameExpr.
3474 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3475 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00003476 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003477 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003478}
3479
Sebastian Redl2c499f62010-08-18 23:56:43 +00003480Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00003481 return GetDecl(ID);
3482}
3483
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003484uint64_t
3485ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3486 if (ID == 0)
3487 return 0;
3488
3489 --ID;
3490 uint64_t Offset = 0;
3491 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3492 if (ID < Chain[I]->LocalNumCXXBaseSpecifiers)
3493 return Offset + Chain[I]->CXXBaseSpecifiersOffsets[ID];
3494
3495 ID -= Chain[I]->LocalNumCXXBaseSpecifiers;
3496 Offset += Chain[I]->SizeInBits;
3497 }
3498
3499 assert(false && "CXXBaseSpecifiers not found");
3500 return 0;
3501}
3502
3503CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3504 // Figure out which AST file contains this offset.
3505 PerFileData *F = 0;
3506 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3507 if (Offset < Chain[I]->SizeInBits) {
3508 F = Chain[I];
3509 break;
3510 }
3511
3512 Offset -= Chain[I]->SizeInBits;
3513 }
3514
3515 if (!F) {
3516 Error("Malformed AST file: C++ base specifiers at impossible offset");
3517 return 0;
3518 }
3519
3520 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3521 SavedStreamPosition SavedPosition(Cursor);
3522 Cursor.JumpToBit(Offset);
3523 ReadingKindTracker ReadingKind(Read_Decl, *this);
3524 RecordData Record;
3525 unsigned Code = Cursor.ReadCode();
3526 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3527 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3528 Error("Malformed AST file: missing C++ base specifiers");
3529 return 0;
3530 }
3531
3532 unsigned Idx = 0;
3533 unsigned NumBases = Record[Idx++];
3534 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3535 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3536 for (unsigned I = 0; I != NumBases; ++I)
3537 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3538 return Bases;
3539}
3540
Sebastian Redl2c499f62010-08-18 23:56:43 +00003541TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003542 if (!DeclsLoaded[0]) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00003543 ReadDeclRecord(0, 1);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003544 if (DeserializationListener)
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003545 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003546 }
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00003547
3548 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3549}
3550
Sebastian Redl539c5062010-08-18 23:57:32 +00003551Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003552 if (ID == 0)
3553 return 0;
3554
Douglas Gregor745ed142009-04-25 18:35:21 +00003555 if (ID > DeclsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003556 Error("declaration ID out-of-range for AST file");
Douglas Gregor745ed142009-04-25 18:35:21 +00003557 return 0;
3558 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003559
Douglas Gregor745ed142009-04-25 18:35:21 +00003560 unsigned Index = ID - 1;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003561 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003562 ReadDeclRecord(Index, ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003563 if (DeserializationListener)
3564 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3565 }
Douglas Gregor745ed142009-04-25 18:35:21 +00003566
3567 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003568}
3569
Chris Lattner9c28af02009-04-27 05:46:25 +00003570/// \brief Resolve the offset of a statement into a statement.
3571///
3572/// This operation will read a new statement from the external
3573/// source each time it is called, and is meant to be used via a
3574/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00003575Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00003576 // Switch case IDs are per Decl.
3577 ClearSwitchCaseIDs();
3578
Sebastian Redl5c415f32010-07-22 17:01:13 +00003579 // Offset here is a global offset across the entire chain.
3580 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3581 PerFileData &F = *Chain[N - I - 1];
3582 if (Offset < F.SizeInBits) {
3583 // Since we know that this statement is part of a decl, make sure to use
3584 // the decl cursor to read it.
3585 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003586 return ReadStmtFromStream(F);
Sebastian Redl5c415f32010-07-22 17:01:13 +00003587 }
3588 Offset -= F.SizeInBits;
3589 }
3590 llvm_unreachable("Broken chain");
Douglas Gregor3c3aa612009-04-18 00:07:54 +00003591}
3592
Sebastian Redl2c499f62010-08-18 23:56:43 +00003593bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003594 bool (*isKindWeWant)(Decl::Kind),
John McCall75b960e2010-06-01 09:23:16 +00003595 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump11289f42009-09-09 15:08:12 +00003596 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003597 "DeclContext has no lexical decls in storage");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003598
Sebastian Redl5c415f32010-07-22 17:01:13 +00003599 // There might be lexical decls in multiple parts of the chain, for the TU
3600 // at least.
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003601 // DeclContextOffsets might reallocate as we load additional decls below,
3602 // so make a copy of the vector.
3603 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl5c415f32010-07-22 17:01:13 +00003604 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3605 I != E; ++I) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003606 // IDs can be 0 if this context doesn't contain declarations.
3607 if (!I->LexicalDecls)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003608 continue;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003609
3610 // Load all of the declaration IDs
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003611 for (const KindDeclIDPair *ID = I->LexicalDecls,
3612 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3613 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3614 continue;
3615
3616 Decl *D = GetDecl(ID->second);
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003617 assert(D && "Null decl in lexical decls");
3618 Decls.push_back(D);
3619 }
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003620 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003621
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003622 ++NumLexicalDeclContextsRead;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003623 return false;
3624}
3625
John McCall75b960e2010-06-01 09:23:16 +00003626DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00003627ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00003628 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003629 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003630 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003631 if (!Name)
3632 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3633 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003634
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003635 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl471ac2f2010-08-24 00:49:55 +00003636 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003637 // and namespaces. For any given name, the last available results replace
3638 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl5c415f32010-07-22 17:01:13 +00003639 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003640 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl5c415f32010-07-22 17:01:13 +00003641 I != E; ++I) {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003642 if (!I->NameLookupTableData)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003643 continue;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003644
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003645 ASTDeclContextNameLookupTable *LookupTable =
3646 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3647 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3648 if (Pos == LookupTable->end())
Sebastian Redl5c415f32010-07-22 17:01:13 +00003649 continue;
3650
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003651 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3652 for (; Data.first != Data.second; ++Data.first)
3653 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003654 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003655 }
3656
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003657 ++NumVisibleDeclContextsRead;
John McCall75b960e2010-06-01 09:23:16 +00003658
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003659 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00003660 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003661}
3662
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00003663void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3664 assert(DC->hasExternalVisibleStorage() &&
3665 "DeclContext has no visible decls in storage");
3666
3667 llvm::SmallVector<NamedDecl *, 64> Decls;
3668 // There might be visible decls in multiple parts of the chain, for the TU
3669 // and namespaces.
3670 DeclContextInfos &Infos = DeclContextOffsets[DC];
3671 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3672 I != E; ++I) {
3673 if (!I->NameLookupTableData)
3674 continue;
3675
3676 ASTDeclContextNameLookupTable *LookupTable =
3677 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3678 for (ASTDeclContextNameLookupTable::item_iterator
3679 ItemI = LookupTable->item_begin(),
3680 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3681 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3682 = *ItemI;
3683 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3684 Decls.clear();
3685 for (; Data.first != Data.second; ++Data.first)
3686 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3687 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3688 }
3689 }
3690}
3691
Sebastian Redl2c499f62010-08-18 23:56:43 +00003692void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003693 assert(Consumer);
3694 while (!InterestingDecls.empty()) {
3695 DeclGroupRef DG(InterestingDecls.front());
3696 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00003697 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003698 }
3699}
3700
Sebastian Redl2c499f62010-08-18 23:56:43 +00003701void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00003702 this->Consumer = Consumer;
3703
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003704 if (!Consumer)
3705 return;
3706
3707 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003708 // Force deserialization of this decl, which will cause it to be queued for
3709 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00003710 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003711 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00003712
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003713 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003714}
3715
Sebastian Redl2c499f62010-08-18 23:56:43 +00003716void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003717 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003718
Mike Stump11289f42009-09-09 15:08:12 +00003719 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003720 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00003721 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00003722 unsigned NumDeclsLoaded
3723 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3724 (Decl *)0);
3725 unsigned NumIdentifiersLoaded
3726 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3727 IdentifiersLoaded.end(),
3728 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00003729 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003730 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3731 SelectorsLoaded.end(),
3732 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00003733
Douglas Gregorc5046832009-04-27 18:38:38 +00003734 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3735 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor258ae542009-04-27 06:38:32 +00003736 if (TotalNumSLocEntries)
3737 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3738 NumSLocEntriesRead, TotalNumSLocEntries,
3739 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00003740 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003741 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003742 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3743 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3744 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003745 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003746 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3747 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00003748 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003749 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00003750 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3751 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00003752 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003753 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00003754 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3755 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003756 if (TotalNumStatements)
3757 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3758 NumStatementsRead, TotalNumStatements,
3759 ((float)NumStatementsRead/TotalNumStatements * 100));
3760 if (TotalNumMacros)
3761 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3762 NumMacrosRead, TotalNumMacros,
3763 ((float)NumMacrosRead/TotalNumMacros * 100));
3764 if (TotalLexicalDeclContexts)
3765 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3766 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3767 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3768 * 100));
3769 if (TotalVisibleDeclContexts)
3770 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3771 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3772 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3773 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003774 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003775 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003776 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3777 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00003778 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003779 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003780 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003781 std::fprintf(stderr, "\n");
3782}
3783
Sebastian Redl2c499f62010-08-18 23:56:43 +00003784void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00003785 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003786 S.ExternalSource = this;
3787
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003788 // Makes sure any declarations that were deserialized "too early"
3789 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003790 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3791 if (SemaObj->TUScope)
John McCall48871652010-08-21 09:40:31 +00003792 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003793
3794 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003795 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003796 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00003797
3798 // If there were any tentative definitions, deserialize them and add
Sebastian Redl35351a92010-01-31 22:27:38 +00003799 // them to Sema's list of tentative definitions.
Douglas Gregord4df8652009-04-22 22:02:47 +00003800 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3801 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redl35351a92010-01-31 22:27:38 +00003802 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregord4df8652009-04-22 22:02:47 +00003803 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00003804
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003805 // If there were any unused file scoped decls, deserialize them and add to
3806 // Sema's list of unused file scoped decls.
3807 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3808 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3809 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattner90073802010-02-12 00:07:30 +00003810 }
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003811
3812 // If there were any locally-scoped external declarations,
3813 // deserialize them and add them to Sema's table of locally-scoped
3814 // external declarations.
3815 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3816 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3817 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3818 }
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003819
3820 // If there were any ext_vector type declarations, deserialize them
3821 // and add them to Sema's vector of such declarations.
3822 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3823 SemaObj->ExtVectorDecls.push_back(
3824 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003825
3826 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3827 // Can we cut them down before writing them ?
3828
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003829 // If there were any dynamic classes declarations, deserialize them
3830 // and add them to Sema's vector of such declarations.
3831 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3832 SemaObj->DynamicClasses.push_back(
3833 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003834
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003835 // Load the offsets of the declarations that Sema references.
3836 // They will be lazily deserialized when needed.
3837 if (!SemaDeclRefs.empty()) {
3838 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3839 SemaObj->StdNamespace = SemaDeclRefs[0];
3840 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3841 }
3842
Sebastian Redl2c373b92010-10-05 15:59:54 +00003843 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
3844
3845 // If there are @selector references added them to its pool. This is for
3846 // implementation of -Wselector.
3847 if (!F->ReferencedSelectorsData.empty()) {
3848 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
3849 unsigned I = 0;
3850 while (I < DataSize) {
3851 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
3852 SourceLocation SelLoc = ReadSourceLocation(
3853 *F, F->ReferencedSelectorsData, I);
3854 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3855 }
3856 }
3857
3858 // If there were any pending implicit instantiations, deserialize them
3859 // and add them to Sema's queue of such instantiations.
3860 assert(F->PendingInstantiations.size() % 2 == 0 &&
3861 "Expected pairs of entries");
3862 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
3863 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
3864 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
3865 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
3866 }
3867 }
3868
3869 // The two special data sets below always come from the most recent PCH,
3870 // which is at the front of the chain.
3871 PerFileData &F = *Chain.front();
3872
3873 // If there were any weak undeclared identifiers, deserialize them and add to
3874 // Sema's list of weak undeclared identifiers.
3875 if (!WeakUndeclaredIdentifiers.empty()) {
3876 unsigned Idx = 0;
3877 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3878 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3879 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3880 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
3881 bool Used = WeakUndeclaredIdentifiers[Idx++];
3882 Sema::WeakInfo WI(AliasId, Loc);
3883 WI.setUsed(Used);
3884 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3885 }
3886 }
3887
3888 // If there were any VTable uses, deserialize the information and add it
3889 // to Sema's vector and map of VTable uses.
3890 if (!VTableUses.empty()) {
3891 unsigned Idx = 0;
3892 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3893 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3894 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
3895 bool DefinitionRequired = VTableUses[Idx++];
3896 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3897 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003898 }
3899 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00003900}
3901
Sebastian Redl2c499f62010-08-18 23:56:43 +00003902IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redl78f51772010-08-02 18:30:12 +00003903 // Try to find this name within our on-disk hash tables. We start with the
3904 // most recent one, since that one contains the most up-to-date info.
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003905 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003906 ASTIdentifierLookupTable *IdTable
3907 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003908 if (!IdTable)
3909 continue;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003910 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003911 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003912 if (Pos == IdTable->end())
3913 continue;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003914
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003915 // Dereferencing the iterator has the effect of building the
3916 // IdentifierInfo node and populating it with the various
3917 // declarations it needs.
Sebastian Redl78f51772010-08-02 18:30:12 +00003918 return *Pos;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003919 }
Sebastian Redl78f51772010-08-02 18:30:12 +00003920 return 0;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003921}
3922
Douglas Gregor57756ea2010-10-14 22:11:03 +00003923namespace clang {
3924 /// \brief An identifier-lookup iterator that enumerates all of the
3925 /// identifiers stored within a set of AST files.
3926 class ASTIdentifierIterator : public IdentifierIterator {
3927 /// \brief The AST reader whose identifiers are being enumerated.
3928 const ASTReader &Reader;
3929
3930 /// \brief The current index into the chain of AST files stored in
3931 /// the AST reader.
3932 unsigned Index;
3933
3934 /// \brief The current position within the identifier lookup table
3935 /// of the current AST file.
3936 ASTIdentifierLookupTable::key_iterator Current;
3937
3938 /// \brief The end position within the identifier lookup table of
3939 /// the current AST file.
3940 ASTIdentifierLookupTable::key_iterator End;
3941
3942 public:
3943 explicit ASTIdentifierIterator(const ASTReader &Reader);
3944
3945 virtual llvm::StringRef Next();
3946 };
3947}
3948
3949ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
3950 : Reader(Reader), Index(Reader.Chain.size() - 1) {
3951 ASTIdentifierLookupTable *IdTable
3952 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3953 Current = IdTable->key_begin();
3954 End = IdTable->key_end();
3955}
3956
3957llvm::StringRef ASTIdentifierIterator::Next() {
3958 while (Current == End) {
3959 // If we have exhausted all of our AST files, we're done.
3960 if (Index == 0)
3961 return llvm::StringRef();
3962
3963 --Index;
3964 ASTIdentifierLookupTable *IdTable
3965 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3966 Current = IdTable->key_begin();
3967 End = IdTable->key_end();
3968 }
3969
3970 // We have any identifiers remaining in the current AST file; return
3971 // the next one.
3972 std::pair<const char*, unsigned> Key = *Current;
3973 ++Current;
3974 return llvm::StringRef(Key.first, Key.second);
3975}
3976
3977IdentifierIterator *ASTReader::getIdentifiers() const {
3978 return new ASTIdentifierIterator(*this);
3979}
3980
Mike Stump11289f42009-09-09 15:08:12 +00003981std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redl2c499f62010-08-18 23:56:43 +00003982ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redlada023c2010-08-04 20:40:17 +00003983 // Find this selector in a hash table. We want to find the most recent entry.
3984 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3985 PerFileData &F = *Chain[I];
3986 if (!F.SelectorLookupTable)
3987 continue;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003988
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003989 ASTSelectorLookupTable *PoolTable
3990 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3991 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redlada023c2010-08-04 20:40:17 +00003992 if (Pos != PoolTable->end()) {
3993 ++NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003994 // FIXME: Not quite happy with the statistics here. We probably should
3995 // disable this tracking when called via LoadSelector.
3996 // Also, should entries without methods count as misses?
3997 ++NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003998 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redlada023c2010-08-04 20:40:17 +00003999 if (DeserializationListener)
4000 DeserializationListener->SelectorRead(Data.ID, Sel);
4001 return std::make_pair(Data.Instance, Data.Factory);
4002 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004003 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00004004
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004005 ++NumMethodPoolMisses;
Sebastian Redlada023c2010-08-04 20:40:17 +00004006 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorc78d3462009-04-24 21:10:55 +00004007}
4008
Sebastian Redl2c499f62010-08-18 23:56:43 +00004009void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004010 // It would be complicated to avoid reading the methods anyway. So don't.
4011 ReadMethodPool(Sel);
4012}
4013
Sebastian Redl2c499f62010-08-18 23:56:43 +00004014void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004015 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00004016 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00004017 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00004018 if (DeserializationListener)
4019 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004020}
4021
Douglas Gregor1342e842009-07-06 18:54:52 +00004022/// \brief Set the globally-visible declarations associated with the given
4023/// identifier.
4024///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004025/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00004026/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00004027/// them.
4028///
4029/// \param II an IdentifierInfo that refers to one or more globally-visible
4030/// declarations.
4031///
4032/// \param DeclIDs the set of declaration IDs with the name @p II that are
4033/// visible at global scope.
4034///
4035/// \param Nonrecursive should be true to indicate that the caller knows that
4036/// this call is non-recursive, and therefore the globally-visible declarations
4037/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00004038void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004039ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregor1342e842009-07-06 18:54:52 +00004040 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
4041 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004042 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004043 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4044 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4045 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004046 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00004047 return;
4048 }
Mike Stump11289f42009-09-09 15:08:12 +00004049
Douglas Gregor1342e842009-07-06 18:54:52 +00004050 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4051 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4052 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004053 if (SemaObj->TUScope) {
4054 // Introduce this declaration into the translation-unit scope
4055 // and add it to the declaration chain for this identifier, so
4056 // that (unqualified) name lookup will find it.
John McCall48871652010-08-21 09:40:31 +00004057 SemaObj->TUScope->AddDecl(D);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004058 }
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004059 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregor1342e842009-07-06 18:54:52 +00004060 } else {
4061 // Queue this declaration so that it will be added to the
4062 // translation unit scope and identifier's declaration chain
4063 // once a Sema object is known.
4064 PreloadedDecls.push_back(D);
4065 }
4066 }
4067}
4068
Sebastian Redl2c499f62010-08-18 23:56:43 +00004069IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004070 if (ID == 0)
4071 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00004072
Sebastian Redlc713b962010-07-21 00:46:22 +00004073 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004074 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004075 return 0;
4076 }
Mike Stump11289f42009-09-09 15:08:12 +00004077
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004078 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc713b962010-07-21 00:46:22 +00004079 ID -= 1;
4080 if (!IdentifiersLoaded[ID]) {
4081 unsigned Index = ID;
4082 const char *Str = 0;
4083 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4084 PerFileData *F = Chain[N - I - 1];
4085 if (Index < F->LocalNumIdentifiers) {
4086 uint32_t Offset = F->IdentifierOffsets[Index];
4087 Str = F->IdentifierTableData + Offset;
4088 break;
4089 }
4090 Index -= F->LocalNumIdentifiers;
4091 }
4092 assert(Str && "Broken Chain");
Douglas Gregor5287b4e2009-04-25 21:04:17 +00004093
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004094 // All of the strings in the AST file are preceded by a 16-bit length.
4095 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00004096 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4097 // unsigned integers. This is important to avoid integer overflow when
4098 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00004099 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00004100 unsigned StrLen = (((unsigned) StrLenPtr[0])
4101 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00004102 IdentifiersLoaded[ID]
Kovarththanan Rajaratnama3b09592010-03-12 10:32:27 +00004103 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlff4a2952010-07-23 23:49:55 +00004104 if (DeserializationListener)
4105 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004106 }
Mike Stump11289f42009-09-09 15:08:12 +00004107
Sebastian Redlc713b962010-07-21 00:46:22 +00004108 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004109}
4110
Sebastian Redl2c499f62010-08-18 23:56:43 +00004111void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00004112 ReadSLocEntryRecord(ID);
4113}
4114
Sebastian Redl2c499f62010-08-18 23:56:43 +00004115Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00004116 if (ID == 0)
4117 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00004118
Sebastian Redlada023c2010-08-04 20:40:17 +00004119 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004120 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00004121 return Selector();
4122 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004123
Sebastian Redlada023c2010-08-04 20:40:17 +00004124 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004125 // Load this selector from the selector table.
Sebastian Redlada023c2010-08-04 20:40:17 +00004126 unsigned Idx = ID - 1;
4127 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4128 PerFileData &F = *Chain[N - I - 1];
4129 if (Idx < F.LocalNumSelectors) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004130 ASTSelectorLookupTrait Trait(*this);
Sebastian Redlada023c2010-08-04 20:40:17 +00004131 SelectorsLoaded[ID - 1] =
4132 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4133 if (DeserializationListener)
4134 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4135 break;
4136 }
4137 Idx -= F.LocalNumSelectors;
4138 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004139 }
4140
Sebastian Redlada023c2010-08-04 20:40:17 +00004141 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00004142}
4143
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004144Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00004145 return DecodeSelector(ID);
4146}
4147
Sebastian Redl2c499f62010-08-18 23:56:43 +00004148uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00004149 // ID 0 (the null selector) is considered an external selector.
4150 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00004151}
4152
Mike Stump11289f42009-09-09 15:08:12 +00004153DeclarationName
Sebastian Redl2c499f62010-08-18 23:56:43 +00004154ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004155 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4156 switch (Kind) {
4157 case DeclarationName::Identifier:
4158 return DeclarationName(GetIdentifierInfo(Record, Idx));
4159
4160 case DeclarationName::ObjCZeroArgSelector:
4161 case DeclarationName::ObjCOneArgSelector:
4162 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff3c301dc2009-04-23 15:15:40 +00004163 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004164
4165 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004166 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004167 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004168
4169 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004170 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004171 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004172
4173 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004174 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004175 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004176
4177 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004178 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004179 (OverloadedOperatorKind)Record[Idx++]);
4180
Alexis Hunt3d221f22009-11-29 07:34:05 +00004181 case DeclarationName::CXXLiteralOperatorName:
4182 return Context->DeclarationNames.getCXXLiteralOperatorName(
4183 GetIdentifierInfo(Record, Idx));
4184
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004185 case DeclarationName::CXXUsingDirective:
4186 return DeclarationName::getUsingDirectiveName();
4187 }
4188
4189 // Required to silence GCC warning
4190 return DeclarationName();
4191}
Douglas Gregor55abb232009-04-10 20:39:37 +00004192
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004193void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4194 DeclarationNameLoc &DNLoc,
4195 DeclarationName Name,
4196 const RecordData &Record, unsigned &Idx) {
4197 switch (Name.getNameKind()) {
4198 case DeclarationName::CXXConstructorName:
4199 case DeclarationName::CXXDestructorName:
4200 case DeclarationName::CXXConversionFunctionName:
4201 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4202 break;
4203
4204 case DeclarationName::CXXOperatorName:
4205 DNLoc.CXXOperatorName.BeginOpNameLoc
4206 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4207 DNLoc.CXXOperatorName.EndOpNameLoc
4208 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4209 break;
4210
4211 case DeclarationName::CXXLiteralOperatorName:
4212 DNLoc.CXXLiteralOperatorName.OpNameLoc
4213 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4214 break;
4215
4216 case DeclarationName::Identifier:
4217 case DeclarationName::ObjCZeroArgSelector:
4218 case DeclarationName::ObjCOneArgSelector:
4219 case DeclarationName::ObjCMultiArgSelector:
4220 case DeclarationName::CXXUsingDirective:
4221 break;
4222 }
4223}
4224
4225void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4226 DeclarationNameInfo &NameInfo,
4227 const RecordData &Record, unsigned &Idx) {
4228 NameInfo.setName(ReadDeclarationName(Record, Idx));
4229 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4230 DeclarationNameLoc DNLoc;
4231 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4232 NameInfo.setInfo(DNLoc);
4233}
4234
4235void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4236 const RecordData &Record, unsigned &Idx) {
4237 Info.NNS = ReadNestedNameSpecifier(Record, Idx);
4238 Info.NNSRange = ReadSourceRange(F, Record, Idx);
4239 unsigned NumTPLists = Record[Idx++];
4240 Info.NumTemplParamLists = NumTPLists;
4241 if (NumTPLists) {
4242 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4243 for (unsigned i=0; i != NumTPLists; ++i)
4244 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4245 }
4246}
4247
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004248TemplateName
Douglas Gregor5590be02011-01-15 06:45:20 +00004249ASTReader::ReadTemplateName(PerFileData &F, const RecordData &Record,
4250 unsigned &Idx) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004251 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004252 switch (Kind) {
4253 case TemplateName::Template:
4254 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4255
4256 case TemplateName::OverloadedTemplate: {
4257 unsigned size = Record[Idx++];
4258 UnresolvedSet<8> Decls;
4259 while (size--)
4260 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4261
4262 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4263 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004264
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004265 case TemplateName::QualifiedTemplate: {
4266 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4267 bool hasTemplKeyword = Record[Idx++];
4268 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4269 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4270 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004271
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004272 case TemplateName::DependentTemplate: {
4273 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4274 if (Record[Idx++]) // isIdentifier
4275 return Context->getDependentTemplateName(NNS,
4276 GetIdentifierInfo(Record, Idx));
4277 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004278 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004279 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004280
4281 case TemplateName::SubstTemplateTemplateParmPack: {
4282 TemplateTemplateParmDecl *Param
4283 = cast_or_null<TemplateTemplateParmDecl>(GetDecl(Record[Idx++]));
4284 if (!Param)
4285 return TemplateName();
4286
4287 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
4288 if (ArgPack.getKind() != TemplateArgument::Pack)
4289 return TemplateName();
4290
4291 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
4292 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004293 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004294
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004295 assert(0 && "Unhandled template name kind!");
4296 return TemplateName();
4297}
4298
4299TemplateArgument
Sebastian Redl2c373b92010-10-05 15:59:54 +00004300ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00004301 const RecordData &Record, unsigned &Idx) {
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004302 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
4303 switch (Kind) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004304 case TemplateArgument::Null:
4305 return TemplateArgument();
4306 case TemplateArgument::Type:
4307 return TemplateArgument(GetType(Record[Idx++]));
4308 case TemplateArgument::Declaration:
4309 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00004310 case TemplateArgument::Integral: {
4311 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4312 QualType T = GetType(Record[Idx++]);
4313 return TemplateArgument(Value, T);
4314 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004315 case TemplateArgument::Template:
Douglas Gregor5590be02011-01-15 06:45:20 +00004316 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004317 case TemplateArgument::TemplateExpansion: {
Douglas Gregor5590be02011-01-15 06:45:20 +00004318 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregore1d60df2011-01-14 23:41:42 +00004319 llvm::Optional<unsigned> NumTemplateExpansions;
4320 if (unsigned NumExpansions = Record[Idx++])
4321 NumTemplateExpansions = NumExpansions - 1;
4322 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004323 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004324 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004325 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004326 case TemplateArgument::Pack: {
4327 unsigned NumArgs = Record[Idx++];
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004328 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4329 for (unsigned I = 0; I != NumArgs; ++I)
4330 Args[I] = ReadTemplateArgument(F, Record, Idx);
4331 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004332 }
4333 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004334
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004335 assert(0 && "Unhandled template argument kind!");
4336 return TemplateArgument();
4337}
4338
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004339TemplateParameterList *
Sebastian Redl2c373b92010-10-05 15:59:54 +00004340ASTReader::ReadTemplateParameterList(PerFileData &F,
4341 const RecordData &Record, unsigned &Idx) {
4342 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4343 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4344 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004345
4346 unsigned NumParams = Record[Idx++];
4347 llvm::SmallVector<NamedDecl *, 16> Params;
4348 Params.reserve(NumParams);
4349 while (NumParams--)
4350 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004351
4352 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004353 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4354 Params.data(), Params.size(), RAngleLoc);
4355 return TemplateParams;
4356}
4357
4358void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004359ASTReader::
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004360ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004361 PerFileData &F, const RecordData &Record,
4362 unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004363 unsigned NumTemplateArgs = Record[Idx++];
4364 TemplArgs.reserve(NumTemplateArgs);
4365 while (NumTemplateArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004366 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004367}
4368
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004369/// \brief Read a UnresolvedSet structure.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004370void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004371 const RecordData &Record, unsigned &Idx) {
4372 unsigned NumDecls = Record[Idx++];
4373 while (NumDecls--) {
4374 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4375 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4376 Set.addDecl(D, AS);
4377 }
4378}
4379
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004380CXXBaseSpecifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00004381ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky19b9f952010-07-26 16:56:01 +00004382 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004383 bool isVirtual = static_cast<bool>(Record[Idx++]);
4384 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4385 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redl08905022011-02-05 19:23:19 +00004386 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004387 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4388 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor752a5952011-01-03 22:36:02 +00004389 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl08905022011-02-05 19:23:19 +00004390 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregor752a5952011-01-03 22:36:02 +00004391 EllipsisLoc);
Sebastian Redl08905022011-02-05 19:23:19 +00004392 Result.setInheritConstructors(inheritConstructors);
4393 return Result;
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004394}
4395
Alexis Hunt1d792652011-01-08 20:30:50 +00004396std::pair<CXXCtorInitializer **, unsigned>
4397ASTReader::ReadCXXCtorInitializers(PerFileData &F, const RecordData &Record,
4398 unsigned &Idx) {
4399 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004400 unsigned NumInitializers = Record[Idx++];
4401 if (NumInitializers) {
4402 ASTContext &C = *getContext();
4403
Alexis Hunt1d792652011-01-08 20:30:50 +00004404 CtorInitializers
4405 = new (C) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004406 for (unsigned i=0; i != NumInitializers; ++i) {
4407 TypeSourceInfo *BaseClassInfo = 0;
4408 bool IsBaseVirtual = false;
4409 FieldDecl *Member = 0;
Francois Pichetd583da02010-12-04 09:14:42 +00004410 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004411
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004412 bool IsBaseInitializer = Record[Idx++];
4413 if (IsBaseInitializer) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004414 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004415 IsBaseVirtual = Record[Idx++];
4416 } else {
Francois Pichetd583da02010-12-04 09:14:42 +00004417 bool IsIndirectMemberInitializer = Record[Idx++];
4418 if (IsIndirectMemberInitializer)
4419 IndirectMember = cast<IndirectFieldDecl>(GetDecl(Record[Idx++]));
4420 else
4421 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004422 }
Douglas Gregor44e7df62011-01-04 00:32:56 +00004423 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004424 Expr *Init = ReadExpr(F);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004425 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4426 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004427 bool IsWritten = Record[Idx++];
4428 unsigned SourceOrderOrNumArrayIndices;
4429 llvm::SmallVector<VarDecl *, 8> Indices;
4430 if (IsWritten) {
4431 SourceOrderOrNumArrayIndices = Record[Idx++];
4432 } else {
4433 SourceOrderOrNumArrayIndices = Record[Idx++];
4434 Indices.reserve(SourceOrderOrNumArrayIndices);
4435 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4436 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4437 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004438
Alexis Hunt1d792652011-01-08 20:30:50 +00004439 CXXCtorInitializer *BOMInit;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004440 if (IsBaseInitializer) {
Alexis Hunt1d792652011-01-08 20:30:50 +00004441 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
4442 LParenLoc, Init, RParenLoc,
4443 MemberOrEllipsisLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004444 } else if (IsWritten) {
Francois Pichetd583da02010-12-04 09:14:42 +00004445 if (Member)
Alexis Hunt1d792652011-01-08 20:30:50 +00004446 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
4447 LParenLoc, Init, RParenLoc);
Francois Pichetd583da02010-12-04 09:14:42 +00004448 else
Alexis Hunt1d792652011-01-08 20:30:50 +00004449 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
4450 MemberOrEllipsisLoc, LParenLoc,
4451 Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004452 } else {
Alexis Hunt1d792652011-01-08 20:30:50 +00004453 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
4454 LParenLoc, Init, RParenLoc,
4455 Indices.data(), Indices.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004456 }
4457
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00004458 if (IsWritten)
4459 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Alexis Hunt1d792652011-01-08 20:30:50 +00004460 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004461 }
4462 }
4463
Alexis Hunt1d792652011-01-08 20:30:50 +00004464 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004465}
4466
Chris Lattnerca025db2010-05-07 21:43:38 +00004467NestedNameSpecifier *
Sebastian Redl2c499f62010-08-18 23:56:43 +00004468ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004469 unsigned N = Record[Idx++];
4470 NestedNameSpecifier *NNS = 0, *Prev = 0;
4471 for (unsigned I = 0; I != N; ++I) {
4472 NestedNameSpecifier::SpecifierKind Kind
4473 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4474 switch (Kind) {
4475 case NestedNameSpecifier::Identifier: {
4476 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4477 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4478 break;
4479 }
4480
4481 case NestedNameSpecifier::Namespace: {
4482 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4483 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4484 break;
4485 }
4486
4487 case NestedNameSpecifier::TypeSpec:
4488 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall424cec92011-01-19 06:33:43 +00004489 const Type *T = GetType(Record[Idx++]).getTypePtrOrNull();
Douglas Gregor0cdc8322010-12-10 17:03:06 +00004490 if (!T)
4491 return 0;
4492
Chris Lattnerca025db2010-05-07 21:43:38 +00004493 bool Template = Record[Idx++];
4494 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4495 break;
4496 }
4497
4498 case NestedNameSpecifier::Global: {
4499 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4500 // No associated value, and there can't be a prefix.
4501 break;
4502 }
Chris Lattnerca025db2010-05-07 21:43:38 +00004503 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00004504 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00004505 }
4506 return NNS;
4507}
4508
4509SourceRange
Sebastian Redl2c373b92010-10-05 15:59:54 +00004510ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4511 unsigned &Idx) {
4512 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4513 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00004514 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00004515}
4516
Douglas Gregor1daeb692009-04-13 18:14:40 +00004517/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004518llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004519 unsigned BitWidth = Record[Idx++];
4520 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4521 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4522 Idx += NumWords;
4523 return Result;
4524}
4525
4526/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004527llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004528 bool isUnsigned = Record[Idx++];
4529 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4530}
4531
Douglas Gregore0a3a512009-04-14 21:55:33 +00004532/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004533llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004534 return llvm::APFloat(ReadAPInt(Record, Idx));
4535}
4536
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004537// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00004538std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004539 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00004540 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004541 Idx += Len;
4542 return Result;
4543}
4544
Sebastian Redl2c499f62010-08-18 23:56:43 +00004545CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00004546 unsigned &Idx) {
4547 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4548 return CXXTemporary::Create(*Context, Decl);
4549}
4550
Sebastian Redl2c499f62010-08-18 23:56:43 +00004551DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00004552 return Diag(SourceLocation(), DiagID);
4553}
4554
Sebastian Redl2c499f62010-08-18 23:56:43 +00004555DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004556 return Diags.Report(Loc, DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00004557}
Douglas Gregora9af1d12009-04-17 00:04:06 +00004558
Douglas Gregora868bbd2009-04-21 22:25:48 +00004559/// \brief Retrieve the identifier table associated with the
4560/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004561IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004562 assert(PP && "Forgot to set Preprocessor ?");
4563 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00004564}
4565
Douglas Gregora9af1d12009-04-17 00:04:06 +00004566/// \brief Record that the given ID maps to the given switch-case
4567/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004568void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004569 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4570 SwitchCaseStmts[ID] = SC;
4571}
4572
4573/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004574SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004575 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4576 return SwitchCaseStmts[ID];
4577}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004578
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00004579void ASTReader::ClearSwitchCaseIDs() {
4580 SwitchCaseStmts.clear();
4581}
4582
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004583/// \brief Record that the given label statement has been
4584/// deserialized and has the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004585void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump11289f42009-09-09 15:08:12 +00004586 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004587 "Deserialized label twice");
4588 LabelStmts[ID] = S;
4589
4590 // If we've already seen any goto statements that point to this
4591 // label, resolve them now.
4592 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
4593 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
4594 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
4595 Goto->second->setLabel(S);
4596 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor779d8652009-04-17 18:58:21 +00004597
4598 // If we've already seen any address-label statements that point to
4599 // this label, resolve them now.
4600 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump11289f42009-09-09 15:08:12 +00004601 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor779d8652009-04-17 18:58:21 +00004602 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump11289f42009-09-09 15:08:12 +00004603 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor779d8652009-04-17 18:58:21 +00004604 AddrLabel != AddrLabels.second; ++AddrLabel)
4605 AddrLabel->second->setLabel(S);
4606 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004607}
4608
4609/// \brief Set the label of the given statement to the label
4610/// identified by ID.
4611///
4612/// Depending on the order in which the label and other statements
4613/// referencing that label occur, this operation may complete
4614/// immediately (updating the statement) or it may queue the
4615/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004616void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004617 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4618 if (Label != LabelStmts.end()) {
4619 // We've already seen this label, so set the label of the goto and
4620 // we're done.
4621 S->setLabel(Label->second);
4622 } else {
4623 // We haven't seen this label yet, so add this goto to the set of
4624 // unresolved goto statements.
4625 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
4626 }
4627}
Douglas Gregor779d8652009-04-17 18:58:21 +00004628
4629/// \brief Set the label of the given expression to the label
4630/// identified by ID.
4631///
4632/// Depending on the order in which the label and other statements
4633/// referencing that label occur, this operation may complete
4634/// immediately (updating the statement) or it may queue the
4635/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004636void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor779d8652009-04-17 18:58:21 +00004637 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4638 if (Label != LabelStmts.end()) {
4639 // We've already seen this label, so set the label of the
4640 // label-address expression and we're done.
4641 S->setLabel(Label->second);
4642 } else {
4643 // We haven't seen this label yet, so add this label-address
4644 // expression to the set of unresolved label-address expressions.
4645 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
4646 }
4647}
Douglas Gregor1342e842009-07-06 18:54:52 +00004648
Sebastian Redl2c499f62010-08-18 23:56:43 +00004649void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004650 assert(NumCurrentElementsDeserializing &&
4651 "FinishedDeserializing not paired with StartedDeserializing");
4652 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004653 // If any identifiers with corresponding top-level declarations have
4654 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004655 while (!PendingIdentifierInfos.empty()) {
4656 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4657 PendingIdentifierInfos.front().DeclIDs, true);
4658 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00004659 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004660
4661 // We are not in recursive loading, so it's safe to pass the "interesting"
4662 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004663 if (Consumer)
4664 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidisad5f95c2010-10-24 17:26:31 +00004665
4666 assert(PendingForwardRefs.size() == 0 &&
4667 "Some forward refs did not get linked to the definition!");
Douglas Gregor1342e842009-07-06 18:54:52 +00004668 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004669 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00004670}
Douglas Gregorb473b072010-08-19 00:28:17 +00004671
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004672ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00004673 const char *isysroot, bool DisableValidation,
4674 bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004675 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4676 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4677 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4678 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00004679 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
4680 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NextSLocOffset(0),
4681 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
4682 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
4683 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
4684 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
4685 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
4686 NumCurrentElementsDeserializing(0)
4687{
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004688 RelocatablePCH = false;
4689}
4690
4691ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4692 Diagnostic &Diags, const char *isysroot,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00004693 bool DisableValidation, bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004694 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4695 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00004696 isysroot(isysroot), DisableValidation(DisableValidation),
4697 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
4698 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00004699 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4700 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4701 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4702 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4703 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4704 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004705 RelocatablePCH = false;
4706}
4707
4708ASTReader::~ASTReader() {
4709 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4710 delete Chain[e - i - 1];
4711 // Delete all visible decl lookup tables
4712 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4713 E = DeclContextOffsets.end();
4714 I != E; ++I) {
4715 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4716 J != F; ++J) {
4717 if (J->NameLookupTableData)
4718 delete static_cast<ASTDeclContextNameLookupTable*>(
4719 J->NameLookupTableData);
4720 }
4721 }
4722 for (DeclContextVisibleUpdatesPending::iterator
4723 I = PendingVisibleUpdates.begin(),
4724 E = PendingVisibleUpdates.end();
4725 I != E; ++I) {
4726 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4727 F = I->second.end();
4728 J != F; ++J)
4729 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4730 }
4731}
4732
Sebastian Redl009e7f22010-10-05 16:15:19 +00004733ASTReader::PerFileData::PerFileData(ASTFileType Ty)
4734 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00004735 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
4736 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
4737 MacroDefinitionOffsets(0), LocalNumSelectors(0), SelectorOffsets(0),
4738 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004739 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
4740 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redl3f6b7532010-10-01 19:59:12 +00004741 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregorb473b072010-08-19 00:28:17 +00004742{}
4743
4744ASTReader::PerFileData::~PerFileData() {
4745 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
4746 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4747}
4748