blob: 26d2ed250bc070aa29efa26f57e06344bd0f11b3 [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);
Peter Collingbourne546d0792010-12-01 19:14:57 +0000134 PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda);
Mike Stumpd9546382009-12-12 01:27:46 +0000135 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbar143021e2009-09-21 04:16:19 +0000136 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000137 PARSE_LANGOPT_BENIGN(SpellChecking);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +0000138#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000139#undef PARSE_LANGOPT_BENIGN
140
141 return false;
142}
143
Daniel Dunbar20a682d2009-11-11 00:52:11 +0000144bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
145 if (Triple == PP.getTargetInfo().getTriple().str())
146 return false;
147
148 Reader.Diag(diag::warn_pch_target_triple)
149 << Triple << PP.getTargetInfo().getTriple().str();
150 return true;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000151}
152
Benjamin Kramer90b5b682010-11-25 18:29:30 +0000153namespace {
154 struct EmptyStringRef {
155 bool operator ()(llvm::StringRef r) const { return r.empty(); }
156 };
157 struct EmptyBlock {
158 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
159 };
160}
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000161
162static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
163 PCHPredefinesBlocks R) {
164 // First, sum up the lengths.
165 unsigned LL = 0, RL = 0;
166 for (unsigned I = 0, N = L.size(); I != N; ++I) {
167 LL += L[I].size();
168 }
169 for (unsigned I = 0, N = R.size(); I != N; ++I) {
170 RL += R[I].Data.size();
171 }
172 if (LL != RL)
173 return false;
174 if (LL == 0 && RL == 0)
175 return true;
176
177 // Kick out empty parts, they confuse the algorithm below.
178 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
179 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
180
181 // Do it the hard way. At this point, both vectors must be non-empty.
182 llvm::StringRef LR = L[0], RR = R[0].Data;
183 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbar01ad0a72010-07-16 00:00:11 +0000184 (void) RN;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000185 for (;;) {
186 // Compare the current pieces.
187 if (LR.size() == RR.size()) {
188 // If they're the same length, it's pretty easy.
189 if (LR != RR)
190 return false;
191 // Both pieces are done, advance.
192 ++LI;
193 ++RI;
194 // If either string is done, they're both done, since they're the same
195 // length.
196 if (LI == LN) {
197 assert(RI == RN && "Strings not the same length after all?");
198 return true;
199 }
200 LR = L[LI];
201 RR = R[RI].Data;
202 } else if (LR.size() < RR.size()) {
203 // Right piece is longer.
204 if (!RR.startswith(LR))
205 return false;
206 ++LI;
207 assert(LI != LN && "Strings not the same length after all?");
208 RR = RR.substr(LR.size());
209 LR = L[LI];
210 } else {
211 // Left piece is longer.
212 if (!LR.startswith(RR))
213 return false;
214 ++RI;
215 assert(RI != RN && "Strings not the same length after all?");
216 LR = LR.substr(RR.size());
217 RR = R[RI].Data;
218 }
219 }
220}
221
222static std::pair<FileID, llvm::StringRef::size_type>
223FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
224 std::pair<FileID, llvm::StringRef::size_type> Res;
225 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
226 Res.second = Buffers[I].Data.find(MacroDef);
227 if (Res.second != llvm::StringRef::npos) {
228 Res.first = Buffers[I].BufferID;
229 break;
230 }
231 }
232 return Res;
233}
234
235bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000236 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000237 std::string &SuggestedPredefines) {
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000238 // We are in the context of an implicit include, so the predefines buffer will
239 // have a #include entry for the PCH file itself (as normalized by the
240 // preprocessor initialization). Find it and skip over it in the checking
241 // below.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000242 llvm::SmallString<256> PCHInclude;
243 PCHInclude += "#include \"";
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000244 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000245 PCHInclude += "\"\n";
246 std::pair<llvm::StringRef,llvm::StringRef> Split =
247 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
248 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000249 if (Left == PP.getPredefines()) {
250 Error("Missing PCH include entry!");
251 return true;
252 }
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000253
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000254 // If the concatenation of all the PCH buffers is equal to the adjusted
255 // command line, we're done.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000256 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
257 CommandLine.push_back(Left);
258 CommandLine.push_back(Right);
259 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000260 return false;
261
262 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000263
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000264 // The predefines buffers are different. Determine what the differences are,
265 // and whether they require us to reject the PCH file.
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000266 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000267 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
268 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000269
270 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
271 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000272
273 // Pick out implicit #includes after the PCH and don't consider them for
274 // validation; we will insert them into SuggestedPredefines so that the
275 // preprocessor includes them.
276 std::string IncludesAfterPCH;
277 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
278 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
279 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
280 if (AfterPCHLines[i].startswith("#include ")) {
281 IncludesAfterPCH += AfterPCHLines[i];
282 IncludesAfterPCH += '\n';
283 } else {
284 CmdLineLines.push_back(AfterPCHLines[i]);
285 }
286 }
287
288 // Make sure we add the includes last into SuggestedPredefines before we
289 // exit this function.
290 struct AddIncludesRAII {
291 std::string &SuggestedPredefines;
292 std::string &IncludesAfterPCH;
293
294 AddIncludesRAII(std::string &SuggestedPredefines,
295 std::string &IncludesAfterPCH)
296 : SuggestedPredefines(SuggestedPredefines),
297 IncludesAfterPCH(IncludesAfterPCH) { }
298 ~AddIncludesRAII() {
299 SuggestedPredefines += IncludesAfterPCH;
300 }
301 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000302
Daniel Dunbar499baed2009-11-11 05:26:28 +0000303 // Sort both sets of predefined buffer lines, since we allow some extra
304 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000305 std::sort(CmdLineLines.begin(), CmdLineLines.end());
306 std::sort(PCHLines.begin(), PCHLines.end());
307
Daniel Dunbar499baed2009-11-11 05:26:28 +0000308 // Determine which predefines that were used to build the PCH file are missing
309 // from the command line.
310 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000311 std::set_difference(PCHLines.begin(), PCHLines.end(),
312 CmdLineLines.begin(), CmdLineLines.end(),
313 std::back_inserter(MissingPredefines));
314
315 bool MissingDefines = false;
316 bool ConflictingDefines = false;
317 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000318 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000319 if (Missing.startswith("#include ")) {
320 // An -include was specified when generating the PCH; it is included in
321 // the PCH, just ignore it.
322 continue;
323 }
Daniel Dunbar499baed2009-11-11 05:26:28 +0000324 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000325 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
326 return true;
327 }
Mike Stump11289f42009-09-09 15:08:12 +0000328
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000329 // This is a macro definition. Determine the name of the macro we're
330 // defining.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000331 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000332 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000333 = Missing.find_first_of("( \n\r", StartOfMacroName);
334 assert(EndOfMacroName != std::string::npos &&
335 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000336 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000337
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000338 // Determine whether this macro was given a different definition on the
339 // command line.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000340 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000341 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000342 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000343 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
344 MacroDefStart);
345 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000346 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000347 // Different macro; we're done.
348 ConflictPos = CmdLineLines.end();
Mike Stump11289f42009-09-09 15:08:12 +0000349 break;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000350 }
Mike Stump11289f42009-09-09 15:08:12 +0000351
352 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000353 "Invalid #define in predefines buffer?");
Mike Stump11289f42009-09-09 15:08:12 +0000354 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000355 (*ConflictPos)[MacroDefLen] != '(')
356 continue; // Longer macro name; keep trying.
Mike Stump11289f42009-09-09 15:08:12 +0000357
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000358 // We found a conflicting macro definition.
359 break;
360 }
Mike Stump11289f42009-09-09 15:08:12 +0000361
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000362 if (ConflictPos != CmdLineLines.end()) {
363 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
364 << MacroName;
365
366 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000367 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
368 FindMacro(Buffers, Missing);
369 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
370 SourceLocation PCHMissingLoc =
371 SourceMgr.getLocForStartOfFile(MacroLoc.first)
372 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar499baed2009-11-11 05:26:28 +0000373 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000374
375 ConflictingDefines = true;
376 continue;
377 }
Mike Stump11289f42009-09-09 15:08:12 +0000378
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000379 // If the macro doesn't conflict, then we'll just pick up the macro
380 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000381 if (ConflictingDefines)
382 continue; // Don't complain if there are already conflicting defs
Mike Stump11289f42009-09-09 15:08:12 +0000383
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000384 if (!MissingDefines) {
385 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
386 MissingDefines = true;
387 }
388
389 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000390 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
391 FindMacro(Buffers, Missing);
392 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
393 SourceLocation PCHMissingLoc =
394 SourceMgr.getLocForStartOfFile(MacroLoc.first)
395 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000396 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
397 }
Mike Stump11289f42009-09-09 15:08:12 +0000398
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000399 if (ConflictingDefines)
400 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000401
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000402 // Determine what predefines were introduced based on command-line
403 // parameters that were not present when building the PCH
404 // file. Extra #defines are okay, so long as the identifiers being
405 // defined were not used within the precompiled header.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000406 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000407 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
408 PCHLines.begin(), PCHLines.end(),
Mike Stump11289f42009-09-09 15:08:12 +0000409 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000410 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000411 llvm::StringRef &Extra = ExtraPredefines[I];
412 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000413 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
414 return true;
415 }
416
417 // This is an extra macro definition. Determine the name of the
418 // macro we're defining.
419 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000420 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000421 = Extra.find_first_of("( \n\r", StartOfMacroName);
422 assert(EndOfMacroName != std::string::npos &&
423 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000424 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000425
426 // Check whether this name was used somewhere in the PCH file. If
427 // so, defining it as a macro could change behavior, so we reject
428 // the PCH file.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000429 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar045c92f2009-11-11 00:52:00 +0000430 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000431 return true;
432 }
433
434 // Add this definition to the suggested predefines buffer.
435 SuggestedPredefines += Extra;
436 SuggestedPredefines += '\n';
437 }
438
439 // If we get here, it's because the predefines buffer had compatible
440 // contents. Accept the PCH file.
441 return false;
442}
443
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000444void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
445 unsigned ID) {
446 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
447 ++NumHeaderInfos;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000448}
449
450void PCHValidator::ReadCounter(unsigned Value) {
451 PP.setCounterValue(Value);
452}
453
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000454//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +0000455// AST reader implementation
Douglas Gregora868bbd2009-04-21 22:25:48 +0000456//===----------------------------------------------------------------------===//
457
Sebastian Redl07a89a82010-07-30 00:29:29 +0000458void
Sebastian Redl3e31c722010-08-18 23:56:56 +0000459ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redl07a89a82010-07-30 00:29:29 +0000460 DeserializationListener = Listener;
Sebastian Redl07a89a82010-07-30 00:29:29 +0000461}
462
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000463
Douglas Gregora868bbd2009-04-21 22:25:48 +0000464namespace {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000465class ASTSelectorLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000466 ASTReader &Reader;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000467
468public:
Sebastian Redl834bb972010-08-04 17:20:04 +0000469 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +0000470 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +0000471 ObjCMethodList Instance, Factory;
472 };
Douglas Gregorc78d3462009-04-24 21:10:55 +0000473
474 typedef Selector external_key_type;
475 typedef external_key_type internal_key_type;
476
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000477 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump11289f42009-09-09 15:08:12 +0000478
Douglas Gregorc78d3462009-04-24 21:10:55 +0000479 static bool EqualKey(const internal_key_type& a,
480 const internal_key_type& b) {
481 return a == b;
482 }
Mike Stump11289f42009-09-09 15:08:12 +0000483
Douglas Gregorc78d3462009-04-24 21:10:55 +0000484 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +0000485 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000486 }
Mike Stump11289f42009-09-09 15:08:12 +0000487
Douglas Gregorc78d3462009-04-24 21:10:55 +0000488 // This hopefully will just get inlined and removed by the optimizer.
489 static const internal_key_type&
490 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000491
Douglas Gregorc78d3462009-04-24 21:10:55 +0000492 static std::pair<unsigned, unsigned>
493 ReadKeyDataLength(const unsigned char*& d) {
494 using namespace clang::io;
495 unsigned KeyLen = ReadUnalignedLE16(d);
496 unsigned DataLen = ReadUnalignedLE16(d);
497 return std::make_pair(KeyLen, DataLen);
498 }
Mike Stump11289f42009-09-09 15:08:12 +0000499
Douglas Gregor95c13f52009-04-25 17:48:32 +0000500 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000501 using namespace clang::io;
Chris Lattner8575daa2009-04-27 21:45:14 +0000502 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000503 unsigned N = ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000504 IdentifierInfo *FirstII
Douglas Gregorc78d3462009-04-24 21:10:55 +0000505 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
506 if (N == 0)
507 return SelTable.getNullarySelector(FirstII);
508 else if (N == 1)
509 return SelTable.getUnarySelector(FirstII);
510
511 llvm::SmallVector<IdentifierInfo *, 16> Args;
512 Args.push_back(FirstII);
513 for (unsigned I = 1; I != N; ++I)
514 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
515
Douglas Gregor038c3382009-05-22 22:45:36 +0000516 return SelTable.getSelector(N, Args.data());
Douglas Gregorc78d3462009-04-24 21:10:55 +0000517 }
Mike Stump11289f42009-09-09 15:08:12 +0000518
Douglas Gregorc78d3462009-04-24 21:10:55 +0000519 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
520 using namespace clang::io;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000521
522 data_type Result;
523
Sebastian Redl834bb972010-08-04 17:20:04 +0000524 Result.ID = ReadUnalignedLE32(d);
525 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
526 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
527
Douglas Gregorc78d3462009-04-24 21:10:55 +0000528 // Load instance methods
529 ObjCMethodList *Prev = 0;
530 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000531 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000532 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000533 if (!Result.Instance.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000534 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000535 Result.Instance.Method = Method;
536 Prev = &Result.Instance;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000537 continue;
538 }
539
Ted Kremenekda4abf12010-02-11 00:53:01 +0000540 ObjCMethodList *Mem =
541 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
542 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000543 Prev = Prev->Next;
544 }
545
546 // Load factory methods
547 Prev = 0;
548 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000549 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000550 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000551 if (!Result.Factory.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000552 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000553 Result.Factory.Method = Method;
554 Prev = &Result.Factory;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000555 continue;
556 }
557
Ted Kremenekda4abf12010-02-11 00:53:01 +0000558 ObjCMethodList *Mem =
559 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
560 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000561 Prev = Prev->Next;
562 }
563
564 return Result;
565 }
566};
Mike Stump11289f42009-09-09 15:08:12 +0000567
568} // end anonymous namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +0000569
570/// \brief The on-disk hash table used for the global method pool.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000571typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
572 ASTSelectorLookupTable;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000573
Sebastian Redl2c373b92010-10-05 15:59:54 +0000574namespace clang {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000575class ASTIdentifierLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000576 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +0000577 ASTReader::PerFileData &F;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000578
579 // If we know the IdentifierInfo in advance, it is here and we will
580 // not build a new one. Used when deserializing information about an
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000581 // identifier that was constructed before the AST file was read.
Douglas Gregora868bbd2009-04-21 22:25:48 +0000582 IdentifierInfo *KnownII;
583
584public:
585 typedef IdentifierInfo * data_type;
586
587 typedef const std::pair<const char*, unsigned> external_key_type;
588
589 typedef external_key_type internal_key_type;
590
Sebastian Redl2c373b92010-10-05 15:59:54 +0000591 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000592 IdentifierInfo *II = 0)
Sebastian Redl2c373b92010-10-05 15:59:54 +0000593 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump11289f42009-09-09 15:08:12 +0000594
Douglas Gregora868bbd2009-04-21 22:25:48 +0000595 static bool EqualKey(const internal_key_type& a,
596 const internal_key_type& b) {
597 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
598 : false;
599 }
Mike Stump11289f42009-09-09 15:08:12 +0000600
Douglas Gregora868bbd2009-04-21 22:25:48 +0000601 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000602 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregora868bbd2009-04-21 22:25:48 +0000603 }
Mike Stump11289f42009-09-09 15:08:12 +0000604
Douglas Gregora868bbd2009-04-21 22:25:48 +0000605 // This hopefully will just get inlined and removed by the optimizer.
606 static const internal_key_type&
607 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000608
Douglas Gregor57756ea2010-10-14 22:11:03 +0000609 // This hopefully will just get inlined and removed by the optimizer.
610 static const external_key_type&
611 GetExternalKey(const internal_key_type& x) { return x; }
612
Douglas Gregora868bbd2009-04-21 22:25:48 +0000613 static std::pair<unsigned, unsigned>
614 ReadKeyDataLength(const unsigned char*& d) {
615 using namespace clang::io;
Douglas Gregor6b7bf5a2009-04-25 20:26:24 +0000616 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor5287b4e2009-04-25 21:04:17 +0000617 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000618 return std::make_pair(KeyLen, DataLen);
619 }
Mike Stump11289f42009-09-09 15:08:12 +0000620
Douglas Gregora868bbd2009-04-21 22:25:48 +0000621 static std::pair<const char*, unsigned>
622 ReadKey(const unsigned char* d, unsigned n) {
623 assert(n >= 2 && d[n-1] == '\0');
624 return std::make_pair((const char*) d, n-1);
625 }
Mike Stump11289f42009-09-09 15:08:12 +0000626
627 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregora868bbd2009-04-21 22:25:48 +0000628 const unsigned char* d,
629 unsigned DataLen) {
630 using namespace clang::io;
Sebastian Redl539c5062010-08-18 23:57:32 +0000631 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000632 bool IsInteresting = ID & 0x01;
633
634 // Wipe out the "is interesting" bit.
635 ID = ID >> 1;
636
637 if (!IsInteresting) {
Sebastian Redl98912122010-07-27 23:01:28 +0000638 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregor1d583f22009-04-28 21:18:29 +0000639 // and associate it with the persistent ID.
640 IdentifierInfo *II = KnownII;
641 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000642 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000643 Reader.SetIdentifierInfo(ID, II);
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000644 II->setIsFromAST();
Douglas Gregor1d583f22009-04-28 21:18:29 +0000645 return II;
646 }
647
Douglas Gregorb9256522009-04-28 21:32:13 +0000648 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000649 bool CPlusPlusOperatorKeyword = Bits & 0x01;
650 Bits >>= 1;
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000651 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
652 Bits >>= 1;
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000653 bool Poisoned = Bits & 0x01;
654 Bits >>= 1;
655 bool ExtensionToken = Bits & 0x01;
656 Bits >>= 1;
657 bool hasMacroDefinition = Bits & 0x01;
658 Bits >>= 1;
659 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
660 Bits >>= 10;
Mike Stump11289f42009-09-09 15:08:12 +0000661
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000662 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorb9256522009-04-28 21:32:13 +0000663 DataLen -= 6;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000664
665 // Build the IdentifierInfo itself and link the identifier ID with
666 // the new IdentifierInfo.
667 IdentifierInfo *II = KnownII;
668 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000669 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000670 Reader.SetIdentifierInfo(ID, II);
671
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000672 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000673 // Token IDs are read-only.
674 if (HasRevertedTokenIDToIdentifier)
675 II->RevertTokenIDToIdentifier();
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000676 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump11289f42009-09-09 15:08:12 +0000677 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000678 "Incorrect extension token flag");
679 (void)ExtensionToken;
680 II->setIsPoisoned(Poisoned);
681 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
682 "Incorrect C++ operator keyword flag");
683 (void)CPlusPlusOperatorKeyword;
684
Douglas Gregorc3366a52009-04-21 23:56:24 +0000685 // If this identifier is a macro, deserialize the macro
686 // definition.
687 if (hasMacroDefinition) {
Douglas Gregorb9256522009-04-28 21:32:13 +0000688 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor5ef9e332010-10-30 00:23:06 +0000689 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregorb9256522009-04-28 21:32:13 +0000690 DataLen -= 4;
Douglas Gregorc3366a52009-04-21 23:56:24 +0000691 }
Douglas Gregora868bbd2009-04-21 22:25:48 +0000692
693 // Read all of the declarations visible at global scope with this
694 // name.
Chris Lattner1d728882009-04-27 22:17:41 +0000695 if (Reader.getContext() == 0) return II;
Douglas Gregor1342e842009-07-06 18:54:52 +0000696 if (DataLen > 0) {
697 llvm::SmallVector<uint32_t, 4> DeclIDs;
698 for (; DataLen > 0; DataLen -= 4)
699 DeclIDs.push_back(ReadUnalignedLE32(d));
700 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000701 }
Mike Stump11289f42009-09-09 15:08:12 +0000702
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000703 II->setIsFromAST();
Douglas Gregora868bbd2009-04-21 22:25:48 +0000704 return II;
705 }
706};
Mike Stump11289f42009-09-09 15:08:12 +0000707
708} // end anonymous namespace
Douglas Gregora868bbd2009-04-21 22:25:48 +0000709
710/// \brief The on-disk hash table used to contain information about
711/// all of the identifiers in the program.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000712typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
713 ASTIdentifierLookupTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000714
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000715namespace {
716class ASTDeclContextNameLookupTrait {
717 ASTReader &Reader;
718
719public:
720 /// \brief Pair of begin/end iterators for DeclIDs.
721 typedef std::pair<DeclID *, DeclID *> data_type;
722
723 /// \brief Special internal key for declaration names.
724 /// The hash table creates keys for comparison; we do not create
725 /// a DeclarationName for the internal key to avoid deserializing types.
726 struct DeclNameKey {
727 DeclarationName::NameKind Kind;
728 uint64_t Data;
729 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
730 };
731
732 typedef DeclarationName external_key_type;
733 typedef DeclNameKey internal_key_type;
734
735 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
736
737 static bool EqualKey(const internal_key_type& a,
738 const internal_key_type& b) {
739 return a.Kind == b.Kind && a.Data == b.Data;
740 }
741
742 unsigned ComputeHash(const DeclNameKey &Key) const {
743 llvm::FoldingSetNodeID ID;
744 ID.AddInteger(Key.Kind);
745
746 switch (Key.Kind) {
747 case DeclarationName::Identifier:
748 case DeclarationName::CXXLiteralOperatorName:
749 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
750 break;
751 case DeclarationName::ObjCZeroArgSelector:
752 case DeclarationName::ObjCOneArgSelector:
753 case DeclarationName::ObjCMultiArgSelector:
754 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
755 break;
756 case DeclarationName::CXXConstructorName:
757 case DeclarationName::CXXDestructorName:
758 case DeclarationName::CXXConversionFunctionName:
759 ID.AddInteger((TypeID)Key.Data);
760 break;
761 case DeclarationName::CXXOperatorName:
762 ID.AddInteger((OverloadedOperatorKind)Key.Data);
763 break;
764 case DeclarationName::CXXUsingDirective:
765 break;
766 }
767
768 return ID.ComputeHash();
769 }
770
771 internal_key_type GetInternalKey(const external_key_type& Name) const {
772 DeclNameKey Key;
773 Key.Kind = Name.getNameKind();
774 switch (Name.getNameKind()) {
775 case DeclarationName::Identifier:
776 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
777 break;
778 case DeclarationName::ObjCZeroArgSelector:
779 case DeclarationName::ObjCOneArgSelector:
780 case DeclarationName::ObjCMultiArgSelector:
781 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
782 break;
783 case DeclarationName::CXXConstructorName:
784 case DeclarationName::CXXDestructorName:
785 case DeclarationName::CXXConversionFunctionName:
786 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
787 break;
788 case DeclarationName::CXXOperatorName:
789 Key.Data = Name.getCXXOverloadedOperator();
790 break;
791 case DeclarationName::CXXLiteralOperatorName:
792 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
793 break;
794 case DeclarationName::CXXUsingDirective:
795 break;
796 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000797
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000798 return Key;
799 }
800
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000801 external_key_type GetExternalKey(const internal_key_type& Key) const {
802 ASTContext *Context = Reader.getContext();
803 switch (Key.Kind) {
804 case DeclarationName::Identifier:
805 return DeclarationName((IdentifierInfo*)Key.Data);
806
807 case DeclarationName::ObjCZeroArgSelector:
808 case DeclarationName::ObjCOneArgSelector:
809 case DeclarationName::ObjCMultiArgSelector:
810 return DeclarationName(Selector(Key.Data));
811
812 case DeclarationName::CXXConstructorName:
813 return Context->DeclarationNames.getCXXConstructorName(
814 Context->getCanonicalType(Reader.GetType(Key.Data)));
815
816 case DeclarationName::CXXDestructorName:
817 return Context->DeclarationNames.getCXXDestructorName(
818 Context->getCanonicalType(Reader.GetType(Key.Data)));
819
820 case DeclarationName::CXXConversionFunctionName:
821 return Context->DeclarationNames.getCXXConversionFunctionName(
822 Context->getCanonicalType(Reader.GetType(Key.Data)));
823
824 case DeclarationName::CXXOperatorName:
825 return Context->DeclarationNames.getCXXOperatorName(
826 (OverloadedOperatorKind)Key.Data);
827
828 case DeclarationName::CXXLiteralOperatorName:
829 return Context->DeclarationNames.getCXXLiteralOperatorName(
830 (IdentifierInfo*)Key.Data);
831
832 case DeclarationName::CXXUsingDirective:
833 return DeclarationName::getUsingDirectiveName();
834 }
835
836 llvm_unreachable("Invalid Name Kind ?");
837 }
838
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000839 static std::pair<unsigned, unsigned>
840 ReadKeyDataLength(const unsigned char*& d) {
841 using namespace clang::io;
842 unsigned KeyLen = ReadUnalignedLE16(d);
843 unsigned DataLen = ReadUnalignedLE16(d);
844 return std::make_pair(KeyLen, DataLen);
845 }
846
847 internal_key_type ReadKey(const unsigned char* d, unsigned) {
848 using namespace clang::io;
849
850 DeclNameKey Key;
851 Key.Kind = (DeclarationName::NameKind)*d++;
852 switch (Key.Kind) {
853 case DeclarationName::Identifier:
854 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
855 break;
856 case DeclarationName::ObjCZeroArgSelector:
857 case DeclarationName::ObjCOneArgSelector:
858 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000859 Key.Data =
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000860 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
861 break;
862 case DeclarationName::CXXConstructorName:
863 case DeclarationName::CXXDestructorName:
864 case DeclarationName::CXXConversionFunctionName:
865 Key.Data = ReadUnalignedLE32(d); // TypeID
866 break;
867 case DeclarationName::CXXOperatorName:
868 Key.Data = *d++; // OverloadedOperatorKind
869 break;
870 case DeclarationName::CXXLiteralOperatorName:
871 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
872 break;
873 case DeclarationName::CXXUsingDirective:
874 break;
875 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000876
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000877 return Key;
878 }
879
880 data_type ReadData(internal_key_type, const unsigned char* d,
881 unsigned DataLen) {
882 using namespace clang::io;
883 unsigned NumDecls = ReadUnalignedLE16(d);
884 DeclID *Start = (DeclID *)d;
885 return std::make_pair(Start, Start + NumDecls);
886 }
887};
888
889} // end anonymous namespace
890
891/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
892typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
893 ASTDeclContextNameLookupTable;
894
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000895bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
896 const std::pair<uint64_t, uint64_t> &Offsets,
897 DeclContextInfo &Info) {
898 SavedStreamPosition SavedPosition(Cursor);
899 // First the lexical decls.
900 if (Offsets.first != 0) {
901 Cursor.JumpToBit(Offsets.first);
902
903 RecordData Record;
904 const char *Blob;
905 unsigned BlobLen;
906 unsigned Code = Cursor.ReadCode();
907 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
908 if (RecCode != DECL_CONTEXT_LEXICAL) {
909 Error("Expected lexical block");
910 return true;
911 }
912
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000913 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
914 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000915 } else {
916 Info.LexicalDecls = 0;
917 Info.NumLexicalDecls = 0;
918 }
919
920 // Now the lookup table.
921 if (Offsets.second != 0) {
922 Cursor.JumpToBit(Offsets.second);
923
924 RecordData Record;
925 const char *Blob;
926 unsigned BlobLen;
927 unsigned Code = Cursor.ReadCode();
928 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
929 if (RecCode != DECL_CONTEXT_VISIBLE) {
930 Error("Expected visible lookup table block");
931 return true;
932 }
933 Info.NameLookupTableData
934 = ASTDeclContextNameLookupTable::Create(
935 (const unsigned char *)Blob + Record[0],
936 (const unsigned char *)Blob,
937 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl9d8f58b2010-08-24 00:50:00 +0000938 } else {
939 Info.NameLookupTableData = 0;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000940 }
941
942 return false;
943}
944
Sebastian Redl2c499f62010-08-18 23:56:43 +0000945void ASTReader::Error(const char *Msg) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000946 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000947}
948
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000949/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000950bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000951 if (Listener)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000952 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000953 ActualOriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000954 SuggestedPredefines);
Douglas Gregorc379c072009-04-28 18:58:38 +0000955 return false;
Douglas Gregor92863e42009-04-10 23:10:45 +0000956}
957
Douglas Gregorc5046832009-04-27 18:38:38 +0000958//===----------------------------------------------------------------------===//
959// Source Manager Deserialization
960//===----------------------------------------------------------------------===//
961
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000962/// \brief Read the line table in the source manager block.
Sebastian Redl2c373b92010-10-05 15:59:54 +0000963/// \returns true if there was an error.
964bool ASTReader::ParseLineTable(PerFileData &F,
965 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000966 unsigned Idx = 0;
967 LineTableInfo &LineTable = SourceMgr.getLineTable();
968
969 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +0000970 std::map<int, int> FileIDs;
971 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000972 // Extract the file name
973 unsigned FilenameLen = Record[Idx++];
974 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
975 Idx += FilenameLen;
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000976 MaybeAddSystemRootToFilename(Filename);
Mike Stump11289f42009-09-09 15:08:12 +0000977 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregora8854652009-04-13 17:12:42 +0000978 Filename.size());
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000979 }
980
981 // Parse the line entries
982 std::vector<LineEntry> Entries;
983 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000984 int FID = Record[Idx++];
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000985
986 // Extract the line entries
987 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000988 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000989 Entries.clear();
990 Entries.reserve(NumEntries);
991 for (unsigned I = 0; I != NumEntries; ++I) {
992 unsigned FileOffset = Record[Idx++];
993 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000994 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +0000995 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000996 = (SrcMgr::CharacteristicKind)Record[Idx++];
997 unsigned IncludeOffset = Record[Idx++];
998 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
999 FileKind, IncludeOffset));
1000 }
1001 LineTable.AddEntry(FID, Entries);
1002 }
1003
1004 return false;
1005}
1006
Douglas Gregorc5046832009-04-27 18:38:38 +00001007namespace {
1008
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001009class ASTStatData {
Douglas Gregorc5046832009-04-27 18:38:38 +00001010public:
Douglas Gregorc5046832009-04-27 18:38:38 +00001011 const ino_t ino;
1012 const dev_t dev;
1013 const mode_t mode;
1014 const time_t mtime;
1015 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +00001016
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001017 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner2a6fa472010-11-23 19:28:12 +00001018 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregorc5046832009-04-27 18:38:38 +00001019};
1020
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001021class ASTStatLookupTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001022 public:
1023 typedef const char *external_key_type;
1024 typedef const char *internal_key_type;
1025
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001026 typedef ASTStatData data_type;
Douglas Gregorc5046832009-04-27 18:38:38 +00001027
1028 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001029 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001030 }
1031
1032 static internal_key_type GetInternalKey(const char *path) { return path; }
1033
1034 static bool EqualKey(internal_key_type a, internal_key_type b) {
1035 return strcmp(a, b) == 0;
1036 }
1037
1038 static std::pair<unsigned, unsigned>
1039 ReadKeyDataLength(const unsigned char*& d) {
1040 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1041 unsigned DataLen = (unsigned) *d++;
1042 return std::make_pair(KeyLen + 1, DataLen);
1043 }
1044
1045 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1046 return (const char *)d;
1047 }
1048
1049 static data_type ReadData(const internal_key_type, const unsigned char *d,
1050 unsigned /*DataLen*/) {
1051 using namespace clang::io;
1052
Douglas Gregorc5046832009-04-27 18:38:38 +00001053 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1054 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1055 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +00001056 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +00001057 off_t size = (off_t) ReadUnalignedLE64(d);
1058 return data_type(ino, dev, mode, mtime, size);
1059 }
1060};
1061
1062/// \brief stat() cache for precompiled headers.
1063///
1064/// This cache is very similar to the stat cache used by pretokenized
1065/// headers.
Chris Lattner226efd32010-11-23 19:19:34 +00001066class ASTStatCache : public FileSystemStatCache {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001067 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregorc5046832009-04-27 18:38:38 +00001068 CacheTy *Cache;
1069
1070 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +00001071public:
Chris Lattner2a6fa472010-11-23 19:28:12 +00001072 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1073 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +00001074 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1075 Cache = CacheTy::Create(Buckets, Base);
1076 }
1077
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001078 ~ASTStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +00001079
Chris Lattnerdd278432010-11-23 21:17:56 +00001080 LookupResult getStat(const char *Path, struct stat &StatBuf,
1081 int *FileDescriptor) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001082 // Do the lookup for the file's data in the AST file.
Chris Lattner226efd32010-11-23 19:19:34 +00001083 CacheTy::iterator I = Cache->find(Path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001084
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001085 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregorc5046832009-04-27 18:38:38 +00001086 if (I == Cache->end()) {
1087 ++NumStatMisses;
Chris Lattnerdd278432010-11-23 21:17:56 +00001088 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregorc5046832009-04-27 18:38:38 +00001089 }
Mike Stump11289f42009-09-09 15:08:12 +00001090
Douglas Gregorc5046832009-04-27 18:38:38 +00001091 ++NumStatHits;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001092 ASTStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001093
Chris Lattner226efd32010-11-23 19:19:34 +00001094 StatBuf.st_ino = Data.ino;
1095 StatBuf.st_dev = Data.dev;
1096 StatBuf.st_mtime = Data.mtime;
1097 StatBuf.st_mode = Data.mode;
1098 StatBuf.st_size = Data.size;
Chris Lattner8f0583d2010-11-23 20:05:15 +00001099 return CacheExists;
Douglas Gregorc5046832009-04-27 18:38:38 +00001100 }
1101};
1102} // end anonymous namespace
1103
1104
Sebastian Redl393f8b72010-07-19 20:52:06 +00001105/// \brief Read a source manager block
Sebastian Redl2c499f62010-08-18 23:56:43 +00001106ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001107 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +00001108
Sebastian Redl393f8b72010-07-19 20:52:06 +00001109 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001110
Douglas Gregor258ae542009-04-27 06:38:32 +00001111 // Set the source-location entry cursor to the current position in
1112 // the stream. This cursor will be used to read the contents of the
1113 // source manager block initially, and then lazily read
1114 // source-location entries as needed.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001115 SLocEntryCursor = F.Stream;
Douglas Gregor258ae542009-04-27 06:38:32 +00001116
1117 // The stream itself is going to skip over the source manager block.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001118 if (F.Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001119 Error("malformed block record in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001120 return Failure;
1121 }
1122
1123 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001124 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001125 Error("malformed source manager block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001126 return Failure;
1127 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001128
Douglas Gregora7f71a92009-04-10 03:52:48 +00001129 RecordData Record;
1130 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001131 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001132 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001133 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001134 Error("error at end of Source Manager block in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001135 return Failure;
1136 }
Douglas Gregor92863e42009-04-10 23:10:45 +00001137 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001138 }
Mike Stump11289f42009-09-09 15:08:12 +00001139
Douglas Gregora7f71a92009-04-10 03:52:48 +00001140 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1141 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +00001142 SLocEntryCursor.ReadSubBlockID();
1143 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001144 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001145 return Failure;
1146 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001147 continue;
1148 }
Mike Stump11289f42009-09-09 15:08:12 +00001149
Douglas Gregora7f71a92009-04-10 03:52:48 +00001150 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001151 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001152 continue;
1153 }
Mike Stump11289f42009-09-09 15:08:12 +00001154
Douglas Gregora7f71a92009-04-10 03:52:48 +00001155 // Read a record.
1156 const char *BlobStart;
1157 unsigned BlobLen;
1158 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +00001159 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001160 default: // Default behavior: ignore.
1161 break;
1162
Sebastian Redl539c5062010-08-18 23:57:32 +00001163 case SM_LINE_TABLE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00001164 if (ParseLineTable(F, Record))
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001165 return Failure;
Chris Lattner184e65d2009-04-14 23:22:57 +00001166 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00001167
Sebastian Redl539c5062010-08-18 23:57:32 +00001168 case SM_SLOC_FILE_ENTRY:
1169 case SM_SLOC_BUFFER_ENTRY:
1170 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor258ae542009-04-27 06:38:32 +00001171 // Once we hit one of the source location entries, we're done.
1172 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001173 }
1174 }
1175}
1176
Sebastian Redl06750302010-07-20 21:50:20 +00001177/// \brief Get a cursor that's correctly positioned for reading the source
1178/// location entry with the given ID.
Sebastian Redl2c373b92010-10-05 15:59:54 +00001179ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl06750302010-07-20 21:50:20 +00001180 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1181 "SLocCursorForID should only be called for real IDs.");
1182
1183 ID -= 1;
1184 PerFileData *F = 0;
1185 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1186 F = Chain[N - I - 1];
1187 if (ID < F->LocalNumSLocEntries)
1188 break;
1189 ID -= F->LocalNumSLocEntries;
1190 }
1191 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1192
1193 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00001194 return F;
Sebastian Redl06750302010-07-20 21:50:20 +00001195}
1196
Douglas Gregor258ae542009-04-27 06:38:32 +00001197/// \brief Read in the source location entry with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001198ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001199 if (ID == 0)
1200 return Success;
1201
1202 if (ID > TotalNumSLocEntries) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001203 Error("source location entry ID out-of-range for AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001204 return Failure;
1205 }
1206
Sebastian Redl2c373b92010-10-05 15:59:54 +00001207 PerFileData *F = SLocCursorForID(ID);
1208 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001209
Douglas Gregor258ae542009-04-27 06:38:32 +00001210 ++NumSLocEntriesRead;
Douglas Gregor258ae542009-04-27 06:38:32 +00001211 unsigned Code = SLocEntryCursor.ReadCode();
1212 if (Code == llvm::bitc::END_BLOCK ||
1213 Code == llvm::bitc::ENTER_SUBBLOCK ||
1214 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001215 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001216 return Failure;
1217 }
1218
Douglas Gregor258ae542009-04-27 06:38:32 +00001219 RecordData Record;
1220 const char *BlobStart;
1221 unsigned BlobLen;
1222 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1223 default:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001224 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001225 return Failure;
1226
Sebastian Redl539c5062010-08-18 23:57:32 +00001227 case SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001228 std::string Filename(BlobStart, BlobStart + BlobLen);
1229 MaybeAddSystemRootToFilename(Filename);
Chris Lattner5159f612010-11-23 08:35:12 +00001230 const FileEntry *File = FileMgr.getFile(Filename);
Chris Lattnerd20dc872009-06-15 04:35:16 +00001231 if (File == 0) {
1232 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001233 ErrorStr += Filename;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001234 ErrorStr += "' referenced by AST file";
Chris Lattnerd20dc872009-06-15 04:35:16 +00001235 Error(ErrorStr.c_str());
1236 return Failure;
1237 }
Mike Stump11289f42009-09-09 15:08:12 +00001238
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001239 if (Record.size() < 10) {
Ted Kremenekabb1ddd2010-03-18 21:23:05 +00001240 Error("source location entry is incorrect");
1241 return Failure;
1242 }
1243
Douglas Gregorce3a8292010-07-27 00:27:13 +00001244 if (!DisableValidation &&
1245 ((off_t)Record[4] != File->getSize()
Douglas Gregor08288f22010-04-09 15:54:22 +00001246#if !defined(LLVM_ON_WIN32)
1247 // In our regression testing, the Windows file system seems to
1248 // have inconsistent modification times that sometimes
1249 // erroneously trigger this error-handling path.
Douglas Gregorce3a8292010-07-27 00:27:13 +00001250 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor08288f22010-04-09 15:54:22 +00001251#endif
Douglas Gregorce3a8292010-07-27 00:27:13 +00001252 )) {
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001253 Diag(diag::err_fe_pch_file_modified)
1254 << Filename;
1255 return Failure;
1256 }
1257
Chris Lattner26b5c192010-11-23 09:19:42 +00001258 FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]),
1259 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor258ae542009-04-27 06:38:32 +00001260 ID, Record[0]);
1261 if (Record[3])
1262 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1263 .setHasLineDirectives();
1264
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001265 // Reconstruct header-search information for this file.
1266 HeaderFileInfo HFI;
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001267 HFI.isImport = Record[6];
1268 HFI.DirInfo = Record[7];
1269 HFI.NumIncludes = Record[8];
1270 HFI.ControllingMacroID = Record[9];
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001271 if (Listener)
1272 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor258ae542009-04-27 06:38:32 +00001273 break;
1274 }
1275
Sebastian Redl539c5062010-08-18 23:57:32 +00001276 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor258ae542009-04-27 06:38:32 +00001277 const char *Name = BlobStart;
1278 unsigned Offset = Record[0];
1279 unsigned Code = SLocEntryCursor.ReadCode();
1280 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001281 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +00001282 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001283
Sebastian Redl539c5062010-08-18 23:57:32 +00001284 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001285 Error("AST record has invalid code");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001286 return Failure;
1287 }
1288
Douglas Gregor258ae542009-04-27 06:38:32 +00001289 llvm::MemoryBuffer *Buffer
Chris Lattner58c79342010-04-05 22:42:27 +00001290 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1291 Name);
Douglas Gregor258ae542009-04-27 06:38:32 +00001292 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump11289f42009-09-09 15:08:12 +00001293
Douglas Gregore6648fb2009-04-28 20:33:11 +00001294 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001295 PCHPredefinesBlock Block = {
1296 BufferID,
1297 llvm::StringRef(BlobStart, BlobLen - 1)
1298 };
1299 PCHPredefinesBuffers.push_back(Block);
Douglas Gregore6648fb2009-04-28 20:33:11 +00001300 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001301
1302 break;
1303 }
1304
Sebastian Redl539c5062010-08-18 23:57:32 +00001305 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001306 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001307 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001308 ReadSourceLocation(*F, Record[2]),
1309 ReadSourceLocation(*F, Record[3]),
Douglas Gregor258ae542009-04-27 06:38:32 +00001310 Record[4],
1311 ID,
1312 Record[0]);
1313 break;
Mike Stump11289f42009-09-09 15:08:12 +00001314 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001315 }
1316
1317 return Success;
1318}
1319
Chris Lattnere78a6be2009-04-27 01:05:14 +00001320/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1321/// specified cursor. Read the abbreviations that are at the top of the block
1322/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001323bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001324 unsigned BlockID) {
1325 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001326 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001327 return Failure;
1328 }
Mike Stump11289f42009-09-09 15:08:12 +00001329
Chris Lattnere78a6be2009-04-27 01:05:14 +00001330 while (true) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001331 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattnere78a6be2009-04-27 01:05:14 +00001332 unsigned Code = Cursor.ReadCode();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001333
Chris Lattnere78a6be2009-04-27 01:05:14 +00001334 // We expect all abbrevs to be at the start of the block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001335 if (Code != llvm::bitc::DEFINE_ABBREV) {
1336 Cursor.JumpToBit(Offset);
Chris Lattnere78a6be2009-04-27 01:05:14 +00001337 return false;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001338 }
Chris Lattnere78a6be2009-04-27 01:05:14 +00001339 Cursor.ReadAbbrevRecord();
1340 }
1341}
1342
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001343PreprocessedEntity *ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001344 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregor796d76a2010-10-20 22:00:55 +00001345 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump11289f42009-09-09 15:08:12 +00001346
Douglas Gregorc3366a52009-04-21 23:56:24 +00001347 // Keep track of where we are in the stream, then jump back there
1348 // after reading this macro.
1349 SavedStreamPosition SavedPosition(Stream);
1350
1351 Stream.JumpToBit(Offset);
1352 RecordData Record;
1353 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1354 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001355
Douglas Gregorc3366a52009-04-21 23:56:24 +00001356 while (true) {
1357 unsigned Code = Stream.ReadCode();
1358 switch (Code) {
1359 case llvm::bitc::END_BLOCK:
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001360 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001361
1362 case llvm::bitc::ENTER_SUBBLOCK:
1363 // No known subblocks, always skip them.
1364 Stream.ReadSubBlockID();
1365 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001366 Error("malformed block record in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001367 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001368 }
1369 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001370
Douglas Gregorc3366a52009-04-21 23:56:24 +00001371 case llvm::bitc::DEFINE_ABBREV:
1372 Stream.ReadAbbrevRecord();
1373 continue;
1374 default: break;
1375 }
1376
1377 // Read a record.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001378 const char *BlobStart = 0;
1379 unsigned BlobLen = 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001380 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001381 PreprocessorRecordTypes RecType =
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001382 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001383 BlobLen);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001384 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001385 case PP_MACRO_OBJECT_LIKE:
1386 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001387 // If we already have a macro, that means that we've hit the end
1388 // of the definition of the macro we were looking for. We're
1389 // done.
1390 if (Macro)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001391 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001392
1393 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1394 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001395 Error("macro must have a name in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001396 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001397 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00001398 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001399 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001400
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001401 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001402 MI->setIsUsed(isUsed);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001403 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001404
Douglas Gregoraae92242010-03-19 21:51:54 +00001405 unsigned NextIndex = 3;
Sebastian Redl539c5062010-08-18 23:57:32 +00001406 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001407 // Decode function-like macro info.
1408 bool isC99VarArgs = Record[3];
1409 bool isGNUVarArgs = Record[4];
1410 MacroArgs.clear();
1411 unsigned NumArgs = Record[5];
Douglas Gregoraae92242010-03-19 21:51:54 +00001412 NextIndex = 6 + NumArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001413 for (unsigned i = 0; i != NumArgs; ++i)
1414 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1415
1416 // Install function-like macro info.
1417 MI->setIsFunctionLike();
1418 if (isC99VarArgs) MI->setIsC99Varargs();
1419 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001420 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001421 PP->getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001422 }
1423
1424 // Finally, install the macro.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001425 PP->setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001426
1427 // Remember that we saw this macro last so that we add the tokens that
1428 // form its body to it.
1429 Macro = MI;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001430
Douglas Gregoraae92242010-03-19 21:51:54 +00001431 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1432 // We have a macro definition. Load it now.
1433 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1434 getMacroDefinition(Record[NextIndex]));
1435 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001436
Douglas Gregorc3366a52009-04-21 23:56:24 +00001437 ++NumMacrosRead;
1438 break;
1439 }
Mike Stump11289f42009-09-09 15:08:12 +00001440
Sebastian Redl539c5062010-08-18 23:57:32 +00001441 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001442 // If we see a TOKEN before a PP_MACRO_*, then the file is
1443 // erroneous, just pretend we didn't see this.
1444 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001445
Douglas Gregorc3366a52009-04-21 23:56:24 +00001446 Token Tok;
1447 Tok.startToken();
Sebastian Redl2c373b92010-10-05 15:59:54 +00001448 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001449 Tok.setLength(Record[1]);
1450 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1451 Tok.setIdentifierInfo(II);
1452 Tok.setKind((tok::TokenKind)Record[3]);
1453 Tok.setFlag((Token::TokenFlags)Record[4]);
1454 Macro->AddTokenToBody(Tok);
1455 break;
1456 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001457
Sebastian Redl539c5062010-08-18 23:57:32 +00001458 case PP_MACRO_INSTANTIATION: {
Douglas Gregoraae92242010-03-19 21:51:54 +00001459 // If we already have a macro, that means that we've hit the end
1460 // of the definition of the macro we were looking for. We're
1461 // done.
1462 if (Macro)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001463 return 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001464
Douglas Gregoraae92242010-03-19 21:51:54 +00001465 if (!PP->getPreprocessingRecord()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001466 Error("missing preprocessing record in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001467 return 0;
Douglas Gregoraae92242010-03-19 21:51:54 +00001468 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001469
Douglas Gregoraae92242010-03-19 21:51:54 +00001470 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001471 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1472 return PE;
Douglas Gregoraae92242010-03-19 21:51:54 +00001473
1474 MacroInstantiation *MI
1475 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
Sebastian Redl2c373b92010-10-05 15:59:54 +00001476 SourceRange(ReadSourceLocation(F, Record[1]),
1477 ReadSourceLocation(F, Record[2])),
Douglas Gregoraae92242010-03-19 21:51:54 +00001478 getMacroDefinition(Record[4]));
1479 PPRec.SetPreallocatedEntity(Record[0], MI);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001480 return MI;
Douglas Gregoraae92242010-03-19 21:51:54 +00001481 }
1482
Sebastian Redl539c5062010-08-18 23:57:32 +00001483 case PP_MACRO_DEFINITION: {
Douglas Gregoraae92242010-03-19 21:51:54 +00001484 // If we already have a macro, that means that we've hit the end
1485 // of the definition of the macro we were looking for. We're
1486 // done.
1487 if (Macro)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001488 return 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001489
Douglas Gregoraae92242010-03-19 21:51:54 +00001490 if (!PP->getPreprocessingRecord()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001491 Error("missing preprocessing record in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001492 return 0;
Douglas Gregoraae92242010-03-19 21:51:54 +00001493 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001494
Douglas Gregoraae92242010-03-19 21:51:54 +00001495 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001496 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1497 return PE;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001498
Douglas Gregor91096292010-10-02 19:29:26 +00001499 if (Record[1] > MacroDefinitionsLoaded.size()) {
Douglas Gregoraae92242010-03-19 21:51:54 +00001500 Error("out-of-bounds macro definition record");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001501 return 0;
Douglas Gregoraae92242010-03-19 21:51:54 +00001502 }
1503
Douglas Gregor91096292010-10-02 19:29:26 +00001504 // Decode the identifier info and then check again; if the macro is
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001505 // still defined and associated with the identifier,
Douglas Gregor91096292010-10-02 19:29:26 +00001506 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1507 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1508 MacroDefinition *MD
1509 = new (PPRec) MacroDefinition(II,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001510 ReadSourceLocation(F, Record[5]),
Douglas Gregor36ea4d42010-10-01 20:33:34 +00001511 SourceRange(
Sebastian Redl2c373b92010-10-05 15:59:54 +00001512 ReadSourceLocation(F, Record[2]),
1513 ReadSourceLocation(F, Record[3])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001514
Douglas Gregor91096292010-10-02 19:29:26 +00001515 PPRec.SetPreallocatedEntity(Record[0], MD);
1516 MacroDefinitionsLoaded[Record[1] - 1] = MD;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001517
Douglas Gregor91096292010-10-02 19:29:26 +00001518 if (DeserializationListener)
1519 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1520 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001521
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001522 return MacroDefinitionsLoaded[Record[1] - 1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001523 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001524
Douglas Gregor796d76a2010-10-20 22:00:55 +00001525 case PP_INCLUSION_DIRECTIVE: {
1526 // If we already have a macro, that means that we've hit the end
1527 // of the definition of the macro we were looking for. We're
1528 // done.
1529 if (Macro)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001530 return 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001531
Douglas Gregor796d76a2010-10-20 22:00:55 +00001532 if (!PP->getPreprocessingRecord()) {
1533 Error("missing preprocessing record in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001534 return 0;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001535 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001536
Douglas Gregor796d76a2010-10-20 22:00:55 +00001537 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001538 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1539 return PE;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001540
1541 const char *FullFileNameStart = BlobStart + Record[3];
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001542 const FileEntry *File
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001543 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
1544 BlobLen - Record[3]));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001545
Douglas Gregor796d76a2010-10-20 22:00:55 +00001546 // FIXME: Stable encoding
1547 InclusionDirective::InclusionKind Kind
1548 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1549 InclusionDirective *ID
Douglas Gregorf09b6c92010-11-01 15:03:47 +00001550 = new (PPRec) InclusionDirective(PPRec, Kind,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001551 llvm::StringRef(BlobStart, Record[3]),
1552 Record[4],
1553 File,
1554 SourceRange(ReadSourceLocation(F, Record[1]),
1555 ReadSourceLocation(F, Record[2])));
1556 PPRec.SetPreallocatedEntity(Record[0], ID);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001557 return ID;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001558 }
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001559 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001560 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001561
1562 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001563}
1564
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001565void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1566 uint64_t Offset) {
1567 // Note that this identifier has a macro definition.
1568 II->setHasMacroDefinition(true);
1569
1570 // Adjust the offset based on our position in the chain.
1571 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1572 if (Chain[I] == &F)
1573 break;
1574
1575 Offset += Chain[I]->SizeInBits;
1576 }
1577
1578 UnreadMacroRecordOffsets[II] = Offset;
1579}
1580
Sebastian Redl2c499f62010-08-18 23:56:43 +00001581void ASTReader::ReadDefinedMacros() {
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001582 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001583 PerFileData &F = *Chain[N - I - 1];
1584 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001585
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001586 // If there was no preprocessor block, skip this file.
1587 if (!MacroCursor.getBitStreamReader())
1588 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001589
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001590 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001591 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001592
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001593 RecordData Record;
1594 while (true) {
Sebastian Redl4102dd52010-09-28 02:55:49 +00001595 uint64_t Offset = Cursor.GetCurrentBitNo();
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001596 unsigned Code = Cursor.ReadCode();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001597 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001598 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001599
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001600 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1601 // No known subblocks, always skip them.
1602 Cursor.ReadSubBlockID();
1603 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001604 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001605 return;
1606 }
1607 continue;
1608 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001609
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001610 if (Code == llvm::bitc::DEFINE_ABBREV) {
1611 Cursor.ReadAbbrevRecord();
1612 continue;
1613 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001614
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001615 // Read a record.
1616 const char *BlobStart;
1617 unsigned BlobLen;
1618 Record.clear();
1619 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1620 default: // Default behavior: ignore.
1621 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001622
Sebastian Redl539c5062010-08-18 23:57:32 +00001623 case PP_MACRO_OBJECT_LIKE:
1624 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001625 DecodeIdentifierInfo(Record[0]);
1626 break;
1627
Sebastian Redl539c5062010-08-18 23:57:32 +00001628 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001629 // Ignore tokens.
1630 break;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001631
Sebastian Redl539c5062010-08-18 23:57:32 +00001632 case PP_MACRO_INSTANTIATION:
1633 case PP_MACRO_DEFINITION:
Douglas Gregor796d76a2010-10-20 22:00:55 +00001634 case PP_INCLUSION_DIRECTIVE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001635 // Read the macro record.
Sebastian Redl4102dd52010-09-28 02:55:49 +00001636 // FIXME: That's a stupid way to do this. We should reuse this cursor.
Sebastian Redl2c373b92010-10-05 15:59:54 +00001637 ReadMacroRecord(F, Offset);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001638 break;
1639 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001640 }
1641 }
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001642
1643 // Drain the unread macro-record offsets map.
1644 while (!UnreadMacroRecordOffsets.empty())
1645 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1646}
1647
1648void ASTReader::LoadMacroDefinition(
1649 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1650 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1651 PerFileData *F = 0;
1652 uint64_t Offset = Pos->second;
1653 UnreadMacroRecordOffsets.erase(Pos);
1654
1655 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1656 if (Offset < Chain[I]->SizeInBits) {
1657 F = Chain[I];
1658 break;
1659 }
1660
1661 Offset -= Chain[I]->SizeInBits;
1662 }
1663 if (!F) {
1664 Error("Malformed macro record offset");
1665 return;
1666 }
1667
1668 ReadMacroRecord(*F, Offset);
1669}
1670
1671void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1672 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1673 = UnreadMacroRecordOffsets.find(II);
1674 LoadMacroDefinition(Pos);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001675}
1676
Sebastian Redl50e26582010-09-15 19:54:06 +00001677MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor91096292010-10-02 19:29:26 +00001678 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregoraae92242010-03-19 21:51:54 +00001679 return 0;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001680
Douglas Gregor91096292010-10-02 19:29:26 +00001681 if (!MacroDefinitionsLoaded[ID - 1]) {
1682 unsigned Index = ID - 1;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001683 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1684 PerFileData &F = *Chain[N - I - 1];
1685 if (Index < F.LocalNumMacroDefinitions) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001686 ReadMacroRecord(F, F.MacroDefinitionOffsets[Index]);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001687 break;
1688 }
1689 Index -= F.LocalNumMacroDefinitions;
1690 }
Douglas Gregor91096292010-10-02 19:29:26 +00001691 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001692 }
1693
Douglas Gregor91096292010-10-02 19:29:26 +00001694 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001695}
1696
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001697/// \brief If we are loading a relocatable PCH file, and the filename is
1698/// not an absolute path, add the system root to the beginning of the file
1699/// name.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001700void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001701 // If this is not a relocatable PCH file, there's nothing to do.
1702 if (!RelocatablePCH)
1703 return;
Mike Stump11289f42009-09-09 15:08:12 +00001704
Daniel Dunbarf2ce9a22009-11-18 19:50:41 +00001705 if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001706 return;
1707
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001708 if (isysroot == 0) {
1709 // If no system root was given, default to '/'
1710 Filename.insert(Filename.begin(), '/');
1711 return;
1712 }
Mike Stump11289f42009-09-09 15:08:12 +00001713
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001714 unsigned Length = strlen(isysroot);
1715 if (isysroot[Length - 1] != '/')
1716 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001717
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001718 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1719}
1720
Sebastian Redl2c499f62010-08-18 23:56:43 +00001721ASTReader::ASTReadResult
Sebastian Redl3e31c722010-08-18 23:56:56 +00001722ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001723 llvm::BitstreamCursor &Stream = F.Stream;
1724
Sebastian Redl539c5062010-08-18 23:57:32 +00001725 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001726 Error("malformed block record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001727 return Failure;
1728 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001729
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001730 // Read all of the records and blocks for the ASt file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001731 RecordData Record;
Sebastian Redl393f8b72010-07-19 20:52:06 +00001732 bool First = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001733 while (!Stream.AtEndOfStream()) {
1734 unsigned Code = Stream.ReadCode();
1735 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001736 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001737 Error("error at end of module block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001738 return Failure;
1739 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001740
Douglas Gregor55abb232009-04-10 20:39:37 +00001741 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001742 }
1743
1744 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1745 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001746 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001747 // We lazily load the decls block, but we want to set up the
1748 // DeclsCursor cursor to point into it. Clone our current bitcode
1749 // cursor to it, enter the block and read the abbrevs in that block.
1750 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001751 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001752 if (Stream.SkipBlock() || // Skip with the main cursor.
1753 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001754 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001755 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001756 return Failure;
1757 }
1758 break;
Mike Stump11289f42009-09-09 15:08:12 +00001759
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00001760 case DECL_UPDATES_BLOCK_ID:
1761 if (Stream.SkipBlock()) {
1762 Error("malformed block record in AST file");
1763 return Failure;
1764 }
1765 break;
1766
Sebastian Redl539c5062010-08-18 23:57:32 +00001767 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001768 F.MacroCursor = Stream;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001769 if (PP)
1770 PP->setExternalSource(this);
1771
Douglas Gregor796d76a2010-10-20 22:00:55 +00001772 if (Stream.SkipBlock() ||
1773 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001774 Error("malformed block record in AST file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001775 return Failure;
1776 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001777 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001778 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001779
Sebastian Redl539c5062010-08-18 23:57:32 +00001780 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001781 switch (ReadSourceManagerBlock(F)) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001782 case Success:
1783 break;
1784
1785 case Failure:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001786 Error("malformed source manager block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001787 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001788
1789 case IgnorePCH:
1790 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001791 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001792 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001793 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00001794 First = false;
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001795 continue;
1796 }
1797
1798 if (Code == llvm::bitc::DEFINE_ABBREV) {
1799 Stream.ReadAbbrevRecord();
1800 continue;
1801 }
1802
1803 // Read and process a record.
1804 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001805 const char *BlobStart = 0;
1806 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001807 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001808 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001809 default: // Default behavior: ignore.
1810 break;
1811
Sebastian Redl539c5062010-08-18 23:57:32 +00001812 case METADATA: {
1813 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1814 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001815 : diag::warn_pch_version_too_new);
1816 return IgnorePCH;
1817 }
1818
1819 RelocatablePCH = Record[4];
1820 if (Listener) {
1821 std::string TargetTriple(BlobStart, BlobLen);
1822 if (Listener->ReadTargetTriple(TargetTriple))
1823 return IgnorePCH;
1824 }
1825 break;
1826 }
1827
Sebastian Redl539c5062010-08-18 23:57:32 +00001828 case CHAINED_METADATA: {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001829 if (!First) {
1830 Error("CHAINED_METADATA is not first record in block");
1831 return Failure;
1832 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001833 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1834 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001835 : diag::warn_pch_version_too_new);
1836 return IgnorePCH;
1837 }
1838
Sebastian Redl009e7f22010-10-05 16:15:19 +00001839 // Load the chained file, which is always a PCH file.
1840 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001841 case Failure: return Failure;
1842 // If we have to ignore the dependency, we'll have to ignore this too.
1843 case IgnorePCH: return IgnorePCH;
1844 case Success: break;
1845 }
1846 break;
1847 }
1848
Sebastian Redl539c5062010-08-18 23:57:32 +00001849 case TYPE_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001850 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001851 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001852 return Failure;
1853 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001854 F.TypeOffsets = (const uint32_t *)BlobStart;
1855 F.LocalNumTypes = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001856 break;
1857
Sebastian Redl539c5062010-08-18 23:57:32 +00001858 case DECL_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001859 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001860 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001861 return Failure;
1862 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001863 F.DeclOffsets = (const uint32_t *)BlobStart;
1864 F.LocalNumDecls = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001865 break;
Douglas Gregor55abb232009-04-10 20:39:37 +00001866
Sebastian Redl539c5062010-08-18 23:57:32 +00001867 case TU_UPDATE_LEXICAL: {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001868 DeclContextInfo Info = {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001869 /* No visible information */ 0,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001870 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
1871 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001872 };
Douglas Gregoraa433012010-10-01 01:18:02 +00001873 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
1874 .push_back(Info);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001875 break;
1876 }
1877
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001878 case UPDATE_VISIBLE: {
1879 serialization::DeclID ID = Record[0];
1880 void *Table = ASTDeclContextNameLookupTable::Create(
1881 (const unsigned char *)BlobStart + Record[1],
1882 (const unsigned char *)BlobStart,
1883 ASTDeclContextNameLookupTrait(*this));
Douglas Gregoraa433012010-10-01 01:18:02 +00001884 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001885 DeclContextInfo Info = {
1886 Table, /* No lexical inforamtion */ 0, 0
1887 };
1888 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1889 } else
1890 PendingVisibleUpdates[ID].push_back(Table);
1891 break;
1892 }
1893
Sebastian Redl539c5062010-08-18 23:57:32 +00001894 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001895 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1896 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001897 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001898 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1899 Latest > FirstLatestDeclIDs[First]) &&
1900 "The new latest is supposed to come after the previous latest");
1901 FirstLatestDeclIDs[First] = Latest;
1902 }
1903 break;
1904 }
1905
Sebastian Redl539c5062010-08-18 23:57:32 +00001906 case LANGUAGE_OPTIONS:
Douglas Gregorce3a8292010-07-27 00:27:13 +00001907 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor55abb232009-04-10 20:39:37 +00001908 return IgnorePCH;
1909 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00001910
Sebastian Redl539c5062010-08-18 23:57:32 +00001911 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001912 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00001913 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001914 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001915 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00001916 (const unsigned char *)F.IdentifierTableData + Record[0],
1917 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001918 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001919 if (PP)
1920 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00001921 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001922 break;
1923
Sebastian Redl539c5062010-08-18 23:57:32 +00001924 case IDENTIFIER_OFFSET:
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001925 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001926 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001927 return Failure;
1928 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001929 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1930 F.LocalNumIdentifiers = Record[0];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001931 break;
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001932
Sebastian Redl539c5062010-08-18 23:57:32 +00001933 case EXTERNAL_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001934 // Optimization for the first block.
1935 if (ExternalDefinitions.empty())
1936 ExternalDefinitions.swap(Record);
1937 else
1938 ExternalDefinitions.insert(ExternalDefinitions.end(),
1939 Record.begin(), Record.end());
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001940 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00001941
Sebastian Redl539c5062010-08-18 23:57:32 +00001942 case SPECIAL_TYPES:
Sebastian Redlb293a452010-07-20 21:20:32 +00001943 // Optimization for the first block
1944 if (SpecialTypes.empty())
1945 SpecialTypes.swap(Record);
1946 else
1947 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregor652d82a2009-04-18 05:55:16 +00001948 break;
1949
Sebastian Redl539c5062010-08-18 23:57:32 +00001950 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001951 TotalNumStatements += Record[0];
1952 TotalNumMacros += Record[1];
1953 TotalLexicalDeclContexts += Record[2];
1954 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00001955 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00001956
Sebastian Redl539c5062010-08-18 23:57:32 +00001957 case TENTATIVE_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001958 // Optimization for the first block.
1959 if (TentativeDefinitions.empty())
1960 TentativeDefinitions.swap(Record);
1961 else
1962 TentativeDefinitions.insert(TentativeDefinitions.end(),
1963 Record.begin(), Record.end());
Douglas Gregord4df8652009-04-22 22:02:47 +00001964 break;
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001965
Sebastian Redl539c5062010-08-18 23:57:32 +00001966 case UNUSED_FILESCOPED_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001967 // Optimization for the first block.
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00001968 if (UnusedFileScopedDecls.empty())
1969 UnusedFileScopedDecls.swap(Record);
Sebastian Redlb293a452010-07-20 21:20:32 +00001970 else
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00001971 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1972 Record.begin(), Record.end());
Tanya Lattner90073802010-02-12 00:07:30 +00001973 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001974
Sebastian Redl539c5062010-08-18 23:57:32 +00001975 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001976 // Later blocks overwrite earlier ones.
1977 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00001978 break;
1979
Sebastian Redl539c5062010-08-18 23:57:32 +00001980 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001981 // Optimization for the first block.
1982 if (LocallyScopedExternalDecls.empty())
1983 LocallyScopedExternalDecls.swap(Record);
1984 else
1985 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1986 Record.begin(), Record.end());
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001987 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001988
Sebastian Redl539c5062010-08-18 23:57:32 +00001989 case SELECTOR_OFFSETS:
Sebastian Redla19a67f2010-08-03 21:58:15 +00001990 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00001991 F.LocalNumSelectors = Record[0];
Douglas Gregor95c13f52009-04-25 17:48:32 +00001992 break;
1993
Sebastian Redl539c5062010-08-18 23:57:32 +00001994 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00001995 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001996 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00001997 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001998 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00001999 F.SelectorLookupTableData + Record[0],
2000 F.SelectorLookupTableData,
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002001 ASTSelectorLookupTrait(*this));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002002 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002003 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00002004
Sebastian Redl96371b42010-09-22 00:42:30 +00002005 case REFERENCED_SELECTOR_POOL:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002006 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002007 break;
2008
Sebastian Redl539c5062010-08-18 23:57:32 +00002009 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002010 if (!Record.empty() && Listener)
2011 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002012 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002013
Sebastian Redl539c5062010-08-18 23:57:32 +00002014 case SOURCE_LOCATION_OFFSETS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002015 F.SLocOffsets = (const uint32_t *)BlobStart;
2016 F.LocalNumSLocEntries = Record[0];
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002017 F.LocalSLocSize = Record[1];
Douglas Gregor258ae542009-04-27 06:38:32 +00002018 break;
2019
Sebastian Redl539c5062010-08-18 23:57:32 +00002020 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl96371b42010-09-22 00:42:30 +00002021 if (PreloadSLocEntries.empty())
2022 PreloadSLocEntries.swap(Record);
2023 else
2024 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2025 Record.begin(), Record.end());
Douglas Gregor258ae542009-04-27 06:38:32 +00002026 break;
Douglas Gregorc5046832009-04-27 18:38:38 +00002027
Sebastian Redl539c5062010-08-18 23:57:32 +00002028 case STAT_CACHE: {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002029 ASTStatCache *MyStatCache =
2030 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
Douglas Gregord2eb58a2009-10-16 18:18:30 +00002031 (const unsigned char *)BlobStart,
2032 NumStatHits, NumStatMisses);
2033 FileMgr.addStatCache(MyStatCache);
Sebastian Redl34522812010-07-16 17:50:48 +00002034 F.StatCache = MyStatCache;
Douglas Gregorc5046832009-04-27 18:38:38 +00002035 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00002036 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002037
Sebastian Redl539c5062010-08-18 23:57:32 +00002038 case EXT_VECTOR_DECLS:
Sebastian Redl04f5c312010-07-28 21:38:49 +00002039 // Optimization for the first block.
2040 if (ExtVectorDecls.empty())
2041 ExtVectorDecls.swap(Record);
2042 else
2043 ExtVectorDecls.insert(ExtVectorDecls.end(),
2044 Record.begin(), Record.end());
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002045 break;
2046
Sebastian Redl539c5062010-08-18 23:57:32 +00002047 case VTABLE_USES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002048 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002049 VTableUses.swap(Record);
2050 break;
2051
Sebastian Redl539c5062010-08-18 23:57:32 +00002052 case DYNAMIC_CLASSES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002053 // Optimization for the first block.
2054 if (DynamicClasses.empty())
2055 DynamicClasses.swap(Record);
2056 else
2057 DynamicClasses.insert(DynamicClasses.end(),
2058 Record.begin(), Record.end());
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002059 break;
2060
Sebastian Redl539c5062010-08-18 23:57:32 +00002061 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002062 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002063 break;
2064
Sebastian Redl539c5062010-08-18 23:57:32 +00002065 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002066 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002067 SemaDeclRefs.swap(Record);
2068 break;
2069
Sebastian Redl539c5062010-08-18 23:57:32 +00002070 case ORIGINAL_FILE_NAME:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002071 // The primary AST will be the last to get here, so it will be the one
Sebastian Redlb293a452010-07-20 21:20:32 +00002072 // that's used.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00002073 ActualOriginalFileName.assign(BlobStart, BlobLen);
2074 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002075 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00002076 break;
Mike Stump11289f42009-09-09 15:08:12 +00002077
Sebastian Redl539c5062010-08-18 23:57:32 +00002078 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00002079 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002080 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2081 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2082 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregord54f3a12009-10-05 21:07:28 +00002083 return IgnorePCH;
2084 }
2085 break;
2086 }
Sebastian Redlfa061442010-07-21 20:07:32 +00002087
Sebastian Redl539c5062010-08-18 23:57:32 +00002088 case MACRO_DEFINITION_OFFSETS:
Sebastian Redlfa061442010-07-21 20:07:32 +00002089 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2090 F.NumPreallocatedPreprocessingEntities = Record[0];
2091 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregoraae92242010-03-19 21:51:54 +00002092 break;
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002093
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002094 case DECL_UPDATE_OFFSETS: {
2095 if (Record.size() % 2 != 0) {
2096 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2097 return Failure;
2098 }
2099 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2100 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2101 .push_back(std::make_pair(&F, Record[I+1]));
2102 break;
2103 }
2104
Sebastian Redl539c5062010-08-18 23:57:32 +00002105 case DECL_REPLACEMENTS: {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002106 if (Record.size() % 2 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002107 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002108 return Failure;
2109 }
2110 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl539c5062010-08-18 23:57:32 +00002111 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002112 std::make_pair(&F, Record[I+1]);
2113 break;
2114 }
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002115
2116 case CXX_BASE_SPECIFIER_OFFSETS: {
2117 if (F.LocalNumCXXBaseSpecifiers != 0) {
2118 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2119 return Failure;
2120 }
2121
2122 F.LocalNumCXXBaseSpecifiers = Record[0];
2123 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2124 break;
2125 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002126
2127 case DIAG_USER_MAPPINGS:
2128 if (Record.size() % 2 != 0) {
2129 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2130 return Failure;
2131 }
2132 if (UserDiagMappings.empty())
2133 UserDiagMappings.swap(Record);
2134 else
2135 UserDiagMappings.insert(UserDiagMappings.end(),
2136 Record.begin(), Record.end());
2137 break;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002138 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00002139 First = false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002140 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002141 Error("premature end of bitstream in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002142 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002143}
2144
Sebastian Redl009e7f22010-10-05 16:15:19 +00002145ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2146 ASTFileType Type) {
2147 switch(ReadASTCore(FileName, Type)) {
Sebastian Redl2abc0382010-07-16 20:41:52 +00002148 case Failure: return Failure;
2149 case IgnorePCH: return IgnorePCH;
2150 case Success: break;
2151 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002152
2153 // Here comes stuff that we only do once the entire chain is loaded.
2154
Sebastian Redl96371b42010-09-22 00:42:30 +00002155 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redlfa061442010-07-21 20:07:32 +00002156 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redlada023c2010-08-04 20:40:17 +00002157 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2158 TotalNumSelectors = 0;
Sebastian Redl9e687992010-07-19 22:06:55 +00002159 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl96371b42010-09-22 00:42:30 +00002160 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002161 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002162 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl9e687992010-07-19 22:06:55 +00002163 TotalNumTypes += Chain[I]->LocalNumTypes;
2164 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redlfa061442010-07-21 20:07:32 +00002165 TotalNumPreallocatedPreprocessingEntities +=
2166 Chain[I]->NumPreallocatedPreprocessingEntities;
2167 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redlada023c2010-08-04 20:40:17 +00002168 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl9e687992010-07-19 22:06:55 +00002169 }
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002170 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002171 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl9e687992010-07-19 22:06:55 +00002172 TypesLoaded.resize(TotalNumTypes);
2173 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redlfa061442010-07-21 20:07:32 +00002174 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2175 if (PP) {
2176 if (TotalNumIdentifiers > 0)
2177 PP->getHeaderSearchInfo().SetExternalLookup(this);
2178 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2179 if (!PP->getPreprocessingRecord())
2180 PP->createPreprocessingRecord();
2181 PP->getPreprocessingRecord()->SetExternalSource(*this,
2182 TotalNumPreallocatedPreprocessingEntities);
2183 }
2184 }
Sebastian Redlada023c2010-08-04 20:40:17 +00002185 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl96371b42010-09-22 00:42:30 +00002186 // Preload SLocEntries.
2187 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2188 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2189 if (Result != Success)
2190 return Result;
2191 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002192
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002193 // Check the predefines buffers.
Douglas Gregorce3a8292010-07-27 00:27:13 +00002194 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002195 return IgnorePCH;
2196
2197 if (PP) {
2198 // Initialization of keywords and pragmas occurs before the
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002199 // AST file is read, so there may be some identifiers that were
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002200 // loaded into the IdentifierTable before we intercepted the
2201 // creation of identifiers. Iterate through the list of known
2202 // identifiers and determine whether we have to establish
2203 // preprocessor definitions or top-level identifier declaration
2204 // chains for those identifiers.
2205 //
2206 // We copy the IdentifierInfo pointers to a small vector first,
2207 // since de-serializing declarations or macro definitions can add
2208 // new entries into the identifier table, invalidating the
2209 // iterators.
2210 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2211 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2212 IdEnd = PP->getIdentifierTable().end();
2213 Id != IdEnd; ++Id)
2214 Identifiers.push_back(Id->second);
Sebastian Redlfa061442010-07-21 20:07:32 +00002215 // We need to search the tables in all files.
Sebastian Redlfa061442010-07-21 20:07:32 +00002216 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002217 ASTIdentifierLookupTable *IdTable
2218 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2219 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl5c415f32010-07-22 17:01:13 +00002220 // ones.
2221 if (!IdTable)
2222 continue;
Sebastian Redlfa061442010-07-21 20:07:32 +00002223 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2224 IdentifierInfo *II = Identifiers[I];
2225 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00002226 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redlfa061442010-07-21 20:07:32 +00002227 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002228 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redlb293a452010-07-20 21:20:32 +00002229 if (Pos == IdTable->end())
2230 continue;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002231
Sebastian Redlb293a452010-07-20 21:20:32 +00002232 // Dereferencing the iterator has the effect of populating the
2233 // IdentifierInfo node with the various declarations it needs.
2234 (void)*Pos;
2235 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002236 }
2237 }
2238
2239 if (Context)
2240 InitializeContext(*Context);
2241
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002242 if (DeserializationListener)
2243 DeserializationListener->ReaderInitialized(this);
2244
Douglas Gregor936a5b42010-11-30 05:23:00 +00002245 // If this AST file is a precompiled preamble, then set the main file ID of
2246 // the source manager to the file source file from which the preamble was
2247 // built. This is the only valid way to use a precompiled preamble.
2248 if (Type == Preamble) {
2249 SourceLocation Loc
2250 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2251 if (Loc.isValid()) {
2252 std::pair<FileID, unsigned> Decomposed = SourceMgr.getDecomposedLoc(Loc);
2253 SourceMgr.SetPreambleFileID(Decomposed.first);
2254 }
2255 }
2256
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002257 return Success;
2258}
2259
Sebastian Redl009e7f22010-10-05 16:15:19 +00002260ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2261 ASTFileType Type) {
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002262 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl009e7f22010-10-05 16:15:19 +00002263 Chain.push_back(new PerFileData(Type));
Sebastian Redl34522812010-07-16 17:50:48 +00002264 PerFileData &F = *Chain.back();
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002265 if (Prev)
2266 Prev->NextInSource = &F;
2267 else
2268 FirstInSource = &F;
2269 F.Loaders.push_back(Prev);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002270
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002271 // Set the AST file name.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002272 F.FileName = FileName;
2273
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002274 // Open the AST file.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002275 //
2276 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2277 std::string ErrStr;
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00002278 if (FileName == "-")
2279 F.Buffer.reset(llvm::MemoryBuffer::getSTDIN(&ErrStr));
2280 else
Chris Lattner5159f612010-11-23 08:35:12 +00002281 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002282 if (!F.Buffer) {
2283 Error(ErrStr.c_str());
2284 return IgnorePCH;
2285 }
2286
2287 // Initialize the stream
2288 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2289 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl34522812010-07-16 17:50:48 +00002290 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002291 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002292 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002293
2294 // Sniff for the signature.
2295 if (Stream.Read(8) != 'C' ||
2296 Stream.Read(8) != 'P' ||
2297 Stream.Read(8) != 'C' ||
2298 Stream.Read(8) != 'H') {
2299 Diag(diag::err_not_a_pch_file) << FileName;
2300 return Failure;
2301 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002302
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002303 while (!Stream.AtEndOfStream()) {
2304 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002305
Douglas Gregor92863e42009-04-10 23:10:45 +00002306 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002307 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002308 return Failure;
2309 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002310
2311 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002312
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002313 // We only know the AST subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002314 switch (BlockID) {
2315 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002316 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002317 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002318 return Failure;
2319 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002320 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00002321 case AST_BLOCK_ID:
Sebastian Redl3e31c722010-08-18 23:56:56 +00002322 switch (ReadASTBlock(F)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002323 case Success:
2324 break;
2325
2326 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00002327 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00002328
2329 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00002330 // FIXME: We could consider reading through to the end of this
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002331 // AST block, skipping subblocks, to see if there are other
2332 // AST blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00002333
2334 // Clear out any preallocated source location entries, so that
2335 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002336 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00002337
2338 // Remove the stat cache.
Sebastian Redl34522812010-07-16 17:50:48 +00002339 if (F.StatCache)
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002340 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00002341
Douglas Gregor92863e42009-04-10 23:10:45 +00002342 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002343 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002344 break;
2345 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002346 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002347 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002348 return Failure;
2349 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002350 break;
2351 }
Mike Stump11289f42009-09-09 15:08:12 +00002352 }
2353
Sebastian Redl2abc0382010-07-16 20:41:52 +00002354 return Success;
2355}
2356
Sebastian Redl2c499f62010-08-18 23:56:43 +00002357void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002358 PP = &pp;
Sebastian Redlfa061442010-07-21 20:07:32 +00002359
2360 unsigned TotalNum = 0;
2361 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2362 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2363 if (TotalNum) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002364 if (!PP->getPreprocessingRecord())
2365 PP->createPreprocessingRecord();
Sebastian Redlfa061442010-07-21 20:07:32 +00002366 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregoraae92242010-03-19 21:51:54 +00002367 }
2368}
2369
Sebastian Redl2c499f62010-08-18 23:56:43 +00002370void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002371 Context = &Ctx;
2372 assert(Context && "Passed null context!");
2373
2374 assert(PP && "Forgot to set Preprocessor ?");
2375 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2376 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00002377 PP->setExternalSource(this);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002378
Douglas Gregoraa433012010-10-01 01:18:02 +00002379 // If we have an update block for the TU waiting, we have to add it before
2380 // deserializing the decl.
2381 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2382 if (DCU != DeclContextOffsets.end()) {
2383 // Insertion could invalidate map, so grab vector.
2384 DeclContextInfos T;
2385 T.swap(DCU->second);
2386 DeclContextOffsets.erase(DCU);
2387 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2388 }
2389
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002390 // Load the translation unit declaration
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00002391 GetTranslationUnitDecl();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002392
2393 // Load the special types.
2394 Context->setBuiltinVaListType(
Sebastian Redl539c5062010-08-18 23:57:32 +00002395 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2396 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002397 Context->setObjCIdType(GetType(Id));
Sebastian Redl539c5062010-08-18 23:57:32 +00002398 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002399 Context->setObjCSelType(GetType(Sel));
Sebastian Redl539c5062010-08-18 23:57:32 +00002400 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002401 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl539c5062010-08-18 23:57:32 +00002402 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002403 Context->setObjCClassType(GetType(Class));
Steve Naroff7cae42b2009-07-10 23:34:53 +00002404
Sebastian Redl539c5062010-08-18 23:57:32 +00002405 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002406 Context->setCFConstantStringType(GetType(String));
Mike Stump11289f42009-09-09 15:08:12 +00002407 if (unsigned FastEnum
Sebastian Redl539c5062010-08-18 23:57:32 +00002408 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002409 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl539c5062010-08-18 23:57:32 +00002410 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregor27821ce2009-07-07 16:35:42 +00002411 QualType FileType = GetType(File);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002412 if (FileType.isNull()) {
2413 Error("FILE type is NULL");
2414 return;
2415 }
John McCall9dd450b2009-09-21 23:43:11 +00002416 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregor27821ce2009-07-07 16:35:42 +00002417 Context->setFILEDecl(Typedef->getDecl());
2418 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002419 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002420 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002421 Error("Invalid FILE type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002422 return;
2423 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002424 Context->setFILEDecl(Tag->getDecl());
2425 }
2426 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002427 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002428 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002429 if (Jmp_bufType.isNull()) {
2430 Error("jmp_bug type is NULL");
2431 return;
2432 }
John McCall9dd450b2009-09-21 23:43:11 +00002433 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002434 Context->setjmp_bufDecl(Typedef->getDecl());
2435 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002436 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002437 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002438 Error("Invalid jmp_buf type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002439 return;
2440 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002441 Context->setjmp_bufDecl(Tag->getDecl());
2442 }
2443 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002444 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002445 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002446 if (Sigjmp_bufType.isNull()) {
2447 Error("sigjmp_buf type is NULL");
2448 return;
2449 }
John McCall9dd450b2009-09-21 23:43:11 +00002450 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002451 Context->setsigjmp_bufDecl(Typedef->getDecl());
2452 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002453 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002454 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stumpa4de80b2009-07-28 02:25:19 +00002455 Context->setsigjmp_bufDecl(Tag->getDecl());
2456 }
2457 }
Mike Stump11289f42009-09-09 15:08:12 +00002458 if (unsigned ObjCIdRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002459 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002460 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump11289f42009-09-09 15:08:12 +00002461 if (unsigned ObjCClassRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002462 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002463 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002464 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpd0153282009-10-20 02:12:22 +00002465 Context->setBlockDescriptorType(GetType(String));
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002466 if (unsigned String
Sebastian Redl539c5062010-08-18 23:57:32 +00002467 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002468 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002469 if (unsigned ObjCSelRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002470 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002471 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002472 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002473 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002474
Sebastian Redl539c5062010-08-18 23:57:32 +00002475 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002476 Context->setInt128Installed();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002477
2478 ReadUserDiagnosticMappings(Context->getDiagnostics());
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002479}
2480
Douglas Gregor45fe0362009-05-12 01:31:05 +00002481/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002482/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00002483/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002484std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00002485 FileManager &FileMgr,
Daniel Dunbar3b951482009-12-03 09:13:06 +00002486 Diagnostic &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002487 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002488 std::string ErrStr;
2489 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner5159f612010-11-23 08:35:12 +00002490 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00002491 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00002492 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002493 return std::string();
2494 }
2495
2496 // Initialize the stream
2497 llvm::BitstreamReader StreamFile;
2498 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00002499 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00002500 (const unsigned char *)Buffer->getBufferEnd());
2501 Stream.init(StreamFile);
2502
2503 // Sniff for the signature.
2504 if (Stream.Read(8) != 'C' ||
2505 Stream.Read(8) != 'P' ||
2506 Stream.Read(8) != 'C' ||
2507 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002508 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002509 return std::string();
2510 }
2511
2512 RecordData Record;
2513 while (!Stream.AtEndOfStream()) {
2514 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002515
Douglas Gregor45fe0362009-05-12 01:31:05 +00002516 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2517 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00002518
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002519 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002520 switch (BlockID) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002521 case AST_BLOCK_ID:
2522 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002523 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002524 return std::string();
2525 }
2526 break;
Mike Stump11289f42009-09-09 15:08:12 +00002527
Douglas Gregor45fe0362009-05-12 01:31:05 +00002528 default:
2529 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002530 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002531 return std::string();
2532 }
2533 break;
2534 }
2535 continue;
2536 }
2537
2538 if (Code == llvm::bitc::END_BLOCK) {
2539 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002540 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002541 return std::string();
2542 }
2543 continue;
2544 }
2545
2546 if (Code == llvm::bitc::DEFINE_ABBREV) {
2547 Stream.ReadAbbrevRecord();
2548 continue;
2549 }
2550
2551 Record.clear();
2552 const char *BlobStart = 0;
2553 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002554 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl539c5062010-08-18 23:57:32 +00002555 == ORIGINAL_FILE_NAME)
Douglas Gregor45fe0362009-05-12 01:31:05 +00002556 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00002557 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00002558
2559 return std::string();
2560}
2561
Douglas Gregor55abb232009-04-10 20:39:37 +00002562/// \brief Parse the record that corresponds to a LangOptions data
2563/// structure.
2564///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002565/// This routine parses the language options from the AST file and then gives
2566/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00002567///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002568/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002569bool ASTReader::ParseLanguageOptions(
Douglas Gregor55abb232009-04-10 20:39:37 +00002570 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002571 if (Listener) {
2572 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00002573
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002574 #define PARSE_LANGOPT(Option) \
2575 LangOpts.Option = Record[Idx]; \
2576 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00002577
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002578 unsigned Idx = 0;
2579 PARSE_LANGOPT(Trigraphs);
2580 PARSE_LANGOPT(BCPLComment);
2581 PARSE_LANGOPT(DollarIdents);
2582 PARSE_LANGOPT(AsmPreprocessor);
2583 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00002584 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002585 PARSE_LANGOPT(ImplicitInt);
2586 PARSE_LANGOPT(Digraphs);
2587 PARSE_LANGOPT(HexFloats);
2588 PARSE_LANGOPT(C99);
2589 PARSE_LANGOPT(Microsoft);
2590 PARSE_LANGOPT(CPlusPlus);
2591 PARSE_LANGOPT(CPlusPlus0x);
2592 PARSE_LANGOPT(CXXOperatorNames);
2593 PARSE_LANGOPT(ObjC1);
2594 PARSE_LANGOPT(ObjC2);
2595 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00002596 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00002597 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002598 PARSE_LANGOPT(PascalStrings);
2599 PARSE_LANGOPT(WritableStrings);
2600 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00002601 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002602 PARSE_LANGOPT(Exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +00002603 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002604 PARSE_LANGOPT(NeXTRuntime);
2605 PARSE_LANGOPT(Freestanding);
2606 PARSE_LANGOPT(NoBuiltin);
2607 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00002608 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002609 PARSE_LANGOPT(Blocks);
2610 PARSE_LANGOPT(EmitAllDecls);
2611 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00002612 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2613 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002614 PARSE_LANGOPT(HeinousExtensions);
2615 PARSE_LANGOPT(Optimize);
2616 PARSE_LANGOPT(OptimizeSize);
2617 PARSE_LANGOPT(Static);
2618 PARSE_LANGOPT(PICLevel);
2619 PARSE_LANGOPT(GNUInline);
2620 PARSE_LANGOPT(NoInline);
2621 PARSE_LANGOPT(AccessControl);
2622 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00002623 PARSE_LANGOPT(ShortWChar);
Chris Lattner51924e512010-06-26 21:25:03 +00002624 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall457a04e2010-10-22 21:05:15 +00002625 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00002626 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00002627 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002628 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00002629 PARSE_LANGOPT(OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00002630 PARSE_LANGOPT(CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00002631 PARSE_LANGOPT(CatchUndefined);
2632 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002633 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00002634
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002635 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00002636 }
Douglas Gregor55abb232009-04-10 20:39:37 +00002637
2638 return false;
2639}
2640
Sebastian Redl2c499f62010-08-18 23:56:43 +00002641void ASTReader::ReadPreprocessedEntities() {
Douglas Gregoraae92242010-03-19 21:51:54 +00002642 ReadDefinedMacros();
2643}
2644
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002645PreprocessedEntity *ASTReader::ReadPreprocessedEntity(uint64_t Offset) {
2646 PerFileData *F = 0;
2647 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2648 if (Offset < Chain[I]->SizeInBits) {
2649 F = Chain[I];
2650 break;
2651 }
2652
2653 Offset -= Chain[I]->SizeInBits;
2654 }
2655
2656 if (!F) {
2657 Error("Malformed preprocessed entity offset");
2658 return 0;
2659 }
2660
2661 return ReadMacroRecord(*F, Offset);
2662}
2663
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002664void ASTReader::ReadUserDiagnosticMappings(Diagnostic &Diag) {
2665 unsigned Idx = 0;
2666 while (Idx < UserDiagMappings.size()) {
2667 unsigned DiagID = UserDiagMappings[Idx++];
2668 unsigned Map = UserDiagMappings[Idx++];
2669 Diag.setDiagnosticMappingInternal(DiagID, Map, /*isUser=*/true);
2670 }
2671}
2672
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002673/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002674ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002675 PerFileData *F = 0;
2676 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2677 F = Chain[N - I - 1];
2678 if (Index < F->LocalNumTypes)
2679 break;
2680 Index -= F->LocalNumTypes;
2681 }
2682 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redl2c373b92010-10-05 15:59:54 +00002683 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002684}
2685
2686/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002687///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002688/// The index is the type ID, shifted and minus the number of predefs. This
2689/// routine actually reads the record corresponding to the type at the given
2690/// location. It is a helper routine for GetType, which deals with reading type
2691/// IDs.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002692QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002693 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00002694 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00002695
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002696 // Keep track of where we are in the stream, then jump back there
2697 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002698 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002699
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002700 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00002701
Douglas Gregor1342e842009-07-06 18:54:52 +00002702 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00002703 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00002704
Sebastian Redl2c373b92010-10-05 15:59:54 +00002705 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002706 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002707 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00002708 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2709 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002710 if (Record.size() != 2) {
2711 Error("Incorrect encoding of extended qualifier type");
2712 return QualType();
2713 }
Douglas Gregor455b8f42009-04-15 22:00:08 +00002714 QualType Base = GetType(Record[0]);
John McCall8ccfcb52009-09-24 19:53:00 +00002715 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2716 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00002717 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002718
Sebastian Redl539c5062010-08-18 23:57:32 +00002719 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002720 if (Record.size() != 1) {
2721 Error("Incorrect encoding of complex type");
2722 return QualType();
2723 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002724 QualType ElemType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002725 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002726 }
2727
Sebastian Redl539c5062010-08-18 23:57:32 +00002728 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002729 if (Record.size() != 1) {
2730 Error("Incorrect encoding of pointer type");
2731 return QualType();
2732 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002733 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002734 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002735 }
2736
Sebastian Redl539c5062010-08-18 23:57:32 +00002737 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002738 if (Record.size() != 1) {
2739 Error("Incorrect encoding of block pointer type");
2740 return QualType();
2741 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002742 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002743 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002744 }
2745
Sebastian Redl539c5062010-08-18 23:57:32 +00002746 case TYPE_LVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002747 if (Record.size() != 1) {
2748 Error("Incorrect encoding of lvalue reference type");
2749 return QualType();
2750 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002751 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002752 return Context->getLValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002753 }
2754
Sebastian Redl539c5062010-08-18 23:57:32 +00002755 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002756 if (Record.size() != 1) {
2757 Error("Incorrect encoding of rvalue reference type");
2758 return QualType();
2759 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002760 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002761 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002762 }
2763
Sebastian Redl539c5062010-08-18 23:57:32 +00002764 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00002765 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002766 Error("Incorrect encoding of member pointer type");
2767 return QualType();
2768 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002769 QualType PointeeType = GetType(Record[0]);
2770 QualType ClassType = GetType(Record[1]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002771 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002772 }
2773
Sebastian Redl539c5062010-08-18 23:57:32 +00002774 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002775 QualType ElementType = GetType(Record[0]);
2776 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2777 unsigned IndexTypeQuals = Record[2];
2778 unsigned Idx = 3;
2779 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00002780 return Context->getConstantArrayType(ElementType, Size,
2781 ASM, IndexTypeQuals);
2782 }
2783
Sebastian Redl539c5062010-08-18 23:57:32 +00002784 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002785 QualType ElementType = GetType(Record[0]);
2786 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2787 unsigned IndexTypeQuals = Record[2];
Chris Lattner8575daa2009-04-27 21:45:14 +00002788 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002789 }
2790
Sebastian Redl539c5062010-08-18 23:57:32 +00002791 case TYPE_VARIABLE_ARRAY: {
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002792 QualType ElementType = GetType(Record[0]);
2793 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2794 unsigned IndexTypeQuals = Record[2];
Sebastian Redl2c373b92010-10-05 15:59:54 +00002795 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
2796 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
2797 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor04318252009-07-06 15:59:29 +00002798 ASM, IndexTypeQuals,
2799 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002800 }
2801
Sebastian Redl539c5062010-08-18 23:57:32 +00002802 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002803 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002804 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002805 return QualType();
2806 }
2807
2808 QualType ElementType = GetType(Record[0]);
2809 unsigned NumElements = Record[1];
Bob Wilsonaeb56442010-11-10 21:56:12 +00002810 unsigned VecKind = Record[2];
Chris Lattner37141f42010-06-23 06:00:24 +00002811 return Context->getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00002812 (VectorType::VectorKind)VecKind);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002813 }
2814
Sebastian Redl539c5062010-08-18 23:57:32 +00002815 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002816 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002817 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002818 return QualType();
2819 }
2820
2821 QualType ElementType = GetType(Record[0]);
2822 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00002823 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002824 }
2825
Sebastian Redl539c5062010-08-18 23:57:32 +00002826 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002827 if (Record.size() != 4) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002828 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002829 return QualType();
2830 }
2831 QualType ResultType = GetType(Record[0]);
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002832 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00002833 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002834 }
2835
Sebastian Redl539c5062010-08-18 23:57:32 +00002836 case TYPE_FUNCTION_PROTO: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002837 QualType ResultType = GetType(Record[0]);
Douglas Gregordc728752009-12-22 18:11:50 +00002838 bool NoReturn = Record[1];
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002839 unsigned RegParm = Record[2];
2840 CallingConv CallConv = (CallingConv)Record[3];
2841 unsigned Idx = 4;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002842 unsigned NumParams = Record[Idx++];
2843 llvm::SmallVector<QualType, 16> ParamTypes;
2844 for (unsigned I = 0; I != NumParams; ++I)
2845 ParamTypes.push_back(GetType(Record[Idx++]));
2846 bool isVariadic = Record[Idx++];
2847 unsigned Quals = Record[Idx++];
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002848 bool hasExceptionSpec = Record[Idx++];
2849 bool hasAnyExceptionSpec = Record[Idx++];
2850 unsigned NumExceptions = Record[Idx++];
2851 llvm::SmallVector<QualType, 2> Exceptions;
2852 for (unsigned I = 0; I != NumExceptions; ++I)
2853 Exceptions.push_back(GetType(Record[Idx++]));
Jay Foad7d0479f2009-05-21 09:52:38 +00002854 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002855 isVariadic, Quals, hasExceptionSpec,
2856 hasAnyExceptionSpec, NumExceptions,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00002857 Exceptions.data(),
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002858 FunctionType::ExtInfo(NoReturn, RegParm,
2859 CallConv));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002860 }
2861
Sebastian Redl539c5062010-08-18 23:57:32 +00002862 case TYPE_UNRESOLVED_USING:
John McCallb96ec562009-12-04 22:46:56 +00002863 return Context->getTypeDeclType(
2864 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2865
Sebastian Redl539c5062010-08-18 23:57:32 +00002866 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002867 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002868 Error("incorrect encoding of typedef type");
2869 return QualType();
2870 }
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002871 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2872 QualType Canonical = GetType(Record[1]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00002873 if (!Canonical.isNull())
2874 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002875 return Context->getTypedefType(Decl, Canonical);
2876 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002877
Sebastian Redl539c5062010-08-18 23:57:32 +00002878 case TYPE_TYPEOF_EXPR:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002879 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002880
Sebastian Redl539c5062010-08-18 23:57:32 +00002881 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002882 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002883 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002884 return QualType();
2885 }
2886 QualType UnderlyingType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002887 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002888 }
Mike Stump11289f42009-09-09 15:08:12 +00002889
Sebastian Redl539c5062010-08-18 23:57:32 +00002890 case TYPE_DECLTYPE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002891 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson81df7b82009-06-24 19:06:50 +00002892
Sebastian Redl539c5062010-08-18 23:57:32 +00002893 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002894 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002895 Error("incorrect encoding of record type");
2896 return QualType();
2897 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002898 bool IsDependent = Record[0];
2899 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCall25c9d112010-10-14 21:48:26 +00002900 T->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002901 return T;
2902 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002903
Sebastian Redl539c5062010-08-18 23:57:32 +00002904 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002905 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002906 Error("incorrect encoding of enum type");
2907 return QualType();
2908 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002909 bool IsDependent = Record[0];
2910 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCall25c9d112010-10-14 21:48:26 +00002911 T->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002912 return T;
2913 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00002914
Sebastian Redl539c5062010-08-18 23:57:32 +00002915 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002916 unsigned Idx = 0;
2917 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2918 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2919 QualType NamedType = GetType(Record[Idx++]);
2920 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00002921 }
2922
Sebastian Redl539c5062010-08-18 23:57:32 +00002923 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00002924 unsigned Idx = 0;
2925 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00002926 return Context->getObjCInterfaceType(ItfD);
2927 }
2928
Sebastian Redl539c5062010-08-18 23:57:32 +00002929 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00002930 unsigned Idx = 0;
2931 QualType Base = GetType(Record[Idx++]);
Chris Lattner587cbe12009-04-22 06:45:28 +00002932 unsigned NumProtos = Record[Idx++];
2933 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2934 for (unsigned I = 0; I != NumProtos; ++I)
2935 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002936 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00002937 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002938
Sebastian Redl539c5062010-08-18 23:57:32 +00002939 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00002940 unsigned Idx = 0;
John McCall8b07ec22010-05-15 11:32:37 +00002941 QualType Pointee = GetType(Record[Idx++]);
2942 return Context->getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00002943 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00002944
Sebastian Redl539c5062010-08-18 23:57:32 +00002945 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00002946 unsigned Idx = 0;
2947 QualType Parm = GetType(Record[Idx++]);
2948 QualType Replacement = GetType(Record[Idx++]);
2949 return
2950 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2951 Replacement);
2952 }
John McCalle78aac42010-03-10 03:28:59 +00002953
Sebastian Redl539c5062010-08-18 23:57:32 +00002954 case TYPE_INJECTED_CLASS_NAME: {
John McCalle78aac42010-03-10 03:28:59 +00002955 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2956 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00002957 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002958 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00002959 return
2960 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00002961 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002962
Sebastian Redl539c5062010-08-18 23:57:32 +00002963 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002964 unsigned Idx = 0;
2965 unsigned Depth = Record[Idx++];
2966 unsigned Index = Record[Idx++];
2967 bool Pack = Record[Idx++];
2968 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
2969 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
2970 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002971
Sebastian Redl539c5062010-08-18 23:57:32 +00002972 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002973 unsigned Idx = 0;
2974 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2975 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2976 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00002977 QualType Canon = GetType(Record[Idx++]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00002978 if (!Canon.isNull())
2979 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00002980 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002981 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002982
Sebastian Redl539c5062010-08-18 23:57:32 +00002983 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002984 unsigned Idx = 0;
2985 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2986 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2987 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2988 unsigned NumArgs = Record[Idx++];
2989 llvm::SmallVector<TemplateArgument, 8> Args;
2990 Args.reserve(NumArgs);
2991 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00002992 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002993 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
2994 Args.size(), Args.data());
2995 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002996
Sebastian Redl539c5062010-08-18 23:57:32 +00002997 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00002998 unsigned Idx = 0;
2999
3000 // ArrayType
3001 QualType ElementType = GetType(Record[Idx++]);
3002 ArrayType::ArraySizeModifier ASM
3003 = (ArrayType::ArraySizeModifier)Record[Idx++];
3004 unsigned IndexTypeQuals = Record[Idx++];
3005
3006 // DependentSizedArrayType
Sebastian Redl2c373b92010-10-05 15:59:54 +00003007 Expr *NumElts = ReadExpr(*Loc.F);
3008 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003009
3010 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3011 IndexTypeQuals, Brackets);
3012 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003013
Sebastian Redl539c5062010-08-18 23:57:32 +00003014 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003015 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003016 bool IsDependent = Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003017 TemplateName Name = ReadTemplateName(Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003018 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003019 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003020 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003021 QualType T;
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003022 if (Canon.isNull())
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003023 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3024 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003025 else
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003026 T = Context->getTemplateSpecializationType(Name, Args.data(),
3027 Args.size(), Canon);
John McCall25c9d112010-10-14 21:48:26 +00003028 T->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003029 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003030 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003031 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003032 // Suppress a GCC warning
3033 return QualType();
3034}
3035
Sebastian Redl2c373b92010-10-05 15:59:54 +00003036class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00003037 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003038 ASTReader::PerFileData &F;
Sebastian Redlc67764e2010-07-22 22:43:28 +00003039 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redl2c499f62010-08-18 23:56:43 +00003040 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00003041 unsigned &Idx;
3042
Sebastian Redl2c373b92010-10-05 15:59:54 +00003043 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3044 unsigned &I) {
3045 return Reader.ReadSourceLocation(F, R, I);
3046 }
3047
John McCall8f115c62009-10-16 21:56:05 +00003048public:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003049 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl2c499f62010-08-18 23:56:43 +00003050 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003051 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3052 { }
John McCall8f115c62009-10-16 21:56:05 +00003053
John McCall17001972009-10-18 01:05:36 +00003054 // We want compile-time assurance that we've enumerated all of
3055 // these, so unfortunately we have to declare them first, then
3056 // define them out-of-line.
3057#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00003058#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00003059 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00003060#include "clang/AST/TypeLocNodes.def"
3061
John McCall17001972009-10-18 01:05:36 +00003062 void VisitFunctionTypeLoc(FunctionTypeLoc);
3063 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00003064};
3065
John McCall17001972009-10-18 01:05:36 +00003066void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00003067 // nothing to do
3068}
John McCall17001972009-10-18 01:05:36 +00003069void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003070 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorc9b7a592010-01-18 18:04:31 +00003071 if (TL.needsExtraLocalData()) {
3072 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3073 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3074 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3075 TL.setModeAttr(Record[Idx++]);
3076 }
John McCall8f115c62009-10-16 21:56:05 +00003077}
John McCall17001972009-10-18 01:05:36 +00003078void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003079 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003080}
John McCall17001972009-10-18 01:05:36 +00003081void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003082 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003083}
John McCall17001972009-10-18 01:05:36 +00003084void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003085 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003086}
John McCall17001972009-10-18 01:05:36 +00003087void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003088 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003089}
John McCall17001972009-10-18 01:05:36 +00003090void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003091 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003092}
John McCall17001972009-10-18 01:05:36 +00003093void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003094 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003095}
John McCall17001972009-10-18 01:05:36 +00003096void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003097 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3098 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003099 if (Record[Idx++])
Sebastian Redl2c373b92010-10-05 15:59:54 +00003100 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003101 else
John McCall17001972009-10-18 01:05:36 +00003102 TL.setSizeExpr(0);
3103}
3104void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3105 VisitArrayTypeLoc(TL);
3106}
3107void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3108 VisitArrayTypeLoc(TL);
3109}
3110void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3111 VisitArrayTypeLoc(TL);
3112}
3113void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3114 DependentSizedArrayTypeLoc TL) {
3115 VisitArrayTypeLoc(TL);
3116}
3117void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3118 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003119 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003120}
3121void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003122 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003123}
3124void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003125 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003126}
3127void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003128 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3129 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor7fb25412010-10-01 18:44:50 +00003130 TL.setTrailingReturn(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00003131 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCalle6347002009-10-23 01:28:53 +00003132 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall17001972009-10-18 01:05:36 +00003133 }
3134}
3135void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3136 VisitFunctionTypeLoc(TL);
3137}
3138void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3139 VisitFunctionTypeLoc(TL);
3140}
John McCallb96ec562009-12-04 22:46:56 +00003141void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003142 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallb96ec562009-12-04 22:46:56 +00003143}
John McCall17001972009-10-18 01:05:36 +00003144void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003145 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003146}
3147void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003148 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3149 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3150 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003151}
3152void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003153 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3154 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3155 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3156 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003157}
3158void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003159 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003160}
3161void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003162 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003163}
3164void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003165 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003166}
John McCall17001972009-10-18 01:05:36 +00003167void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003168 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003169}
John McCallcebee162009-10-18 09:09:24 +00003170void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3171 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003172 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallcebee162009-10-18 09:09:24 +00003173}
John McCall17001972009-10-18 01:05:36 +00003174void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3175 TemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003176 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3177 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3178 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall0ad16662009-10-29 08:12:44 +00003179 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3180 TL.setArgLocInfo(i,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003181 Reader.GetTemplateArgumentLocInfo(F,
3182 TL.getTypePtr()->getArg(i).getKind(),
3183 Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003184}
Abramo Bagnara6150c882010-05-11 21:36:43 +00003185void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003186 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3187 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003188}
John McCalle78aac42010-03-10 03:28:59 +00003189void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003190 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalle78aac42010-03-10 03:28:59 +00003191}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00003192void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003193 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3194 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3195 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003196}
John McCallc392f372010-06-11 00:33:02 +00003197void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3198 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003199 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3200 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3201 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3202 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3203 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003204 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3205 TL.setArgLocInfo(I,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003206 Reader.GetTemplateArgumentLocInfo(F,
3207 TL.getTypePtr()->getArg(I).getKind(),
3208 Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003209}
John McCall17001972009-10-18 01:05:36 +00003210void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003211 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8b07ec22010-05-15 11:32:37 +00003212}
3213void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3214 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003215 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3216 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003217 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003218 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003219}
John McCallfc93cf92009-10-22 22:37:11 +00003220void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003221 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCallfc93cf92009-10-22 22:37:11 +00003222}
John McCall8f115c62009-10-16 21:56:05 +00003223
Sebastian Redl2c373b92010-10-05 15:59:54 +00003224TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003225 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00003226 unsigned &Idx) {
3227 QualType InfoTy = GetType(Record[Idx++]);
3228 if (InfoTy.isNull())
3229 return 0;
3230
John McCallbcd03502009-12-07 02:54:59 +00003231 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003232 TypeLocReader TLR(*this, F, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00003233 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00003234 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00003235 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00003236}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003237
Sebastian Redl539c5062010-08-18 23:57:32 +00003238QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00003239 unsigned FastQuals = ID & Qualifiers::FastMask;
3240 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003241
Sebastian Redl539c5062010-08-18 23:57:32 +00003242 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003243 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00003244 switch ((PredefinedTypeIDs)Index) {
3245 case PREDEF_TYPE_NULL_ID: return QualType();
3246 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3247 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003248
Sebastian Redl539c5062010-08-18 23:57:32 +00003249 case PREDEF_TYPE_CHAR_U_ID:
3250 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003251 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00003252 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003253 break;
3254
Sebastian Redl539c5062010-08-18 23:57:32 +00003255 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3256 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3257 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3258 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3259 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3260 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3261 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3262 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3263 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3264 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3265 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3266 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3267 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3268 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3269 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3270 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3271 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3272 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3273 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3274 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3275 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3276 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3277 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3278 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003279 }
3280
3281 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00003282 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003283 }
3284
Sebastian Redl539c5062010-08-18 23:57:32 +00003285 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003286 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00003287 if (TypesLoaded[Index].isNull()) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003288 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003289 if (TypesLoaded[Index].isNull())
3290 return QualType();
3291
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003292 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003293 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003294 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003295 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003296 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00003297 }
Mike Stump11289f42009-09-09 15:08:12 +00003298
John McCall8ccfcb52009-09-24 19:53:00 +00003299 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003300}
3301
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003302TypeID ASTReader::GetTypeID(QualType T) const {
3303 return MakeTypeID(T,
3304 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3305}
3306
3307TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3308 if (T.isNull())
3309 return TypeIdx();
3310 assert(!T.getLocalFastQualifiers());
3311
3312 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3313 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3314 // comparing keys of ASTDeclContextNameLookupTable.
3315 // If the type didn't come from the AST file use a specially marked index
3316 // so that any hash/key comparison fail since no such index is stored
3317 // in a AST file.
3318 if (I == TypeIdxs.end())
3319 return TypeIdx(-1);
3320 return I->second;
3321}
3322
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003323unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3324 unsigned Result = 0;
3325 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3326 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3327
3328 return Result;
3329}
3330
John McCall0ad16662009-10-29 08:12:44 +00003331TemplateArgumentLocInfo
Sebastian Redl2c373b92010-10-05 15:59:54 +00003332ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3333 TemplateArgument::ArgKind Kind,
John McCall0ad16662009-10-29 08:12:44 +00003334 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003335 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00003336 switch (Kind) {
3337 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003338 return ReadExpr(F);
John McCall0ad16662009-10-29 08:12:44 +00003339 case TemplateArgument::Type:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003340 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003341 case TemplateArgument::Template: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003342 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3343 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003344 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003345 }
John McCall0ad16662009-10-29 08:12:44 +00003346 case TemplateArgument::Null:
3347 case TemplateArgument::Integral:
3348 case TemplateArgument::Declaration:
3349 case TemplateArgument::Pack:
3350 return TemplateArgumentLocInfo();
3351 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00003352 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00003353 return TemplateArgumentLocInfo();
3354}
3355
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003356TemplateArgumentLoc
Sebastian Redl2c373b92010-10-05 15:59:54 +00003357ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003358 const RecordData &Record, unsigned &Index) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003359 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003360
3361 if (Arg.getKind() == TemplateArgument::Expression) {
3362 if (Record[Index++]) // bool InfoHasSameExpr.
3363 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3364 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00003365 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003366 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003367}
3368
Sebastian Redl2c499f62010-08-18 23:56:43 +00003369Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00003370 return GetDecl(ID);
3371}
3372
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003373uint64_t
3374ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3375 if (ID == 0)
3376 return 0;
3377
3378 --ID;
3379 uint64_t Offset = 0;
3380 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3381 if (ID < Chain[I]->LocalNumCXXBaseSpecifiers)
3382 return Offset + Chain[I]->CXXBaseSpecifiersOffsets[ID];
3383
3384 ID -= Chain[I]->LocalNumCXXBaseSpecifiers;
3385 Offset += Chain[I]->SizeInBits;
3386 }
3387
3388 assert(false && "CXXBaseSpecifiers not found");
3389 return 0;
3390}
3391
3392CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3393 // Figure out which AST file contains this offset.
3394 PerFileData *F = 0;
3395 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3396 if (Offset < Chain[I]->SizeInBits) {
3397 F = Chain[I];
3398 break;
3399 }
3400
3401 Offset -= Chain[I]->SizeInBits;
3402 }
3403
3404 if (!F) {
3405 Error("Malformed AST file: C++ base specifiers at impossible offset");
3406 return 0;
3407 }
3408
3409 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3410 SavedStreamPosition SavedPosition(Cursor);
3411 Cursor.JumpToBit(Offset);
3412 ReadingKindTracker ReadingKind(Read_Decl, *this);
3413 RecordData Record;
3414 unsigned Code = Cursor.ReadCode();
3415 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3416 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3417 Error("Malformed AST file: missing C++ base specifiers");
3418 return 0;
3419 }
3420
3421 unsigned Idx = 0;
3422 unsigned NumBases = Record[Idx++];
3423 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3424 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3425 for (unsigned I = 0; I != NumBases; ++I)
3426 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3427 return Bases;
3428}
3429
Sebastian Redl2c499f62010-08-18 23:56:43 +00003430TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003431 if (!DeclsLoaded[0]) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00003432 ReadDeclRecord(0, 1);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003433 if (DeserializationListener)
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003434 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003435 }
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00003436
3437 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3438}
3439
Sebastian Redl539c5062010-08-18 23:57:32 +00003440Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003441 if (ID == 0)
3442 return 0;
3443
Douglas Gregor745ed142009-04-25 18:35:21 +00003444 if (ID > DeclsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003445 Error("declaration ID out-of-range for AST file");
Douglas Gregor745ed142009-04-25 18:35:21 +00003446 return 0;
3447 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003448
Douglas Gregor745ed142009-04-25 18:35:21 +00003449 unsigned Index = ID - 1;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003450 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003451 ReadDeclRecord(Index, ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003452 if (DeserializationListener)
3453 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3454 }
Douglas Gregor745ed142009-04-25 18:35:21 +00003455
3456 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003457}
3458
Chris Lattner9c28af02009-04-27 05:46:25 +00003459/// \brief Resolve the offset of a statement into a statement.
3460///
3461/// This operation will read a new statement from the external
3462/// source each time it is called, and is meant to be used via a
3463/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00003464Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00003465 // Switch case IDs are per Decl.
3466 ClearSwitchCaseIDs();
3467
Sebastian Redl5c415f32010-07-22 17:01:13 +00003468 // Offset here is a global offset across the entire chain.
3469 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3470 PerFileData &F = *Chain[N - I - 1];
3471 if (Offset < F.SizeInBits) {
3472 // Since we know that this statement is part of a decl, make sure to use
3473 // the decl cursor to read it.
3474 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003475 return ReadStmtFromStream(F);
Sebastian Redl5c415f32010-07-22 17:01:13 +00003476 }
3477 Offset -= F.SizeInBits;
3478 }
3479 llvm_unreachable("Broken chain");
Douglas Gregor3c3aa612009-04-18 00:07:54 +00003480}
3481
Sebastian Redl2c499f62010-08-18 23:56:43 +00003482bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003483 bool (*isKindWeWant)(Decl::Kind),
John McCall75b960e2010-06-01 09:23:16 +00003484 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump11289f42009-09-09 15:08:12 +00003485 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003486 "DeclContext has no lexical decls in storage");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003487
Sebastian Redl5c415f32010-07-22 17:01:13 +00003488 // There might be lexical decls in multiple parts of the chain, for the TU
3489 // at least.
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003490 // DeclContextOffsets might reallocate as we load additional decls below,
3491 // so make a copy of the vector.
3492 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl5c415f32010-07-22 17:01:13 +00003493 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3494 I != E; ++I) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003495 // IDs can be 0 if this context doesn't contain declarations.
3496 if (!I->LexicalDecls)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003497 continue;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003498
3499 // Load all of the declaration IDs
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003500 for (const KindDeclIDPair *ID = I->LexicalDecls,
3501 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3502 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3503 continue;
3504
3505 Decl *D = GetDecl(ID->second);
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003506 assert(D && "Null decl in lexical decls");
3507 Decls.push_back(D);
3508 }
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003509 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003510
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003511 ++NumLexicalDeclContextsRead;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003512 return false;
3513}
3514
John McCall75b960e2010-06-01 09:23:16 +00003515DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00003516ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00003517 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003518 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003519 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003520 if (!Name)
3521 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3522 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003523
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003524 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl471ac2f2010-08-24 00:49:55 +00003525 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003526 // and namespaces. For any given name, the last available results replace
3527 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl5c415f32010-07-22 17:01:13 +00003528 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003529 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl5c415f32010-07-22 17:01:13 +00003530 I != E; ++I) {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003531 if (!I->NameLookupTableData)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003532 continue;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003533
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003534 ASTDeclContextNameLookupTable *LookupTable =
3535 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3536 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3537 if (Pos == LookupTable->end())
Sebastian Redl5c415f32010-07-22 17:01:13 +00003538 continue;
3539
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003540 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3541 for (; Data.first != Data.second; ++Data.first)
3542 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003543 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003544 }
3545
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003546 ++NumVisibleDeclContextsRead;
John McCall75b960e2010-06-01 09:23:16 +00003547
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003548 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00003549 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003550}
3551
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00003552void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3553 assert(DC->hasExternalVisibleStorage() &&
3554 "DeclContext has no visible decls in storage");
3555
3556 llvm::SmallVector<NamedDecl *, 64> Decls;
3557 // There might be visible decls in multiple parts of the chain, for the TU
3558 // and namespaces.
3559 DeclContextInfos &Infos = DeclContextOffsets[DC];
3560 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3561 I != E; ++I) {
3562 if (!I->NameLookupTableData)
3563 continue;
3564
3565 ASTDeclContextNameLookupTable *LookupTable =
3566 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3567 for (ASTDeclContextNameLookupTable::item_iterator
3568 ItemI = LookupTable->item_begin(),
3569 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3570 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3571 = *ItemI;
3572 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3573 Decls.clear();
3574 for (; Data.first != Data.second; ++Data.first)
3575 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3576 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3577 }
3578 }
3579}
3580
Sebastian Redl2c499f62010-08-18 23:56:43 +00003581void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003582 assert(Consumer);
3583 while (!InterestingDecls.empty()) {
3584 DeclGroupRef DG(InterestingDecls.front());
3585 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00003586 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003587 }
3588}
3589
Sebastian Redl2c499f62010-08-18 23:56:43 +00003590void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00003591 this->Consumer = Consumer;
3592
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003593 if (!Consumer)
3594 return;
3595
3596 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003597 // Force deserialization of this decl, which will cause it to be queued for
3598 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00003599 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003600 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00003601
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003602 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003603}
3604
Sebastian Redl2c499f62010-08-18 23:56:43 +00003605void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003606 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003607
Mike Stump11289f42009-09-09 15:08:12 +00003608 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003609 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00003610 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00003611 unsigned NumDeclsLoaded
3612 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3613 (Decl *)0);
3614 unsigned NumIdentifiersLoaded
3615 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3616 IdentifiersLoaded.end(),
3617 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00003618 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003619 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3620 SelectorsLoaded.end(),
3621 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00003622
Douglas Gregorc5046832009-04-27 18:38:38 +00003623 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3624 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor258ae542009-04-27 06:38:32 +00003625 if (TotalNumSLocEntries)
3626 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3627 NumSLocEntriesRead, TotalNumSLocEntries,
3628 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00003629 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003630 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003631 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3632 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3633 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003634 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003635 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3636 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00003637 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003638 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00003639 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3640 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00003641 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003642 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00003643 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3644 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003645 if (TotalNumStatements)
3646 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3647 NumStatementsRead, TotalNumStatements,
3648 ((float)NumStatementsRead/TotalNumStatements * 100));
3649 if (TotalNumMacros)
3650 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3651 NumMacrosRead, TotalNumMacros,
3652 ((float)NumMacrosRead/TotalNumMacros * 100));
3653 if (TotalLexicalDeclContexts)
3654 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3655 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3656 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3657 * 100));
3658 if (TotalVisibleDeclContexts)
3659 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3660 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3661 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3662 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003663 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003664 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003665 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3666 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00003667 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003668 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003669 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003670 std::fprintf(stderr, "\n");
3671}
3672
Sebastian Redl2c499f62010-08-18 23:56:43 +00003673void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00003674 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003675 S.ExternalSource = this;
3676
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003677 // Makes sure any declarations that were deserialized "too early"
3678 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003679 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3680 if (SemaObj->TUScope)
John McCall48871652010-08-21 09:40:31 +00003681 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003682
3683 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003684 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003685 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00003686
3687 // If there were any tentative definitions, deserialize them and add
Sebastian Redl35351a92010-01-31 22:27:38 +00003688 // them to Sema's list of tentative definitions.
Douglas Gregord4df8652009-04-22 22:02:47 +00003689 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3690 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redl35351a92010-01-31 22:27:38 +00003691 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregord4df8652009-04-22 22:02:47 +00003692 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00003693
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003694 // If there were any unused file scoped decls, deserialize them and add to
3695 // Sema's list of unused file scoped decls.
3696 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3697 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3698 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattner90073802010-02-12 00:07:30 +00003699 }
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003700
3701 // If there were any locally-scoped external declarations,
3702 // deserialize them and add them to Sema's table of locally-scoped
3703 // external declarations.
3704 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3705 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3706 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3707 }
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003708
3709 // If there were any ext_vector type declarations, deserialize them
3710 // and add them to Sema's vector of such declarations.
3711 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3712 SemaObj->ExtVectorDecls.push_back(
3713 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003714
3715 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3716 // Can we cut them down before writing them ?
3717
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003718 // If there were any dynamic classes declarations, deserialize them
3719 // and add them to Sema's vector of such declarations.
3720 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3721 SemaObj->DynamicClasses.push_back(
3722 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003723
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003724 // Load the offsets of the declarations that Sema references.
3725 // They will be lazily deserialized when needed.
3726 if (!SemaDeclRefs.empty()) {
3727 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3728 SemaObj->StdNamespace = SemaDeclRefs[0];
3729 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3730 }
3731
Sebastian Redl2c373b92010-10-05 15:59:54 +00003732 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
3733
3734 // If there are @selector references added them to its pool. This is for
3735 // implementation of -Wselector.
3736 if (!F->ReferencedSelectorsData.empty()) {
3737 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
3738 unsigned I = 0;
3739 while (I < DataSize) {
3740 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
3741 SourceLocation SelLoc = ReadSourceLocation(
3742 *F, F->ReferencedSelectorsData, I);
3743 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3744 }
3745 }
3746
3747 // If there were any pending implicit instantiations, deserialize them
3748 // and add them to Sema's queue of such instantiations.
3749 assert(F->PendingInstantiations.size() % 2 == 0 &&
3750 "Expected pairs of entries");
3751 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
3752 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
3753 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
3754 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
3755 }
3756 }
3757
3758 // The two special data sets below always come from the most recent PCH,
3759 // which is at the front of the chain.
3760 PerFileData &F = *Chain.front();
3761
3762 // If there were any weak undeclared identifiers, deserialize them and add to
3763 // Sema's list of weak undeclared identifiers.
3764 if (!WeakUndeclaredIdentifiers.empty()) {
3765 unsigned Idx = 0;
3766 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3767 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3768 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3769 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
3770 bool Used = WeakUndeclaredIdentifiers[Idx++];
3771 Sema::WeakInfo WI(AliasId, Loc);
3772 WI.setUsed(Used);
3773 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3774 }
3775 }
3776
3777 // If there were any VTable uses, deserialize the information and add it
3778 // to Sema's vector and map of VTable uses.
3779 if (!VTableUses.empty()) {
3780 unsigned Idx = 0;
3781 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3782 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3783 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
3784 bool DefinitionRequired = VTableUses[Idx++];
3785 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3786 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003787 }
3788 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00003789}
3790
Sebastian Redl2c499f62010-08-18 23:56:43 +00003791IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redl78f51772010-08-02 18:30:12 +00003792 // Try to find this name within our on-disk hash tables. We start with the
3793 // most recent one, since that one contains the most up-to-date info.
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003794 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003795 ASTIdentifierLookupTable *IdTable
3796 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003797 if (!IdTable)
3798 continue;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003799 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003800 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003801 if (Pos == IdTable->end())
3802 continue;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003803
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003804 // Dereferencing the iterator has the effect of building the
3805 // IdentifierInfo node and populating it with the various
3806 // declarations it needs.
Sebastian Redl78f51772010-08-02 18:30:12 +00003807 return *Pos;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003808 }
Sebastian Redl78f51772010-08-02 18:30:12 +00003809 return 0;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003810}
3811
Douglas Gregor57756ea2010-10-14 22:11:03 +00003812namespace clang {
3813 /// \brief An identifier-lookup iterator that enumerates all of the
3814 /// identifiers stored within a set of AST files.
3815 class ASTIdentifierIterator : public IdentifierIterator {
3816 /// \brief The AST reader whose identifiers are being enumerated.
3817 const ASTReader &Reader;
3818
3819 /// \brief The current index into the chain of AST files stored in
3820 /// the AST reader.
3821 unsigned Index;
3822
3823 /// \brief The current position within the identifier lookup table
3824 /// of the current AST file.
3825 ASTIdentifierLookupTable::key_iterator Current;
3826
3827 /// \brief The end position within the identifier lookup table of
3828 /// the current AST file.
3829 ASTIdentifierLookupTable::key_iterator End;
3830
3831 public:
3832 explicit ASTIdentifierIterator(const ASTReader &Reader);
3833
3834 virtual llvm::StringRef Next();
3835 };
3836}
3837
3838ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
3839 : Reader(Reader), Index(Reader.Chain.size() - 1) {
3840 ASTIdentifierLookupTable *IdTable
3841 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3842 Current = IdTable->key_begin();
3843 End = IdTable->key_end();
3844}
3845
3846llvm::StringRef ASTIdentifierIterator::Next() {
3847 while (Current == End) {
3848 // If we have exhausted all of our AST files, we're done.
3849 if (Index == 0)
3850 return llvm::StringRef();
3851
3852 --Index;
3853 ASTIdentifierLookupTable *IdTable
3854 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3855 Current = IdTable->key_begin();
3856 End = IdTable->key_end();
3857 }
3858
3859 // We have any identifiers remaining in the current AST file; return
3860 // the next one.
3861 std::pair<const char*, unsigned> Key = *Current;
3862 ++Current;
3863 return llvm::StringRef(Key.first, Key.second);
3864}
3865
3866IdentifierIterator *ASTReader::getIdentifiers() const {
3867 return new ASTIdentifierIterator(*this);
3868}
3869
Mike Stump11289f42009-09-09 15:08:12 +00003870std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redl2c499f62010-08-18 23:56:43 +00003871ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redlada023c2010-08-04 20:40:17 +00003872 // Find this selector in a hash table. We want to find the most recent entry.
3873 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3874 PerFileData &F = *Chain[I];
3875 if (!F.SelectorLookupTable)
3876 continue;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003877
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003878 ASTSelectorLookupTable *PoolTable
3879 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3880 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redlada023c2010-08-04 20:40:17 +00003881 if (Pos != PoolTable->end()) {
3882 ++NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003883 // FIXME: Not quite happy with the statistics here. We probably should
3884 // disable this tracking when called via LoadSelector.
3885 // Also, should entries without methods count as misses?
3886 ++NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003887 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redlada023c2010-08-04 20:40:17 +00003888 if (DeserializationListener)
3889 DeserializationListener->SelectorRead(Data.ID, Sel);
3890 return std::make_pair(Data.Instance, Data.Factory);
3891 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003892 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003893
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003894 ++NumMethodPoolMisses;
Sebastian Redlada023c2010-08-04 20:40:17 +00003895 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorc78d3462009-04-24 21:10:55 +00003896}
3897
Sebastian Redl2c499f62010-08-18 23:56:43 +00003898void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003899 // It would be complicated to avoid reading the methods anyway. So don't.
3900 ReadMethodPool(Sel);
3901}
3902
Sebastian Redl2c499f62010-08-18 23:56:43 +00003903void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00003904 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00003905 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00003906 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00003907 if (DeserializationListener)
3908 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003909}
3910
Douglas Gregor1342e842009-07-06 18:54:52 +00003911/// \brief Set the globally-visible declarations associated with the given
3912/// identifier.
3913///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003914/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00003915/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00003916/// them.
3917///
3918/// \param II an IdentifierInfo that refers to one or more globally-visible
3919/// declarations.
3920///
3921/// \param DeclIDs the set of declaration IDs with the name @p II that are
3922/// visible at global scope.
3923///
3924/// \param Nonrecursive should be true to indicate that the caller knows that
3925/// this call is non-recursive, and therefore the globally-visible declarations
3926/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00003927void
Sebastian Redl2c499f62010-08-18 23:56:43 +00003928ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregor1342e842009-07-06 18:54:52 +00003929 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
3930 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003931 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00003932 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
3933 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
3934 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003935 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00003936 return;
3937 }
Mike Stump11289f42009-09-09 15:08:12 +00003938
Douglas Gregor1342e842009-07-06 18:54:52 +00003939 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
3940 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
3941 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00003942 if (SemaObj->TUScope) {
3943 // Introduce this declaration into the translation-unit scope
3944 // and add it to the declaration chain for this identifier, so
3945 // that (unqualified) name lookup will find it.
John McCall48871652010-08-21 09:40:31 +00003946 SemaObj->TUScope->AddDecl(D);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00003947 }
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003948 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregor1342e842009-07-06 18:54:52 +00003949 } else {
3950 // Queue this declaration so that it will be added to the
3951 // translation unit scope and identifier's declaration chain
3952 // once a Sema object is known.
3953 PreloadedDecls.push_back(D);
3954 }
3955 }
3956}
3957
Sebastian Redl2c499f62010-08-18 23:56:43 +00003958IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003959 if (ID == 0)
3960 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00003961
Sebastian Redlc713b962010-07-21 00:46:22 +00003962 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003963 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003964 return 0;
3965 }
Mike Stump11289f42009-09-09 15:08:12 +00003966
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003967 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc713b962010-07-21 00:46:22 +00003968 ID -= 1;
3969 if (!IdentifiersLoaded[ID]) {
3970 unsigned Index = ID;
3971 const char *Str = 0;
3972 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3973 PerFileData *F = Chain[N - I - 1];
3974 if (Index < F->LocalNumIdentifiers) {
3975 uint32_t Offset = F->IdentifierOffsets[Index];
3976 Str = F->IdentifierTableData + Offset;
3977 break;
3978 }
3979 Index -= F->LocalNumIdentifiers;
3980 }
3981 assert(Str && "Broken Chain");
Douglas Gregor5287b4e2009-04-25 21:04:17 +00003982
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003983 // All of the strings in the AST file are preceded by a 16-bit length.
3984 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00003985 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
3986 // unsigned integers. This is important to avoid integer overflow when
3987 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00003988 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00003989 unsigned StrLen = (((unsigned) StrLenPtr[0])
3990 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00003991 IdentifiersLoaded[ID]
Kovarththanan Rajaratnama3b09592010-03-12 10:32:27 +00003992 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003993 if (DeserializationListener)
3994 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003995 }
Mike Stump11289f42009-09-09 15:08:12 +00003996
Sebastian Redlc713b962010-07-21 00:46:22 +00003997 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003998}
3999
Sebastian Redl2c499f62010-08-18 23:56:43 +00004000void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00004001 ReadSLocEntryRecord(ID);
4002}
4003
Sebastian Redl2c499f62010-08-18 23:56:43 +00004004Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00004005 if (ID == 0)
4006 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00004007
Sebastian Redlada023c2010-08-04 20:40:17 +00004008 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004009 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00004010 return Selector();
4011 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004012
Sebastian Redlada023c2010-08-04 20:40:17 +00004013 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004014 // Load this selector from the selector table.
Sebastian Redlada023c2010-08-04 20:40:17 +00004015 unsigned Idx = ID - 1;
4016 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4017 PerFileData &F = *Chain[N - I - 1];
4018 if (Idx < F.LocalNumSelectors) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004019 ASTSelectorLookupTrait Trait(*this);
Sebastian Redlada023c2010-08-04 20:40:17 +00004020 SelectorsLoaded[ID - 1] =
4021 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4022 if (DeserializationListener)
4023 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4024 break;
4025 }
4026 Idx -= F.LocalNumSelectors;
4027 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004028 }
4029
Sebastian Redlada023c2010-08-04 20:40:17 +00004030 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00004031}
4032
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004033Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00004034 return DecodeSelector(ID);
4035}
4036
Sebastian Redl2c499f62010-08-18 23:56:43 +00004037uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00004038 // ID 0 (the null selector) is considered an external selector.
4039 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00004040}
4041
Mike Stump11289f42009-09-09 15:08:12 +00004042DeclarationName
Sebastian Redl2c499f62010-08-18 23:56:43 +00004043ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004044 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4045 switch (Kind) {
4046 case DeclarationName::Identifier:
4047 return DeclarationName(GetIdentifierInfo(Record, Idx));
4048
4049 case DeclarationName::ObjCZeroArgSelector:
4050 case DeclarationName::ObjCOneArgSelector:
4051 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff3c301dc2009-04-23 15:15:40 +00004052 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004053
4054 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004055 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004056 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004057
4058 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004059 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004060 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004061
4062 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004063 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004064 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004065
4066 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004067 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004068 (OverloadedOperatorKind)Record[Idx++]);
4069
Alexis Hunt3d221f22009-11-29 07:34:05 +00004070 case DeclarationName::CXXLiteralOperatorName:
4071 return Context->DeclarationNames.getCXXLiteralOperatorName(
4072 GetIdentifierInfo(Record, Idx));
4073
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004074 case DeclarationName::CXXUsingDirective:
4075 return DeclarationName::getUsingDirectiveName();
4076 }
4077
4078 // Required to silence GCC warning
4079 return DeclarationName();
4080}
Douglas Gregor55abb232009-04-10 20:39:37 +00004081
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004082void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4083 DeclarationNameLoc &DNLoc,
4084 DeclarationName Name,
4085 const RecordData &Record, unsigned &Idx) {
4086 switch (Name.getNameKind()) {
4087 case DeclarationName::CXXConstructorName:
4088 case DeclarationName::CXXDestructorName:
4089 case DeclarationName::CXXConversionFunctionName:
4090 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4091 break;
4092
4093 case DeclarationName::CXXOperatorName:
4094 DNLoc.CXXOperatorName.BeginOpNameLoc
4095 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4096 DNLoc.CXXOperatorName.EndOpNameLoc
4097 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4098 break;
4099
4100 case DeclarationName::CXXLiteralOperatorName:
4101 DNLoc.CXXLiteralOperatorName.OpNameLoc
4102 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4103 break;
4104
4105 case DeclarationName::Identifier:
4106 case DeclarationName::ObjCZeroArgSelector:
4107 case DeclarationName::ObjCOneArgSelector:
4108 case DeclarationName::ObjCMultiArgSelector:
4109 case DeclarationName::CXXUsingDirective:
4110 break;
4111 }
4112}
4113
4114void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4115 DeclarationNameInfo &NameInfo,
4116 const RecordData &Record, unsigned &Idx) {
4117 NameInfo.setName(ReadDeclarationName(Record, Idx));
4118 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4119 DeclarationNameLoc DNLoc;
4120 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4121 NameInfo.setInfo(DNLoc);
4122}
4123
4124void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4125 const RecordData &Record, unsigned &Idx) {
4126 Info.NNS = ReadNestedNameSpecifier(Record, Idx);
4127 Info.NNSRange = ReadSourceRange(F, Record, Idx);
4128 unsigned NumTPLists = Record[Idx++];
4129 Info.NumTemplParamLists = NumTPLists;
4130 if (NumTPLists) {
4131 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4132 for (unsigned i=0; i != NumTPLists; ++i)
4133 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4134 }
4135}
4136
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004137TemplateName
Sebastian Redl2c499f62010-08-18 23:56:43 +00004138ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004139 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004140 switch (Kind) {
4141 case TemplateName::Template:
4142 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4143
4144 case TemplateName::OverloadedTemplate: {
4145 unsigned size = Record[Idx++];
4146 UnresolvedSet<8> Decls;
4147 while (size--)
4148 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4149
4150 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4151 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004152
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004153 case TemplateName::QualifiedTemplate: {
4154 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4155 bool hasTemplKeyword = Record[Idx++];
4156 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4157 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4158 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004159
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004160 case TemplateName::DependentTemplate: {
4161 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4162 if (Record[Idx++]) // isIdentifier
4163 return Context->getDependentTemplateName(NNS,
4164 GetIdentifierInfo(Record, Idx));
4165 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004166 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004167 }
4168 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004169
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004170 assert(0 && "Unhandled template name kind!");
4171 return TemplateName();
4172}
4173
4174TemplateArgument
Sebastian Redl2c373b92010-10-05 15:59:54 +00004175ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00004176 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004177 switch ((TemplateArgument::ArgKind)Record[Idx++]) {
4178 case TemplateArgument::Null:
4179 return TemplateArgument();
4180 case TemplateArgument::Type:
4181 return TemplateArgument(GetType(Record[Idx++]));
4182 case TemplateArgument::Declaration:
4183 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00004184 case TemplateArgument::Integral: {
4185 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4186 QualType T = GetType(Record[Idx++]);
4187 return TemplateArgument(Value, T);
4188 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004189 case TemplateArgument::Template:
4190 return TemplateArgument(ReadTemplateName(Record, Idx));
4191 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004192 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004193 case TemplateArgument::Pack: {
4194 unsigned NumArgs = Record[Idx++];
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004195 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4196 for (unsigned I = 0; I != NumArgs; ++I)
4197 Args[I] = ReadTemplateArgument(F, Record, Idx);
4198 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004199 }
4200 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004201
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004202 assert(0 && "Unhandled template argument kind!");
4203 return TemplateArgument();
4204}
4205
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004206TemplateParameterList *
Sebastian Redl2c373b92010-10-05 15:59:54 +00004207ASTReader::ReadTemplateParameterList(PerFileData &F,
4208 const RecordData &Record, unsigned &Idx) {
4209 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4210 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4211 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004212
4213 unsigned NumParams = Record[Idx++];
4214 llvm::SmallVector<NamedDecl *, 16> Params;
4215 Params.reserve(NumParams);
4216 while (NumParams--)
4217 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004218
4219 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004220 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4221 Params.data(), Params.size(), RAngleLoc);
4222 return TemplateParams;
4223}
4224
4225void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004226ASTReader::
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004227ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004228 PerFileData &F, const RecordData &Record,
4229 unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004230 unsigned NumTemplateArgs = Record[Idx++];
4231 TemplArgs.reserve(NumTemplateArgs);
4232 while (NumTemplateArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004233 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004234}
4235
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004236/// \brief Read a UnresolvedSet structure.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004237void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004238 const RecordData &Record, unsigned &Idx) {
4239 unsigned NumDecls = Record[Idx++];
4240 while (NumDecls--) {
4241 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4242 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4243 Set.addDecl(D, AS);
4244 }
4245}
4246
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004247CXXBaseSpecifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00004248ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky19b9f952010-07-26 16:56:01 +00004249 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004250 bool isVirtual = static_cast<bool>(Record[Idx++]);
4251 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4252 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004253 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4254 SourceRange Range = ReadSourceRange(F, Record, Idx);
Nick Lewycky19b9f952010-07-26 16:56:01 +00004255 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004256}
4257
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004258std::pair<CXXBaseOrMemberInitializer **, unsigned>
Sebastian Redl2c373b92010-10-05 15:59:54 +00004259ASTReader::ReadCXXBaseOrMemberInitializers(PerFileData &F,
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004260 const RecordData &Record,
4261 unsigned &Idx) {
4262 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0;
4263 unsigned NumInitializers = Record[Idx++];
4264 if (NumInitializers) {
4265 ASTContext &C = *getContext();
4266
4267 BaseOrMemberInitializers
4268 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
4269 for (unsigned i=0; i != NumInitializers; ++i) {
4270 TypeSourceInfo *BaseClassInfo = 0;
4271 bool IsBaseVirtual = false;
4272 FieldDecl *Member = 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004273
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004274 bool IsBaseInitializer = Record[Idx++];
4275 if (IsBaseInitializer) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004276 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004277 IsBaseVirtual = Record[Idx++];
4278 } else {
4279 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
4280 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00004281 SourceLocation MemberLoc = ReadSourceLocation(F, Record, Idx);
4282 Expr *Init = ReadExpr(F);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004283 FieldDecl *AnonUnionMember
4284 = cast_or_null<FieldDecl>(GetDecl(Record[Idx++]));
Sebastian Redl2c373b92010-10-05 15:59:54 +00004285 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4286 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004287 bool IsWritten = Record[Idx++];
4288 unsigned SourceOrderOrNumArrayIndices;
4289 llvm::SmallVector<VarDecl *, 8> Indices;
4290 if (IsWritten) {
4291 SourceOrderOrNumArrayIndices = Record[Idx++];
4292 } else {
4293 SourceOrderOrNumArrayIndices = Record[Idx++];
4294 Indices.reserve(SourceOrderOrNumArrayIndices);
4295 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4296 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4297 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004298
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004299 CXXBaseOrMemberInitializer *BOMInit;
4300 if (IsBaseInitializer) {
4301 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
4302 IsBaseVirtual, LParenLoc,
4303 Init, RParenLoc);
4304 } else if (IsWritten) {
4305 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc,
4306 LParenLoc, Init, RParenLoc);
4307 } else {
4308 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc,
4309 LParenLoc, Init, RParenLoc,
4310 Indices.data(),
4311 Indices.size());
4312 }
4313
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00004314 if (IsWritten)
4315 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004316 BOMInit->setAnonUnionMember(AnonUnionMember);
4317 BaseOrMemberInitializers[i] = BOMInit;
4318 }
4319 }
4320
4321 return std::make_pair(BaseOrMemberInitializers, NumInitializers);
4322}
4323
Chris Lattnerca025db2010-05-07 21:43:38 +00004324NestedNameSpecifier *
Sebastian Redl2c499f62010-08-18 23:56:43 +00004325ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004326 unsigned N = Record[Idx++];
4327 NestedNameSpecifier *NNS = 0, *Prev = 0;
4328 for (unsigned I = 0; I != N; ++I) {
4329 NestedNameSpecifier::SpecifierKind Kind
4330 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4331 switch (Kind) {
4332 case NestedNameSpecifier::Identifier: {
4333 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4334 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4335 break;
4336 }
4337
4338 case NestedNameSpecifier::Namespace: {
4339 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4340 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4341 break;
4342 }
4343
4344 case NestedNameSpecifier::TypeSpec:
4345 case NestedNameSpecifier::TypeSpecWithTemplate: {
4346 Type *T = GetType(Record[Idx++]).getTypePtr();
4347 bool Template = Record[Idx++];
4348 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4349 break;
4350 }
4351
4352 case NestedNameSpecifier::Global: {
4353 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4354 // No associated value, and there can't be a prefix.
4355 break;
4356 }
Chris Lattnerca025db2010-05-07 21:43:38 +00004357 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00004358 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00004359 }
4360 return NNS;
4361}
4362
4363SourceRange
Sebastian Redl2c373b92010-10-05 15:59:54 +00004364ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4365 unsigned &Idx) {
4366 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4367 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00004368 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00004369}
4370
Douglas Gregor1daeb692009-04-13 18:14:40 +00004371/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004372llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004373 unsigned BitWidth = Record[Idx++];
4374 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4375 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4376 Idx += NumWords;
4377 return Result;
4378}
4379
4380/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004381llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004382 bool isUnsigned = Record[Idx++];
4383 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4384}
4385
Douglas Gregore0a3a512009-04-14 21:55:33 +00004386/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004387llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004388 return llvm::APFloat(ReadAPInt(Record, Idx));
4389}
4390
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004391// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00004392std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004393 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00004394 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004395 Idx += Len;
4396 return Result;
4397}
4398
Sebastian Redl2c499f62010-08-18 23:56:43 +00004399CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00004400 unsigned &Idx) {
4401 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4402 return CXXTemporary::Create(*Context, Decl);
4403}
4404
Sebastian Redl2c499f62010-08-18 23:56:43 +00004405DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00004406 return Diag(SourceLocation(), DiagID);
4407}
4408
Sebastian Redl2c499f62010-08-18 23:56:43 +00004409DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004410 return Diags.Report(Loc, DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00004411}
Douglas Gregora9af1d12009-04-17 00:04:06 +00004412
Douglas Gregora868bbd2009-04-21 22:25:48 +00004413/// \brief Retrieve the identifier table associated with the
4414/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004415IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004416 assert(PP && "Forgot to set Preprocessor ?");
4417 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00004418}
4419
Douglas Gregora9af1d12009-04-17 00:04:06 +00004420/// \brief Record that the given ID maps to the given switch-case
4421/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004422void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004423 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4424 SwitchCaseStmts[ID] = SC;
4425}
4426
4427/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004428SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004429 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4430 return SwitchCaseStmts[ID];
4431}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004432
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00004433void ASTReader::ClearSwitchCaseIDs() {
4434 SwitchCaseStmts.clear();
4435}
4436
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004437/// \brief Record that the given label statement has been
4438/// deserialized and has the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004439void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump11289f42009-09-09 15:08:12 +00004440 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004441 "Deserialized label twice");
4442 LabelStmts[ID] = S;
4443
4444 // If we've already seen any goto statements that point to this
4445 // label, resolve them now.
4446 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
4447 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
4448 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
4449 Goto->second->setLabel(S);
4450 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor779d8652009-04-17 18:58:21 +00004451
4452 // If we've already seen any address-label statements that point to
4453 // this label, resolve them now.
4454 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump11289f42009-09-09 15:08:12 +00004455 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor779d8652009-04-17 18:58:21 +00004456 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump11289f42009-09-09 15:08:12 +00004457 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor779d8652009-04-17 18:58:21 +00004458 AddrLabel != AddrLabels.second; ++AddrLabel)
4459 AddrLabel->second->setLabel(S);
4460 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004461}
4462
4463/// \brief Set the label of the given statement to the label
4464/// identified by ID.
4465///
4466/// Depending on the order in which the label and other statements
4467/// referencing that label occur, this operation may complete
4468/// immediately (updating the statement) or it may queue the
4469/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004470void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004471 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4472 if (Label != LabelStmts.end()) {
4473 // We've already seen this label, so set the label of the goto and
4474 // we're done.
4475 S->setLabel(Label->second);
4476 } else {
4477 // We haven't seen this label yet, so add this goto to the set of
4478 // unresolved goto statements.
4479 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
4480 }
4481}
Douglas Gregor779d8652009-04-17 18:58:21 +00004482
4483/// \brief Set the label of the given expression to the label
4484/// identified by ID.
4485///
4486/// Depending on the order in which the label and other statements
4487/// referencing that label occur, this operation may complete
4488/// immediately (updating the statement) or it may queue the
4489/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004490void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor779d8652009-04-17 18:58:21 +00004491 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4492 if (Label != LabelStmts.end()) {
4493 // We've already seen this label, so set the label of the
4494 // label-address expression and we're done.
4495 S->setLabel(Label->second);
4496 } else {
4497 // We haven't seen this label yet, so add this label-address
4498 // expression to the set of unresolved label-address expressions.
4499 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
4500 }
4501}
Douglas Gregor1342e842009-07-06 18:54:52 +00004502
Sebastian Redl2c499f62010-08-18 23:56:43 +00004503void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004504 assert(NumCurrentElementsDeserializing &&
4505 "FinishedDeserializing not paired with StartedDeserializing");
4506 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004507 // If any identifiers with corresponding top-level declarations have
4508 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004509 while (!PendingIdentifierInfos.empty()) {
4510 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4511 PendingIdentifierInfos.front().DeclIDs, true);
4512 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00004513 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004514
4515 // We are not in recursive loading, so it's safe to pass the "interesting"
4516 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004517 if (Consumer)
4518 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidisad5f95c2010-10-24 17:26:31 +00004519
4520 assert(PendingForwardRefs.size() == 0 &&
4521 "Some forward refs did not get linked to the definition!");
Douglas Gregor1342e842009-07-06 18:54:52 +00004522 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004523 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00004524}
Douglas Gregorb473b072010-08-19 00:28:17 +00004525
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004526ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
4527 const char *isysroot, bool DisableValidation)
4528 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4529 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4530 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4531 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
4532 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00004533 TotalNumSLocEntries(0), NextSLocOffset(0), NumStatementsRead(0),
4534 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
4535 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004536 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4537 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4538 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
4539 RelocatablePCH = false;
4540}
4541
4542ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4543 Diagnostic &Diags, const char *isysroot,
4544 bool DisableValidation)
4545 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4546 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
4547 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
4548 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00004549 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4550 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4551 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4552 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4553 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4554 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004555 RelocatablePCH = false;
4556}
4557
4558ASTReader::~ASTReader() {
4559 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4560 delete Chain[e - i - 1];
4561 // Delete all visible decl lookup tables
4562 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4563 E = DeclContextOffsets.end();
4564 I != E; ++I) {
4565 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4566 J != F; ++J) {
4567 if (J->NameLookupTableData)
4568 delete static_cast<ASTDeclContextNameLookupTable*>(
4569 J->NameLookupTableData);
4570 }
4571 }
4572 for (DeclContextVisibleUpdatesPending::iterator
4573 I = PendingVisibleUpdates.begin(),
4574 E = PendingVisibleUpdates.end();
4575 I != E; ++I) {
4576 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4577 F = I->second.end();
4578 J != F; ++J)
4579 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4580 }
4581}
4582
Sebastian Redl009e7f22010-10-05 16:15:19 +00004583ASTReader::PerFileData::PerFileData(ASTFileType Ty)
4584 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00004585 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
4586 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
4587 MacroDefinitionOffsets(0), LocalNumSelectors(0), SelectorOffsets(0),
4588 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004589 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
4590 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redl3f6b7532010-10-01 19:59:12 +00004591 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregorb473b072010-08-19 00:28:17 +00004592{}
4593
4594ASTReader::PerFileData::~PerFileData() {
4595 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
4596 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4597}
4598