blob: 57002eb5d0bd130ef9618c774ce3213fa422b25b [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"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000044#include <algorithm>
Douglas Gregorc379c072009-04-28 18:58:38 +000045#include <iterator>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000046#include <cstdio>
Douglas Gregorc5046832009-04-27 18:38:38 +000047#include <sys/stat.h>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000048using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000049using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000050
51//===----------------------------------------------------------------------===//
Sebastian Redld44cd6a2010-08-18 23:57:06 +000052// PCH validator implementation
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000053//===----------------------------------------------------------------------===//
54
Sebastian Redl3e31c722010-08-18 23:56:56 +000055ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000056
57bool
58PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
59 const LangOptions &PPLangOpts = PP.getLangOptions();
60#define PARSE_LANGOPT_BENIGN(Option)
61#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
62 if (PPLangOpts.Option != LangOpts.Option) { \
63 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
64 return true; \
65 }
66
67 PARSE_LANGOPT_BENIGN(Trigraphs);
68 PARSE_LANGOPT_BENIGN(BCPLComment);
69 PARSE_LANGOPT_BENIGN(DollarIdents);
70 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
71 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carruthe03aa552010-04-17 20:17:31 +000072 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000073 PARSE_LANGOPT_BENIGN(ImplicitInt);
74 PARSE_LANGOPT_BENIGN(Digraphs);
75 PARSE_LANGOPT_BENIGN(HexFloats);
76 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
77 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +000078 PARSE_LANGOPT_BENIGN(MSCVersion);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000079 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
80 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
81 PARSE_LANGOPT_BENIGN(CXXOperatorName);
82 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
83 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
84 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian45878032010-02-09 19:31:38 +000085 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +000086 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian62c56022010-04-22 21:01:59 +000087 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000088 PARSE_LANGOPT_BENIGN(PascalStrings);
89 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump11289f42009-09-09 15:08:12 +000090 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000091 diag::warn_pch_lax_vector_conversions);
Nate Begeman9d905792009-06-25 22:57:40 +000092 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000093 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +000094 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000095 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
96 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
97 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump11289f42009-09-09 15:08:12 +000098 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000099 diag::warn_pch_thread_safe_statics);
Daniel Dunbara77eaeb2009-09-03 04:54:28 +0000100 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000101 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
102 PARSE_LANGOPT_BENIGN(EmitAllDecls);
103 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattner51924e512010-06-26 21:25:03 +0000104 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump11289f42009-09-09 15:08:12 +0000105 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000106 diag::warn_pch_heinous_extensions);
107 // FIXME: Most of the options below are benign if the macro wasn't
108 // used. Unfortunately, this means that a PCH compiled without
109 // optimization can't be used with optimization turned on, even
110 // though the only thing that changes is whether __OPTIMIZE__ was
111 // defined... but if __OPTIMIZE__ never showed up in the header, it
112 // doesn't matter. We could consider making this some special kind
113 // of check.
114 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
115 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
116 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
117 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
118 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
119 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
120 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
121 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsoned4e2952009-11-05 20:14:16 +0000122 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +0000123 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000124 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump11289f42009-09-09 15:08:12 +0000125 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000126 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
127 return true;
128 }
129 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000130 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
131 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000132 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman9d905792009-06-25 22:57:40 +0000133 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Mike Stumpd9546382009-12-12 01:27:46 +0000134 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbar143021e2009-09-21 04:16:19 +0000135 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000136 PARSE_LANGOPT_BENIGN(SpellChecking);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +0000137#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000138#undef PARSE_LANGOPT_BENIGN
139
140 return false;
141}
142
Daniel Dunbar20a682d2009-11-11 00:52:11 +0000143bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
144 if (Triple == PP.getTargetInfo().getTriple().str())
145 return false;
146
147 Reader.Diag(diag::warn_pch_target_triple)
148 << Triple << PP.getTargetInfo().getTriple().str();
149 return true;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000150}
151
Benjamin Kramer90b5b682010-11-25 18:29:30 +0000152namespace {
153 struct EmptyStringRef {
154 bool operator ()(llvm::StringRef r) const { return r.empty(); }
155 };
156 struct EmptyBlock {
157 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
158 };
159}
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000160
161static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
162 PCHPredefinesBlocks R) {
163 // First, sum up the lengths.
164 unsigned LL = 0, RL = 0;
165 for (unsigned I = 0, N = L.size(); I != N; ++I) {
166 LL += L[I].size();
167 }
168 for (unsigned I = 0, N = R.size(); I != N; ++I) {
169 RL += R[I].Data.size();
170 }
171 if (LL != RL)
172 return false;
173 if (LL == 0 && RL == 0)
174 return true;
175
176 // Kick out empty parts, they confuse the algorithm below.
177 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
178 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
179
180 // Do it the hard way. At this point, both vectors must be non-empty.
181 llvm::StringRef LR = L[0], RR = R[0].Data;
182 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbar01ad0a72010-07-16 00:00:11 +0000183 (void) RN;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000184 for (;;) {
185 // Compare the current pieces.
186 if (LR.size() == RR.size()) {
187 // If they're the same length, it's pretty easy.
188 if (LR != RR)
189 return false;
190 // Both pieces are done, advance.
191 ++LI;
192 ++RI;
193 // If either string is done, they're both done, since they're the same
194 // length.
195 if (LI == LN) {
196 assert(RI == RN && "Strings not the same length after all?");
197 return true;
198 }
199 LR = L[LI];
200 RR = R[RI].Data;
201 } else if (LR.size() < RR.size()) {
202 // Right piece is longer.
203 if (!RR.startswith(LR))
204 return false;
205 ++LI;
206 assert(LI != LN && "Strings not the same length after all?");
207 RR = RR.substr(LR.size());
208 LR = L[LI];
209 } else {
210 // Left piece is longer.
211 if (!LR.startswith(RR))
212 return false;
213 ++RI;
214 assert(RI != RN && "Strings not the same length after all?");
215 LR = LR.substr(RR.size());
216 RR = R[RI].Data;
217 }
218 }
219}
220
221static std::pair<FileID, llvm::StringRef::size_type>
222FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
223 std::pair<FileID, llvm::StringRef::size_type> Res;
224 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
225 Res.second = Buffers[I].Data.find(MacroDef);
226 if (Res.second != llvm::StringRef::npos) {
227 Res.first = Buffers[I].BufferID;
228 break;
229 }
230 }
231 return Res;
232}
233
234bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000235 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000236 std::string &SuggestedPredefines) {
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000237 // We are in the context of an implicit include, so the predefines buffer will
238 // have a #include entry for the PCH file itself (as normalized by the
239 // preprocessor initialization). Find it and skip over it in the checking
240 // below.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000241 llvm::SmallString<256> PCHInclude;
242 PCHInclude += "#include \"";
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000243 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000244 PCHInclude += "\"\n";
245 std::pair<llvm::StringRef,llvm::StringRef> Split =
246 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
247 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000248 if (Left == PP.getPredefines()) {
249 Error("Missing PCH include entry!");
250 return true;
251 }
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000252
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000253 // If the concatenation of all the PCH buffers is equal to the adjusted
254 // command line, we're done.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000255 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
256 CommandLine.push_back(Left);
257 CommandLine.push_back(Right);
258 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000259 return false;
260
261 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000262
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000263 // The predefines buffers are different. Determine what the differences are,
264 // and whether they require us to reject the PCH file.
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000265 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000266 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
267 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000268
269 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
270 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000271
272 // Pick out implicit #includes after the PCH and don't consider them for
273 // validation; we will insert them into SuggestedPredefines so that the
274 // preprocessor includes them.
275 std::string IncludesAfterPCH;
276 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
277 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
278 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
279 if (AfterPCHLines[i].startswith("#include ")) {
280 IncludesAfterPCH += AfterPCHLines[i];
281 IncludesAfterPCH += '\n';
282 } else {
283 CmdLineLines.push_back(AfterPCHLines[i]);
284 }
285 }
286
287 // Make sure we add the includes last into SuggestedPredefines before we
288 // exit this function.
289 struct AddIncludesRAII {
290 std::string &SuggestedPredefines;
291 std::string &IncludesAfterPCH;
292
293 AddIncludesRAII(std::string &SuggestedPredefines,
294 std::string &IncludesAfterPCH)
295 : SuggestedPredefines(SuggestedPredefines),
296 IncludesAfterPCH(IncludesAfterPCH) { }
297 ~AddIncludesRAII() {
298 SuggestedPredefines += IncludesAfterPCH;
299 }
300 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000301
Daniel Dunbar499baed2009-11-11 05:26:28 +0000302 // Sort both sets of predefined buffer lines, since we allow some extra
303 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000304 std::sort(CmdLineLines.begin(), CmdLineLines.end());
305 std::sort(PCHLines.begin(), PCHLines.end());
306
Daniel Dunbar499baed2009-11-11 05:26:28 +0000307 // Determine which predefines that were used to build the PCH file are missing
308 // from the command line.
309 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000310 std::set_difference(PCHLines.begin(), PCHLines.end(),
311 CmdLineLines.begin(), CmdLineLines.end(),
312 std::back_inserter(MissingPredefines));
313
314 bool MissingDefines = false;
315 bool ConflictingDefines = false;
316 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000317 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000318 if (Missing.startswith("#include ")) {
319 // An -include was specified when generating the PCH; it is included in
320 // the PCH, just ignore it.
321 continue;
322 }
Daniel Dunbar499baed2009-11-11 05:26:28 +0000323 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000324 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
325 return true;
326 }
Mike Stump11289f42009-09-09 15:08:12 +0000327
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000328 // This is a macro definition. Determine the name of the macro we're
329 // defining.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000330 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000331 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000332 = Missing.find_first_of("( \n\r", StartOfMacroName);
333 assert(EndOfMacroName != std::string::npos &&
334 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000335 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000336
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000337 // Determine whether this macro was given a different definition on the
338 // command line.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000339 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000340 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000341 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000342 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
343 MacroDefStart);
344 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000345 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000346 // Different macro; we're done.
347 ConflictPos = CmdLineLines.end();
Mike Stump11289f42009-09-09 15:08:12 +0000348 break;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000349 }
Mike Stump11289f42009-09-09 15:08:12 +0000350
351 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000352 "Invalid #define in predefines buffer?");
Mike Stump11289f42009-09-09 15:08:12 +0000353 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000354 (*ConflictPos)[MacroDefLen] != '(')
355 continue; // Longer macro name; keep trying.
Mike Stump11289f42009-09-09 15:08:12 +0000356
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000357 // We found a conflicting macro definition.
358 break;
359 }
Mike Stump11289f42009-09-09 15:08:12 +0000360
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000361 if (ConflictPos != CmdLineLines.end()) {
362 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
363 << MacroName;
364
365 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000366 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
367 FindMacro(Buffers, Missing);
368 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
369 SourceLocation PCHMissingLoc =
370 SourceMgr.getLocForStartOfFile(MacroLoc.first)
371 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar499baed2009-11-11 05:26:28 +0000372 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000373
374 ConflictingDefines = true;
375 continue;
376 }
Mike Stump11289f42009-09-09 15:08:12 +0000377
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000378 // If the macro doesn't conflict, then we'll just pick up the macro
379 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000380 if (ConflictingDefines)
381 continue; // Don't complain if there are already conflicting defs
Mike Stump11289f42009-09-09 15:08:12 +0000382
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000383 if (!MissingDefines) {
384 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
385 MissingDefines = true;
386 }
387
388 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000389 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
390 FindMacro(Buffers, Missing);
391 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
392 SourceLocation PCHMissingLoc =
393 SourceMgr.getLocForStartOfFile(MacroLoc.first)
394 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000395 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
396 }
Mike Stump11289f42009-09-09 15:08:12 +0000397
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000398 if (ConflictingDefines)
399 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000400
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000401 // Determine what predefines were introduced based on command-line
402 // parameters that were not present when building the PCH
403 // file. Extra #defines are okay, so long as the identifiers being
404 // defined were not used within the precompiled header.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000405 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000406 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
407 PCHLines.begin(), PCHLines.end(),
Mike Stump11289f42009-09-09 15:08:12 +0000408 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000409 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000410 llvm::StringRef &Extra = ExtraPredefines[I];
411 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000412 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
413 return true;
414 }
415
416 // This is an extra macro definition. Determine the name of the
417 // macro we're defining.
418 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000419 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000420 = Extra.find_first_of("( \n\r", StartOfMacroName);
421 assert(EndOfMacroName != std::string::npos &&
422 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000423 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000424
425 // Check whether this name was used somewhere in the PCH file. If
426 // so, defining it as a macro could change behavior, so we reject
427 // the PCH file.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000428 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar045c92f2009-11-11 00:52:00 +0000429 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000430 return true;
431 }
432
433 // Add this definition to the suggested predefines buffer.
434 SuggestedPredefines += Extra;
435 SuggestedPredefines += '\n';
436 }
437
438 // If we get here, it's because the predefines buffer had compatible
439 // contents. Accept the PCH file.
440 return false;
441}
442
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000443void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
444 unsigned ID) {
445 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
446 ++NumHeaderInfos;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000447}
448
449void PCHValidator::ReadCounter(unsigned Value) {
450 PP.setCounterValue(Value);
451}
452
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000453//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +0000454// AST reader implementation
Douglas Gregora868bbd2009-04-21 22:25:48 +0000455//===----------------------------------------------------------------------===//
456
Sebastian Redl07a89a82010-07-30 00:29:29 +0000457void
Sebastian Redl3e31c722010-08-18 23:56:56 +0000458ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redl07a89a82010-07-30 00:29:29 +0000459 DeserializationListener = Listener;
Sebastian Redl07a89a82010-07-30 00:29:29 +0000460}
461
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000462
Douglas Gregora868bbd2009-04-21 22:25:48 +0000463namespace {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000464class ASTSelectorLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000465 ASTReader &Reader;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000466
467public:
Sebastian Redl834bb972010-08-04 17:20:04 +0000468 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +0000469 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +0000470 ObjCMethodList Instance, Factory;
471 };
Douglas Gregorc78d3462009-04-24 21:10:55 +0000472
473 typedef Selector external_key_type;
474 typedef external_key_type internal_key_type;
475
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000476 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump11289f42009-09-09 15:08:12 +0000477
Douglas Gregorc78d3462009-04-24 21:10:55 +0000478 static bool EqualKey(const internal_key_type& a,
479 const internal_key_type& b) {
480 return a == b;
481 }
Mike Stump11289f42009-09-09 15:08:12 +0000482
Douglas Gregorc78d3462009-04-24 21:10:55 +0000483 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +0000484 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000485 }
Mike Stump11289f42009-09-09 15:08:12 +0000486
Douglas Gregorc78d3462009-04-24 21:10:55 +0000487 // This hopefully will just get inlined and removed by the optimizer.
488 static const internal_key_type&
489 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000490
Douglas Gregorc78d3462009-04-24 21:10:55 +0000491 static std::pair<unsigned, unsigned>
492 ReadKeyDataLength(const unsigned char*& d) {
493 using namespace clang::io;
494 unsigned KeyLen = ReadUnalignedLE16(d);
495 unsigned DataLen = ReadUnalignedLE16(d);
496 return std::make_pair(KeyLen, DataLen);
497 }
Mike Stump11289f42009-09-09 15:08:12 +0000498
Douglas Gregor95c13f52009-04-25 17:48:32 +0000499 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000500 using namespace clang::io;
Chris Lattner8575daa2009-04-27 21:45:14 +0000501 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000502 unsigned N = ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000503 IdentifierInfo *FirstII
Douglas Gregorc78d3462009-04-24 21:10:55 +0000504 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
505 if (N == 0)
506 return SelTable.getNullarySelector(FirstII);
507 else if (N == 1)
508 return SelTable.getUnarySelector(FirstII);
509
510 llvm::SmallVector<IdentifierInfo *, 16> Args;
511 Args.push_back(FirstII);
512 for (unsigned I = 1; I != N; ++I)
513 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
514
Douglas Gregor038c3382009-05-22 22:45:36 +0000515 return SelTable.getSelector(N, Args.data());
Douglas Gregorc78d3462009-04-24 21:10:55 +0000516 }
Mike Stump11289f42009-09-09 15:08:12 +0000517
Douglas Gregorc78d3462009-04-24 21:10:55 +0000518 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
519 using namespace clang::io;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000520
521 data_type Result;
522
Sebastian Redl834bb972010-08-04 17:20:04 +0000523 Result.ID = ReadUnalignedLE32(d);
524 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
525 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
526
Douglas Gregorc78d3462009-04-24 21:10:55 +0000527 // Load instance methods
528 ObjCMethodList *Prev = 0;
529 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000530 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000531 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000532 if (!Result.Instance.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000533 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000534 Result.Instance.Method = Method;
535 Prev = &Result.Instance;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000536 continue;
537 }
538
Ted Kremenekda4abf12010-02-11 00:53:01 +0000539 ObjCMethodList *Mem =
540 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
541 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000542 Prev = Prev->Next;
543 }
544
545 // Load factory methods
546 Prev = 0;
547 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000548 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000549 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000550 if (!Result.Factory.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000551 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000552 Result.Factory.Method = Method;
553 Prev = &Result.Factory;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000554 continue;
555 }
556
Ted Kremenekda4abf12010-02-11 00:53:01 +0000557 ObjCMethodList *Mem =
558 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
559 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000560 Prev = Prev->Next;
561 }
562
563 return Result;
564 }
565};
Mike Stump11289f42009-09-09 15:08:12 +0000566
567} // end anonymous namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +0000568
569/// \brief The on-disk hash table used for the global method pool.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000570typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
571 ASTSelectorLookupTable;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000572
Sebastian Redl2c373b92010-10-05 15:59:54 +0000573namespace clang {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000574class ASTIdentifierLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000575 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +0000576 ASTReader::PerFileData &F;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000577
578 // If we know the IdentifierInfo in advance, it is here and we will
579 // not build a new one. Used when deserializing information about an
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000580 // identifier that was constructed before the AST file was read.
Douglas Gregora868bbd2009-04-21 22:25:48 +0000581 IdentifierInfo *KnownII;
582
583public:
584 typedef IdentifierInfo * data_type;
585
586 typedef const std::pair<const char*, unsigned> external_key_type;
587
588 typedef external_key_type internal_key_type;
589
Sebastian Redl2c373b92010-10-05 15:59:54 +0000590 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000591 IdentifierInfo *II = 0)
Sebastian Redl2c373b92010-10-05 15:59:54 +0000592 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump11289f42009-09-09 15:08:12 +0000593
Douglas Gregora868bbd2009-04-21 22:25:48 +0000594 static bool EqualKey(const internal_key_type& a,
595 const internal_key_type& b) {
596 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
597 : false;
598 }
Mike Stump11289f42009-09-09 15:08:12 +0000599
Douglas Gregora868bbd2009-04-21 22:25:48 +0000600 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000601 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregora868bbd2009-04-21 22:25:48 +0000602 }
Mike Stump11289f42009-09-09 15:08:12 +0000603
Douglas Gregora868bbd2009-04-21 22:25:48 +0000604 // This hopefully will just get inlined and removed by the optimizer.
605 static const internal_key_type&
606 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000607
Douglas Gregor57756ea2010-10-14 22:11:03 +0000608 // This hopefully will just get inlined and removed by the optimizer.
609 static const external_key_type&
610 GetExternalKey(const internal_key_type& x) { return x; }
611
Douglas Gregora868bbd2009-04-21 22:25:48 +0000612 static std::pair<unsigned, unsigned>
613 ReadKeyDataLength(const unsigned char*& d) {
614 using namespace clang::io;
Douglas Gregor6b7bf5a2009-04-25 20:26:24 +0000615 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor5287b4e2009-04-25 21:04:17 +0000616 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000617 return std::make_pair(KeyLen, DataLen);
618 }
Mike Stump11289f42009-09-09 15:08:12 +0000619
Douglas Gregora868bbd2009-04-21 22:25:48 +0000620 static std::pair<const char*, unsigned>
621 ReadKey(const unsigned char* d, unsigned n) {
622 assert(n >= 2 && d[n-1] == '\0');
623 return std::make_pair((const char*) d, n-1);
624 }
Mike Stump11289f42009-09-09 15:08:12 +0000625
626 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregora868bbd2009-04-21 22:25:48 +0000627 const unsigned char* d,
628 unsigned DataLen) {
629 using namespace clang::io;
Sebastian Redl539c5062010-08-18 23:57:32 +0000630 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000631 bool IsInteresting = ID & 0x01;
632
633 // Wipe out the "is interesting" bit.
634 ID = ID >> 1;
635
636 if (!IsInteresting) {
Sebastian Redl98912122010-07-27 23:01:28 +0000637 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregor1d583f22009-04-28 21:18:29 +0000638 // and associate it with the persistent ID.
639 IdentifierInfo *II = KnownII;
640 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000641 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000642 Reader.SetIdentifierInfo(ID, II);
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000643 II->setIsFromAST();
Douglas Gregor1d583f22009-04-28 21:18:29 +0000644 return II;
645 }
646
Douglas Gregorb9256522009-04-28 21:32:13 +0000647 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000648 bool CPlusPlusOperatorKeyword = Bits & 0x01;
649 Bits >>= 1;
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000650 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
651 Bits >>= 1;
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000652 bool Poisoned = Bits & 0x01;
653 Bits >>= 1;
654 bool ExtensionToken = Bits & 0x01;
655 Bits >>= 1;
656 bool hasMacroDefinition = Bits & 0x01;
657 Bits >>= 1;
658 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
659 Bits >>= 10;
Mike Stump11289f42009-09-09 15:08:12 +0000660
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000661 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorb9256522009-04-28 21:32:13 +0000662 DataLen -= 6;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000663
664 // Build the IdentifierInfo itself and link the identifier ID with
665 // the new IdentifierInfo.
666 IdentifierInfo *II = KnownII;
667 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000668 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000669 Reader.SetIdentifierInfo(ID, II);
670
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000671 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000672 // Token IDs are read-only.
673 if (HasRevertedTokenIDToIdentifier)
674 II->RevertTokenIDToIdentifier();
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000675 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump11289f42009-09-09 15:08:12 +0000676 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000677 "Incorrect extension token flag");
678 (void)ExtensionToken;
679 II->setIsPoisoned(Poisoned);
680 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
681 "Incorrect C++ operator keyword flag");
682 (void)CPlusPlusOperatorKeyword;
683
Douglas Gregorc3366a52009-04-21 23:56:24 +0000684 // If this identifier is a macro, deserialize the macro
685 // definition.
686 if (hasMacroDefinition) {
Douglas Gregorb9256522009-04-28 21:32:13 +0000687 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor5ef9e332010-10-30 00:23:06 +0000688 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregorb9256522009-04-28 21:32:13 +0000689 DataLen -= 4;
Douglas Gregorc3366a52009-04-21 23:56:24 +0000690 }
Douglas Gregora868bbd2009-04-21 22:25:48 +0000691
692 // Read all of the declarations visible at global scope with this
693 // name.
Chris Lattner1d728882009-04-27 22:17:41 +0000694 if (Reader.getContext() == 0) return II;
Douglas Gregor1342e842009-07-06 18:54:52 +0000695 if (DataLen > 0) {
696 llvm::SmallVector<uint32_t, 4> DeclIDs;
697 for (; DataLen > 0; DataLen -= 4)
698 DeclIDs.push_back(ReadUnalignedLE32(d));
699 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000700 }
Mike Stump11289f42009-09-09 15:08:12 +0000701
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000702 II->setIsFromAST();
Douglas Gregora868bbd2009-04-21 22:25:48 +0000703 return II;
704 }
705};
Mike Stump11289f42009-09-09 15:08:12 +0000706
707} // end anonymous namespace
Douglas Gregora868bbd2009-04-21 22:25:48 +0000708
709/// \brief The on-disk hash table used to contain information about
710/// all of the identifiers in the program.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000711typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
712 ASTIdentifierLookupTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000713
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000714namespace {
715class ASTDeclContextNameLookupTrait {
716 ASTReader &Reader;
717
718public:
719 /// \brief Pair of begin/end iterators for DeclIDs.
720 typedef std::pair<DeclID *, DeclID *> data_type;
721
722 /// \brief Special internal key for declaration names.
723 /// The hash table creates keys for comparison; we do not create
724 /// a DeclarationName for the internal key to avoid deserializing types.
725 struct DeclNameKey {
726 DeclarationName::NameKind Kind;
727 uint64_t Data;
728 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
729 };
730
731 typedef DeclarationName external_key_type;
732 typedef DeclNameKey internal_key_type;
733
734 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
735
736 static bool EqualKey(const internal_key_type& a,
737 const internal_key_type& b) {
738 return a.Kind == b.Kind && a.Data == b.Data;
739 }
740
741 unsigned ComputeHash(const DeclNameKey &Key) const {
742 llvm::FoldingSetNodeID ID;
743 ID.AddInteger(Key.Kind);
744
745 switch (Key.Kind) {
746 case DeclarationName::Identifier:
747 case DeclarationName::CXXLiteralOperatorName:
748 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
749 break;
750 case DeclarationName::ObjCZeroArgSelector:
751 case DeclarationName::ObjCOneArgSelector:
752 case DeclarationName::ObjCMultiArgSelector:
753 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
754 break;
755 case DeclarationName::CXXConstructorName:
756 case DeclarationName::CXXDestructorName:
757 case DeclarationName::CXXConversionFunctionName:
758 ID.AddInteger((TypeID)Key.Data);
759 break;
760 case DeclarationName::CXXOperatorName:
761 ID.AddInteger((OverloadedOperatorKind)Key.Data);
762 break;
763 case DeclarationName::CXXUsingDirective:
764 break;
765 }
766
767 return ID.ComputeHash();
768 }
769
770 internal_key_type GetInternalKey(const external_key_type& Name) const {
771 DeclNameKey Key;
772 Key.Kind = Name.getNameKind();
773 switch (Name.getNameKind()) {
774 case DeclarationName::Identifier:
775 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
776 break;
777 case DeclarationName::ObjCZeroArgSelector:
778 case DeclarationName::ObjCOneArgSelector:
779 case DeclarationName::ObjCMultiArgSelector:
780 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
781 break;
782 case DeclarationName::CXXConstructorName:
783 case DeclarationName::CXXDestructorName:
784 case DeclarationName::CXXConversionFunctionName:
785 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
786 break;
787 case DeclarationName::CXXOperatorName:
788 Key.Data = Name.getCXXOverloadedOperator();
789 break;
790 case DeclarationName::CXXLiteralOperatorName:
791 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
792 break;
793 case DeclarationName::CXXUsingDirective:
794 break;
795 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000796
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000797 return Key;
798 }
799
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000800 external_key_type GetExternalKey(const internal_key_type& Key) const {
801 ASTContext *Context = Reader.getContext();
802 switch (Key.Kind) {
803 case DeclarationName::Identifier:
804 return DeclarationName((IdentifierInfo*)Key.Data);
805
806 case DeclarationName::ObjCZeroArgSelector:
807 case DeclarationName::ObjCOneArgSelector:
808 case DeclarationName::ObjCMultiArgSelector:
809 return DeclarationName(Selector(Key.Data));
810
811 case DeclarationName::CXXConstructorName:
812 return Context->DeclarationNames.getCXXConstructorName(
813 Context->getCanonicalType(Reader.GetType(Key.Data)));
814
815 case DeclarationName::CXXDestructorName:
816 return Context->DeclarationNames.getCXXDestructorName(
817 Context->getCanonicalType(Reader.GetType(Key.Data)));
818
819 case DeclarationName::CXXConversionFunctionName:
820 return Context->DeclarationNames.getCXXConversionFunctionName(
821 Context->getCanonicalType(Reader.GetType(Key.Data)));
822
823 case DeclarationName::CXXOperatorName:
824 return Context->DeclarationNames.getCXXOperatorName(
825 (OverloadedOperatorKind)Key.Data);
826
827 case DeclarationName::CXXLiteralOperatorName:
828 return Context->DeclarationNames.getCXXLiteralOperatorName(
829 (IdentifierInfo*)Key.Data);
830
831 case DeclarationName::CXXUsingDirective:
832 return DeclarationName::getUsingDirectiveName();
833 }
834
835 llvm_unreachable("Invalid Name Kind ?");
836 }
837
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000838 static std::pair<unsigned, unsigned>
839 ReadKeyDataLength(const unsigned char*& d) {
840 using namespace clang::io;
841 unsigned KeyLen = ReadUnalignedLE16(d);
842 unsigned DataLen = ReadUnalignedLE16(d);
843 return std::make_pair(KeyLen, DataLen);
844 }
845
846 internal_key_type ReadKey(const unsigned char* d, unsigned) {
847 using namespace clang::io;
848
849 DeclNameKey Key;
850 Key.Kind = (DeclarationName::NameKind)*d++;
851 switch (Key.Kind) {
852 case DeclarationName::Identifier:
853 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
854 break;
855 case DeclarationName::ObjCZeroArgSelector:
856 case DeclarationName::ObjCOneArgSelector:
857 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000858 Key.Data =
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000859 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
860 break;
861 case DeclarationName::CXXConstructorName:
862 case DeclarationName::CXXDestructorName:
863 case DeclarationName::CXXConversionFunctionName:
864 Key.Data = ReadUnalignedLE32(d); // TypeID
865 break;
866 case DeclarationName::CXXOperatorName:
867 Key.Data = *d++; // OverloadedOperatorKind
868 break;
869 case DeclarationName::CXXLiteralOperatorName:
870 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
871 break;
872 case DeclarationName::CXXUsingDirective:
873 break;
874 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000875
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000876 return Key;
877 }
878
879 data_type ReadData(internal_key_type, const unsigned char* d,
880 unsigned DataLen) {
881 using namespace clang::io;
882 unsigned NumDecls = ReadUnalignedLE16(d);
883 DeclID *Start = (DeclID *)d;
884 return std::make_pair(Start, Start + NumDecls);
885 }
886};
887
888} // end anonymous namespace
889
890/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
891typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
892 ASTDeclContextNameLookupTable;
893
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000894bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
895 const std::pair<uint64_t, uint64_t> &Offsets,
896 DeclContextInfo &Info) {
897 SavedStreamPosition SavedPosition(Cursor);
898 // First the lexical decls.
899 if (Offsets.first != 0) {
900 Cursor.JumpToBit(Offsets.first);
901
902 RecordData Record;
903 const char *Blob;
904 unsigned BlobLen;
905 unsigned Code = Cursor.ReadCode();
906 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
907 if (RecCode != DECL_CONTEXT_LEXICAL) {
908 Error("Expected lexical block");
909 return true;
910 }
911
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000912 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
913 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000914 } else {
915 Info.LexicalDecls = 0;
916 Info.NumLexicalDecls = 0;
917 }
918
919 // Now the lookup table.
920 if (Offsets.second != 0) {
921 Cursor.JumpToBit(Offsets.second);
922
923 RecordData Record;
924 const char *Blob;
925 unsigned BlobLen;
926 unsigned Code = Cursor.ReadCode();
927 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
928 if (RecCode != DECL_CONTEXT_VISIBLE) {
929 Error("Expected visible lookup table block");
930 return true;
931 }
932 Info.NameLookupTableData
933 = ASTDeclContextNameLookupTable::Create(
934 (const unsigned char *)Blob + Record[0],
935 (const unsigned char *)Blob,
936 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl9d8f58b2010-08-24 00:50:00 +0000937 } else {
938 Info.NameLookupTableData = 0;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000939 }
940
941 return false;
942}
943
Sebastian Redl2c499f62010-08-18 23:56:43 +0000944void ASTReader::Error(const char *Msg) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000945 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000946}
947
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000948/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000949bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000950 if (Listener)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000951 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000952 ActualOriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000953 SuggestedPredefines);
Douglas Gregorc379c072009-04-28 18:58:38 +0000954 return false;
Douglas Gregor92863e42009-04-10 23:10:45 +0000955}
956
Douglas Gregorc5046832009-04-27 18:38:38 +0000957//===----------------------------------------------------------------------===//
958// Source Manager Deserialization
959//===----------------------------------------------------------------------===//
960
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000961/// \brief Read the line table in the source manager block.
Sebastian Redl2c373b92010-10-05 15:59:54 +0000962/// \returns true if there was an error.
963bool ASTReader::ParseLineTable(PerFileData &F,
964 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000965 unsigned Idx = 0;
966 LineTableInfo &LineTable = SourceMgr.getLineTable();
967
968 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +0000969 std::map<int, int> FileIDs;
970 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000971 // Extract the file name
972 unsigned FilenameLen = Record[Idx++];
973 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
974 Idx += FilenameLen;
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000975 MaybeAddSystemRootToFilename(Filename);
Mike Stump11289f42009-09-09 15:08:12 +0000976 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregora8854652009-04-13 17:12:42 +0000977 Filename.size());
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000978 }
979
980 // Parse the line entries
981 std::vector<LineEntry> Entries;
982 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000983 int FID = Record[Idx++];
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000984
985 // Extract the line entries
986 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000987 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000988 Entries.clear();
989 Entries.reserve(NumEntries);
990 for (unsigned I = 0; I != NumEntries; ++I) {
991 unsigned FileOffset = Record[Idx++];
992 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000993 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +0000994 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000995 = (SrcMgr::CharacteristicKind)Record[Idx++];
996 unsigned IncludeOffset = Record[Idx++];
997 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
998 FileKind, IncludeOffset));
999 }
1000 LineTable.AddEntry(FID, Entries);
1001 }
1002
1003 return false;
1004}
1005
Douglas Gregorc5046832009-04-27 18:38:38 +00001006namespace {
1007
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001008class ASTStatData {
Douglas Gregorc5046832009-04-27 18:38:38 +00001009public:
Douglas Gregorc5046832009-04-27 18:38:38 +00001010 const ino_t ino;
1011 const dev_t dev;
1012 const mode_t mode;
1013 const time_t mtime;
1014 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +00001015
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001016 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner2a6fa472010-11-23 19:28:12 +00001017 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregorc5046832009-04-27 18:38:38 +00001018};
1019
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001020class ASTStatLookupTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001021 public:
1022 typedef const char *external_key_type;
1023 typedef const char *internal_key_type;
1024
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001025 typedef ASTStatData data_type;
Douglas Gregorc5046832009-04-27 18:38:38 +00001026
1027 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001028 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001029 }
1030
1031 static internal_key_type GetInternalKey(const char *path) { return path; }
1032
1033 static bool EqualKey(internal_key_type a, internal_key_type b) {
1034 return strcmp(a, b) == 0;
1035 }
1036
1037 static std::pair<unsigned, unsigned>
1038 ReadKeyDataLength(const unsigned char*& d) {
1039 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1040 unsigned DataLen = (unsigned) *d++;
1041 return std::make_pair(KeyLen + 1, DataLen);
1042 }
1043
1044 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1045 return (const char *)d;
1046 }
1047
1048 static data_type ReadData(const internal_key_type, const unsigned char *d,
1049 unsigned /*DataLen*/) {
1050 using namespace clang::io;
1051
Douglas Gregorc5046832009-04-27 18:38:38 +00001052 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1053 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1054 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +00001055 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +00001056 off_t size = (off_t) ReadUnalignedLE64(d);
1057 return data_type(ino, dev, mode, mtime, size);
1058 }
1059};
1060
1061/// \brief stat() cache for precompiled headers.
1062///
1063/// This cache is very similar to the stat cache used by pretokenized
1064/// headers.
Chris Lattner226efd32010-11-23 19:19:34 +00001065class ASTStatCache : public FileSystemStatCache {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001066 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregorc5046832009-04-27 18:38:38 +00001067 CacheTy *Cache;
1068
1069 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +00001070public:
Chris Lattner2a6fa472010-11-23 19:28:12 +00001071 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1072 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +00001073 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1074 Cache = CacheTy::Create(Buckets, Base);
1075 }
1076
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001077 ~ASTStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +00001078
Chris Lattnerdd278432010-11-23 21:17:56 +00001079 LookupResult getStat(const char *Path, struct stat &StatBuf,
1080 int *FileDescriptor) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001081 // Do the lookup for the file's data in the AST file.
Chris Lattner226efd32010-11-23 19:19:34 +00001082 CacheTy::iterator I = Cache->find(Path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001083
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001084 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregorc5046832009-04-27 18:38:38 +00001085 if (I == Cache->end()) {
1086 ++NumStatMisses;
Chris Lattnerdd278432010-11-23 21:17:56 +00001087 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregorc5046832009-04-27 18:38:38 +00001088 }
Mike Stump11289f42009-09-09 15:08:12 +00001089
Douglas Gregorc5046832009-04-27 18:38:38 +00001090 ++NumStatHits;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001091 ASTStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001092
Chris Lattner226efd32010-11-23 19:19:34 +00001093 StatBuf.st_ino = Data.ino;
1094 StatBuf.st_dev = Data.dev;
1095 StatBuf.st_mtime = Data.mtime;
1096 StatBuf.st_mode = Data.mode;
1097 StatBuf.st_size = Data.size;
Chris Lattner8f0583d2010-11-23 20:05:15 +00001098 return CacheExists;
Douglas Gregorc5046832009-04-27 18:38:38 +00001099 }
1100};
1101} // end anonymous namespace
1102
1103
Sebastian Redl393f8b72010-07-19 20:52:06 +00001104/// \brief Read a source manager block
Sebastian Redl2c499f62010-08-18 23:56:43 +00001105ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001106 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +00001107
Sebastian Redl393f8b72010-07-19 20:52:06 +00001108 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001109
Douglas Gregor258ae542009-04-27 06:38:32 +00001110 // Set the source-location entry cursor to the current position in
1111 // the stream. This cursor will be used to read the contents of the
1112 // source manager block initially, and then lazily read
1113 // source-location entries as needed.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001114 SLocEntryCursor = F.Stream;
Douglas Gregor258ae542009-04-27 06:38:32 +00001115
1116 // The stream itself is going to skip over the source manager block.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001117 if (F.Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001118 Error("malformed block record in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001119 return Failure;
1120 }
1121
1122 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001123 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001124 Error("malformed source manager block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001125 return Failure;
1126 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001127
Douglas Gregora7f71a92009-04-10 03:52:48 +00001128 RecordData Record;
1129 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001130 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001131 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001132 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001133 Error("error at end of Source Manager block in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001134 return Failure;
1135 }
Douglas Gregor92863e42009-04-10 23:10:45 +00001136 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001137 }
Mike Stump11289f42009-09-09 15:08:12 +00001138
Douglas Gregora7f71a92009-04-10 03:52:48 +00001139 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1140 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +00001141 SLocEntryCursor.ReadSubBlockID();
1142 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001143 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001144 return Failure;
1145 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001146 continue;
1147 }
Mike Stump11289f42009-09-09 15:08:12 +00001148
Douglas Gregora7f71a92009-04-10 03:52:48 +00001149 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001150 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001151 continue;
1152 }
Mike Stump11289f42009-09-09 15:08:12 +00001153
Douglas Gregora7f71a92009-04-10 03:52:48 +00001154 // Read a record.
1155 const char *BlobStart;
1156 unsigned BlobLen;
1157 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +00001158 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001159 default: // Default behavior: ignore.
1160 break;
1161
Sebastian Redl539c5062010-08-18 23:57:32 +00001162 case SM_LINE_TABLE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00001163 if (ParseLineTable(F, Record))
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001164 return Failure;
Chris Lattner184e65d2009-04-14 23:22:57 +00001165 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00001166
Sebastian Redl539c5062010-08-18 23:57:32 +00001167 case SM_SLOC_FILE_ENTRY:
1168 case SM_SLOC_BUFFER_ENTRY:
1169 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor258ae542009-04-27 06:38:32 +00001170 // Once we hit one of the source location entries, we're done.
1171 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001172 }
1173 }
1174}
1175
Sebastian Redl06750302010-07-20 21:50:20 +00001176/// \brief Get a cursor that's correctly positioned for reading the source
1177/// location entry with the given ID.
Sebastian Redl2c373b92010-10-05 15:59:54 +00001178ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl06750302010-07-20 21:50:20 +00001179 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1180 "SLocCursorForID should only be called for real IDs.");
1181
1182 ID -= 1;
1183 PerFileData *F = 0;
1184 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1185 F = Chain[N - I - 1];
1186 if (ID < F->LocalNumSLocEntries)
1187 break;
1188 ID -= F->LocalNumSLocEntries;
1189 }
1190 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1191
1192 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00001193 return F;
Sebastian Redl06750302010-07-20 21:50:20 +00001194}
1195
Douglas Gregor258ae542009-04-27 06:38:32 +00001196/// \brief Read in the source location entry with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001197ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001198 if (ID == 0)
1199 return Success;
1200
1201 if (ID > TotalNumSLocEntries) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001202 Error("source location entry ID out-of-range for AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001203 return Failure;
1204 }
1205
Sebastian Redl2c373b92010-10-05 15:59:54 +00001206 PerFileData *F = SLocCursorForID(ID);
1207 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001208
Douglas Gregor258ae542009-04-27 06:38:32 +00001209 ++NumSLocEntriesRead;
Douglas Gregor258ae542009-04-27 06:38:32 +00001210 unsigned Code = SLocEntryCursor.ReadCode();
1211 if (Code == llvm::bitc::END_BLOCK ||
1212 Code == llvm::bitc::ENTER_SUBBLOCK ||
1213 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001214 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001215 return Failure;
1216 }
1217
Douglas Gregor258ae542009-04-27 06:38:32 +00001218 RecordData Record;
1219 const char *BlobStart;
1220 unsigned BlobLen;
1221 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1222 default:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001223 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001224 return Failure;
1225
Sebastian Redl539c5062010-08-18 23:57:32 +00001226 case SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001227 std::string Filename(BlobStart, BlobStart + BlobLen);
1228 MaybeAddSystemRootToFilename(Filename);
Chris Lattner5159f612010-11-23 08:35:12 +00001229 const FileEntry *File = FileMgr.getFile(Filename);
Chris Lattnerd20dc872009-06-15 04:35:16 +00001230 if (File == 0) {
1231 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001232 ErrorStr += Filename;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001233 ErrorStr += "' referenced by AST file";
Chris Lattnerd20dc872009-06-15 04:35:16 +00001234 Error(ErrorStr.c_str());
1235 return Failure;
1236 }
Mike Stump11289f42009-09-09 15:08:12 +00001237
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001238 if (Record.size() < 10) {
Ted Kremenekabb1ddd2010-03-18 21:23:05 +00001239 Error("source location entry is incorrect");
1240 return Failure;
1241 }
1242
Douglas Gregorce3a8292010-07-27 00:27:13 +00001243 if (!DisableValidation &&
1244 ((off_t)Record[4] != File->getSize()
Douglas Gregor08288f22010-04-09 15:54:22 +00001245#if !defined(LLVM_ON_WIN32)
1246 // In our regression testing, the Windows file system seems to
1247 // have inconsistent modification times that sometimes
1248 // erroneously trigger this error-handling path.
Douglas Gregorce3a8292010-07-27 00:27:13 +00001249 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor08288f22010-04-09 15:54:22 +00001250#endif
Douglas Gregorce3a8292010-07-27 00:27:13 +00001251 )) {
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001252 Diag(diag::err_fe_pch_file_modified)
1253 << Filename;
1254 return Failure;
1255 }
1256
Chris Lattner26b5c192010-11-23 09:19:42 +00001257 FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]),
1258 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor258ae542009-04-27 06:38:32 +00001259 ID, Record[0]);
1260 if (Record[3])
1261 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1262 .setHasLineDirectives();
1263
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001264 // Reconstruct header-search information for this file.
1265 HeaderFileInfo HFI;
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001266 HFI.isImport = Record[6];
1267 HFI.DirInfo = Record[7];
1268 HFI.NumIncludes = Record[8];
1269 HFI.ControllingMacroID = Record[9];
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001270 if (Listener)
1271 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor258ae542009-04-27 06:38:32 +00001272 break;
1273 }
1274
Sebastian Redl539c5062010-08-18 23:57:32 +00001275 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor258ae542009-04-27 06:38:32 +00001276 const char *Name = BlobStart;
1277 unsigned Offset = Record[0];
1278 unsigned Code = SLocEntryCursor.ReadCode();
1279 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001280 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +00001281 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001282
Sebastian Redl539c5062010-08-18 23:57:32 +00001283 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001284 Error("AST record has invalid code");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001285 return Failure;
1286 }
1287
Douglas Gregor258ae542009-04-27 06:38:32 +00001288 llvm::MemoryBuffer *Buffer
Chris Lattner58c79342010-04-05 22:42:27 +00001289 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1290 Name);
Douglas Gregor258ae542009-04-27 06:38:32 +00001291 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump11289f42009-09-09 15:08:12 +00001292
Douglas Gregore6648fb2009-04-28 20:33:11 +00001293 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001294 PCHPredefinesBlock Block = {
1295 BufferID,
1296 llvm::StringRef(BlobStart, BlobLen - 1)
1297 };
1298 PCHPredefinesBuffers.push_back(Block);
Douglas Gregore6648fb2009-04-28 20:33:11 +00001299 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001300
1301 break;
1302 }
1303
Sebastian Redl539c5062010-08-18 23:57:32 +00001304 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001305 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001306 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001307 ReadSourceLocation(*F, Record[2]),
1308 ReadSourceLocation(*F, Record[3]),
Douglas Gregor258ae542009-04-27 06:38:32 +00001309 Record[4],
1310 ID,
1311 Record[0]);
1312 break;
Mike Stump11289f42009-09-09 15:08:12 +00001313 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001314 }
1315
1316 return Success;
1317}
1318
Chris Lattnere78a6be2009-04-27 01:05:14 +00001319/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1320/// specified cursor. Read the abbreviations that are at the top of the block
1321/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001322bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001323 unsigned BlockID) {
1324 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001325 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001326 return Failure;
1327 }
Mike Stump11289f42009-09-09 15:08:12 +00001328
Chris Lattnere78a6be2009-04-27 01:05:14 +00001329 while (true) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001330 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattnere78a6be2009-04-27 01:05:14 +00001331 unsigned Code = Cursor.ReadCode();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001332
Chris Lattnere78a6be2009-04-27 01:05:14 +00001333 // We expect all abbrevs to be at the start of the block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001334 if (Code != llvm::bitc::DEFINE_ABBREV) {
1335 Cursor.JumpToBit(Offset);
Chris Lattnere78a6be2009-04-27 01:05:14 +00001336 return false;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001337 }
Chris Lattnere78a6be2009-04-27 01:05:14 +00001338 Cursor.ReadAbbrevRecord();
1339 }
1340}
1341
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001342PreprocessedEntity *ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001343 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregor796d76a2010-10-20 22:00:55 +00001344 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump11289f42009-09-09 15:08:12 +00001345
Douglas Gregorc3366a52009-04-21 23:56:24 +00001346 // Keep track of where we are in the stream, then jump back there
1347 // after reading this macro.
1348 SavedStreamPosition SavedPosition(Stream);
1349
1350 Stream.JumpToBit(Offset);
1351 RecordData Record;
1352 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1353 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001354
Douglas Gregorc3366a52009-04-21 23:56:24 +00001355 while (true) {
1356 unsigned Code = Stream.ReadCode();
1357 switch (Code) {
1358 case llvm::bitc::END_BLOCK:
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001359 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001360
1361 case llvm::bitc::ENTER_SUBBLOCK:
1362 // No known subblocks, always skip them.
1363 Stream.ReadSubBlockID();
1364 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001365 Error("malformed block record in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001366 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001367 }
1368 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001369
Douglas Gregorc3366a52009-04-21 23:56:24 +00001370 case llvm::bitc::DEFINE_ABBREV:
1371 Stream.ReadAbbrevRecord();
1372 continue;
1373 default: break;
1374 }
1375
1376 // Read a record.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001377 const char *BlobStart = 0;
1378 unsigned BlobLen = 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001379 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001380 PreprocessorRecordTypes RecType =
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001381 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001382 BlobLen);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001383 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001384 case PP_MACRO_OBJECT_LIKE:
1385 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001386 // If we already have a macro, that means that we've hit the end
1387 // of the definition of the macro we were looking for. We're
1388 // done.
1389 if (Macro)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001390 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001391
1392 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1393 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001394 Error("macro must have a name in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001395 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001396 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00001397 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001398 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001399
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001400 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001401 MI->setIsUsed(isUsed);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001402 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001403
Douglas Gregoraae92242010-03-19 21:51:54 +00001404 unsigned NextIndex = 3;
Sebastian Redl539c5062010-08-18 23:57:32 +00001405 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001406 // Decode function-like macro info.
1407 bool isC99VarArgs = Record[3];
1408 bool isGNUVarArgs = Record[4];
1409 MacroArgs.clear();
1410 unsigned NumArgs = Record[5];
Douglas Gregoraae92242010-03-19 21:51:54 +00001411 NextIndex = 6 + NumArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001412 for (unsigned i = 0; i != NumArgs; ++i)
1413 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1414
1415 // Install function-like macro info.
1416 MI->setIsFunctionLike();
1417 if (isC99VarArgs) MI->setIsC99Varargs();
1418 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001419 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001420 PP->getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001421 }
1422
1423 // Finally, install the macro.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001424 PP->setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001425
1426 // Remember that we saw this macro last so that we add the tokens that
1427 // form its body to it.
1428 Macro = MI;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001429
Douglas Gregoraae92242010-03-19 21:51:54 +00001430 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1431 // We have a macro definition. Load it now.
1432 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1433 getMacroDefinition(Record[NextIndex]));
1434 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001435
Douglas Gregorc3366a52009-04-21 23:56:24 +00001436 ++NumMacrosRead;
1437 break;
1438 }
Mike Stump11289f42009-09-09 15:08:12 +00001439
Sebastian Redl539c5062010-08-18 23:57:32 +00001440 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001441 // If we see a TOKEN before a PP_MACRO_*, then the file is
1442 // erroneous, just pretend we didn't see this.
1443 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001444
Douglas Gregorc3366a52009-04-21 23:56:24 +00001445 Token Tok;
1446 Tok.startToken();
Sebastian Redl2c373b92010-10-05 15:59:54 +00001447 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001448 Tok.setLength(Record[1]);
1449 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1450 Tok.setIdentifierInfo(II);
1451 Tok.setKind((tok::TokenKind)Record[3]);
1452 Tok.setFlag((Token::TokenFlags)Record[4]);
1453 Macro->AddTokenToBody(Tok);
1454 break;
1455 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001456
Sebastian Redl539c5062010-08-18 23:57:32 +00001457 case PP_MACRO_INSTANTIATION: {
Douglas Gregoraae92242010-03-19 21:51:54 +00001458 // If we already have a macro, that means that we've hit the end
1459 // of the definition of the macro we were looking for. We're
1460 // done.
1461 if (Macro)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001462 return 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001463
Douglas Gregoraae92242010-03-19 21:51:54 +00001464 if (!PP->getPreprocessingRecord()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001465 Error("missing preprocessing record in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001466 return 0;
Douglas Gregoraae92242010-03-19 21:51:54 +00001467 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001468
Douglas Gregoraae92242010-03-19 21:51:54 +00001469 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001470 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1471 return PE;
Douglas Gregoraae92242010-03-19 21:51:54 +00001472
1473 MacroInstantiation *MI
1474 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
Sebastian Redl2c373b92010-10-05 15:59:54 +00001475 SourceRange(ReadSourceLocation(F, Record[1]),
1476 ReadSourceLocation(F, Record[2])),
Douglas Gregoraae92242010-03-19 21:51:54 +00001477 getMacroDefinition(Record[4]));
1478 PPRec.SetPreallocatedEntity(Record[0], MI);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001479 return MI;
Douglas Gregoraae92242010-03-19 21:51:54 +00001480 }
1481
Sebastian Redl539c5062010-08-18 23:57:32 +00001482 case PP_MACRO_DEFINITION: {
Douglas Gregoraae92242010-03-19 21:51:54 +00001483 // If we already have a macro, that means that we've hit the end
1484 // of the definition of the macro we were looking for. We're
1485 // done.
1486 if (Macro)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001487 return 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001488
Douglas Gregoraae92242010-03-19 21:51:54 +00001489 if (!PP->getPreprocessingRecord()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001490 Error("missing preprocessing record in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001491 return 0;
Douglas Gregoraae92242010-03-19 21:51:54 +00001492 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001493
Douglas Gregoraae92242010-03-19 21:51:54 +00001494 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001495 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1496 return PE;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001497
Douglas Gregor91096292010-10-02 19:29:26 +00001498 if (Record[1] > MacroDefinitionsLoaded.size()) {
Douglas Gregoraae92242010-03-19 21:51:54 +00001499 Error("out-of-bounds macro definition record");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001500 return 0;
Douglas Gregoraae92242010-03-19 21:51:54 +00001501 }
1502
Douglas Gregor91096292010-10-02 19:29:26 +00001503 // Decode the identifier info and then check again; if the macro is
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001504 // still defined and associated with the identifier,
Douglas Gregor91096292010-10-02 19:29:26 +00001505 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1506 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1507 MacroDefinition *MD
1508 = new (PPRec) MacroDefinition(II,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001509 ReadSourceLocation(F, Record[5]),
Douglas Gregor36ea4d42010-10-01 20:33:34 +00001510 SourceRange(
Sebastian Redl2c373b92010-10-05 15:59:54 +00001511 ReadSourceLocation(F, Record[2]),
1512 ReadSourceLocation(F, Record[3])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001513
Douglas Gregor91096292010-10-02 19:29:26 +00001514 PPRec.SetPreallocatedEntity(Record[0], MD);
1515 MacroDefinitionsLoaded[Record[1] - 1] = MD;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001516
Douglas Gregor91096292010-10-02 19:29:26 +00001517 if (DeserializationListener)
1518 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1519 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001520
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001521 return MacroDefinitionsLoaded[Record[1] - 1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001522 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001523
Douglas Gregor796d76a2010-10-20 22:00:55 +00001524 case PP_INCLUSION_DIRECTIVE: {
1525 // If we already have a macro, that means that we've hit the end
1526 // of the definition of the macro we were looking for. We're
1527 // done.
1528 if (Macro)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001529 return 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001530
Douglas Gregor796d76a2010-10-20 22:00:55 +00001531 if (!PP->getPreprocessingRecord()) {
1532 Error("missing preprocessing record in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001533 return 0;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001534 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001535
Douglas Gregor796d76a2010-10-20 22:00:55 +00001536 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001537 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1538 return PE;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001539
1540 const char *FullFileNameStart = BlobStart + Record[3];
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001541 const FileEntry *File
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001542 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
1543 BlobLen - Record[3]));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001544
Douglas Gregor796d76a2010-10-20 22:00:55 +00001545 // FIXME: Stable encoding
1546 InclusionDirective::InclusionKind Kind
1547 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1548 InclusionDirective *ID
Douglas Gregorf09b6c92010-11-01 15:03:47 +00001549 = new (PPRec) InclusionDirective(PPRec, Kind,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001550 llvm::StringRef(BlobStart, Record[3]),
1551 Record[4],
1552 File,
1553 SourceRange(ReadSourceLocation(F, Record[1]),
1554 ReadSourceLocation(F, Record[2])));
1555 PPRec.SetPreallocatedEntity(Record[0], ID);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001556 return ID;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001557 }
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001558 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001559 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001560
1561 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001562}
1563
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001564void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1565 uint64_t Offset) {
1566 // Note that this identifier has a macro definition.
1567 II->setHasMacroDefinition(true);
1568
1569 // Adjust the offset based on our position in the chain.
1570 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1571 if (Chain[I] == &F)
1572 break;
1573
1574 Offset += Chain[I]->SizeInBits;
1575 }
1576
1577 UnreadMacroRecordOffsets[II] = Offset;
1578}
1579
Sebastian Redl2c499f62010-08-18 23:56:43 +00001580void ASTReader::ReadDefinedMacros() {
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001581 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001582 PerFileData &F = *Chain[N - I - 1];
1583 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001584
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001585 // If there was no preprocessor block, skip this file.
1586 if (!MacroCursor.getBitStreamReader())
1587 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001588
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001589 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001590 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001591
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001592 RecordData Record;
1593 while (true) {
Sebastian Redl4102dd52010-09-28 02:55:49 +00001594 uint64_t Offset = Cursor.GetCurrentBitNo();
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001595 unsigned Code = Cursor.ReadCode();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001596 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001597 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001598
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001599 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1600 // No known subblocks, always skip them.
1601 Cursor.ReadSubBlockID();
1602 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001603 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001604 return;
1605 }
1606 continue;
1607 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001608
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001609 if (Code == llvm::bitc::DEFINE_ABBREV) {
1610 Cursor.ReadAbbrevRecord();
1611 continue;
1612 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001613
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001614 // Read a record.
1615 const char *BlobStart;
1616 unsigned BlobLen;
1617 Record.clear();
1618 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1619 default: // Default behavior: ignore.
1620 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001621
Sebastian Redl539c5062010-08-18 23:57:32 +00001622 case PP_MACRO_OBJECT_LIKE:
1623 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001624 DecodeIdentifierInfo(Record[0]);
1625 break;
1626
Sebastian Redl539c5062010-08-18 23:57:32 +00001627 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001628 // Ignore tokens.
1629 break;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001630
Sebastian Redl539c5062010-08-18 23:57:32 +00001631 case PP_MACRO_INSTANTIATION:
1632 case PP_MACRO_DEFINITION:
Douglas Gregor796d76a2010-10-20 22:00:55 +00001633 case PP_INCLUSION_DIRECTIVE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001634 // Read the macro record.
Sebastian Redl4102dd52010-09-28 02:55:49 +00001635 // FIXME: That's a stupid way to do this. We should reuse this cursor.
Sebastian Redl2c373b92010-10-05 15:59:54 +00001636 ReadMacroRecord(F, Offset);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001637 break;
1638 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001639 }
1640 }
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001641
1642 // Drain the unread macro-record offsets map.
1643 while (!UnreadMacroRecordOffsets.empty())
1644 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1645}
1646
1647void ASTReader::LoadMacroDefinition(
1648 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1649 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1650 PerFileData *F = 0;
1651 uint64_t Offset = Pos->second;
1652 UnreadMacroRecordOffsets.erase(Pos);
1653
1654 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1655 if (Offset < Chain[I]->SizeInBits) {
1656 F = Chain[I];
1657 break;
1658 }
1659
1660 Offset -= Chain[I]->SizeInBits;
1661 }
1662 if (!F) {
1663 Error("Malformed macro record offset");
1664 return;
1665 }
1666
1667 ReadMacroRecord(*F, Offset);
1668}
1669
1670void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1671 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1672 = UnreadMacroRecordOffsets.find(II);
1673 LoadMacroDefinition(Pos);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001674}
1675
Sebastian Redl50e26582010-09-15 19:54:06 +00001676MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor91096292010-10-02 19:29:26 +00001677 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregoraae92242010-03-19 21:51:54 +00001678 return 0;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001679
Douglas Gregor91096292010-10-02 19:29:26 +00001680 if (!MacroDefinitionsLoaded[ID - 1]) {
1681 unsigned Index = ID - 1;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001682 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1683 PerFileData &F = *Chain[N - I - 1];
1684 if (Index < F.LocalNumMacroDefinitions) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001685 ReadMacroRecord(F, F.MacroDefinitionOffsets[Index]);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001686 break;
1687 }
1688 Index -= F.LocalNumMacroDefinitions;
1689 }
Douglas Gregor91096292010-10-02 19:29:26 +00001690 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001691 }
1692
Douglas Gregor91096292010-10-02 19:29:26 +00001693 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001694}
1695
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001696/// \brief If we are loading a relocatable PCH file, and the filename is
1697/// not an absolute path, add the system root to the beginning of the file
1698/// name.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001699void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001700 // If this is not a relocatable PCH file, there's nothing to do.
1701 if (!RelocatablePCH)
1702 return;
Mike Stump11289f42009-09-09 15:08:12 +00001703
Daniel Dunbarf2ce9a22009-11-18 19:50:41 +00001704 if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001705 return;
1706
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001707 if (isysroot == 0) {
1708 // If no system root was given, default to '/'
1709 Filename.insert(Filename.begin(), '/');
1710 return;
1711 }
Mike Stump11289f42009-09-09 15:08:12 +00001712
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001713 unsigned Length = strlen(isysroot);
1714 if (isysroot[Length - 1] != '/')
1715 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001716
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001717 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1718}
1719
Sebastian Redl2c499f62010-08-18 23:56:43 +00001720ASTReader::ASTReadResult
Sebastian Redl3e31c722010-08-18 23:56:56 +00001721ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001722 llvm::BitstreamCursor &Stream = F.Stream;
1723
Sebastian Redl539c5062010-08-18 23:57:32 +00001724 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001725 Error("malformed block record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001726 return Failure;
1727 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001728
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001729 // Read all of the records and blocks for the ASt file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001730 RecordData Record;
Sebastian Redl393f8b72010-07-19 20:52:06 +00001731 bool First = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001732 while (!Stream.AtEndOfStream()) {
1733 unsigned Code = Stream.ReadCode();
1734 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001735 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001736 Error("error at end of module block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001737 return Failure;
1738 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001739
Douglas Gregor55abb232009-04-10 20:39:37 +00001740 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001741 }
1742
1743 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1744 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001745 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001746 // We lazily load the decls block, but we want to set up the
1747 // DeclsCursor cursor to point into it. Clone our current bitcode
1748 // cursor to it, enter the block and read the abbrevs in that block.
1749 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001750 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001751 if (Stream.SkipBlock() || // Skip with the main cursor.
1752 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001753 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001754 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001755 return Failure;
1756 }
1757 break;
Mike Stump11289f42009-09-09 15:08:12 +00001758
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00001759 case DECL_UPDATES_BLOCK_ID:
1760 if (Stream.SkipBlock()) {
1761 Error("malformed block record in AST file");
1762 return Failure;
1763 }
1764 break;
1765
Sebastian Redl539c5062010-08-18 23:57:32 +00001766 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001767 F.MacroCursor = Stream;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001768 if (PP)
1769 PP->setExternalSource(this);
1770
Douglas Gregor796d76a2010-10-20 22:00:55 +00001771 if (Stream.SkipBlock() ||
1772 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001773 Error("malformed block record in AST file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001774 return Failure;
1775 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001776 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001777 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001778
Sebastian Redl539c5062010-08-18 23:57:32 +00001779 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001780 switch (ReadSourceManagerBlock(F)) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001781 case Success:
1782 break;
1783
1784 case Failure:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001785 Error("malformed source manager block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001786 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001787
1788 case IgnorePCH:
1789 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001790 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001791 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001792 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00001793 First = false;
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001794 continue;
1795 }
1796
1797 if (Code == llvm::bitc::DEFINE_ABBREV) {
1798 Stream.ReadAbbrevRecord();
1799 continue;
1800 }
1801
1802 // Read and process a record.
1803 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001804 const char *BlobStart = 0;
1805 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001806 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001807 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001808 default: // Default behavior: ignore.
1809 break;
1810
Sebastian Redl539c5062010-08-18 23:57:32 +00001811 case METADATA: {
1812 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1813 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001814 : diag::warn_pch_version_too_new);
1815 return IgnorePCH;
1816 }
1817
1818 RelocatablePCH = Record[4];
1819 if (Listener) {
1820 std::string TargetTriple(BlobStart, BlobLen);
1821 if (Listener->ReadTargetTriple(TargetTriple))
1822 return IgnorePCH;
1823 }
1824 break;
1825 }
1826
Sebastian Redl539c5062010-08-18 23:57:32 +00001827 case CHAINED_METADATA: {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001828 if (!First) {
1829 Error("CHAINED_METADATA is not first record in block");
1830 return Failure;
1831 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001832 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1833 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001834 : diag::warn_pch_version_too_new);
1835 return IgnorePCH;
1836 }
1837
Sebastian Redl009e7f22010-10-05 16:15:19 +00001838 // Load the chained file, which is always a PCH file.
1839 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001840 case Failure: return Failure;
1841 // If we have to ignore the dependency, we'll have to ignore this too.
1842 case IgnorePCH: return IgnorePCH;
1843 case Success: break;
1844 }
1845 break;
1846 }
1847
Sebastian Redl539c5062010-08-18 23:57:32 +00001848 case TYPE_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001849 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001850 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001851 return Failure;
1852 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001853 F.TypeOffsets = (const uint32_t *)BlobStart;
1854 F.LocalNumTypes = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001855 break;
1856
Sebastian Redl539c5062010-08-18 23:57:32 +00001857 case DECL_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001858 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001859 Error("duplicate DECL_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.DeclOffsets = (const uint32_t *)BlobStart;
1863 F.LocalNumDecls = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001864 break;
Douglas Gregor55abb232009-04-10 20:39:37 +00001865
Sebastian Redl539c5062010-08-18 23:57:32 +00001866 case TU_UPDATE_LEXICAL: {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001867 DeclContextInfo Info = {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001868 /* No visible information */ 0,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001869 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
1870 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001871 };
Douglas Gregoraa433012010-10-01 01:18:02 +00001872 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
1873 .push_back(Info);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001874 break;
1875 }
1876
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001877 case UPDATE_VISIBLE: {
1878 serialization::DeclID ID = Record[0];
1879 void *Table = ASTDeclContextNameLookupTable::Create(
1880 (const unsigned char *)BlobStart + Record[1],
1881 (const unsigned char *)BlobStart,
1882 ASTDeclContextNameLookupTrait(*this));
Douglas Gregoraa433012010-10-01 01:18:02 +00001883 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001884 DeclContextInfo Info = {
1885 Table, /* No lexical inforamtion */ 0, 0
1886 };
1887 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1888 } else
1889 PendingVisibleUpdates[ID].push_back(Table);
1890 break;
1891 }
1892
Sebastian Redl539c5062010-08-18 23:57:32 +00001893 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001894 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1895 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001896 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001897 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1898 Latest > FirstLatestDeclIDs[First]) &&
1899 "The new latest is supposed to come after the previous latest");
1900 FirstLatestDeclIDs[First] = Latest;
1901 }
1902 break;
1903 }
1904
Sebastian Redl539c5062010-08-18 23:57:32 +00001905 case LANGUAGE_OPTIONS:
Douglas Gregorce3a8292010-07-27 00:27:13 +00001906 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor55abb232009-04-10 20:39:37 +00001907 return IgnorePCH;
1908 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00001909
Sebastian Redl539c5062010-08-18 23:57:32 +00001910 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001911 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00001912 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001913 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001914 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00001915 (const unsigned char *)F.IdentifierTableData + Record[0],
1916 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001917 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001918 if (PP)
1919 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00001920 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001921 break;
1922
Sebastian Redl539c5062010-08-18 23:57:32 +00001923 case IDENTIFIER_OFFSET:
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001924 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001925 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001926 return Failure;
1927 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001928 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1929 F.LocalNumIdentifiers = Record[0];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001930 break;
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001931
Sebastian Redl539c5062010-08-18 23:57:32 +00001932 case EXTERNAL_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001933 // Optimization for the first block.
1934 if (ExternalDefinitions.empty())
1935 ExternalDefinitions.swap(Record);
1936 else
1937 ExternalDefinitions.insert(ExternalDefinitions.end(),
1938 Record.begin(), Record.end());
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001939 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00001940
Sebastian Redl539c5062010-08-18 23:57:32 +00001941 case SPECIAL_TYPES:
Sebastian Redlb293a452010-07-20 21:20:32 +00001942 // Optimization for the first block
1943 if (SpecialTypes.empty())
1944 SpecialTypes.swap(Record);
1945 else
1946 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregor652d82a2009-04-18 05:55:16 +00001947 break;
1948
Sebastian Redl539c5062010-08-18 23:57:32 +00001949 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001950 TotalNumStatements += Record[0];
1951 TotalNumMacros += Record[1];
1952 TotalLexicalDeclContexts += Record[2];
1953 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00001954 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00001955
Sebastian Redl539c5062010-08-18 23:57:32 +00001956 case TENTATIVE_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001957 // Optimization for the first block.
1958 if (TentativeDefinitions.empty())
1959 TentativeDefinitions.swap(Record);
1960 else
1961 TentativeDefinitions.insert(TentativeDefinitions.end(),
1962 Record.begin(), Record.end());
Douglas Gregord4df8652009-04-22 22:02:47 +00001963 break;
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001964
Sebastian Redl539c5062010-08-18 23:57:32 +00001965 case UNUSED_FILESCOPED_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001966 // Optimization for the first block.
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00001967 if (UnusedFileScopedDecls.empty())
1968 UnusedFileScopedDecls.swap(Record);
Sebastian Redlb293a452010-07-20 21:20:32 +00001969 else
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00001970 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1971 Record.begin(), Record.end());
Tanya Lattner90073802010-02-12 00:07:30 +00001972 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001973
Sebastian Redl539c5062010-08-18 23:57:32 +00001974 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001975 // Later blocks overwrite earlier ones.
1976 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00001977 break;
1978
Sebastian Redl539c5062010-08-18 23:57:32 +00001979 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001980 // Optimization for the first block.
1981 if (LocallyScopedExternalDecls.empty())
1982 LocallyScopedExternalDecls.swap(Record);
1983 else
1984 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1985 Record.begin(), Record.end());
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001986 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001987
Sebastian Redl539c5062010-08-18 23:57:32 +00001988 case SELECTOR_OFFSETS:
Sebastian Redla19a67f2010-08-03 21:58:15 +00001989 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00001990 F.LocalNumSelectors = Record[0];
Douglas Gregor95c13f52009-04-25 17:48:32 +00001991 break;
1992
Sebastian Redl539c5062010-08-18 23:57:32 +00001993 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00001994 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001995 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00001996 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001997 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00001998 F.SelectorLookupTableData + Record[0],
1999 F.SelectorLookupTableData,
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002000 ASTSelectorLookupTrait(*this));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002001 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002002 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00002003
Sebastian Redl96371b42010-09-22 00:42:30 +00002004 case REFERENCED_SELECTOR_POOL:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002005 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002006 break;
2007
Sebastian Redl539c5062010-08-18 23:57:32 +00002008 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002009 if (!Record.empty() && Listener)
2010 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002011 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002012
Sebastian Redl539c5062010-08-18 23:57:32 +00002013 case SOURCE_LOCATION_OFFSETS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002014 F.SLocOffsets = (const uint32_t *)BlobStart;
2015 F.LocalNumSLocEntries = Record[0];
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002016 F.LocalSLocSize = Record[1];
Douglas Gregor258ae542009-04-27 06:38:32 +00002017 break;
2018
Sebastian Redl539c5062010-08-18 23:57:32 +00002019 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl96371b42010-09-22 00:42:30 +00002020 if (PreloadSLocEntries.empty())
2021 PreloadSLocEntries.swap(Record);
2022 else
2023 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2024 Record.begin(), Record.end());
Douglas Gregor258ae542009-04-27 06:38:32 +00002025 break;
Douglas Gregorc5046832009-04-27 18:38:38 +00002026
Sebastian Redl539c5062010-08-18 23:57:32 +00002027 case STAT_CACHE: {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002028 ASTStatCache *MyStatCache =
2029 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
Douglas Gregord2eb58a2009-10-16 18:18:30 +00002030 (const unsigned char *)BlobStart,
2031 NumStatHits, NumStatMisses);
2032 FileMgr.addStatCache(MyStatCache);
Sebastian Redl34522812010-07-16 17:50:48 +00002033 F.StatCache = MyStatCache;
Douglas Gregorc5046832009-04-27 18:38:38 +00002034 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00002035 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002036
Sebastian Redl539c5062010-08-18 23:57:32 +00002037 case EXT_VECTOR_DECLS:
Sebastian Redl04f5c312010-07-28 21:38:49 +00002038 // Optimization for the first block.
2039 if (ExtVectorDecls.empty())
2040 ExtVectorDecls.swap(Record);
2041 else
2042 ExtVectorDecls.insert(ExtVectorDecls.end(),
2043 Record.begin(), Record.end());
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002044 break;
2045
Sebastian Redl539c5062010-08-18 23:57:32 +00002046 case VTABLE_USES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002047 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002048 VTableUses.swap(Record);
2049 break;
2050
Sebastian Redl539c5062010-08-18 23:57:32 +00002051 case DYNAMIC_CLASSES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002052 // Optimization for the first block.
2053 if (DynamicClasses.empty())
2054 DynamicClasses.swap(Record);
2055 else
2056 DynamicClasses.insert(DynamicClasses.end(),
2057 Record.begin(), Record.end());
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002058 break;
2059
Sebastian Redl539c5062010-08-18 23:57:32 +00002060 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002061 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002062 break;
2063
Sebastian Redl539c5062010-08-18 23:57:32 +00002064 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002065 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002066 SemaDeclRefs.swap(Record);
2067 break;
2068
Sebastian Redl539c5062010-08-18 23:57:32 +00002069 case ORIGINAL_FILE_NAME:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002070 // The primary AST will be the last to get here, so it will be the one
Sebastian Redlb293a452010-07-20 21:20:32 +00002071 // that's used.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00002072 ActualOriginalFileName.assign(BlobStart, BlobLen);
2073 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002074 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00002075 break;
Mike Stump11289f42009-09-09 15:08:12 +00002076
Sebastian Redl539c5062010-08-18 23:57:32 +00002077 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00002078 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002079 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2080 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2081 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregord54f3a12009-10-05 21:07:28 +00002082 return IgnorePCH;
2083 }
2084 break;
2085 }
Sebastian Redlfa061442010-07-21 20:07:32 +00002086
Sebastian Redl539c5062010-08-18 23:57:32 +00002087 case MACRO_DEFINITION_OFFSETS:
Sebastian Redlfa061442010-07-21 20:07:32 +00002088 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2089 F.NumPreallocatedPreprocessingEntities = Record[0];
2090 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregoraae92242010-03-19 21:51:54 +00002091 break;
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002092
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002093 case DECL_UPDATE_OFFSETS: {
2094 if (Record.size() % 2 != 0) {
2095 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2096 return Failure;
2097 }
2098 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2099 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2100 .push_back(std::make_pair(&F, Record[I+1]));
2101 break;
2102 }
2103
Sebastian Redl539c5062010-08-18 23:57:32 +00002104 case DECL_REPLACEMENTS: {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002105 if (Record.size() % 2 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002106 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002107 return Failure;
2108 }
2109 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl539c5062010-08-18 23:57:32 +00002110 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002111 std::make_pair(&F, Record[I+1]);
2112 break;
2113 }
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002114
2115 case CXX_BASE_SPECIFIER_OFFSETS: {
2116 if (F.LocalNumCXXBaseSpecifiers != 0) {
2117 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2118 return Failure;
2119 }
2120
2121 F.LocalNumCXXBaseSpecifiers = Record[0];
2122 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2123 break;
2124 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002125
2126 case DIAG_USER_MAPPINGS:
2127 if (Record.size() % 2 != 0) {
2128 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2129 return Failure;
2130 }
2131 if (UserDiagMappings.empty())
2132 UserDiagMappings.swap(Record);
2133 else
2134 UserDiagMappings.insert(UserDiagMappings.end(),
2135 Record.begin(), Record.end());
2136 break;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002137 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00002138 First = false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002139 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002140 Error("premature end of bitstream in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002141 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002142}
2143
Sebastian Redl009e7f22010-10-05 16:15:19 +00002144ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2145 ASTFileType Type) {
2146 switch(ReadASTCore(FileName, Type)) {
Sebastian Redl2abc0382010-07-16 20:41:52 +00002147 case Failure: return Failure;
2148 case IgnorePCH: return IgnorePCH;
2149 case Success: break;
2150 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002151
2152 // Here comes stuff that we only do once the entire chain is loaded.
2153
Sebastian Redl96371b42010-09-22 00:42:30 +00002154 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redlfa061442010-07-21 20:07:32 +00002155 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redlada023c2010-08-04 20:40:17 +00002156 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2157 TotalNumSelectors = 0;
Sebastian Redl9e687992010-07-19 22:06:55 +00002158 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl96371b42010-09-22 00:42:30 +00002159 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002160 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002161 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl9e687992010-07-19 22:06:55 +00002162 TotalNumTypes += Chain[I]->LocalNumTypes;
2163 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redlfa061442010-07-21 20:07:32 +00002164 TotalNumPreallocatedPreprocessingEntities +=
2165 Chain[I]->NumPreallocatedPreprocessingEntities;
2166 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redlada023c2010-08-04 20:40:17 +00002167 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl9e687992010-07-19 22:06:55 +00002168 }
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002169 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002170 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl9e687992010-07-19 22:06:55 +00002171 TypesLoaded.resize(TotalNumTypes);
2172 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redlfa061442010-07-21 20:07:32 +00002173 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2174 if (PP) {
2175 if (TotalNumIdentifiers > 0)
2176 PP->getHeaderSearchInfo().SetExternalLookup(this);
2177 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2178 if (!PP->getPreprocessingRecord())
2179 PP->createPreprocessingRecord();
2180 PP->getPreprocessingRecord()->SetExternalSource(*this,
2181 TotalNumPreallocatedPreprocessingEntities);
2182 }
2183 }
Sebastian Redlada023c2010-08-04 20:40:17 +00002184 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl96371b42010-09-22 00:42:30 +00002185 // Preload SLocEntries.
2186 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2187 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2188 if (Result != Success)
2189 return Result;
2190 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002191
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002192 // Check the predefines buffers.
Douglas Gregorce3a8292010-07-27 00:27:13 +00002193 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002194 return IgnorePCH;
2195
2196 if (PP) {
2197 // Initialization of keywords and pragmas occurs before the
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002198 // AST file is read, so there may be some identifiers that were
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002199 // loaded into the IdentifierTable before we intercepted the
2200 // creation of identifiers. Iterate through the list of known
2201 // identifiers and determine whether we have to establish
2202 // preprocessor definitions or top-level identifier declaration
2203 // chains for those identifiers.
2204 //
2205 // We copy the IdentifierInfo pointers to a small vector first,
2206 // since de-serializing declarations or macro definitions can add
2207 // new entries into the identifier table, invalidating the
2208 // iterators.
2209 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2210 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2211 IdEnd = PP->getIdentifierTable().end();
2212 Id != IdEnd; ++Id)
2213 Identifiers.push_back(Id->second);
Sebastian Redlfa061442010-07-21 20:07:32 +00002214 // We need to search the tables in all files.
Sebastian Redlfa061442010-07-21 20:07:32 +00002215 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002216 ASTIdentifierLookupTable *IdTable
2217 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2218 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl5c415f32010-07-22 17:01:13 +00002219 // ones.
2220 if (!IdTable)
2221 continue;
Sebastian Redlfa061442010-07-21 20:07:32 +00002222 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2223 IdentifierInfo *II = Identifiers[I];
2224 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00002225 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redlfa061442010-07-21 20:07:32 +00002226 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002227 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redlb293a452010-07-20 21:20:32 +00002228 if (Pos == IdTable->end())
2229 continue;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002230
Sebastian Redlb293a452010-07-20 21:20:32 +00002231 // Dereferencing the iterator has the effect of populating the
2232 // IdentifierInfo node with the various declarations it needs.
2233 (void)*Pos;
2234 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002235 }
2236 }
2237
2238 if (Context)
2239 InitializeContext(*Context);
2240
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002241 if (DeserializationListener)
2242 DeserializationListener->ReaderInitialized(this);
2243
Douglas Gregor936a5b42010-11-30 05:23:00 +00002244 // If this AST file is a precompiled preamble, then set the main file ID of
2245 // the source manager to the file source file from which the preamble was
2246 // built. This is the only valid way to use a precompiled preamble.
2247 if (Type == Preamble) {
2248 SourceLocation Loc
2249 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2250 if (Loc.isValid()) {
2251 std::pair<FileID, unsigned> Decomposed = SourceMgr.getDecomposedLoc(Loc);
2252 SourceMgr.SetPreambleFileID(Decomposed.first);
2253 }
2254 }
2255
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002256 return Success;
2257}
2258
Sebastian Redl009e7f22010-10-05 16:15:19 +00002259ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2260 ASTFileType Type) {
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002261 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl009e7f22010-10-05 16:15:19 +00002262 Chain.push_back(new PerFileData(Type));
Sebastian Redl34522812010-07-16 17:50:48 +00002263 PerFileData &F = *Chain.back();
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002264 if (Prev)
2265 Prev->NextInSource = &F;
2266 else
2267 FirstInSource = &F;
2268 F.Loaders.push_back(Prev);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002269
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002270 // Set the AST file name.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002271 F.FileName = FileName;
2272
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002273 // Open the AST file.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002274 //
2275 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2276 std::string ErrStr;
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00002277 if (FileName == "-")
2278 F.Buffer.reset(llvm::MemoryBuffer::getSTDIN(&ErrStr));
2279 else
Chris Lattner5159f612010-11-23 08:35:12 +00002280 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002281 if (!F.Buffer) {
2282 Error(ErrStr.c_str());
2283 return IgnorePCH;
2284 }
2285
2286 // Initialize the stream
2287 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2288 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl34522812010-07-16 17:50:48 +00002289 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002290 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002291 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002292
2293 // Sniff for the signature.
2294 if (Stream.Read(8) != 'C' ||
2295 Stream.Read(8) != 'P' ||
2296 Stream.Read(8) != 'C' ||
2297 Stream.Read(8) != 'H') {
2298 Diag(diag::err_not_a_pch_file) << FileName;
2299 return Failure;
2300 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002301
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002302 while (!Stream.AtEndOfStream()) {
2303 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002304
Douglas Gregor92863e42009-04-10 23:10:45 +00002305 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002306 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002307 return Failure;
2308 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002309
2310 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002311
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002312 // We only know the AST subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002313 switch (BlockID) {
2314 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002315 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002316 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002317 return Failure;
2318 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002319 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00002320 case AST_BLOCK_ID:
Sebastian Redl3e31c722010-08-18 23:56:56 +00002321 switch (ReadASTBlock(F)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002322 case Success:
2323 break;
2324
2325 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00002326 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00002327
2328 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00002329 // FIXME: We could consider reading through to the end of this
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002330 // AST block, skipping subblocks, to see if there are other
2331 // AST blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00002332
2333 // Clear out any preallocated source location entries, so that
2334 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002335 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00002336
2337 // Remove the stat cache.
Sebastian Redl34522812010-07-16 17:50:48 +00002338 if (F.StatCache)
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002339 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00002340
Douglas Gregor92863e42009-04-10 23:10:45 +00002341 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002342 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002343 break;
2344 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002345 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002346 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002347 return Failure;
2348 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002349 break;
2350 }
Mike Stump11289f42009-09-09 15:08:12 +00002351 }
2352
Sebastian Redl2abc0382010-07-16 20:41:52 +00002353 return Success;
2354}
2355
Sebastian Redl2c499f62010-08-18 23:56:43 +00002356void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002357 PP = &pp;
Sebastian Redlfa061442010-07-21 20:07:32 +00002358
2359 unsigned TotalNum = 0;
2360 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2361 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2362 if (TotalNum) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002363 if (!PP->getPreprocessingRecord())
2364 PP->createPreprocessingRecord();
Sebastian Redlfa061442010-07-21 20:07:32 +00002365 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregoraae92242010-03-19 21:51:54 +00002366 }
2367}
2368
Sebastian Redl2c499f62010-08-18 23:56:43 +00002369void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002370 Context = &Ctx;
2371 assert(Context && "Passed null context!");
2372
2373 assert(PP && "Forgot to set Preprocessor ?");
2374 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2375 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00002376 PP->setExternalSource(this);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002377
Douglas Gregoraa433012010-10-01 01:18:02 +00002378 // If we have an update block for the TU waiting, we have to add it before
2379 // deserializing the decl.
2380 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2381 if (DCU != DeclContextOffsets.end()) {
2382 // Insertion could invalidate map, so grab vector.
2383 DeclContextInfos T;
2384 T.swap(DCU->second);
2385 DeclContextOffsets.erase(DCU);
2386 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2387 }
2388
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002389 // Load the translation unit declaration
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00002390 GetTranslationUnitDecl();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002391
2392 // Load the special types.
2393 Context->setBuiltinVaListType(
Sebastian Redl539c5062010-08-18 23:57:32 +00002394 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2395 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002396 Context->setObjCIdType(GetType(Id));
Sebastian Redl539c5062010-08-18 23:57:32 +00002397 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002398 Context->setObjCSelType(GetType(Sel));
Sebastian Redl539c5062010-08-18 23:57:32 +00002399 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002400 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl539c5062010-08-18 23:57:32 +00002401 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002402 Context->setObjCClassType(GetType(Class));
Steve Naroff7cae42b2009-07-10 23:34:53 +00002403
Sebastian Redl539c5062010-08-18 23:57:32 +00002404 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002405 Context->setCFConstantStringType(GetType(String));
Mike Stump11289f42009-09-09 15:08:12 +00002406 if (unsigned FastEnum
Sebastian Redl539c5062010-08-18 23:57:32 +00002407 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002408 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl539c5062010-08-18 23:57:32 +00002409 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregor27821ce2009-07-07 16:35:42 +00002410 QualType FileType = GetType(File);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002411 if (FileType.isNull()) {
2412 Error("FILE type is NULL");
2413 return;
2414 }
John McCall9dd450b2009-09-21 23:43:11 +00002415 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregor27821ce2009-07-07 16:35:42 +00002416 Context->setFILEDecl(Typedef->getDecl());
2417 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002418 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002419 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002420 Error("Invalid FILE type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002421 return;
2422 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002423 Context->setFILEDecl(Tag->getDecl());
2424 }
2425 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002426 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002427 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002428 if (Jmp_bufType.isNull()) {
2429 Error("jmp_bug type is NULL");
2430 return;
2431 }
John McCall9dd450b2009-09-21 23:43:11 +00002432 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002433 Context->setjmp_bufDecl(Typedef->getDecl());
2434 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002435 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002436 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002437 Error("Invalid jmp_buf type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002438 return;
2439 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002440 Context->setjmp_bufDecl(Tag->getDecl());
2441 }
2442 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002443 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002444 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002445 if (Sigjmp_bufType.isNull()) {
2446 Error("sigjmp_buf type is NULL");
2447 return;
2448 }
John McCall9dd450b2009-09-21 23:43:11 +00002449 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002450 Context->setsigjmp_bufDecl(Typedef->getDecl());
2451 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002452 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002453 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stumpa4de80b2009-07-28 02:25:19 +00002454 Context->setsigjmp_bufDecl(Tag->getDecl());
2455 }
2456 }
Mike Stump11289f42009-09-09 15:08:12 +00002457 if (unsigned ObjCIdRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002458 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002459 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump11289f42009-09-09 15:08:12 +00002460 if (unsigned ObjCClassRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002461 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002462 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002463 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpd0153282009-10-20 02:12:22 +00002464 Context->setBlockDescriptorType(GetType(String));
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002465 if (unsigned String
Sebastian Redl539c5062010-08-18 23:57:32 +00002466 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002467 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002468 if (unsigned ObjCSelRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002469 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002470 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002471 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002472 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002473
Sebastian Redl539c5062010-08-18 23:57:32 +00002474 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002475 Context->setInt128Installed();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002476
2477 ReadUserDiagnosticMappings(Context->getDiagnostics());
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002478}
2479
Douglas Gregor45fe0362009-05-12 01:31:05 +00002480/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002481/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00002482/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002483std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00002484 FileManager &FileMgr,
Daniel Dunbar3b951482009-12-03 09:13:06 +00002485 Diagnostic &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002486 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002487 std::string ErrStr;
2488 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner5159f612010-11-23 08:35:12 +00002489 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00002490 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00002491 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002492 return std::string();
2493 }
2494
2495 // Initialize the stream
2496 llvm::BitstreamReader StreamFile;
2497 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00002498 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00002499 (const unsigned char *)Buffer->getBufferEnd());
2500 Stream.init(StreamFile);
2501
2502 // Sniff for the signature.
2503 if (Stream.Read(8) != 'C' ||
2504 Stream.Read(8) != 'P' ||
2505 Stream.Read(8) != 'C' ||
2506 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002507 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002508 return std::string();
2509 }
2510
2511 RecordData Record;
2512 while (!Stream.AtEndOfStream()) {
2513 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002514
Douglas Gregor45fe0362009-05-12 01:31:05 +00002515 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2516 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00002517
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002518 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002519 switch (BlockID) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002520 case AST_BLOCK_ID:
2521 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002522 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002523 return std::string();
2524 }
2525 break;
Mike Stump11289f42009-09-09 15:08:12 +00002526
Douglas Gregor45fe0362009-05-12 01:31:05 +00002527 default:
2528 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002529 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002530 return std::string();
2531 }
2532 break;
2533 }
2534 continue;
2535 }
2536
2537 if (Code == llvm::bitc::END_BLOCK) {
2538 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002539 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002540 return std::string();
2541 }
2542 continue;
2543 }
2544
2545 if (Code == llvm::bitc::DEFINE_ABBREV) {
2546 Stream.ReadAbbrevRecord();
2547 continue;
2548 }
2549
2550 Record.clear();
2551 const char *BlobStart = 0;
2552 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002553 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl539c5062010-08-18 23:57:32 +00002554 == ORIGINAL_FILE_NAME)
Douglas Gregor45fe0362009-05-12 01:31:05 +00002555 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00002556 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00002557
2558 return std::string();
2559}
2560
Douglas Gregor55abb232009-04-10 20:39:37 +00002561/// \brief Parse the record that corresponds to a LangOptions data
2562/// structure.
2563///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002564/// This routine parses the language options from the AST file and then gives
2565/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00002566///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002567/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002568bool ASTReader::ParseLanguageOptions(
Douglas Gregor55abb232009-04-10 20:39:37 +00002569 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002570 if (Listener) {
2571 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00002572
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002573 #define PARSE_LANGOPT(Option) \
2574 LangOpts.Option = Record[Idx]; \
2575 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00002576
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002577 unsigned Idx = 0;
2578 PARSE_LANGOPT(Trigraphs);
2579 PARSE_LANGOPT(BCPLComment);
2580 PARSE_LANGOPT(DollarIdents);
2581 PARSE_LANGOPT(AsmPreprocessor);
2582 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00002583 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002584 PARSE_LANGOPT(ImplicitInt);
2585 PARSE_LANGOPT(Digraphs);
2586 PARSE_LANGOPT(HexFloats);
2587 PARSE_LANGOPT(C99);
2588 PARSE_LANGOPT(Microsoft);
2589 PARSE_LANGOPT(CPlusPlus);
2590 PARSE_LANGOPT(CPlusPlus0x);
2591 PARSE_LANGOPT(CXXOperatorNames);
2592 PARSE_LANGOPT(ObjC1);
2593 PARSE_LANGOPT(ObjC2);
2594 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00002595 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00002596 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002597 PARSE_LANGOPT(PascalStrings);
2598 PARSE_LANGOPT(WritableStrings);
2599 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00002600 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002601 PARSE_LANGOPT(Exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +00002602 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002603 PARSE_LANGOPT(NeXTRuntime);
2604 PARSE_LANGOPT(Freestanding);
2605 PARSE_LANGOPT(NoBuiltin);
2606 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00002607 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002608 PARSE_LANGOPT(Blocks);
2609 PARSE_LANGOPT(EmitAllDecls);
2610 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00002611 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2612 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002613 PARSE_LANGOPT(HeinousExtensions);
2614 PARSE_LANGOPT(Optimize);
2615 PARSE_LANGOPT(OptimizeSize);
2616 PARSE_LANGOPT(Static);
2617 PARSE_LANGOPT(PICLevel);
2618 PARSE_LANGOPT(GNUInline);
2619 PARSE_LANGOPT(NoInline);
2620 PARSE_LANGOPT(AccessControl);
2621 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00002622 PARSE_LANGOPT(ShortWChar);
Chris Lattner51924e512010-06-26 21:25:03 +00002623 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall457a04e2010-10-22 21:05:15 +00002624 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00002625 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00002626 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002627 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00002628 PARSE_LANGOPT(OpenCL);
Mike Stumpd9546382009-12-12 01:27:46 +00002629 PARSE_LANGOPT(CatchUndefined);
2630 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002631 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00002632
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002633 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00002634 }
Douglas Gregor55abb232009-04-10 20:39:37 +00002635
2636 return false;
2637}
2638
Sebastian Redl2c499f62010-08-18 23:56:43 +00002639void ASTReader::ReadPreprocessedEntities() {
Douglas Gregoraae92242010-03-19 21:51:54 +00002640 ReadDefinedMacros();
2641}
2642
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002643PreprocessedEntity *ASTReader::ReadPreprocessedEntity(uint64_t Offset) {
2644 PerFileData *F = 0;
2645 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2646 if (Offset < Chain[I]->SizeInBits) {
2647 F = Chain[I];
2648 break;
2649 }
2650
2651 Offset -= Chain[I]->SizeInBits;
2652 }
2653
2654 if (!F) {
2655 Error("Malformed preprocessed entity offset");
2656 return 0;
2657 }
2658
2659 return ReadMacroRecord(*F, Offset);
2660}
2661
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002662void ASTReader::ReadUserDiagnosticMappings(Diagnostic &Diag) {
2663 unsigned Idx = 0;
2664 while (Idx < UserDiagMappings.size()) {
2665 unsigned DiagID = UserDiagMappings[Idx++];
2666 unsigned Map = UserDiagMappings[Idx++];
2667 Diag.setDiagnosticMappingInternal(DiagID, Map, /*isUser=*/true);
2668 }
2669}
2670
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002671/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002672ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002673 PerFileData *F = 0;
2674 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2675 F = Chain[N - I - 1];
2676 if (Index < F->LocalNumTypes)
2677 break;
2678 Index -= F->LocalNumTypes;
2679 }
2680 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redl2c373b92010-10-05 15:59:54 +00002681 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002682}
2683
2684/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002685///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002686/// The index is the type ID, shifted and minus the number of predefs. This
2687/// routine actually reads the record corresponding to the type at the given
2688/// location. It is a helper routine for GetType, which deals with reading type
2689/// IDs.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002690QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002691 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00002692 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00002693
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002694 // Keep track of where we are in the stream, then jump back there
2695 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002696 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002697
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002698 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00002699
Douglas Gregor1342e842009-07-06 18:54:52 +00002700 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00002701 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00002702
Sebastian Redl2c373b92010-10-05 15:59:54 +00002703 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002704 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002705 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00002706 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2707 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002708 if (Record.size() != 2) {
2709 Error("Incorrect encoding of extended qualifier type");
2710 return QualType();
2711 }
Douglas Gregor455b8f42009-04-15 22:00:08 +00002712 QualType Base = GetType(Record[0]);
John McCall8ccfcb52009-09-24 19:53:00 +00002713 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2714 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00002715 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002716
Sebastian Redl539c5062010-08-18 23:57:32 +00002717 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002718 if (Record.size() != 1) {
2719 Error("Incorrect encoding of complex type");
2720 return QualType();
2721 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002722 QualType ElemType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002723 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002724 }
2725
Sebastian Redl539c5062010-08-18 23:57:32 +00002726 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002727 if (Record.size() != 1) {
2728 Error("Incorrect encoding of pointer type");
2729 return QualType();
2730 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002731 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002732 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002733 }
2734
Sebastian Redl539c5062010-08-18 23:57:32 +00002735 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002736 if (Record.size() != 1) {
2737 Error("Incorrect encoding of block pointer type");
2738 return QualType();
2739 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002740 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002741 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002742 }
2743
Sebastian Redl539c5062010-08-18 23:57:32 +00002744 case TYPE_LVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002745 if (Record.size() != 1) {
2746 Error("Incorrect encoding of lvalue reference type");
2747 return QualType();
2748 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002749 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002750 return Context->getLValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002751 }
2752
Sebastian Redl539c5062010-08-18 23:57:32 +00002753 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002754 if (Record.size() != 1) {
2755 Error("Incorrect encoding of rvalue reference type");
2756 return QualType();
2757 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002758 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002759 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002760 }
2761
Sebastian Redl539c5062010-08-18 23:57:32 +00002762 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00002763 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002764 Error("Incorrect encoding of member pointer type");
2765 return QualType();
2766 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002767 QualType PointeeType = GetType(Record[0]);
2768 QualType ClassType = GetType(Record[1]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002769 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002770 }
2771
Sebastian Redl539c5062010-08-18 23:57:32 +00002772 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002773 QualType ElementType = GetType(Record[0]);
2774 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2775 unsigned IndexTypeQuals = Record[2];
2776 unsigned Idx = 3;
2777 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00002778 return Context->getConstantArrayType(ElementType, Size,
2779 ASM, IndexTypeQuals);
2780 }
2781
Sebastian Redl539c5062010-08-18 23:57:32 +00002782 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002783 QualType ElementType = GetType(Record[0]);
2784 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2785 unsigned IndexTypeQuals = Record[2];
Chris Lattner8575daa2009-04-27 21:45:14 +00002786 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002787 }
2788
Sebastian Redl539c5062010-08-18 23:57:32 +00002789 case TYPE_VARIABLE_ARRAY: {
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002790 QualType ElementType = GetType(Record[0]);
2791 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2792 unsigned IndexTypeQuals = Record[2];
Sebastian Redl2c373b92010-10-05 15:59:54 +00002793 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
2794 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
2795 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor04318252009-07-06 15:59:29 +00002796 ASM, IndexTypeQuals,
2797 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002798 }
2799
Sebastian Redl539c5062010-08-18 23:57:32 +00002800 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002801 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002802 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002803 return QualType();
2804 }
2805
2806 QualType ElementType = GetType(Record[0]);
2807 unsigned NumElements = Record[1];
Bob Wilsonaeb56442010-11-10 21:56:12 +00002808 unsigned VecKind = Record[2];
Chris Lattner37141f42010-06-23 06:00:24 +00002809 return Context->getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00002810 (VectorType::VectorKind)VecKind);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002811 }
2812
Sebastian Redl539c5062010-08-18 23:57:32 +00002813 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002814 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002815 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002816 return QualType();
2817 }
2818
2819 QualType ElementType = GetType(Record[0]);
2820 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00002821 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002822 }
2823
Sebastian Redl539c5062010-08-18 23:57:32 +00002824 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002825 if (Record.size() != 4) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002826 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002827 return QualType();
2828 }
2829 QualType ResultType = GetType(Record[0]);
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002830 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00002831 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002832 }
2833
Sebastian Redl539c5062010-08-18 23:57:32 +00002834 case TYPE_FUNCTION_PROTO: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002835 QualType ResultType = GetType(Record[0]);
Douglas Gregordc728752009-12-22 18:11:50 +00002836 bool NoReturn = Record[1];
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002837 unsigned RegParm = Record[2];
2838 CallingConv CallConv = (CallingConv)Record[3];
2839 unsigned Idx = 4;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002840 unsigned NumParams = Record[Idx++];
2841 llvm::SmallVector<QualType, 16> ParamTypes;
2842 for (unsigned I = 0; I != NumParams; ++I)
2843 ParamTypes.push_back(GetType(Record[Idx++]));
2844 bool isVariadic = Record[Idx++];
2845 unsigned Quals = Record[Idx++];
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002846 bool hasExceptionSpec = Record[Idx++];
2847 bool hasAnyExceptionSpec = Record[Idx++];
2848 unsigned NumExceptions = Record[Idx++];
2849 llvm::SmallVector<QualType, 2> Exceptions;
2850 for (unsigned I = 0; I != NumExceptions; ++I)
2851 Exceptions.push_back(GetType(Record[Idx++]));
Jay Foad7d0479f2009-05-21 09:52:38 +00002852 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002853 isVariadic, Quals, hasExceptionSpec,
2854 hasAnyExceptionSpec, NumExceptions,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00002855 Exceptions.data(),
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002856 FunctionType::ExtInfo(NoReturn, RegParm,
2857 CallConv));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002858 }
2859
Sebastian Redl539c5062010-08-18 23:57:32 +00002860 case TYPE_UNRESOLVED_USING:
John McCallb96ec562009-12-04 22:46:56 +00002861 return Context->getTypeDeclType(
2862 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2863
Sebastian Redl539c5062010-08-18 23:57:32 +00002864 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002865 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002866 Error("incorrect encoding of typedef type");
2867 return QualType();
2868 }
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002869 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2870 QualType Canonical = GetType(Record[1]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00002871 if (!Canonical.isNull())
2872 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002873 return Context->getTypedefType(Decl, Canonical);
2874 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002875
Sebastian Redl539c5062010-08-18 23:57:32 +00002876 case TYPE_TYPEOF_EXPR:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002877 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002878
Sebastian Redl539c5062010-08-18 23:57:32 +00002879 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002880 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002881 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002882 return QualType();
2883 }
2884 QualType UnderlyingType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002885 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002886 }
Mike Stump11289f42009-09-09 15:08:12 +00002887
Sebastian Redl539c5062010-08-18 23:57:32 +00002888 case TYPE_DECLTYPE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002889 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson81df7b82009-06-24 19:06:50 +00002890
Sebastian Redl539c5062010-08-18 23:57:32 +00002891 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002892 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002893 Error("incorrect encoding of record type");
2894 return QualType();
2895 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002896 bool IsDependent = Record[0];
2897 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCall25c9d112010-10-14 21:48:26 +00002898 T->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002899 return T;
2900 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002901
Sebastian Redl539c5062010-08-18 23:57:32 +00002902 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002903 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002904 Error("incorrect encoding of enum type");
2905 return QualType();
2906 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002907 bool IsDependent = Record[0];
2908 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCall25c9d112010-10-14 21:48:26 +00002909 T->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002910 return T;
2911 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00002912
Sebastian Redl539c5062010-08-18 23:57:32 +00002913 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002914 unsigned Idx = 0;
2915 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2916 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2917 QualType NamedType = GetType(Record[Idx++]);
2918 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00002919 }
2920
Sebastian Redl539c5062010-08-18 23:57:32 +00002921 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00002922 unsigned Idx = 0;
2923 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00002924 return Context->getObjCInterfaceType(ItfD);
2925 }
2926
Sebastian Redl539c5062010-08-18 23:57:32 +00002927 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00002928 unsigned Idx = 0;
2929 QualType Base = GetType(Record[Idx++]);
Chris Lattner587cbe12009-04-22 06:45:28 +00002930 unsigned NumProtos = Record[Idx++];
2931 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2932 for (unsigned I = 0; I != NumProtos; ++I)
2933 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002934 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00002935 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002936
Sebastian Redl539c5062010-08-18 23:57:32 +00002937 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00002938 unsigned Idx = 0;
John McCall8b07ec22010-05-15 11:32:37 +00002939 QualType Pointee = GetType(Record[Idx++]);
2940 return Context->getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00002941 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00002942
Sebastian Redl539c5062010-08-18 23:57:32 +00002943 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00002944 unsigned Idx = 0;
2945 QualType Parm = GetType(Record[Idx++]);
2946 QualType Replacement = GetType(Record[Idx++]);
2947 return
2948 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2949 Replacement);
2950 }
John McCalle78aac42010-03-10 03:28:59 +00002951
Sebastian Redl539c5062010-08-18 23:57:32 +00002952 case TYPE_INJECTED_CLASS_NAME: {
John McCalle78aac42010-03-10 03:28:59 +00002953 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2954 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00002955 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002956 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00002957 return
2958 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00002959 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002960
Sebastian Redl539c5062010-08-18 23:57:32 +00002961 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002962 unsigned Idx = 0;
2963 unsigned Depth = Record[Idx++];
2964 unsigned Index = Record[Idx++];
2965 bool Pack = Record[Idx++];
2966 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
2967 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
2968 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002969
Sebastian Redl539c5062010-08-18 23:57:32 +00002970 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002971 unsigned Idx = 0;
2972 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2973 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2974 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00002975 QualType Canon = GetType(Record[Idx++]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00002976 if (!Canon.isNull())
2977 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00002978 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002979 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002980
Sebastian Redl539c5062010-08-18 23:57:32 +00002981 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002982 unsigned Idx = 0;
2983 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2984 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2985 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2986 unsigned NumArgs = Record[Idx++];
2987 llvm::SmallVector<TemplateArgument, 8> Args;
2988 Args.reserve(NumArgs);
2989 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00002990 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002991 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
2992 Args.size(), Args.data());
2993 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002994
Sebastian Redl539c5062010-08-18 23:57:32 +00002995 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00002996 unsigned Idx = 0;
2997
2998 // ArrayType
2999 QualType ElementType = GetType(Record[Idx++]);
3000 ArrayType::ArraySizeModifier ASM
3001 = (ArrayType::ArraySizeModifier)Record[Idx++];
3002 unsigned IndexTypeQuals = Record[Idx++];
3003
3004 // DependentSizedArrayType
Sebastian Redl2c373b92010-10-05 15:59:54 +00003005 Expr *NumElts = ReadExpr(*Loc.F);
3006 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003007
3008 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3009 IndexTypeQuals, Brackets);
3010 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003011
Sebastian Redl539c5062010-08-18 23:57:32 +00003012 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003013 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003014 bool IsDependent = Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003015 TemplateName Name = ReadTemplateName(Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003016 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003017 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003018 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003019 QualType T;
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003020 if (Canon.isNull())
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003021 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3022 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003023 else
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003024 T = Context->getTemplateSpecializationType(Name, Args.data(),
3025 Args.size(), Canon);
John McCall25c9d112010-10-14 21:48:26 +00003026 T->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003027 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003028 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003029 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003030 // Suppress a GCC warning
3031 return QualType();
3032}
3033
Sebastian Redl2c373b92010-10-05 15:59:54 +00003034class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00003035 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003036 ASTReader::PerFileData &F;
Sebastian Redlc67764e2010-07-22 22:43:28 +00003037 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redl2c499f62010-08-18 23:56:43 +00003038 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00003039 unsigned &Idx;
3040
Sebastian Redl2c373b92010-10-05 15:59:54 +00003041 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3042 unsigned &I) {
3043 return Reader.ReadSourceLocation(F, R, I);
3044 }
3045
John McCall8f115c62009-10-16 21:56:05 +00003046public:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003047 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl2c499f62010-08-18 23:56:43 +00003048 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003049 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3050 { }
John McCall8f115c62009-10-16 21:56:05 +00003051
John McCall17001972009-10-18 01:05:36 +00003052 // We want compile-time assurance that we've enumerated all of
3053 // these, so unfortunately we have to declare them first, then
3054 // define them out-of-line.
3055#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00003056#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00003057 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00003058#include "clang/AST/TypeLocNodes.def"
3059
John McCall17001972009-10-18 01:05:36 +00003060 void VisitFunctionTypeLoc(FunctionTypeLoc);
3061 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00003062};
3063
John McCall17001972009-10-18 01:05:36 +00003064void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00003065 // nothing to do
3066}
John McCall17001972009-10-18 01:05:36 +00003067void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003068 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorc9b7a592010-01-18 18:04:31 +00003069 if (TL.needsExtraLocalData()) {
3070 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3071 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3072 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3073 TL.setModeAttr(Record[Idx++]);
3074 }
John McCall8f115c62009-10-16 21:56:05 +00003075}
John McCall17001972009-10-18 01:05:36 +00003076void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003077 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003078}
John McCall17001972009-10-18 01:05:36 +00003079void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003080 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003081}
John McCall17001972009-10-18 01:05:36 +00003082void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003083 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003084}
John McCall17001972009-10-18 01:05:36 +00003085void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003086 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003087}
John McCall17001972009-10-18 01:05:36 +00003088void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003089 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003090}
John McCall17001972009-10-18 01:05:36 +00003091void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003092 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003093}
John McCall17001972009-10-18 01:05:36 +00003094void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003095 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3096 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003097 if (Record[Idx++])
Sebastian Redl2c373b92010-10-05 15:59:54 +00003098 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003099 else
John McCall17001972009-10-18 01:05:36 +00003100 TL.setSizeExpr(0);
3101}
3102void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3103 VisitArrayTypeLoc(TL);
3104}
3105void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3106 VisitArrayTypeLoc(TL);
3107}
3108void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3109 VisitArrayTypeLoc(TL);
3110}
3111void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3112 DependentSizedArrayTypeLoc TL) {
3113 VisitArrayTypeLoc(TL);
3114}
3115void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3116 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003117 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003118}
3119void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003120 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003121}
3122void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003123 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003124}
3125void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003126 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3127 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor7fb25412010-10-01 18:44:50 +00003128 TL.setTrailingReturn(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00003129 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCalle6347002009-10-23 01:28:53 +00003130 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall17001972009-10-18 01:05:36 +00003131 }
3132}
3133void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3134 VisitFunctionTypeLoc(TL);
3135}
3136void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3137 VisitFunctionTypeLoc(TL);
3138}
John McCallb96ec562009-12-04 22:46:56 +00003139void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003140 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallb96ec562009-12-04 22:46:56 +00003141}
John McCall17001972009-10-18 01:05:36 +00003142void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003143 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003144}
3145void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003146 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3147 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3148 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003149}
3150void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003151 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3152 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3153 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3154 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003155}
3156void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003157 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003158}
3159void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003160 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003161}
3162void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003163 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003164}
John McCall17001972009-10-18 01:05:36 +00003165void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003166 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003167}
John McCallcebee162009-10-18 09:09:24 +00003168void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3169 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003170 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallcebee162009-10-18 09:09:24 +00003171}
John McCall17001972009-10-18 01:05:36 +00003172void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3173 TemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003174 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3175 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3176 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall0ad16662009-10-29 08:12:44 +00003177 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3178 TL.setArgLocInfo(i,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003179 Reader.GetTemplateArgumentLocInfo(F,
3180 TL.getTypePtr()->getArg(i).getKind(),
3181 Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003182}
Abramo Bagnara6150c882010-05-11 21:36:43 +00003183void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003184 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3185 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003186}
John McCalle78aac42010-03-10 03:28:59 +00003187void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003188 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalle78aac42010-03-10 03:28:59 +00003189}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00003190void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003191 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3192 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3193 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003194}
John McCallc392f372010-06-11 00:33:02 +00003195void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3196 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003197 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3198 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3199 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3200 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3201 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003202 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3203 TL.setArgLocInfo(I,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003204 Reader.GetTemplateArgumentLocInfo(F,
3205 TL.getTypePtr()->getArg(I).getKind(),
3206 Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003207}
John McCall17001972009-10-18 01:05:36 +00003208void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003209 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8b07ec22010-05-15 11:32:37 +00003210}
3211void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3212 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003213 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3214 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003215 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003216 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003217}
John McCallfc93cf92009-10-22 22:37:11 +00003218void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003219 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCallfc93cf92009-10-22 22:37:11 +00003220}
John McCall8f115c62009-10-16 21:56:05 +00003221
Sebastian Redl2c373b92010-10-05 15:59:54 +00003222TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003223 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00003224 unsigned &Idx) {
3225 QualType InfoTy = GetType(Record[Idx++]);
3226 if (InfoTy.isNull())
3227 return 0;
3228
John McCallbcd03502009-12-07 02:54:59 +00003229 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003230 TypeLocReader TLR(*this, F, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00003231 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00003232 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00003233 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00003234}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003235
Sebastian Redl539c5062010-08-18 23:57:32 +00003236QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00003237 unsigned FastQuals = ID & Qualifiers::FastMask;
3238 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003239
Sebastian Redl539c5062010-08-18 23:57:32 +00003240 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003241 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00003242 switch ((PredefinedTypeIDs)Index) {
3243 case PREDEF_TYPE_NULL_ID: return QualType();
3244 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3245 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003246
Sebastian Redl539c5062010-08-18 23:57:32 +00003247 case PREDEF_TYPE_CHAR_U_ID:
3248 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003249 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00003250 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003251 break;
3252
Sebastian Redl539c5062010-08-18 23:57:32 +00003253 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3254 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3255 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3256 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3257 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3258 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3259 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3260 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3261 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3262 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3263 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3264 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3265 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3266 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3267 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3268 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3269 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3270 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3271 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3272 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3273 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3274 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3275 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3276 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003277 }
3278
3279 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00003280 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003281 }
3282
Sebastian Redl539c5062010-08-18 23:57:32 +00003283 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003284 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00003285 if (TypesLoaded[Index].isNull()) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003286 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003287 if (TypesLoaded[Index].isNull())
3288 return QualType();
3289
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003290 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003291 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003292 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003293 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003294 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00003295 }
Mike Stump11289f42009-09-09 15:08:12 +00003296
John McCall8ccfcb52009-09-24 19:53:00 +00003297 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003298}
3299
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003300TypeID ASTReader::GetTypeID(QualType T) const {
3301 return MakeTypeID(T,
3302 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3303}
3304
3305TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3306 if (T.isNull())
3307 return TypeIdx();
3308 assert(!T.getLocalFastQualifiers());
3309
3310 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3311 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3312 // comparing keys of ASTDeclContextNameLookupTable.
3313 // If the type didn't come from the AST file use a specially marked index
3314 // so that any hash/key comparison fail since no such index is stored
3315 // in a AST file.
3316 if (I == TypeIdxs.end())
3317 return TypeIdx(-1);
3318 return I->second;
3319}
3320
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003321unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3322 unsigned Result = 0;
3323 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3324 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3325
3326 return Result;
3327}
3328
John McCall0ad16662009-10-29 08:12:44 +00003329TemplateArgumentLocInfo
Sebastian Redl2c373b92010-10-05 15:59:54 +00003330ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3331 TemplateArgument::ArgKind Kind,
John McCall0ad16662009-10-29 08:12:44 +00003332 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003333 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00003334 switch (Kind) {
3335 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003336 return ReadExpr(F);
John McCall0ad16662009-10-29 08:12:44 +00003337 case TemplateArgument::Type:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003338 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003339 case TemplateArgument::Template: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003340 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3341 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003342 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003343 }
John McCall0ad16662009-10-29 08:12:44 +00003344 case TemplateArgument::Null:
3345 case TemplateArgument::Integral:
3346 case TemplateArgument::Declaration:
3347 case TemplateArgument::Pack:
3348 return TemplateArgumentLocInfo();
3349 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00003350 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00003351 return TemplateArgumentLocInfo();
3352}
3353
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003354TemplateArgumentLoc
Sebastian Redl2c373b92010-10-05 15:59:54 +00003355ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003356 const RecordData &Record, unsigned &Index) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003357 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003358
3359 if (Arg.getKind() == TemplateArgument::Expression) {
3360 if (Record[Index++]) // bool InfoHasSameExpr.
3361 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3362 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00003363 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003364 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003365}
3366
Sebastian Redl2c499f62010-08-18 23:56:43 +00003367Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00003368 return GetDecl(ID);
3369}
3370
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003371uint64_t
3372ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3373 if (ID == 0)
3374 return 0;
3375
3376 --ID;
3377 uint64_t Offset = 0;
3378 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3379 if (ID < Chain[I]->LocalNumCXXBaseSpecifiers)
3380 return Offset + Chain[I]->CXXBaseSpecifiersOffsets[ID];
3381
3382 ID -= Chain[I]->LocalNumCXXBaseSpecifiers;
3383 Offset += Chain[I]->SizeInBits;
3384 }
3385
3386 assert(false && "CXXBaseSpecifiers not found");
3387 return 0;
3388}
3389
3390CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3391 // Figure out which AST file contains this offset.
3392 PerFileData *F = 0;
3393 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3394 if (Offset < Chain[I]->SizeInBits) {
3395 F = Chain[I];
3396 break;
3397 }
3398
3399 Offset -= Chain[I]->SizeInBits;
3400 }
3401
3402 if (!F) {
3403 Error("Malformed AST file: C++ base specifiers at impossible offset");
3404 return 0;
3405 }
3406
3407 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3408 SavedStreamPosition SavedPosition(Cursor);
3409 Cursor.JumpToBit(Offset);
3410 ReadingKindTracker ReadingKind(Read_Decl, *this);
3411 RecordData Record;
3412 unsigned Code = Cursor.ReadCode();
3413 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3414 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3415 Error("Malformed AST file: missing C++ base specifiers");
3416 return 0;
3417 }
3418
3419 unsigned Idx = 0;
3420 unsigned NumBases = Record[Idx++];
3421 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3422 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3423 for (unsigned I = 0; I != NumBases; ++I)
3424 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3425 return Bases;
3426}
3427
Sebastian Redl2c499f62010-08-18 23:56:43 +00003428TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003429 if (!DeclsLoaded[0]) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00003430 ReadDeclRecord(0, 1);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003431 if (DeserializationListener)
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003432 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003433 }
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00003434
3435 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3436}
3437
Sebastian Redl539c5062010-08-18 23:57:32 +00003438Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003439 if (ID == 0)
3440 return 0;
3441
Douglas Gregor745ed142009-04-25 18:35:21 +00003442 if (ID > DeclsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003443 Error("declaration ID out-of-range for AST file");
Douglas Gregor745ed142009-04-25 18:35:21 +00003444 return 0;
3445 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003446
Douglas Gregor745ed142009-04-25 18:35:21 +00003447 unsigned Index = ID - 1;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003448 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003449 ReadDeclRecord(Index, ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003450 if (DeserializationListener)
3451 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3452 }
Douglas Gregor745ed142009-04-25 18:35:21 +00003453
3454 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003455}
3456
Chris Lattner9c28af02009-04-27 05:46:25 +00003457/// \brief Resolve the offset of a statement into a statement.
3458///
3459/// This operation will read a new statement from the external
3460/// source each time it is called, and is meant to be used via a
3461/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00003462Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00003463 // Switch case IDs are per Decl.
3464 ClearSwitchCaseIDs();
3465
Sebastian Redl5c415f32010-07-22 17:01:13 +00003466 // Offset here is a global offset across the entire chain.
3467 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3468 PerFileData &F = *Chain[N - I - 1];
3469 if (Offset < F.SizeInBits) {
3470 // Since we know that this statement is part of a decl, make sure to use
3471 // the decl cursor to read it.
3472 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003473 return ReadStmtFromStream(F);
Sebastian Redl5c415f32010-07-22 17:01:13 +00003474 }
3475 Offset -= F.SizeInBits;
3476 }
3477 llvm_unreachable("Broken chain");
Douglas Gregor3c3aa612009-04-18 00:07:54 +00003478}
3479
Sebastian Redl2c499f62010-08-18 23:56:43 +00003480bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003481 bool (*isKindWeWant)(Decl::Kind),
John McCall75b960e2010-06-01 09:23:16 +00003482 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump11289f42009-09-09 15:08:12 +00003483 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003484 "DeclContext has no lexical decls in storage");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003485
Sebastian Redl5c415f32010-07-22 17:01:13 +00003486 // There might be lexical decls in multiple parts of the chain, for the TU
3487 // at least.
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003488 // DeclContextOffsets might reallocate as we load additional decls below,
3489 // so make a copy of the vector.
3490 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl5c415f32010-07-22 17:01:13 +00003491 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3492 I != E; ++I) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003493 // IDs can be 0 if this context doesn't contain declarations.
3494 if (!I->LexicalDecls)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003495 continue;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003496
3497 // Load all of the declaration IDs
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003498 for (const KindDeclIDPair *ID = I->LexicalDecls,
3499 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3500 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3501 continue;
3502
3503 Decl *D = GetDecl(ID->second);
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003504 assert(D && "Null decl in lexical decls");
3505 Decls.push_back(D);
3506 }
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003507 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003508
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003509 ++NumLexicalDeclContextsRead;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003510 return false;
3511}
3512
John McCall75b960e2010-06-01 09:23:16 +00003513DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00003514ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00003515 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003516 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003517 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003518 if (!Name)
3519 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3520 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003521
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003522 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl471ac2f2010-08-24 00:49:55 +00003523 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003524 // and namespaces. For any given name, the last available results replace
3525 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl5c415f32010-07-22 17:01:13 +00003526 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003527 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl5c415f32010-07-22 17:01:13 +00003528 I != E; ++I) {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003529 if (!I->NameLookupTableData)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003530 continue;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003531
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003532 ASTDeclContextNameLookupTable *LookupTable =
3533 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3534 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3535 if (Pos == LookupTable->end())
Sebastian Redl5c415f32010-07-22 17:01:13 +00003536 continue;
3537
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003538 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3539 for (; Data.first != Data.second; ++Data.first)
3540 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003541 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003542 }
3543
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003544 ++NumVisibleDeclContextsRead;
John McCall75b960e2010-06-01 09:23:16 +00003545
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003546 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00003547 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003548}
3549
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00003550void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3551 assert(DC->hasExternalVisibleStorage() &&
3552 "DeclContext has no visible decls in storage");
3553
3554 llvm::SmallVector<NamedDecl *, 64> Decls;
3555 // There might be visible decls in multiple parts of the chain, for the TU
3556 // and namespaces.
3557 DeclContextInfos &Infos = DeclContextOffsets[DC];
3558 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3559 I != E; ++I) {
3560 if (!I->NameLookupTableData)
3561 continue;
3562
3563 ASTDeclContextNameLookupTable *LookupTable =
3564 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3565 for (ASTDeclContextNameLookupTable::item_iterator
3566 ItemI = LookupTable->item_begin(),
3567 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3568 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3569 = *ItemI;
3570 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3571 Decls.clear();
3572 for (; Data.first != Data.second; ++Data.first)
3573 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3574 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3575 }
3576 }
3577}
3578
Sebastian Redl2c499f62010-08-18 23:56:43 +00003579void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003580 assert(Consumer);
3581 while (!InterestingDecls.empty()) {
3582 DeclGroupRef DG(InterestingDecls.front());
3583 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00003584 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003585 }
3586}
3587
Sebastian Redl2c499f62010-08-18 23:56:43 +00003588void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00003589 this->Consumer = Consumer;
3590
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003591 if (!Consumer)
3592 return;
3593
3594 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003595 // Force deserialization of this decl, which will cause it to be queued for
3596 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00003597 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003598 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00003599
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003600 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003601}
3602
Sebastian Redl2c499f62010-08-18 23:56:43 +00003603void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003604 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003605
Mike Stump11289f42009-09-09 15:08:12 +00003606 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003607 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00003608 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00003609 unsigned NumDeclsLoaded
3610 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3611 (Decl *)0);
3612 unsigned NumIdentifiersLoaded
3613 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3614 IdentifiersLoaded.end(),
3615 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00003616 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003617 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3618 SelectorsLoaded.end(),
3619 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00003620
Douglas Gregorc5046832009-04-27 18:38:38 +00003621 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3622 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor258ae542009-04-27 06:38:32 +00003623 if (TotalNumSLocEntries)
3624 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3625 NumSLocEntriesRead, TotalNumSLocEntries,
3626 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00003627 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003628 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003629 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3630 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3631 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003632 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003633 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3634 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00003635 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003636 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00003637 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3638 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00003639 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003640 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00003641 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3642 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003643 if (TotalNumStatements)
3644 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3645 NumStatementsRead, TotalNumStatements,
3646 ((float)NumStatementsRead/TotalNumStatements * 100));
3647 if (TotalNumMacros)
3648 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3649 NumMacrosRead, TotalNumMacros,
3650 ((float)NumMacrosRead/TotalNumMacros * 100));
3651 if (TotalLexicalDeclContexts)
3652 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3653 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3654 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3655 * 100));
3656 if (TotalVisibleDeclContexts)
3657 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3658 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3659 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3660 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003661 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003662 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003663 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3664 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00003665 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003666 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003667 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003668 std::fprintf(stderr, "\n");
3669}
3670
Sebastian Redl2c499f62010-08-18 23:56:43 +00003671void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00003672 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003673 S.ExternalSource = this;
3674
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003675 // Makes sure any declarations that were deserialized "too early"
3676 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003677 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3678 if (SemaObj->TUScope)
John McCall48871652010-08-21 09:40:31 +00003679 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003680
3681 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003682 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003683 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00003684
3685 // If there were any tentative definitions, deserialize them and add
Sebastian Redl35351a92010-01-31 22:27:38 +00003686 // them to Sema's list of tentative definitions.
Douglas Gregord4df8652009-04-22 22:02:47 +00003687 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3688 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redl35351a92010-01-31 22:27:38 +00003689 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregord4df8652009-04-22 22:02:47 +00003690 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00003691
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003692 // If there were any unused file scoped decls, deserialize them and add to
3693 // Sema's list of unused file scoped decls.
3694 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3695 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3696 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattner90073802010-02-12 00:07:30 +00003697 }
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003698
3699 // If there were any locally-scoped external declarations,
3700 // deserialize them and add them to Sema's table of locally-scoped
3701 // external declarations.
3702 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3703 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3704 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3705 }
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003706
3707 // If there were any ext_vector type declarations, deserialize them
3708 // and add them to Sema's vector of such declarations.
3709 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3710 SemaObj->ExtVectorDecls.push_back(
3711 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003712
3713 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3714 // Can we cut them down before writing them ?
3715
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003716 // If there were any dynamic classes declarations, deserialize them
3717 // and add them to Sema's vector of such declarations.
3718 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3719 SemaObj->DynamicClasses.push_back(
3720 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003721
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003722 // Load the offsets of the declarations that Sema references.
3723 // They will be lazily deserialized when needed.
3724 if (!SemaDeclRefs.empty()) {
3725 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3726 SemaObj->StdNamespace = SemaDeclRefs[0];
3727 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3728 }
3729
Sebastian Redl2c373b92010-10-05 15:59:54 +00003730 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
3731
3732 // If there are @selector references added them to its pool. This is for
3733 // implementation of -Wselector.
3734 if (!F->ReferencedSelectorsData.empty()) {
3735 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
3736 unsigned I = 0;
3737 while (I < DataSize) {
3738 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
3739 SourceLocation SelLoc = ReadSourceLocation(
3740 *F, F->ReferencedSelectorsData, I);
3741 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3742 }
3743 }
3744
3745 // If there were any pending implicit instantiations, deserialize them
3746 // and add them to Sema's queue of such instantiations.
3747 assert(F->PendingInstantiations.size() % 2 == 0 &&
3748 "Expected pairs of entries");
3749 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
3750 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
3751 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
3752 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
3753 }
3754 }
3755
3756 // The two special data sets below always come from the most recent PCH,
3757 // which is at the front of the chain.
3758 PerFileData &F = *Chain.front();
3759
3760 // If there were any weak undeclared identifiers, deserialize them and add to
3761 // Sema's list of weak undeclared identifiers.
3762 if (!WeakUndeclaredIdentifiers.empty()) {
3763 unsigned Idx = 0;
3764 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3765 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3766 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3767 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
3768 bool Used = WeakUndeclaredIdentifiers[Idx++];
3769 Sema::WeakInfo WI(AliasId, Loc);
3770 WI.setUsed(Used);
3771 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3772 }
3773 }
3774
3775 // If there were any VTable uses, deserialize the information and add it
3776 // to Sema's vector and map of VTable uses.
3777 if (!VTableUses.empty()) {
3778 unsigned Idx = 0;
3779 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3780 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3781 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
3782 bool DefinitionRequired = VTableUses[Idx++];
3783 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3784 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003785 }
3786 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00003787}
3788
Sebastian Redl2c499f62010-08-18 23:56:43 +00003789IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redl78f51772010-08-02 18:30:12 +00003790 // Try to find this name within our on-disk hash tables. We start with the
3791 // most recent one, since that one contains the most up-to-date info.
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003792 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003793 ASTIdentifierLookupTable *IdTable
3794 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003795 if (!IdTable)
3796 continue;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003797 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003798 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003799 if (Pos == IdTable->end())
3800 continue;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003801
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003802 // Dereferencing the iterator has the effect of building the
3803 // IdentifierInfo node and populating it with the various
3804 // declarations it needs.
Sebastian Redl78f51772010-08-02 18:30:12 +00003805 return *Pos;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003806 }
Sebastian Redl78f51772010-08-02 18:30:12 +00003807 return 0;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003808}
3809
Douglas Gregor57756ea2010-10-14 22:11:03 +00003810namespace clang {
3811 /// \brief An identifier-lookup iterator that enumerates all of the
3812 /// identifiers stored within a set of AST files.
3813 class ASTIdentifierIterator : public IdentifierIterator {
3814 /// \brief The AST reader whose identifiers are being enumerated.
3815 const ASTReader &Reader;
3816
3817 /// \brief The current index into the chain of AST files stored in
3818 /// the AST reader.
3819 unsigned Index;
3820
3821 /// \brief The current position within the identifier lookup table
3822 /// of the current AST file.
3823 ASTIdentifierLookupTable::key_iterator Current;
3824
3825 /// \brief The end position within the identifier lookup table of
3826 /// the current AST file.
3827 ASTIdentifierLookupTable::key_iterator End;
3828
3829 public:
3830 explicit ASTIdentifierIterator(const ASTReader &Reader);
3831
3832 virtual llvm::StringRef Next();
3833 };
3834}
3835
3836ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
3837 : Reader(Reader), Index(Reader.Chain.size() - 1) {
3838 ASTIdentifierLookupTable *IdTable
3839 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3840 Current = IdTable->key_begin();
3841 End = IdTable->key_end();
3842}
3843
3844llvm::StringRef ASTIdentifierIterator::Next() {
3845 while (Current == End) {
3846 // If we have exhausted all of our AST files, we're done.
3847 if (Index == 0)
3848 return llvm::StringRef();
3849
3850 --Index;
3851 ASTIdentifierLookupTable *IdTable
3852 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3853 Current = IdTable->key_begin();
3854 End = IdTable->key_end();
3855 }
3856
3857 // We have any identifiers remaining in the current AST file; return
3858 // the next one.
3859 std::pair<const char*, unsigned> Key = *Current;
3860 ++Current;
3861 return llvm::StringRef(Key.first, Key.second);
3862}
3863
3864IdentifierIterator *ASTReader::getIdentifiers() const {
3865 return new ASTIdentifierIterator(*this);
3866}
3867
Mike Stump11289f42009-09-09 15:08:12 +00003868std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redl2c499f62010-08-18 23:56:43 +00003869ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redlada023c2010-08-04 20:40:17 +00003870 // Find this selector in a hash table. We want to find the most recent entry.
3871 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3872 PerFileData &F = *Chain[I];
3873 if (!F.SelectorLookupTable)
3874 continue;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003875
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003876 ASTSelectorLookupTable *PoolTable
3877 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3878 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redlada023c2010-08-04 20:40:17 +00003879 if (Pos != PoolTable->end()) {
3880 ++NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003881 // FIXME: Not quite happy with the statistics here. We probably should
3882 // disable this tracking when called via LoadSelector.
3883 // Also, should entries without methods count as misses?
3884 ++NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003885 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redlada023c2010-08-04 20:40:17 +00003886 if (DeserializationListener)
3887 DeserializationListener->SelectorRead(Data.ID, Sel);
3888 return std::make_pair(Data.Instance, Data.Factory);
3889 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003890 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003891
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003892 ++NumMethodPoolMisses;
Sebastian Redlada023c2010-08-04 20:40:17 +00003893 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorc78d3462009-04-24 21:10:55 +00003894}
3895
Sebastian Redl2c499f62010-08-18 23:56:43 +00003896void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003897 // It would be complicated to avoid reading the methods anyway. So don't.
3898 ReadMethodPool(Sel);
3899}
3900
Sebastian Redl2c499f62010-08-18 23:56:43 +00003901void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00003902 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00003903 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00003904 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00003905 if (DeserializationListener)
3906 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003907}
3908
Douglas Gregor1342e842009-07-06 18:54:52 +00003909/// \brief Set the globally-visible declarations associated with the given
3910/// identifier.
3911///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003912/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00003913/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00003914/// them.
3915///
3916/// \param II an IdentifierInfo that refers to one or more globally-visible
3917/// declarations.
3918///
3919/// \param DeclIDs the set of declaration IDs with the name @p II that are
3920/// visible at global scope.
3921///
3922/// \param Nonrecursive should be true to indicate that the caller knows that
3923/// this call is non-recursive, and therefore the globally-visible declarations
3924/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00003925void
Sebastian Redl2c499f62010-08-18 23:56:43 +00003926ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregor1342e842009-07-06 18:54:52 +00003927 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
3928 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003929 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00003930 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
3931 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
3932 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003933 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00003934 return;
3935 }
Mike Stump11289f42009-09-09 15:08:12 +00003936
Douglas Gregor1342e842009-07-06 18:54:52 +00003937 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
3938 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
3939 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00003940 if (SemaObj->TUScope) {
3941 // Introduce this declaration into the translation-unit scope
3942 // and add it to the declaration chain for this identifier, so
3943 // that (unqualified) name lookup will find it.
John McCall48871652010-08-21 09:40:31 +00003944 SemaObj->TUScope->AddDecl(D);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00003945 }
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003946 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregor1342e842009-07-06 18:54:52 +00003947 } else {
3948 // Queue this declaration so that it will be added to the
3949 // translation unit scope and identifier's declaration chain
3950 // once a Sema object is known.
3951 PreloadedDecls.push_back(D);
3952 }
3953 }
3954}
3955
Sebastian Redl2c499f62010-08-18 23:56:43 +00003956IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003957 if (ID == 0)
3958 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00003959
Sebastian Redlc713b962010-07-21 00:46:22 +00003960 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003961 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003962 return 0;
3963 }
Mike Stump11289f42009-09-09 15:08:12 +00003964
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003965 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc713b962010-07-21 00:46:22 +00003966 ID -= 1;
3967 if (!IdentifiersLoaded[ID]) {
3968 unsigned Index = ID;
3969 const char *Str = 0;
3970 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3971 PerFileData *F = Chain[N - I - 1];
3972 if (Index < F->LocalNumIdentifiers) {
3973 uint32_t Offset = F->IdentifierOffsets[Index];
3974 Str = F->IdentifierTableData + Offset;
3975 break;
3976 }
3977 Index -= F->LocalNumIdentifiers;
3978 }
3979 assert(Str && "Broken Chain");
Douglas Gregor5287b4e2009-04-25 21:04:17 +00003980
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003981 // All of the strings in the AST file are preceded by a 16-bit length.
3982 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00003983 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
3984 // unsigned integers. This is important to avoid integer overflow when
3985 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00003986 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00003987 unsigned StrLen = (((unsigned) StrLenPtr[0])
3988 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00003989 IdentifiersLoaded[ID]
Kovarththanan Rajaratnama3b09592010-03-12 10:32:27 +00003990 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003991 if (DeserializationListener)
3992 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003993 }
Mike Stump11289f42009-09-09 15:08:12 +00003994
Sebastian Redlc713b962010-07-21 00:46:22 +00003995 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003996}
3997
Sebastian Redl2c499f62010-08-18 23:56:43 +00003998void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00003999 ReadSLocEntryRecord(ID);
4000}
4001
Sebastian Redl2c499f62010-08-18 23:56:43 +00004002Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00004003 if (ID == 0)
4004 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00004005
Sebastian Redlada023c2010-08-04 20:40:17 +00004006 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004007 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00004008 return Selector();
4009 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004010
Sebastian Redlada023c2010-08-04 20:40:17 +00004011 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004012 // Load this selector from the selector table.
Sebastian Redlada023c2010-08-04 20:40:17 +00004013 unsigned Idx = ID - 1;
4014 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4015 PerFileData &F = *Chain[N - I - 1];
4016 if (Idx < F.LocalNumSelectors) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004017 ASTSelectorLookupTrait Trait(*this);
Sebastian Redlada023c2010-08-04 20:40:17 +00004018 SelectorsLoaded[ID - 1] =
4019 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4020 if (DeserializationListener)
4021 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4022 break;
4023 }
4024 Idx -= F.LocalNumSelectors;
4025 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004026 }
4027
Sebastian Redlada023c2010-08-04 20:40:17 +00004028 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00004029}
4030
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004031Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00004032 return DecodeSelector(ID);
4033}
4034
Sebastian Redl2c499f62010-08-18 23:56:43 +00004035uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00004036 // ID 0 (the null selector) is considered an external selector.
4037 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00004038}
4039
Mike Stump11289f42009-09-09 15:08:12 +00004040DeclarationName
Sebastian Redl2c499f62010-08-18 23:56:43 +00004041ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004042 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4043 switch (Kind) {
4044 case DeclarationName::Identifier:
4045 return DeclarationName(GetIdentifierInfo(Record, Idx));
4046
4047 case DeclarationName::ObjCZeroArgSelector:
4048 case DeclarationName::ObjCOneArgSelector:
4049 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff3c301dc2009-04-23 15:15:40 +00004050 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004051
4052 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004053 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004054 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004055
4056 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004057 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004058 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004059
4060 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004061 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004062 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004063
4064 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004065 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004066 (OverloadedOperatorKind)Record[Idx++]);
4067
Alexis Hunt3d221f22009-11-29 07:34:05 +00004068 case DeclarationName::CXXLiteralOperatorName:
4069 return Context->DeclarationNames.getCXXLiteralOperatorName(
4070 GetIdentifierInfo(Record, Idx));
4071
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004072 case DeclarationName::CXXUsingDirective:
4073 return DeclarationName::getUsingDirectiveName();
4074 }
4075
4076 // Required to silence GCC warning
4077 return DeclarationName();
4078}
Douglas Gregor55abb232009-04-10 20:39:37 +00004079
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004080void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4081 DeclarationNameLoc &DNLoc,
4082 DeclarationName Name,
4083 const RecordData &Record, unsigned &Idx) {
4084 switch (Name.getNameKind()) {
4085 case DeclarationName::CXXConstructorName:
4086 case DeclarationName::CXXDestructorName:
4087 case DeclarationName::CXXConversionFunctionName:
4088 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4089 break;
4090
4091 case DeclarationName::CXXOperatorName:
4092 DNLoc.CXXOperatorName.BeginOpNameLoc
4093 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4094 DNLoc.CXXOperatorName.EndOpNameLoc
4095 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4096 break;
4097
4098 case DeclarationName::CXXLiteralOperatorName:
4099 DNLoc.CXXLiteralOperatorName.OpNameLoc
4100 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4101 break;
4102
4103 case DeclarationName::Identifier:
4104 case DeclarationName::ObjCZeroArgSelector:
4105 case DeclarationName::ObjCOneArgSelector:
4106 case DeclarationName::ObjCMultiArgSelector:
4107 case DeclarationName::CXXUsingDirective:
4108 break;
4109 }
4110}
4111
4112void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4113 DeclarationNameInfo &NameInfo,
4114 const RecordData &Record, unsigned &Idx) {
4115 NameInfo.setName(ReadDeclarationName(Record, Idx));
4116 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4117 DeclarationNameLoc DNLoc;
4118 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4119 NameInfo.setInfo(DNLoc);
4120}
4121
4122void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4123 const RecordData &Record, unsigned &Idx) {
4124 Info.NNS = ReadNestedNameSpecifier(Record, Idx);
4125 Info.NNSRange = ReadSourceRange(F, Record, Idx);
4126 unsigned NumTPLists = Record[Idx++];
4127 Info.NumTemplParamLists = NumTPLists;
4128 if (NumTPLists) {
4129 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4130 for (unsigned i=0; i != NumTPLists; ++i)
4131 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4132 }
4133}
4134
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004135TemplateName
Sebastian Redl2c499f62010-08-18 23:56:43 +00004136ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004137 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004138 switch (Kind) {
4139 case TemplateName::Template:
4140 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4141
4142 case TemplateName::OverloadedTemplate: {
4143 unsigned size = Record[Idx++];
4144 UnresolvedSet<8> Decls;
4145 while (size--)
4146 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4147
4148 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4149 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004150
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004151 case TemplateName::QualifiedTemplate: {
4152 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4153 bool hasTemplKeyword = Record[Idx++];
4154 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4155 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4156 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004157
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004158 case TemplateName::DependentTemplate: {
4159 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4160 if (Record[Idx++]) // isIdentifier
4161 return Context->getDependentTemplateName(NNS,
4162 GetIdentifierInfo(Record, Idx));
4163 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004164 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004165 }
4166 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004167
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004168 assert(0 && "Unhandled template name kind!");
4169 return TemplateName();
4170}
4171
4172TemplateArgument
Sebastian Redl2c373b92010-10-05 15:59:54 +00004173ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00004174 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004175 switch ((TemplateArgument::ArgKind)Record[Idx++]) {
4176 case TemplateArgument::Null:
4177 return TemplateArgument();
4178 case TemplateArgument::Type:
4179 return TemplateArgument(GetType(Record[Idx++]));
4180 case TemplateArgument::Declaration:
4181 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00004182 case TemplateArgument::Integral: {
4183 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4184 QualType T = GetType(Record[Idx++]);
4185 return TemplateArgument(Value, T);
4186 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004187 case TemplateArgument::Template:
4188 return TemplateArgument(ReadTemplateName(Record, Idx));
4189 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004190 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004191 case TemplateArgument::Pack: {
4192 unsigned NumArgs = Record[Idx++];
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004193 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4194 for (unsigned I = 0; I != NumArgs; ++I)
4195 Args[I] = ReadTemplateArgument(F, Record, Idx);
4196 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004197 }
4198 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004199
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004200 assert(0 && "Unhandled template argument kind!");
4201 return TemplateArgument();
4202}
4203
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004204TemplateParameterList *
Sebastian Redl2c373b92010-10-05 15:59:54 +00004205ASTReader::ReadTemplateParameterList(PerFileData &F,
4206 const RecordData &Record, unsigned &Idx) {
4207 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4208 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4209 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004210
4211 unsigned NumParams = Record[Idx++];
4212 llvm::SmallVector<NamedDecl *, 16> Params;
4213 Params.reserve(NumParams);
4214 while (NumParams--)
4215 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004216
4217 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004218 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4219 Params.data(), Params.size(), RAngleLoc);
4220 return TemplateParams;
4221}
4222
4223void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004224ASTReader::
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004225ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004226 PerFileData &F, const RecordData &Record,
4227 unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004228 unsigned NumTemplateArgs = Record[Idx++];
4229 TemplArgs.reserve(NumTemplateArgs);
4230 while (NumTemplateArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004231 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004232}
4233
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004234/// \brief Read a UnresolvedSet structure.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004235void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004236 const RecordData &Record, unsigned &Idx) {
4237 unsigned NumDecls = Record[Idx++];
4238 while (NumDecls--) {
4239 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4240 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4241 Set.addDecl(D, AS);
4242 }
4243}
4244
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004245CXXBaseSpecifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00004246ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky19b9f952010-07-26 16:56:01 +00004247 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004248 bool isVirtual = static_cast<bool>(Record[Idx++]);
4249 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4250 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004251 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4252 SourceRange Range = ReadSourceRange(F, Record, Idx);
Nick Lewycky19b9f952010-07-26 16:56:01 +00004253 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004254}
4255
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004256std::pair<CXXBaseOrMemberInitializer **, unsigned>
Sebastian Redl2c373b92010-10-05 15:59:54 +00004257ASTReader::ReadCXXBaseOrMemberInitializers(PerFileData &F,
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004258 const RecordData &Record,
4259 unsigned &Idx) {
4260 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0;
4261 unsigned NumInitializers = Record[Idx++];
4262 if (NumInitializers) {
4263 ASTContext &C = *getContext();
4264
4265 BaseOrMemberInitializers
4266 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
4267 for (unsigned i=0; i != NumInitializers; ++i) {
4268 TypeSourceInfo *BaseClassInfo = 0;
4269 bool IsBaseVirtual = false;
4270 FieldDecl *Member = 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004271
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004272 bool IsBaseInitializer = Record[Idx++];
4273 if (IsBaseInitializer) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004274 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004275 IsBaseVirtual = Record[Idx++];
4276 } else {
4277 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
4278 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00004279 SourceLocation MemberLoc = ReadSourceLocation(F, Record, Idx);
4280 Expr *Init = ReadExpr(F);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004281 FieldDecl *AnonUnionMember
4282 = cast_or_null<FieldDecl>(GetDecl(Record[Idx++]));
Sebastian Redl2c373b92010-10-05 15:59:54 +00004283 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4284 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004285 bool IsWritten = Record[Idx++];
4286 unsigned SourceOrderOrNumArrayIndices;
4287 llvm::SmallVector<VarDecl *, 8> Indices;
4288 if (IsWritten) {
4289 SourceOrderOrNumArrayIndices = Record[Idx++];
4290 } else {
4291 SourceOrderOrNumArrayIndices = Record[Idx++];
4292 Indices.reserve(SourceOrderOrNumArrayIndices);
4293 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4294 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4295 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004296
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004297 CXXBaseOrMemberInitializer *BOMInit;
4298 if (IsBaseInitializer) {
4299 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
4300 IsBaseVirtual, LParenLoc,
4301 Init, RParenLoc);
4302 } else if (IsWritten) {
4303 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc,
4304 LParenLoc, Init, RParenLoc);
4305 } else {
4306 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc,
4307 LParenLoc, Init, RParenLoc,
4308 Indices.data(),
4309 Indices.size());
4310 }
4311
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00004312 if (IsWritten)
4313 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004314 BOMInit->setAnonUnionMember(AnonUnionMember);
4315 BaseOrMemberInitializers[i] = BOMInit;
4316 }
4317 }
4318
4319 return std::make_pair(BaseOrMemberInitializers, NumInitializers);
4320}
4321
Chris Lattnerca025db2010-05-07 21:43:38 +00004322NestedNameSpecifier *
Sebastian Redl2c499f62010-08-18 23:56:43 +00004323ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004324 unsigned N = Record[Idx++];
4325 NestedNameSpecifier *NNS = 0, *Prev = 0;
4326 for (unsigned I = 0; I != N; ++I) {
4327 NestedNameSpecifier::SpecifierKind Kind
4328 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4329 switch (Kind) {
4330 case NestedNameSpecifier::Identifier: {
4331 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4332 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4333 break;
4334 }
4335
4336 case NestedNameSpecifier::Namespace: {
4337 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4338 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4339 break;
4340 }
4341
4342 case NestedNameSpecifier::TypeSpec:
4343 case NestedNameSpecifier::TypeSpecWithTemplate: {
4344 Type *T = GetType(Record[Idx++]).getTypePtr();
4345 bool Template = Record[Idx++];
4346 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4347 break;
4348 }
4349
4350 case NestedNameSpecifier::Global: {
4351 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4352 // No associated value, and there can't be a prefix.
4353 break;
4354 }
Chris Lattnerca025db2010-05-07 21:43:38 +00004355 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00004356 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00004357 }
4358 return NNS;
4359}
4360
4361SourceRange
Sebastian Redl2c373b92010-10-05 15:59:54 +00004362ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4363 unsigned &Idx) {
4364 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4365 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00004366 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00004367}
4368
Douglas Gregor1daeb692009-04-13 18:14:40 +00004369/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004370llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004371 unsigned BitWidth = Record[Idx++];
4372 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4373 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4374 Idx += NumWords;
4375 return Result;
4376}
4377
4378/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004379llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004380 bool isUnsigned = Record[Idx++];
4381 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4382}
4383
Douglas Gregore0a3a512009-04-14 21:55:33 +00004384/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004385llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004386 return llvm::APFloat(ReadAPInt(Record, Idx));
4387}
4388
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004389// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00004390std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004391 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00004392 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004393 Idx += Len;
4394 return Result;
4395}
4396
Sebastian Redl2c499f62010-08-18 23:56:43 +00004397CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00004398 unsigned &Idx) {
4399 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4400 return CXXTemporary::Create(*Context, Decl);
4401}
4402
Sebastian Redl2c499f62010-08-18 23:56:43 +00004403DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00004404 return Diag(SourceLocation(), DiagID);
4405}
4406
Sebastian Redl2c499f62010-08-18 23:56:43 +00004407DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004408 return Diags.Report(Loc, DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00004409}
Douglas Gregora9af1d12009-04-17 00:04:06 +00004410
Douglas Gregora868bbd2009-04-21 22:25:48 +00004411/// \brief Retrieve the identifier table associated with the
4412/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004413IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004414 assert(PP && "Forgot to set Preprocessor ?");
4415 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00004416}
4417
Douglas Gregora9af1d12009-04-17 00:04:06 +00004418/// \brief Record that the given ID maps to the given switch-case
4419/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004420void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004421 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4422 SwitchCaseStmts[ID] = SC;
4423}
4424
4425/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004426SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004427 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4428 return SwitchCaseStmts[ID];
4429}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004430
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00004431void ASTReader::ClearSwitchCaseIDs() {
4432 SwitchCaseStmts.clear();
4433}
4434
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004435/// \brief Record that the given label statement has been
4436/// deserialized and has the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004437void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump11289f42009-09-09 15:08:12 +00004438 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004439 "Deserialized label twice");
4440 LabelStmts[ID] = S;
4441
4442 // If we've already seen any goto statements that point to this
4443 // label, resolve them now.
4444 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
4445 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
4446 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
4447 Goto->second->setLabel(S);
4448 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor779d8652009-04-17 18:58:21 +00004449
4450 // If we've already seen any address-label statements that point to
4451 // this label, resolve them now.
4452 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump11289f42009-09-09 15:08:12 +00004453 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor779d8652009-04-17 18:58:21 +00004454 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump11289f42009-09-09 15:08:12 +00004455 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor779d8652009-04-17 18:58:21 +00004456 AddrLabel != AddrLabels.second; ++AddrLabel)
4457 AddrLabel->second->setLabel(S);
4458 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004459}
4460
4461/// \brief Set the label of the given statement to the label
4462/// identified by ID.
4463///
4464/// Depending on the order in which the label and other statements
4465/// referencing that label occur, this operation may complete
4466/// immediately (updating the statement) or it may queue the
4467/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004468void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004469 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4470 if (Label != LabelStmts.end()) {
4471 // We've already seen this label, so set the label of the goto and
4472 // we're done.
4473 S->setLabel(Label->second);
4474 } else {
4475 // We haven't seen this label yet, so add this goto to the set of
4476 // unresolved goto statements.
4477 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
4478 }
4479}
Douglas Gregor779d8652009-04-17 18:58:21 +00004480
4481/// \brief Set the label of the given expression to the label
4482/// identified by ID.
4483///
4484/// Depending on the order in which the label and other statements
4485/// referencing that label occur, this operation may complete
4486/// immediately (updating the statement) or it may queue the
4487/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004488void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor779d8652009-04-17 18:58:21 +00004489 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4490 if (Label != LabelStmts.end()) {
4491 // We've already seen this label, so set the label of the
4492 // label-address expression and we're done.
4493 S->setLabel(Label->second);
4494 } else {
4495 // We haven't seen this label yet, so add this label-address
4496 // expression to the set of unresolved label-address expressions.
4497 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
4498 }
4499}
Douglas Gregor1342e842009-07-06 18:54:52 +00004500
Sebastian Redl2c499f62010-08-18 23:56:43 +00004501void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004502 assert(NumCurrentElementsDeserializing &&
4503 "FinishedDeserializing not paired with StartedDeserializing");
4504 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004505 // If any identifiers with corresponding top-level declarations have
4506 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004507 while (!PendingIdentifierInfos.empty()) {
4508 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4509 PendingIdentifierInfos.front().DeclIDs, true);
4510 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00004511 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004512
4513 // We are not in recursive loading, so it's safe to pass the "interesting"
4514 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004515 if (Consumer)
4516 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidisad5f95c2010-10-24 17:26:31 +00004517
4518 assert(PendingForwardRefs.size() == 0 &&
4519 "Some forward refs did not get linked to the definition!");
Douglas Gregor1342e842009-07-06 18:54:52 +00004520 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004521 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00004522}
Douglas Gregorb473b072010-08-19 00:28:17 +00004523
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004524ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
4525 const char *isysroot, bool DisableValidation)
4526 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4527 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4528 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4529 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
4530 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00004531 TotalNumSLocEntries(0), NextSLocOffset(0), NumStatementsRead(0),
4532 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
4533 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004534 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4535 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4536 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
4537 RelocatablePCH = false;
4538}
4539
4540ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4541 Diagnostic &Diags, const char *isysroot,
4542 bool DisableValidation)
4543 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4544 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
4545 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
4546 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00004547 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4548 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4549 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4550 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4551 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4552 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004553 RelocatablePCH = false;
4554}
4555
4556ASTReader::~ASTReader() {
4557 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4558 delete Chain[e - i - 1];
4559 // Delete all visible decl lookup tables
4560 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4561 E = DeclContextOffsets.end();
4562 I != E; ++I) {
4563 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4564 J != F; ++J) {
4565 if (J->NameLookupTableData)
4566 delete static_cast<ASTDeclContextNameLookupTable*>(
4567 J->NameLookupTableData);
4568 }
4569 }
4570 for (DeclContextVisibleUpdatesPending::iterator
4571 I = PendingVisibleUpdates.begin(),
4572 E = PendingVisibleUpdates.end();
4573 I != E; ++I) {
4574 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4575 F = I->second.end();
4576 J != F; ++J)
4577 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4578 }
4579}
4580
Sebastian Redl009e7f22010-10-05 16:15:19 +00004581ASTReader::PerFileData::PerFileData(ASTFileType Ty)
4582 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00004583 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
4584 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
4585 MacroDefinitionOffsets(0), LocalNumSelectors(0), SelectorOffsets(0),
4586 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004587 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
4588 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redl3f6b7532010-10-01 19:59:12 +00004589 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregorb473b072010-08-19 00:28:17 +00004590{}
4591
4592ASTReader::PerFileData::~PerFileData() {
4593 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
4594 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4595}
4596