blob: 9fe6aac22eaad86a617c863ced56b93fbf36882c [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"
Daniel Dunbarf2ce9a22009-11-18 19:50:41 +000043#include "llvm/System/Path.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000044#include <algorithm>
Douglas Gregorc379c072009-04-28 18:58:38 +000045#include <iterator>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000046#include <cstdio>
Douglas Gregorc5046832009-04-27 18:38:38 +000047#include <sys/stat.h>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000048using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000049using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000050
51//===----------------------------------------------------------------------===//
Sebastian Redld44cd6a2010-08-18 23:57:06 +000052// PCH validator implementation
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000053//===----------------------------------------------------------------------===//
54
Sebastian Redl3e31c722010-08-18 23:56:56 +000055ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000056
57bool
58PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
59 const LangOptions &PPLangOpts = PP.getLangOptions();
60#define PARSE_LANGOPT_BENIGN(Option)
61#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
62 if (PPLangOpts.Option != LangOpts.Option) { \
63 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
64 return true; \
65 }
66
67 PARSE_LANGOPT_BENIGN(Trigraphs);
68 PARSE_LANGOPT_BENIGN(BCPLComment);
69 PARSE_LANGOPT_BENIGN(DollarIdents);
70 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
71 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carruthe03aa552010-04-17 20:17:31 +000072 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000073 PARSE_LANGOPT_BENIGN(ImplicitInt);
74 PARSE_LANGOPT_BENIGN(Digraphs);
75 PARSE_LANGOPT_BENIGN(HexFloats);
76 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
77 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +000078 PARSE_LANGOPT_BENIGN(MSCVersion);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000079 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
80 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
81 PARSE_LANGOPT_BENIGN(CXXOperatorName);
82 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
83 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
84 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian45878032010-02-09 19:31:38 +000085 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +000086 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian62c56022010-04-22 21:01:59 +000087 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000088 PARSE_LANGOPT_BENIGN(PascalStrings);
89 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump11289f42009-09-09 15:08:12 +000090 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000091 diag::warn_pch_lax_vector_conversions);
Nate Begeman9d905792009-06-25 22:57:40 +000092 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000093 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +000094 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000095 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
96 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
97 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump11289f42009-09-09 15:08:12 +000098 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000099 diag::warn_pch_thread_safe_statics);
Daniel Dunbara77eaeb2009-09-03 04:54:28 +0000100 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000101 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
102 PARSE_LANGOPT_BENIGN(EmitAllDecls);
103 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattner51924e512010-06-26 21:25:03 +0000104 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump11289f42009-09-09 15:08:12 +0000105 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000106 diag::warn_pch_heinous_extensions);
107 // FIXME: Most of the options below are benign if the macro wasn't
108 // used. Unfortunately, this means that a PCH compiled without
109 // optimization can't be used with optimization turned on, even
110 // though the only thing that changes is whether __OPTIMIZE__ was
111 // defined... but if __OPTIMIZE__ never showed up in the header, it
112 // doesn't matter. We could consider making this some special kind
113 // of check.
114 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
115 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
116 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
117 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
118 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
119 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
120 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
121 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsoned4e2952009-11-05 20:14:16 +0000122 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +0000123 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000124 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump11289f42009-09-09 15:08:12 +0000125 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000126 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
127 return true;
128 }
129 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000130 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
131 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000132 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman9d905792009-06-25 22:57:40 +0000133 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Mike Stumpd9546382009-12-12 01:27:46 +0000134 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbar143021e2009-09-21 04:16:19 +0000135 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000136 PARSE_LANGOPT_BENIGN(SpellChecking);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +0000137#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000138#undef PARSE_LANGOPT_BENIGN
139
140 return false;
141}
142
Daniel Dunbar20a682d2009-11-11 00:52:11 +0000143bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
144 if (Triple == PP.getTargetInfo().getTriple().str())
145 return false;
146
147 Reader.Diag(diag::warn_pch_target_triple)
148 << Triple << PP.getTargetInfo().getTriple().str();
149 return true;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000150}
151
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000152struct EmptyStringRef {
Benjamin Kramer8d5609b2010-07-14 23:19:41 +0000153 bool operator ()(llvm::StringRef r) const { return r.empty(); }
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000154};
155struct EmptyBlock {
156 bool operator ()(const PCHPredefinesBlock &r) const { return r.Data.empty(); }
157};
158
159static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
160 PCHPredefinesBlocks R) {
161 // First, sum up the lengths.
162 unsigned LL = 0, RL = 0;
163 for (unsigned I = 0, N = L.size(); I != N; ++I) {
164 LL += L[I].size();
165 }
166 for (unsigned I = 0, N = R.size(); I != N; ++I) {
167 RL += R[I].Data.size();
168 }
169 if (LL != RL)
170 return false;
171 if (LL == 0 && RL == 0)
172 return true;
173
174 // Kick out empty parts, they confuse the algorithm below.
175 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
176 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
177
178 // Do it the hard way. At this point, both vectors must be non-empty.
179 llvm::StringRef LR = L[0], RR = R[0].Data;
180 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbar01ad0a72010-07-16 00:00:11 +0000181 (void) RN;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000182 for (;;) {
183 // Compare the current pieces.
184 if (LR.size() == RR.size()) {
185 // If they're the same length, it's pretty easy.
186 if (LR != RR)
187 return false;
188 // Both pieces are done, advance.
189 ++LI;
190 ++RI;
191 // If either string is done, they're both done, since they're the same
192 // length.
193 if (LI == LN) {
194 assert(RI == RN && "Strings not the same length after all?");
195 return true;
196 }
197 LR = L[LI];
198 RR = R[RI].Data;
199 } else if (LR.size() < RR.size()) {
200 // Right piece is longer.
201 if (!RR.startswith(LR))
202 return false;
203 ++LI;
204 assert(LI != LN && "Strings not the same length after all?");
205 RR = RR.substr(LR.size());
206 LR = L[LI];
207 } else {
208 // Left piece is longer.
209 if (!LR.startswith(RR))
210 return false;
211 ++RI;
212 assert(RI != RN && "Strings not the same length after all?");
213 LR = LR.substr(RR.size());
214 RR = R[RI].Data;
215 }
216 }
217}
218
219static std::pair<FileID, llvm::StringRef::size_type>
220FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
221 std::pair<FileID, llvm::StringRef::size_type> Res;
222 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
223 Res.second = Buffers[I].Data.find(MacroDef);
224 if (Res.second != llvm::StringRef::npos) {
225 Res.first = Buffers[I].BufferID;
226 break;
227 }
228 }
229 return Res;
230}
231
232bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000233 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000234 std::string &SuggestedPredefines) {
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000235 // We are in the context of an implicit include, so the predefines buffer will
236 // have a #include entry for the PCH file itself (as normalized by the
237 // preprocessor initialization). Find it and skip over it in the checking
238 // below.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000239 llvm::SmallString<256> PCHInclude;
240 PCHInclude += "#include \"";
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000241 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000242 PCHInclude += "\"\n";
243 std::pair<llvm::StringRef,llvm::StringRef> Split =
244 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
245 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000246 if (Left == PP.getPredefines()) {
247 Error("Missing PCH include entry!");
248 return true;
249 }
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000250
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000251 // If the concatenation of all the PCH buffers is equal to the adjusted
252 // command line, we're done.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000253 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
254 CommandLine.push_back(Left);
255 CommandLine.push_back(Right);
256 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000257 return false;
258
259 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000260
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000261 // The predefines buffers are different. Determine what the differences are,
262 // and whether they require us to reject the PCH file.
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000263 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000264 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
265 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000266
267 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
268 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000269
270 // Pick out implicit #includes after the PCH and don't consider them for
271 // validation; we will insert them into SuggestedPredefines so that the
272 // preprocessor includes them.
273 std::string IncludesAfterPCH;
274 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
275 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
276 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
277 if (AfterPCHLines[i].startswith("#include ")) {
278 IncludesAfterPCH += AfterPCHLines[i];
279 IncludesAfterPCH += '\n';
280 } else {
281 CmdLineLines.push_back(AfterPCHLines[i]);
282 }
283 }
284
285 // Make sure we add the includes last into SuggestedPredefines before we
286 // exit this function.
287 struct AddIncludesRAII {
288 std::string &SuggestedPredefines;
289 std::string &IncludesAfterPCH;
290
291 AddIncludesRAII(std::string &SuggestedPredefines,
292 std::string &IncludesAfterPCH)
293 : SuggestedPredefines(SuggestedPredefines),
294 IncludesAfterPCH(IncludesAfterPCH) { }
295 ~AddIncludesRAII() {
296 SuggestedPredefines += IncludesAfterPCH;
297 }
298 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000299
Daniel Dunbar499baed2009-11-11 05:26:28 +0000300 // Sort both sets of predefined buffer lines, since we allow some extra
301 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000302 std::sort(CmdLineLines.begin(), CmdLineLines.end());
303 std::sort(PCHLines.begin(), PCHLines.end());
304
Daniel Dunbar499baed2009-11-11 05:26:28 +0000305 // Determine which predefines that were used to build the PCH file are missing
306 // from the command line.
307 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000308 std::set_difference(PCHLines.begin(), PCHLines.end(),
309 CmdLineLines.begin(), CmdLineLines.end(),
310 std::back_inserter(MissingPredefines));
311
312 bool MissingDefines = false;
313 bool ConflictingDefines = false;
314 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000315 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000316 if (Missing.startswith("#include ")) {
317 // An -include was specified when generating the PCH; it is included in
318 // the PCH, just ignore it.
319 continue;
320 }
Daniel Dunbar499baed2009-11-11 05:26:28 +0000321 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000322 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
323 return true;
324 }
Mike Stump11289f42009-09-09 15:08:12 +0000325
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000326 // This is a macro definition. Determine the name of the macro we're
327 // defining.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000328 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000329 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000330 = Missing.find_first_of("( \n\r", StartOfMacroName);
331 assert(EndOfMacroName != std::string::npos &&
332 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000333 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000334
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000335 // Determine whether this macro was given a different definition on the
336 // command line.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000337 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000338 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000339 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000340 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
341 MacroDefStart);
342 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000343 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000344 // Different macro; we're done.
345 ConflictPos = CmdLineLines.end();
Mike Stump11289f42009-09-09 15:08:12 +0000346 break;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000347 }
Mike Stump11289f42009-09-09 15:08:12 +0000348
349 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000350 "Invalid #define in predefines buffer?");
Mike Stump11289f42009-09-09 15:08:12 +0000351 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000352 (*ConflictPos)[MacroDefLen] != '(')
353 continue; // Longer macro name; keep trying.
Mike Stump11289f42009-09-09 15:08:12 +0000354
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000355 // We found a conflicting macro definition.
356 break;
357 }
Mike Stump11289f42009-09-09 15:08:12 +0000358
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000359 if (ConflictPos != CmdLineLines.end()) {
360 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
361 << MacroName;
362
363 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000364 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
365 FindMacro(Buffers, Missing);
366 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
367 SourceLocation PCHMissingLoc =
368 SourceMgr.getLocForStartOfFile(MacroLoc.first)
369 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar499baed2009-11-11 05:26:28 +0000370 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000371
372 ConflictingDefines = true;
373 continue;
374 }
Mike Stump11289f42009-09-09 15:08:12 +0000375
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000376 // If the macro doesn't conflict, then we'll just pick up the macro
377 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000378 if (ConflictingDefines)
379 continue; // Don't complain if there are already conflicting defs
Mike Stump11289f42009-09-09 15:08:12 +0000380
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000381 if (!MissingDefines) {
382 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
383 MissingDefines = true;
384 }
385
386 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000387 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
388 FindMacro(Buffers, Missing);
389 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
390 SourceLocation PCHMissingLoc =
391 SourceMgr.getLocForStartOfFile(MacroLoc.first)
392 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000393 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
394 }
Mike Stump11289f42009-09-09 15:08:12 +0000395
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000396 if (ConflictingDefines)
397 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000398
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000399 // Determine what predefines were introduced based on command-line
400 // parameters that were not present when building the PCH
401 // file. Extra #defines are okay, so long as the identifiers being
402 // defined were not used within the precompiled header.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000403 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000404 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
405 PCHLines.begin(), PCHLines.end(),
Mike Stump11289f42009-09-09 15:08:12 +0000406 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000407 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000408 llvm::StringRef &Extra = ExtraPredefines[I];
409 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000410 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
411 return true;
412 }
413
414 // This is an extra macro definition. Determine the name of the
415 // macro we're defining.
416 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000417 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000418 = Extra.find_first_of("( \n\r", StartOfMacroName);
419 assert(EndOfMacroName != std::string::npos &&
420 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000421 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000422
423 // Check whether this name was used somewhere in the PCH file. If
424 // so, defining it as a macro could change behavior, so we reject
425 // the PCH file.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000426 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar045c92f2009-11-11 00:52:00 +0000427 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000428 return true;
429 }
430
431 // Add this definition to the suggested predefines buffer.
432 SuggestedPredefines += Extra;
433 SuggestedPredefines += '\n';
434 }
435
436 // If we get here, it's because the predefines buffer had compatible
437 // contents. Accept the PCH file.
438 return false;
439}
440
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000441void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
442 unsigned ID) {
443 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
444 ++NumHeaderInfos;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000445}
446
447void PCHValidator::ReadCounter(unsigned Value) {
448 PP.setCounterValue(Value);
449}
450
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000451//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +0000452// AST reader implementation
Douglas Gregora868bbd2009-04-21 22:25:48 +0000453//===----------------------------------------------------------------------===//
454
Sebastian Redl07a89a82010-07-30 00:29:29 +0000455void
Sebastian Redl3e31c722010-08-18 23:56:56 +0000456ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redl07a89a82010-07-30 00:29:29 +0000457 DeserializationListener = Listener;
Sebastian Redl07a89a82010-07-30 00:29:29 +0000458}
459
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000460
Douglas Gregora868bbd2009-04-21 22:25:48 +0000461namespace {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000462class ASTSelectorLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000463 ASTReader &Reader;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000464
465public:
Sebastian Redl834bb972010-08-04 17:20:04 +0000466 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +0000467 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +0000468 ObjCMethodList Instance, Factory;
469 };
Douglas Gregorc78d3462009-04-24 21:10:55 +0000470
471 typedef Selector external_key_type;
472 typedef external_key_type internal_key_type;
473
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000474 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump11289f42009-09-09 15:08:12 +0000475
Douglas Gregorc78d3462009-04-24 21:10:55 +0000476 static bool EqualKey(const internal_key_type& a,
477 const internal_key_type& b) {
478 return a == b;
479 }
Mike Stump11289f42009-09-09 15:08:12 +0000480
Douglas Gregorc78d3462009-04-24 21:10:55 +0000481 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +0000482 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000483 }
Mike Stump11289f42009-09-09 15:08:12 +0000484
Douglas Gregorc78d3462009-04-24 21:10:55 +0000485 // This hopefully will just get inlined and removed by the optimizer.
486 static const internal_key_type&
487 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000488
Douglas Gregorc78d3462009-04-24 21:10:55 +0000489 static std::pair<unsigned, unsigned>
490 ReadKeyDataLength(const unsigned char*& d) {
491 using namespace clang::io;
492 unsigned KeyLen = ReadUnalignedLE16(d);
493 unsigned DataLen = ReadUnalignedLE16(d);
494 return std::make_pair(KeyLen, DataLen);
495 }
Mike Stump11289f42009-09-09 15:08:12 +0000496
Douglas Gregor95c13f52009-04-25 17:48:32 +0000497 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000498 using namespace clang::io;
Chris Lattner8575daa2009-04-27 21:45:14 +0000499 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000500 unsigned N = ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000501 IdentifierInfo *FirstII
Douglas Gregorc78d3462009-04-24 21:10:55 +0000502 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
503 if (N == 0)
504 return SelTable.getNullarySelector(FirstII);
505 else if (N == 1)
506 return SelTable.getUnarySelector(FirstII);
507
508 llvm::SmallVector<IdentifierInfo *, 16> Args;
509 Args.push_back(FirstII);
510 for (unsigned I = 1; I != N; ++I)
511 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
512
Douglas Gregor038c3382009-05-22 22:45:36 +0000513 return SelTable.getSelector(N, Args.data());
Douglas Gregorc78d3462009-04-24 21:10:55 +0000514 }
Mike Stump11289f42009-09-09 15:08:12 +0000515
Douglas Gregorc78d3462009-04-24 21:10:55 +0000516 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
517 using namespace clang::io;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000518
519 data_type Result;
520
Sebastian Redl834bb972010-08-04 17:20:04 +0000521 Result.ID = ReadUnalignedLE32(d);
522 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
523 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
524
Douglas Gregorc78d3462009-04-24 21:10:55 +0000525 // Load instance methods
526 ObjCMethodList *Prev = 0;
527 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000528 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000529 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000530 if (!Result.Instance.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000531 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000532 Result.Instance.Method = Method;
533 Prev = &Result.Instance;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000534 continue;
535 }
536
Ted Kremenekda4abf12010-02-11 00:53:01 +0000537 ObjCMethodList *Mem =
538 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
539 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000540 Prev = Prev->Next;
541 }
542
543 // Load factory methods
544 Prev = 0;
545 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000546 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000547 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000548 if (!Result.Factory.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000549 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000550 Result.Factory.Method = Method;
551 Prev = &Result.Factory;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000552 continue;
553 }
554
Ted Kremenekda4abf12010-02-11 00:53:01 +0000555 ObjCMethodList *Mem =
556 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
557 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000558 Prev = Prev->Next;
559 }
560
561 return Result;
562 }
563};
Mike Stump11289f42009-09-09 15:08:12 +0000564
565} // end anonymous namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +0000566
567/// \brief The on-disk hash table used for the global method pool.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000568typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
569 ASTSelectorLookupTable;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000570
Sebastian Redl2c373b92010-10-05 15:59:54 +0000571namespace clang {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000572class ASTIdentifierLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000573 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +0000574 ASTReader::PerFileData &F;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000575
576 // If we know the IdentifierInfo in advance, it is here and we will
577 // not build a new one. Used when deserializing information about an
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000578 // identifier that was constructed before the AST file was read.
Douglas Gregora868bbd2009-04-21 22:25:48 +0000579 IdentifierInfo *KnownII;
580
581public:
582 typedef IdentifierInfo * data_type;
583
584 typedef const std::pair<const char*, unsigned> external_key_type;
585
586 typedef external_key_type internal_key_type;
587
Sebastian Redl2c373b92010-10-05 15:59:54 +0000588 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000589 IdentifierInfo *II = 0)
Sebastian Redl2c373b92010-10-05 15:59:54 +0000590 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump11289f42009-09-09 15:08:12 +0000591
Douglas Gregora868bbd2009-04-21 22:25:48 +0000592 static bool EqualKey(const internal_key_type& a,
593 const internal_key_type& b) {
594 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
595 : false;
596 }
Mike Stump11289f42009-09-09 15:08:12 +0000597
Douglas Gregora868bbd2009-04-21 22:25:48 +0000598 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000599 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregora868bbd2009-04-21 22:25:48 +0000600 }
Mike Stump11289f42009-09-09 15:08:12 +0000601
Douglas Gregora868bbd2009-04-21 22:25:48 +0000602 // This hopefully will just get inlined and removed by the optimizer.
603 static const internal_key_type&
604 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000605
Douglas Gregor57756ea2010-10-14 22:11:03 +0000606 // This hopefully will just get inlined and removed by the optimizer.
607 static const external_key_type&
608 GetExternalKey(const internal_key_type& x) { return x; }
609
Douglas Gregora868bbd2009-04-21 22:25:48 +0000610 static std::pair<unsigned, unsigned>
611 ReadKeyDataLength(const unsigned char*& d) {
612 using namespace clang::io;
Douglas Gregor6b7bf5a2009-04-25 20:26:24 +0000613 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor5287b4e2009-04-25 21:04:17 +0000614 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000615 return std::make_pair(KeyLen, DataLen);
616 }
Mike Stump11289f42009-09-09 15:08:12 +0000617
Douglas Gregora868bbd2009-04-21 22:25:48 +0000618 static std::pair<const char*, unsigned>
619 ReadKey(const unsigned char* d, unsigned n) {
620 assert(n >= 2 && d[n-1] == '\0');
621 return std::make_pair((const char*) d, n-1);
622 }
Mike Stump11289f42009-09-09 15:08:12 +0000623
624 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregora868bbd2009-04-21 22:25:48 +0000625 const unsigned char* d,
626 unsigned DataLen) {
627 using namespace clang::io;
Sebastian Redl539c5062010-08-18 23:57:32 +0000628 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000629 bool IsInteresting = ID & 0x01;
630
631 // Wipe out the "is interesting" bit.
632 ID = ID >> 1;
633
634 if (!IsInteresting) {
Sebastian Redl98912122010-07-27 23:01:28 +0000635 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregor1d583f22009-04-28 21:18:29 +0000636 // and associate it with the persistent ID.
637 IdentifierInfo *II = KnownII;
638 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000639 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000640 Reader.SetIdentifierInfo(ID, II);
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000641 II->setIsFromAST();
Douglas Gregor1d583f22009-04-28 21:18:29 +0000642 return II;
643 }
644
Douglas Gregorb9256522009-04-28 21:32:13 +0000645 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000646 bool CPlusPlusOperatorKeyword = Bits & 0x01;
647 Bits >>= 1;
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000648 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
649 Bits >>= 1;
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000650 bool Poisoned = Bits & 0x01;
651 Bits >>= 1;
652 bool ExtensionToken = Bits & 0x01;
653 Bits >>= 1;
654 bool hasMacroDefinition = Bits & 0x01;
655 Bits >>= 1;
656 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
657 Bits >>= 10;
Mike Stump11289f42009-09-09 15:08:12 +0000658
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000659 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorb9256522009-04-28 21:32:13 +0000660 DataLen -= 6;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000661
662 // Build the IdentifierInfo itself and link the identifier ID with
663 // the new IdentifierInfo.
664 IdentifierInfo *II = KnownII;
665 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000666 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000667 Reader.SetIdentifierInfo(ID, II);
668
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000669 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000670 // Token IDs are read-only.
671 if (HasRevertedTokenIDToIdentifier)
672 II->RevertTokenIDToIdentifier();
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000673 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump11289f42009-09-09 15:08:12 +0000674 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000675 "Incorrect extension token flag");
676 (void)ExtensionToken;
677 II->setIsPoisoned(Poisoned);
678 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
679 "Incorrect C++ operator keyword flag");
680 (void)CPlusPlusOperatorKeyword;
681
Douglas Gregorc3366a52009-04-21 23:56:24 +0000682 // If this identifier is a macro, deserialize the macro
683 // definition.
684 if (hasMacroDefinition) {
Douglas Gregorb9256522009-04-28 21:32:13 +0000685 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor5ef9e332010-10-30 00:23:06 +0000686 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregorb9256522009-04-28 21:32:13 +0000687 DataLen -= 4;
Douglas Gregorc3366a52009-04-21 23:56:24 +0000688 }
Douglas Gregora868bbd2009-04-21 22:25:48 +0000689
690 // Read all of the declarations visible at global scope with this
691 // name.
Chris Lattner1d728882009-04-27 22:17:41 +0000692 if (Reader.getContext() == 0) return II;
Douglas Gregor1342e842009-07-06 18:54:52 +0000693 if (DataLen > 0) {
694 llvm::SmallVector<uint32_t, 4> DeclIDs;
695 for (; DataLen > 0; DataLen -= 4)
696 DeclIDs.push_back(ReadUnalignedLE32(d));
697 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000698 }
Mike Stump11289f42009-09-09 15:08:12 +0000699
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000700 II->setIsFromAST();
Douglas Gregora868bbd2009-04-21 22:25:48 +0000701 return II;
702 }
703};
Mike Stump11289f42009-09-09 15:08:12 +0000704
705} // end anonymous namespace
Douglas Gregora868bbd2009-04-21 22:25:48 +0000706
707/// \brief The on-disk hash table used to contain information about
708/// all of the identifiers in the program.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000709typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
710 ASTIdentifierLookupTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000711
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000712namespace {
713class ASTDeclContextNameLookupTrait {
714 ASTReader &Reader;
715
716public:
717 /// \brief Pair of begin/end iterators for DeclIDs.
718 typedef std::pair<DeclID *, DeclID *> data_type;
719
720 /// \brief Special internal key for declaration names.
721 /// The hash table creates keys for comparison; we do not create
722 /// a DeclarationName for the internal key to avoid deserializing types.
723 struct DeclNameKey {
724 DeclarationName::NameKind Kind;
725 uint64_t Data;
726 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
727 };
728
729 typedef DeclarationName external_key_type;
730 typedef DeclNameKey internal_key_type;
731
732 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
733
734 static bool EqualKey(const internal_key_type& a,
735 const internal_key_type& b) {
736 return a.Kind == b.Kind && a.Data == b.Data;
737 }
738
739 unsigned ComputeHash(const DeclNameKey &Key) const {
740 llvm::FoldingSetNodeID ID;
741 ID.AddInteger(Key.Kind);
742
743 switch (Key.Kind) {
744 case DeclarationName::Identifier:
745 case DeclarationName::CXXLiteralOperatorName:
746 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
747 break;
748 case DeclarationName::ObjCZeroArgSelector:
749 case DeclarationName::ObjCOneArgSelector:
750 case DeclarationName::ObjCMultiArgSelector:
751 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
752 break;
753 case DeclarationName::CXXConstructorName:
754 case DeclarationName::CXXDestructorName:
755 case DeclarationName::CXXConversionFunctionName:
756 ID.AddInteger((TypeID)Key.Data);
757 break;
758 case DeclarationName::CXXOperatorName:
759 ID.AddInteger((OverloadedOperatorKind)Key.Data);
760 break;
761 case DeclarationName::CXXUsingDirective:
762 break;
763 }
764
765 return ID.ComputeHash();
766 }
767
768 internal_key_type GetInternalKey(const external_key_type& Name) const {
769 DeclNameKey Key;
770 Key.Kind = Name.getNameKind();
771 switch (Name.getNameKind()) {
772 case DeclarationName::Identifier:
773 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
774 break;
775 case DeclarationName::ObjCZeroArgSelector:
776 case DeclarationName::ObjCOneArgSelector:
777 case DeclarationName::ObjCMultiArgSelector:
778 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
779 break;
780 case DeclarationName::CXXConstructorName:
781 case DeclarationName::CXXDestructorName:
782 case DeclarationName::CXXConversionFunctionName:
783 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
784 break;
785 case DeclarationName::CXXOperatorName:
786 Key.Data = Name.getCXXOverloadedOperator();
787 break;
788 case DeclarationName::CXXLiteralOperatorName:
789 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
790 break;
791 case DeclarationName::CXXUsingDirective:
792 break;
793 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000794
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000795 return Key;
796 }
797
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000798 external_key_type GetExternalKey(const internal_key_type& Key) const {
799 ASTContext *Context = Reader.getContext();
800 switch (Key.Kind) {
801 case DeclarationName::Identifier:
802 return DeclarationName((IdentifierInfo*)Key.Data);
803
804 case DeclarationName::ObjCZeroArgSelector:
805 case DeclarationName::ObjCOneArgSelector:
806 case DeclarationName::ObjCMultiArgSelector:
807 return DeclarationName(Selector(Key.Data));
808
809 case DeclarationName::CXXConstructorName:
810 return Context->DeclarationNames.getCXXConstructorName(
811 Context->getCanonicalType(Reader.GetType(Key.Data)));
812
813 case DeclarationName::CXXDestructorName:
814 return Context->DeclarationNames.getCXXDestructorName(
815 Context->getCanonicalType(Reader.GetType(Key.Data)));
816
817 case DeclarationName::CXXConversionFunctionName:
818 return Context->DeclarationNames.getCXXConversionFunctionName(
819 Context->getCanonicalType(Reader.GetType(Key.Data)));
820
821 case DeclarationName::CXXOperatorName:
822 return Context->DeclarationNames.getCXXOperatorName(
823 (OverloadedOperatorKind)Key.Data);
824
825 case DeclarationName::CXXLiteralOperatorName:
826 return Context->DeclarationNames.getCXXLiteralOperatorName(
827 (IdentifierInfo*)Key.Data);
828
829 case DeclarationName::CXXUsingDirective:
830 return DeclarationName::getUsingDirectiveName();
831 }
832
833 llvm_unreachable("Invalid Name Kind ?");
834 }
835
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000836 static std::pair<unsigned, unsigned>
837 ReadKeyDataLength(const unsigned char*& d) {
838 using namespace clang::io;
839 unsigned KeyLen = ReadUnalignedLE16(d);
840 unsigned DataLen = ReadUnalignedLE16(d);
841 return std::make_pair(KeyLen, DataLen);
842 }
843
844 internal_key_type ReadKey(const unsigned char* d, unsigned) {
845 using namespace clang::io;
846
847 DeclNameKey Key;
848 Key.Kind = (DeclarationName::NameKind)*d++;
849 switch (Key.Kind) {
850 case DeclarationName::Identifier:
851 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
852 break;
853 case DeclarationName::ObjCZeroArgSelector:
854 case DeclarationName::ObjCOneArgSelector:
855 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000856 Key.Data =
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000857 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
858 break;
859 case DeclarationName::CXXConstructorName:
860 case DeclarationName::CXXDestructorName:
861 case DeclarationName::CXXConversionFunctionName:
862 Key.Data = ReadUnalignedLE32(d); // TypeID
863 break;
864 case DeclarationName::CXXOperatorName:
865 Key.Data = *d++; // OverloadedOperatorKind
866 break;
867 case DeclarationName::CXXLiteralOperatorName:
868 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
869 break;
870 case DeclarationName::CXXUsingDirective:
871 break;
872 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000873
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000874 return Key;
875 }
876
877 data_type ReadData(internal_key_type, const unsigned char* d,
878 unsigned DataLen) {
879 using namespace clang::io;
880 unsigned NumDecls = ReadUnalignedLE16(d);
881 DeclID *Start = (DeclID *)d;
882 return std::make_pair(Start, Start + NumDecls);
883 }
884};
885
886} // end anonymous namespace
887
888/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
889typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
890 ASTDeclContextNameLookupTable;
891
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000892bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
893 const std::pair<uint64_t, uint64_t> &Offsets,
894 DeclContextInfo &Info) {
895 SavedStreamPosition SavedPosition(Cursor);
896 // First the lexical decls.
897 if (Offsets.first != 0) {
898 Cursor.JumpToBit(Offsets.first);
899
900 RecordData Record;
901 const char *Blob;
902 unsigned BlobLen;
903 unsigned Code = Cursor.ReadCode();
904 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
905 if (RecCode != DECL_CONTEXT_LEXICAL) {
906 Error("Expected lexical block");
907 return true;
908 }
909
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000910 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
911 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000912 } else {
913 Info.LexicalDecls = 0;
914 Info.NumLexicalDecls = 0;
915 }
916
917 // Now the lookup table.
918 if (Offsets.second != 0) {
919 Cursor.JumpToBit(Offsets.second);
920
921 RecordData Record;
922 const char *Blob;
923 unsigned BlobLen;
924 unsigned Code = Cursor.ReadCode();
925 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
926 if (RecCode != DECL_CONTEXT_VISIBLE) {
927 Error("Expected visible lookup table block");
928 return true;
929 }
930 Info.NameLookupTableData
931 = ASTDeclContextNameLookupTable::Create(
932 (const unsigned char *)Blob + Record[0],
933 (const unsigned char *)Blob,
934 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl9d8f58b2010-08-24 00:50:00 +0000935 } else {
936 Info.NameLookupTableData = 0;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000937 }
938
939 return false;
940}
941
Sebastian Redl2c499f62010-08-18 23:56:43 +0000942void ASTReader::Error(const char *Msg) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000943 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000944}
945
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000946/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000947bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000948 if (Listener)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000949 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000950 ActualOriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000951 SuggestedPredefines);
Douglas Gregorc379c072009-04-28 18:58:38 +0000952 return false;
Douglas Gregor92863e42009-04-10 23:10:45 +0000953}
954
Douglas Gregorc5046832009-04-27 18:38:38 +0000955//===----------------------------------------------------------------------===//
956// Source Manager Deserialization
957//===----------------------------------------------------------------------===//
958
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000959/// \brief Read the line table in the source manager block.
Sebastian Redl2c373b92010-10-05 15:59:54 +0000960/// \returns true if there was an error.
961bool ASTReader::ParseLineTable(PerFileData &F,
962 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000963 unsigned Idx = 0;
964 LineTableInfo &LineTable = SourceMgr.getLineTable();
965
966 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +0000967 std::map<int, int> FileIDs;
968 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000969 // Extract the file name
970 unsigned FilenameLen = Record[Idx++];
971 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
972 Idx += FilenameLen;
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000973 MaybeAddSystemRootToFilename(Filename);
Mike Stump11289f42009-09-09 15:08:12 +0000974 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregora8854652009-04-13 17:12:42 +0000975 Filename.size());
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000976 }
977
978 // Parse the line entries
979 std::vector<LineEntry> Entries;
980 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000981 int FID = Record[Idx++];
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000982
983 // Extract the line entries
984 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000985 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000986 Entries.clear();
987 Entries.reserve(NumEntries);
988 for (unsigned I = 0; I != NumEntries; ++I) {
989 unsigned FileOffset = Record[Idx++];
990 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000991 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +0000992 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000993 = (SrcMgr::CharacteristicKind)Record[Idx++];
994 unsigned IncludeOffset = Record[Idx++];
995 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
996 FileKind, IncludeOffset));
997 }
998 LineTable.AddEntry(FID, Entries);
999 }
1000
1001 return false;
1002}
1003
Douglas Gregorc5046832009-04-27 18:38:38 +00001004namespace {
1005
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001006class ASTStatData {
Douglas Gregorc5046832009-04-27 18:38:38 +00001007public:
1008 const bool hasStat;
1009 const ino_t ino;
1010 const dev_t dev;
1011 const mode_t mode;
1012 const time_t mtime;
1013 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +00001014
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001015 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump11289f42009-09-09 15:08:12 +00001016 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
1017
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001018 ASTStatData()
Douglas Gregorc5046832009-04-27 18:38:38 +00001019 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
1020};
1021
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001022class ASTStatLookupTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001023 public:
1024 typedef const char *external_key_type;
1025 typedef const char *internal_key_type;
1026
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001027 typedef ASTStatData data_type;
Douglas Gregorc5046832009-04-27 18:38:38 +00001028
1029 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001030 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001031 }
1032
1033 static internal_key_type GetInternalKey(const char *path) { return path; }
1034
1035 static bool EqualKey(internal_key_type a, internal_key_type b) {
1036 return strcmp(a, b) == 0;
1037 }
1038
1039 static std::pair<unsigned, unsigned>
1040 ReadKeyDataLength(const unsigned char*& d) {
1041 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1042 unsigned DataLen = (unsigned) *d++;
1043 return std::make_pair(KeyLen + 1, DataLen);
1044 }
1045
1046 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1047 return (const char *)d;
1048 }
1049
1050 static data_type ReadData(const internal_key_type, const unsigned char *d,
1051 unsigned /*DataLen*/) {
1052 using namespace clang::io;
1053
1054 if (*d++ == 1)
1055 return data_type();
1056
1057 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1058 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1059 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +00001060 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +00001061 off_t size = (off_t) ReadUnalignedLE64(d);
1062 return data_type(ino, dev, mode, mtime, size);
1063 }
1064};
1065
1066/// \brief stat() cache for precompiled headers.
1067///
1068/// This cache is very similar to the stat cache used by pretokenized
1069/// headers.
Chris Lattner226efd32010-11-23 19:19:34 +00001070class ASTStatCache : public FileSystemStatCache {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001071 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregorc5046832009-04-27 18:38:38 +00001072 CacheTy *Cache;
1073
1074 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +00001075public:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001076 ASTStatCache(const unsigned char *Buckets,
Douglas Gregorc5046832009-04-27 18:38:38 +00001077 const unsigned char *Base,
1078 unsigned &NumStatHits,
Mike Stump11289f42009-09-09 15:08:12 +00001079 unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +00001080 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1081 Cache = CacheTy::Create(Buckets, Base);
1082 }
1083
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001084 ~ASTStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +00001085
Chris Lattner226efd32010-11-23 19:19:34 +00001086 LookupResult getStat(const char *Path, struct stat &StatBuf) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001087 // Do the lookup for the file's data in the AST file.
Chris Lattner226efd32010-11-23 19:19:34 +00001088 CacheTy::iterator I = Cache->find(Path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001089
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001090 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregorc5046832009-04-27 18:38:38 +00001091 if (I == Cache->end()) {
1092 ++NumStatMisses;
Chris Lattner226efd32010-11-23 19:19:34 +00001093 return statChained(Path, StatBuf);
Douglas Gregorc5046832009-04-27 18:38:38 +00001094 }
Mike Stump11289f42009-09-09 15:08:12 +00001095
Douglas Gregorc5046832009-04-27 18:38:38 +00001096 ++NumStatHits;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001097 ASTStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001098
Douglas Gregorc5046832009-04-27 18:38:38 +00001099 if (!Data.hasStat)
Chris Lattner226efd32010-11-23 19:19:34 +00001100 return CacheHitMissing;
Douglas Gregorc5046832009-04-27 18:38:38 +00001101
Chris Lattner226efd32010-11-23 19:19:34 +00001102 StatBuf.st_ino = Data.ino;
1103 StatBuf.st_dev = Data.dev;
1104 StatBuf.st_mtime = Data.mtime;
1105 StatBuf.st_mode = Data.mode;
1106 StatBuf.st_size = Data.size;
1107 return CacheHitExists;
Douglas Gregorc5046832009-04-27 18:38:38 +00001108 }
1109};
1110} // end anonymous namespace
1111
1112
Sebastian Redl393f8b72010-07-19 20:52:06 +00001113/// \brief Read a source manager block
Sebastian Redl2c499f62010-08-18 23:56:43 +00001114ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001115 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +00001116
Sebastian Redl393f8b72010-07-19 20:52:06 +00001117 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001118
Douglas Gregor258ae542009-04-27 06:38:32 +00001119 // Set the source-location entry cursor to the current position in
1120 // the stream. This cursor will be used to read the contents of the
1121 // source manager block initially, and then lazily read
1122 // source-location entries as needed.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001123 SLocEntryCursor = F.Stream;
Douglas Gregor258ae542009-04-27 06:38:32 +00001124
1125 // The stream itself is going to skip over the source manager block.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001126 if (F.Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001127 Error("malformed block record in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001128 return Failure;
1129 }
1130
1131 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001132 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001133 Error("malformed source manager block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001134 return Failure;
1135 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001136
Douglas Gregora7f71a92009-04-10 03:52:48 +00001137 RecordData Record;
1138 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001139 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001140 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001141 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001142 Error("error at end of Source Manager block in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001143 return Failure;
1144 }
Douglas Gregor92863e42009-04-10 23:10:45 +00001145 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001146 }
Mike Stump11289f42009-09-09 15:08:12 +00001147
Douglas Gregora7f71a92009-04-10 03:52:48 +00001148 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1149 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +00001150 SLocEntryCursor.ReadSubBlockID();
1151 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001152 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001153 return Failure;
1154 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001155 continue;
1156 }
Mike Stump11289f42009-09-09 15:08:12 +00001157
Douglas Gregora7f71a92009-04-10 03:52:48 +00001158 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001159 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001160 continue;
1161 }
Mike Stump11289f42009-09-09 15:08:12 +00001162
Douglas Gregora7f71a92009-04-10 03:52:48 +00001163 // Read a record.
1164 const char *BlobStart;
1165 unsigned BlobLen;
1166 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +00001167 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001168 default: // Default behavior: ignore.
1169 break;
1170
Sebastian Redl539c5062010-08-18 23:57:32 +00001171 case SM_LINE_TABLE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00001172 if (ParseLineTable(F, Record))
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001173 return Failure;
Chris Lattner184e65d2009-04-14 23:22:57 +00001174 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00001175
Sebastian Redl539c5062010-08-18 23:57:32 +00001176 case SM_SLOC_FILE_ENTRY:
1177 case SM_SLOC_BUFFER_ENTRY:
1178 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor258ae542009-04-27 06:38:32 +00001179 // Once we hit one of the source location entries, we're done.
1180 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001181 }
1182 }
1183}
1184
Sebastian Redl06750302010-07-20 21:50:20 +00001185/// \brief Get a cursor that's correctly positioned for reading the source
1186/// location entry with the given ID.
Sebastian Redl2c373b92010-10-05 15:59:54 +00001187ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl06750302010-07-20 21:50:20 +00001188 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1189 "SLocCursorForID should only be called for real IDs.");
1190
1191 ID -= 1;
1192 PerFileData *F = 0;
1193 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1194 F = Chain[N - I - 1];
1195 if (ID < F->LocalNumSLocEntries)
1196 break;
1197 ID -= F->LocalNumSLocEntries;
1198 }
1199 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1200
1201 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00001202 return F;
Sebastian Redl06750302010-07-20 21:50:20 +00001203}
1204
Douglas Gregor258ae542009-04-27 06:38:32 +00001205/// \brief Read in the source location entry with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001206ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001207 if (ID == 0)
1208 return Success;
1209
1210 if (ID > TotalNumSLocEntries) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001211 Error("source location entry ID out-of-range for AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001212 return Failure;
1213 }
1214
Sebastian Redl2c373b92010-10-05 15:59:54 +00001215 PerFileData *F = SLocCursorForID(ID);
1216 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001217
Douglas Gregor258ae542009-04-27 06:38:32 +00001218 ++NumSLocEntriesRead;
Douglas Gregor258ae542009-04-27 06:38:32 +00001219 unsigned Code = SLocEntryCursor.ReadCode();
1220 if (Code == llvm::bitc::END_BLOCK ||
1221 Code == llvm::bitc::ENTER_SUBBLOCK ||
1222 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001223 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001224 return Failure;
1225 }
1226
Douglas Gregor258ae542009-04-27 06:38:32 +00001227 RecordData Record;
1228 const char *BlobStart;
1229 unsigned BlobLen;
1230 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1231 default:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001232 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001233 return Failure;
1234
Sebastian Redl539c5062010-08-18 23:57:32 +00001235 case SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001236 std::string Filename(BlobStart, BlobStart + BlobLen);
1237 MaybeAddSystemRootToFilename(Filename);
Chris Lattner5159f612010-11-23 08:35:12 +00001238 const FileEntry *File = FileMgr.getFile(Filename);
Chris Lattnerd20dc872009-06-15 04:35:16 +00001239 if (File == 0) {
1240 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001241 ErrorStr += Filename;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001242 ErrorStr += "' referenced by AST file";
Chris Lattnerd20dc872009-06-15 04:35:16 +00001243 Error(ErrorStr.c_str());
1244 return Failure;
1245 }
Mike Stump11289f42009-09-09 15:08:12 +00001246
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001247 if (Record.size() < 10) {
Ted Kremenekabb1ddd2010-03-18 21:23:05 +00001248 Error("source location entry is incorrect");
1249 return Failure;
1250 }
1251
Douglas Gregorce3a8292010-07-27 00:27:13 +00001252 if (!DisableValidation &&
1253 ((off_t)Record[4] != File->getSize()
Douglas Gregor08288f22010-04-09 15:54:22 +00001254#if !defined(LLVM_ON_WIN32)
1255 // In our regression testing, the Windows file system seems to
1256 // have inconsistent modification times that sometimes
1257 // erroneously trigger this error-handling path.
Douglas Gregorce3a8292010-07-27 00:27:13 +00001258 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor08288f22010-04-09 15:54:22 +00001259#endif
Douglas Gregorce3a8292010-07-27 00:27:13 +00001260 )) {
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001261 Diag(diag::err_fe_pch_file_modified)
1262 << Filename;
1263 return Failure;
1264 }
1265
Chris Lattner26b5c192010-11-23 09:19:42 +00001266 FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]),
1267 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor258ae542009-04-27 06:38:32 +00001268 ID, Record[0]);
1269 if (Record[3])
1270 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1271 .setHasLineDirectives();
1272
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001273 // Reconstruct header-search information for this file.
1274 HeaderFileInfo HFI;
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001275 HFI.isImport = Record[6];
1276 HFI.DirInfo = Record[7];
1277 HFI.NumIncludes = Record[8];
1278 HFI.ControllingMacroID = Record[9];
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001279 if (Listener)
1280 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor258ae542009-04-27 06:38:32 +00001281 break;
1282 }
1283
Sebastian Redl539c5062010-08-18 23:57:32 +00001284 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor258ae542009-04-27 06:38:32 +00001285 const char *Name = BlobStart;
1286 unsigned Offset = Record[0];
1287 unsigned Code = SLocEntryCursor.ReadCode();
1288 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001289 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +00001290 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001291
Sebastian Redl539c5062010-08-18 23:57:32 +00001292 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001293 Error("AST record has invalid code");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001294 return Failure;
1295 }
1296
Douglas Gregor258ae542009-04-27 06:38:32 +00001297 llvm::MemoryBuffer *Buffer
Chris Lattner58c79342010-04-05 22:42:27 +00001298 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1299 Name);
Douglas Gregor258ae542009-04-27 06:38:32 +00001300 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump11289f42009-09-09 15:08:12 +00001301
Douglas Gregore6648fb2009-04-28 20:33:11 +00001302 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001303 PCHPredefinesBlock Block = {
1304 BufferID,
1305 llvm::StringRef(BlobStart, BlobLen - 1)
1306 };
1307 PCHPredefinesBuffers.push_back(Block);
Douglas Gregore6648fb2009-04-28 20:33:11 +00001308 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001309
1310 break;
1311 }
1312
Sebastian Redl539c5062010-08-18 23:57:32 +00001313 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001314 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001315 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001316 ReadSourceLocation(*F, Record[2]),
1317 ReadSourceLocation(*F, Record[3]),
Douglas Gregor258ae542009-04-27 06:38:32 +00001318 Record[4],
1319 ID,
1320 Record[0]);
1321 break;
Mike Stump11289f42009-09-09 15:08:12 +00001322 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001323 }
1324
1325 return Success;
1326}
1327
Chris Lattnere78a6be2009-04-27 01:05:14 +00001328/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1329/// specified cursor. Read the abbreviations that are at the top of the block
1330/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001331bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001332 unsigned BlockID) {
1333 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001334 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001335 return Failure;
1336 }
Mike Stump11289f42009-09-09 15:08:12 +00001337
Chris Lattnere78a6be2009-04-27 01:05:14 +00001338 while (true) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001339 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattnere78a6be2009-04-27 01:05:14 +00001340 unsigned Code = Cursor.ReadCode();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001341
Chris Lattnere78a6be2009-04-27 01:05:14 +00001342 // We expect all abbrevs to be at the start of the block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001343 if (Code != llvm::bitc::DEFINE_ABBREV) {
1344 Cursor.JumpToBit(Offset);
Chris Lattnere78a6be2009-04-27 01:05:14 +00001345 return false;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001346 }
Chris Lattnere78a6be2009-04-27 01:05:14 +00001347 Cursor.ReadAbbrevRecord();
1348 }
1349}
1350
Sebastian Redl2c373b92010-10-05 15:59:54 +00001351void ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001352 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregor796d76a2010-10-20 22:00:55 +00001353 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump11289f42009-09-09 15:08:12 +00001354
Douglas Gregorc3366a52009-04-21 23:56:24 +00001355 // Keep track of where we are in the stream, then jump back there
1356 // after reading this macro.
1357 SavedStreamPosition SavedPosition(Stream);
1358
1359 Stream.JumpToBit(Offset);
1360 RecordData Record;
1361 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1362 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001363
Douglas Gregorc3366a52009-04-21 23:56:24 +00001364 while (true) {
1365 unsigned Code = Stream.ReadCode();
1366 switch (Code) {
1367 case llvm::bitc::END_BLOCK:
1368 return;
1369
1370 case llvm::bitc::ENTER_SUBBLOCK:
1371 // No known subblocks, always skip them.
1372 Stream.ReadSubBlockID();
1373 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001374 Error("malformed block record in AST file");
Douglas Gregorc3366a52009-04-21 23:56:24 +00001375 return;
1376 }
1377 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001378
Douglas Gregorc3366a52009-04-21 23:56:24 +00001379 case llvm::bitc::DEFINE_ABBREV:
1380 Stream.ReadAbbrevRecord();
1381 continue;
1382 default: break;
1383 }
1384
1385 // Read a record.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001386 const char *BlobStart = 0;
1387 unsigned BlobLen = 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001388 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001389 PreprocessorRecordTypes RecType =
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001390 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001391 BlobLen);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001392 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001393 case PP_MACRO_OBJECT_LIKE:
1394 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001395 // If we already have a macro, that means that we've hit the end
1396 // of the definition of the macro we were looking for. We're
1397 // done.
1398 if (Macro)
1399 return;
1400
1401 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1402 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001403 Error("macro must have a name in AST file");
Douglas Gregorc3366a52009-04-21 23:56:24 +00001404 return;
1405 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00001406 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001407 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001408
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001409 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001410 MI->setIsUsed(isUsed);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001411 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001412
Douglas Gregoraae92242010-03-19 21:51:54 +00001413 unsigned NextIndex = 3;
Sebastian Redl539c5062010-08-18 23:57:32 +00001414 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001415 // Decode function-like macro info.
1416 bool isC99VarArgs = Record[3];
1417 bool isGNUVarArgs = Record[4];
1418 MacroArgs.clear();
1419 unsigned NumArgs = Record[5];
Douglas Gregoraae92242010-03-19 21:51:54 +00001420 NextIndex = 6 + NumArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001421 for (unsigned i = 0; i != NumArgs; ++i)
1422 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1423
1424 // Install function-like macro info.
1425 MI->setIsFunctionLike();
1426 if (isC99VarArgs) MI->setIsC99Varargs();
1427 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001428 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001429 PP->getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001430 }
1431
1432 // Finally, install the macro.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001433 PP->setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001434
1435 // Remember that we saw this macro last so that we add the tokens that
1436 // form its body to it.
1437 Macro = MI;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001438
Douglas Gregoraae92242010-03-19 21:51:54 +00001439 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1440 // We have a macro definition. Load it now.
1441 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1442 getMacroDefinition(Record[NextIndex]));
1443 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001444
Douglas Gregorc3366a52009-04-21 23:56:24 +00001445 ++NumMacrosRead;
1446 break;
1447 }
Mike Stump11289f42009-09-09 15:08:12 +00001448
Sebastian Redl539c5062010-08-18 23:57:32 +00001449 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001450 // If we see a TOKEN before a PP_MACRO_*, then the file is
1451 // erroneous, just pretend we didn't see this.
1452 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001453
Douglas Gregorc3366a52009-04-21 23:56:24 +00001454 Token Tok;
1455 Tok.startToken();
Sebastian Redl2c373b92010-10-05 15:59:54 +00001456 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001457 Tok.setLength(Record[1]);
1458 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1459 Tok.setIdentifierInfo(II);
1460 Tok.setKind((tok::TokenKind)Record[3]);
1461 Tok.setFlag((Token::TokenFlags)Record[4]);
1462 Macro->AddTokenToBody(Tok);
1463 break;
1464 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001465
Sebastian Redl539c5062010-08-18 23:57:32 +00001466 case PP_MACRO_INSTANTIATION: {
Douglas Gregoraae92242010-03-19 21:51:54 +00001467 // If we already have a macro, that means that we've hit the end
1468 // of the definition of the macro we were looking for. We're
1469 // done.
1470 if (Macro)
1471 return;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001472
Douglas Gregoraae92242010-03-19 21:51:54 +00001473 if (!PP->getPreprocessingRecord()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001474 Error("missing preprocessing record in AST file");
Douglas Gregoraae92242010-03-19 21:51:54 +00001475 return;
1476 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001477
Douglas Gregoraae92242010-03-19 21:51:54 +00001478 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1479 if (PPRec.getPreprocessedEntity(Record[0]))
1480 return;
1481
1482 MacroInstantiation *MI
1483 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
Sebastian Redl2c373b92010-10-05 15:59:54 +00001484 SourceRange(ReadSourceLocation(F, Record[1]),
1485 ReadSourceLocation(F, Record[2])),
Douglas Gregoraae92242010-03-19 21:51:54 +00001486 getMacroDefinition(Record[4]));
1487 PPRec.SetPreallocatedEntity(Record[0], MI);
1488 return;
1489 }
1490
Sebastian Redl539c5062010-08-18 23:57:32 +00001491 case PP_MACRO_DEFINITION: {
Douglas Gregoraae92242010-03-19 21:51:54 +00001492 // If we already have a macro, that means that we've hit the end
1493 // of the definition of the macro we were looking for. We're
1494 // done.
1495 if (Macro)
1496 return;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001497
Douglas Gregoraae92242010-03-19 21:51:54 +00001498 if (!PP->getPreprocessingRecord()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001499 Error("missing preprocessing record in AST file");
Douglas Gregoraae92242010-03-19 21:51:54 +00001500 return;
1501 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001502
Douglas Gregoraae92242010-03-19 21:51:54 +00001503 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1504 if (PPRec.getPreprocessedEntity(Record[0]))
1505 return;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001506
Douglas Gregor91096292010-10-02 19:29:26 +00001507 if (Record[1] > MacroDefinitionsLoaded.size()) {
Douglas Gregoraae92242010-03-19 21:51:54 +00001508 Error("out-of-bounds macro definition record");
1509 return;
1510 }
1511
Douglas Gregor91096292010-10-02 19:29:26 +00001512 // Decode the identifier info and then check again; if the macro is
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001513 // still defined and associated with the identifier,
Douglas Gregor91096292010-10-02 19:29:26 +00001514 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1515 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1516 MacroDefinition *MD
1517 = new (PPRec) MacroDefinition(II,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001518 ReadSourceLocation(F, Record[5]),
Douglas Gregor36ea4d42010-10-01 20:33:34 +00001519 SourceRange(
Sebastian Redl2c373b92010-10-05 15:59:54 +00001520 ReadSourceLocation(F, Record[2]),
1521 ReadSourceLocation(F, Record[3])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001522
Douglas Gregor91096292010-10-02 19:29:26 +00001523 PPRec.SetPreallocatedEntity(Record[0], MD);
1524 MacroDefinitionsLoaded[Record[1] - 1] = MD;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001525
Douglas Gregor91096292010-10-02 19:29:26 +00001526 if (DeserializationListener)
1527 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1528 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001529
Douglas Gregoraae92242010-03-19 21:51:54 +00001530 return;
1531 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001532
Douglas Gregor796d76a2010-10-20 22:00:55 +00001533 case PP_INCLUSION_DIRECTIVE: {
1534 // If we already have a macro, that means that we've hit the end
1535 // of the definition of the macro we were looking for. We're
1536 // done.
1537 if (Macro)
1538 return;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001539
Douglas Gregor796d76a2010-10-20 22:00:55 +00001540 if (!PP->getPreprocessingRecord()) {
1541 Error("missing preprocessing record in AST file");
1542 return;
1543 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001544
Douglas Gregor796d76a2010-10-20 22:00:55 +00001545 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1546 if (PPRec.getPreprocessedEntity(Record[0]))
1547 return;
1548
1549 const char *FullFileNameStart = BlobStart + Record[3];
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001550 const FileEntry *File
Chris Lattner8afa6de2010-11-21 09:55:08 +00001551 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
Chris Lattner5159f612010-11-23 08:35:12 +00001552 BlobLen - Record[3]));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001553
Douglas Gregor796d76a2010-10-20 22:00:55 +00001554 // FIXME: Stable encoding
1555 InclusionDirective::InclusionKind Kind
1556 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1557 InclusionDirective *ID
Douglas Gregorf09b6c92010-11-01 15:03:47 +00001558 = new (PPRec) InclusionDirective(PPRec, Kind,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001559 llvm::StringRef(BlobStart, Record[3]),
1560 Record[4],
1561 File,
1562 SourceRange(ReadSourceLocation(F, Record[1]),
1563 ReadSourceLocation(F, Record[2])));
1564 PPRec.SetPreallocatedEntity(Record[0], ID);
1565 return;
1566 }
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001567 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001568 }
1569}
1570
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001571void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1572 uint64_t Offset) {
1573 // Note that this identifier has a macro definition.
1574 II->setHasMacroDefinition(true);
1575
1576 // Adjust the offset based on our position in the chain.
1577 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1578 if (Chain[I] == &F)
1579 break;
1580
1581 Offset += Chain[I]->SizeInBits;
1582 }
1583
1584 UnreadMacroRecordOffsets[II] = Offset;
1585}
1586
Sebastian Redl2c499f62010-08-18 23:56:43 +00001587void ASTReader::ReadDefinedMacros() {
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001588 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001589 PerFileData &F = *Chain[N - I - 1];
1590 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001591
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001592 // If there was no preprocessor block, skip this file.
1593 if (!MacroCursor.getBitStreamReader())
1594 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001595
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001596 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001597 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001598
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001599 RecordData Record;
1600 while (true) {
Sebastian Redl4102dd52010-09-28 02:55:49 +00001601 uint64_t Offset = Cursor.GetCurrentBitNo();
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001602 unsigned Code = Cursor.ReadCode();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001603 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001604 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001605
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001606 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1607 // No known subblocks, always skip them.
1608 Cursor.ReadSubBlockID();
1609 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001610 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001611 return;
1612 }
1613 continue;
1614 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001615
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001616 if (Code == llvm::bitc::DEFINE_ABBREV) {
1617 Cursor.ReadAbbrevRecord();
1618 continue;
1619 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001620
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001621 // Read a record.
1622 const char *BlobStart;
1623 unsigned BlobLen;
1624 Record.clear();
1625 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1626 default: // Default behavior: ignore.
1627 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001628
Sebastian Redl539c5062010-08-18 23:57:32 +00001629 case PP_MACRO_OBJECT_LIKE:
1630 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001631 DecodeIdentifierInfo(Record[0]);
1632 break;
1633
Sebastian Redl539c5062010-08-18 23:57:32 +00001634 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001635 // Ignore tokens.
1636 break;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001637
Sebastian Redl539c5062010-08-18 23:57:32 +00001638 case PP_MACRO_INSTANTIATION:
1639 case PP_MACRO_DEFINITION:
Douglas Gregor796d76a2010-10-20 22:00:55 +00001640 case PP_INCLUSION_DIRECTIVE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001641 // Read the macro record.
Sebastian Redl4102dd52010-09-28 02:55:49 +00001642 // FIXME: That's a stupid way to do this. We should reuse this cursor.
Sebastian Redl2c373b92010-10-05 15:59:54 +00001643 ReadMacroRecord(F, Offset);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001644 break;
1645 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001646 }
1647 }
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001648
1649 // Drain the unread macro-record offsets map.
1650 while (!UnreadMacroRecordOffsets.empty())
1651 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1652}
1653
1654void ASTReader::LoadMacroDefinition(
1655 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1656 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1657 PerFileData *F = 0;
1658 uint64_t Offset = Pos->second;
1659 UnreadMacroRecordOffsets.erase(Pos);
1660
1661 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1662 if (Offset < Chain[I]->SizeInBits) {
1663 F = Chain[I];
1664 break;
1665 }
1666
1667 Offset -= Chain[I]->SizeInBits;
1668 }
1669 if (!F) {
1670 Error("Malformed macro record offset");
1671 return;
1672 }
1673
1674 ReadMacroRecord(*F, Offset);
1675}
1676
1677void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1678 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1679 = UnreadMacroRecordOffsets.find(II);
1680 LoadMacroDefinition(Pos);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001681}
1682
Sebastian Redl50e26582010-09-15 19:54:06 +00001683MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor91096292010-10-02 19:29:26 +00001684 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregoraae92242010-03-19 21:51:54 +00001685 return 0;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001686
Douglas Gregor91096292010-10-02 19:29:26 +00001687 if (!MacroDefinitionsLoaded[ID - 1]) {
1688 unsigned Index = ID - 1;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001689 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1690 PerFileData &F = *Chain[N - I - 1];
1691 if (Index < F.LocalNumMacroDefinitions) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001692 ReadMacroRecord(F, F.MacroDefinitionOffsets[Index]);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001693 break;
1694 }
1695 Index -= F.LocalNumMacroDefinitions;
1696 }
Douglas Gregor91096292010-10-02 19:29:26 +00001697 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001698 }
1699
Douglas Gregor91096292010-10-02 19:29:26 +00001700 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001701}
1702
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001703/// \brief If we are loading a relocatable PCH file, and the filename is
1704/// not an absolute path, add the system root to the beginning of the file
1705/// name.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001706void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001707 // If this is not a relocatable PCH file, there's nothing to do.
1708 if (!RelocatablePCH)
1709 return;
Mike Stump11289f42009-09-09 15:08:12 +00001710
Daniel Dunbarf2ce9a22009-11-18 19:50:41 +00001711 if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001712 return;
1713
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001714 if (isysroot == 0) {
1715 // If no system root was given, default to '/'
1716 Filename.insert(Filename.begin(), '/');
1717 return;
1718 }
Mike Stump11289f42009-09-09 15:08:12 +00001719
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001720 unsigned Length = strlen(isysroot);
1721 if (isysroot[Length - 1] != '/')
1722 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001723
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001724 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1725}
1726
Sebastian Redl2c499f62010-08-18 23:56:43 +00001727ASTReader::ASTReadResult
Sebastian Redl3e31c722010-08-18 23:56:56 +00001728ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001729 llvm::BitstreamCursor &Stream = F.Stream;
1730
Sebastian Redl539c5062010-08-18 23:57:32 +00001731 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001732 Error("malformed block record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001733 return Failure;
1734 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001735
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001736 // Read all of the records and blocks for the ASt file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001737 RecordData Record;
Sebastian Redl393f8b72010-07-19 20:52:06 +00001738 bool First = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001739 while (!Stream.AtEndOfStream()) {
1740 unsigned Code = Stream.ReadCode();
1741 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001742 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001743 Error("error at end of module block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001744 return Failure;
1745 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001746
Douglas Gregor55abb232009-04-10 20:39:37 +00001747 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001748 }
1749
1750 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1751 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001752 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001753 // We lazily load the decls block, but we want to set up the
1754 // DeclsCursor cursor to point into it. Clone our current bitcode
1755 // cursor to it, enter the block and read the abbrevs in that block.
1756 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001757 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001758 if (Stream.SkipBlock() || // Skip with the main cursor.
1759 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001760 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001761 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001762 return Failure;
1763 }
1764 break;
Mike Stump11289f42009-09-09 15:08:12 +00001765
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00001766 case DECL_UPDATES_BLOCK_ID:
1767 if (Stream.SkipBlock()) {
1768 Error("malformed block record in AST file");
1769 return Failure;
1770 }
1771 break;
1772
Sebastian Redl539c5062010-08-18 23:57:32 +00001773 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001774 F.MacroCursor = Stream;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001775 if (PP)
1776 PP->setExternalSource(this);
1777
Douglas Gregor796d76a2010-10-20 22:00:55 +00001778 if (Stream.SkipBlock() ||
1779 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001780 Error("malformed block record in AST file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001781 return Failure;
1782 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001783 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001784 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001785
Sebastian Redl539c5062010-08-18 23:57:32 +00001786 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001787 switch (ReadSourceManagerBlock(F)) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001788 case Success:
1789 break;
1790
1791 case Failure:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001792 Error("malformed source manager block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001793 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001794
1795 case IgnorePCH:
1796 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001797 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001798 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001799 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00001800 First = false;
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001801 continue;
1802 }
1803
1804 if (Code == llvm::bitc::DEFINE_ABBREV) {
1805 Stream.ReadAbbrevRecord();
1806 continue;
1807 }
1808
1809 // Read and process a record.
1810 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001811 const char *BlobStart = 0;
1812 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001813 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001814 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001815 default: // Default behavior: ignore.
1816 break;
1817
Sebastian Redl539c5062010-08-18 23:57:32 +00001818 case METADATA: {
1819 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1820 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001821 : diag::warn_pch_version_too_new);
1822 return IgnorePCH;
1823 }
1824
1825 RelocatablePCH = Record[4];
1826 if (Listener) {
1827 std::string TargetTriple(BlobStart, BlobLen);
1828 if (Listener->ReadTargetTriple(TargetTriple))
1829 return IgnorePCH;
1830 }
1831 break;
1832 }
1833
Sebastian Redl539c5062010-08-18 23:57:32 +00001834 case CHAINED_METADATA: {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001835 if (!First) {
1836 Error("CHAINED_METADATA is not first record in block");
1837 return Failure;
1838 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001839 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1840 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001841 : diag::warn_pch_version_too_new);
1842 return IgnorePCH;
1843 }
1844
Sebastian Redl009e7f22010-10-05 16:15:19 +00001845 // Load the chained file, which is always a PCH file.
1846 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001847 case Failure: return Failure;
1848 // If we have to ignore the dependency, we'll have to ignore this too.
1849 case IgnorePCH: return IgnorePCH;
1850 case Success: break;
1851 }
1852 break;
1853 }
1854
Sebastian Redl539c5062010-08-18 23:57:32 +00001855 case TYPE_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001856 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001857 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001858 return Failure;
1859 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001860 F.TypeOffsets = (const uint32_t *)BlobStart;
1861 F.LocalNumTypes = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001862 break;
1863
Sebastian Redl539c5062010-08-18 23:57:32 +00001864 case DECL_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001865 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001866 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001867 return Failure;
1868 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001869 F.DeclOffsets = (const uint32_t *)BlobStart;
1870 F.LocalNumDecls = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001871 break;
Douglas Gregor55abb232009-04-10 20:39:37 +00001872
Sebastian Redl539c5062010-08-18 23:57:32 +00001873 case TU_UPDATE_LEXICAL: {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001874 DeclContextInfo Info = {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001875 /* No visible information */ 0,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001876 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
1877 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001878 };
Douglas Gregoraa433012010-10-01 01:18:02 +00001879 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
1880 .push_back(Info);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001881 break;
1882 }
1883
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001884 case UPDATE_VISIBLE: {
1885 serialization::DeclID ID = Record[0];
1886 void *Table = ASTDeclContextNameLookupTable::Create(
1887 (const unsigned char *)BlobStart + Record[1],
1888 (const unsigned char *)BlobStart,
1889 ASTDeclContextNameLookupTrait(*this));
Douglas Gregoraa433012010-10-01 01:18:02 +00001890 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001891 DeclContextInfo Info = {
1892 Table, /* No lexical inforamtion */ 0, 0
1893 };
1894 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1895 } else
1896 PendingVisibleUpdates[ID].push_back(Table);
1897 break;
1898 }
1899
Sebastian Redl539c5062010-08-18 23:57:32 +00001900 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001901 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1902 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001903 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001904 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1905 Latest > FirstLatestDeclIDs[First]) &&
1906 "The new latest is supposed to come after the previous latest");
1907 FirstLatestDeclIDs[First] = Latest;
1908 }
1909 break;
1910 }
1911
Sebastian Redl539c5062010-08-18 23:57:32 +00001912 case LANGUAGE_OPTIONS:
Douglas Gregorce3a8292010-07-27 00:27:13 +00001913 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor55abb232009-04-10 20:39:37 +00001914 return IgnorePCH;
1915 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00001916
Sebastian Redl539c5062010-08-18 23:57:32 +00001917 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001918 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00001919 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001920 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001921 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00001922 (const unsigned char *)F.IdentifierTableData + Record[0],
1923 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001924 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001925 if (PP)
1926 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00001927 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001928 break;
1929
Sebastian Redl539c5062010-08-18 23:57:32 +00001930 case IDENTIFIER_OFFSET:
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001931 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001932 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001933 return Failure;
1934 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001935 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1936 F.LocalNumIdentifiers = Record[0];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001937 break;
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001938
Sebastian Redl539c5062010-08-18 23:57:32 +00001939 case EXTERNAL_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001940 // Optimization for the first block.
1941 if (ExternalDefinitions.empty())
1942 ExternalDefinitions.swap(Record);
1943 else
1944 ExternalDefinitions.insert(ExternalDefinitions.end(),
1945 Record.begin(), Record.end());
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001946 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00001947
Sebastian Redl539c5062010-08-18 23:57:32 +00001948 case SPECIAL_TYPES:
Sebastian Redlb293a452010-07-20 21:20:32 +00001949 // Optimization for the first block
1950 if (SpecialTypes.empty())
1951 SpecialTypes.swap(Record);
1952 else
1953 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregor652d82a2009-04-18 05:55:16 +00001954 break;
1955
Sebastian Redl539c5062010-08-18 23:57:32 +00001956 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001957 TotalNumStatements += Record[0];
1958 TotalNumMacros += Record[1];
1959 TotalLexicalDeclContexts += Record[2];
1960 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00001961 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00001962
Sebastian Redl539c5062010-08-18 23:57:32 +00001963 case TENTATIVE_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001964 // Optimization for the first block.
1965 if (TentativeDefinitions.empty())
1966 TentativeDefinitions.swap(Record);
1967 else
1968 TentativeDefinitions.insert(TentativeDefinitions.end(),
1969 Record.begin(), Record.end());
Douglas Gregord4df8652009-04-22 22:02:47 +00001970 break;
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001971
Sebastian Redl539c5062010-08-18 23:57:32 +00001972 case UNUSED_FILESCOPED_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001973 // Optimization for the first block.
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00001974 if (UnusedFileScopedDecls.empty())
1975 UnusedFileScopedDecls.swap(Record);
Sebastian Redlb293a452010-07-20 21:20:32 +00001976 else
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00001977 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1978 Record.begin(), Record.end());
Tanya Lattner90073802010-02-12 00:07:30 +00001979 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001980
Sebastian Redl539c5062010-08-18 23:57:32 +00001981 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001982 // Later blocks overwrite earlier ones.
1983 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00001984 break;
1985
Sebastian Redl539c5062010-08-18 23:57:32 +00001986 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001987 // Optimization for the first block.
1988 if (LocallyScopedExternalDecls.empty())
1989 LocallyScopedExternalDecls.swap(Record);
1990 else
1991 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1992 Record.begin(), Record.end());
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001993 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001994
Sebastian Redl539c5062010-08-18 23:57:32 +00001995 case SELECTOR_OFFSETS:
Sebastian Redla19a67f2010-08-03 21:58:15 +00001996 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00001997 F.LocalNumSelectors = Record[0];
Douglas Gregor95c13f52009-04-25 17:48:32 +00001998 break;
1999
Sebastian Redl539c5062010-08-18 23:57:32 +00002000 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00002001 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002002 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00002003 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002004 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00002005 F.SelectorLookupTableData + Record[0],
2006 F.SelectorLookupTableData,
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002007 ASTSelectorLookupTrait(*this));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002008 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002009 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00002010
Sebastian Redl96371b42010-09-22 00:42:30 +00002011 case REFERENCED_SELECTOR_POOL:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002012 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002013 break;
2014
Sebastian Redl539c5062010-08-18 23:57:32 +00002015 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002016 if (!Record.empty() && Listener)
2017 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002018 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002019
Sebastian Redl539c5062010-08-18 23:57:32 +00002020 case SOURCE_LOCATION_OFFSETS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002021 F.SLocOffsets = (const uint32_t *)BlobStart;
2022 F.LocalNumSLocEntries = Record[0];
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002023 F.LocalSLocSize = Record[1];
Douglas Gregor258ae542009-04-27 06:38:32 +00002024 break;
2025
Sebastian Redl539c5062010-08-18 23:57:32 +00002026 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl96371b42010-09-22 00:42:30 +00002027 if (PreloadSLocEntries.empty())
2028 PreloadSLocEntries.swap(Record);
2029 else
2030 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2031 Record.begin(), Record.end());
Douglas Gregor258ae542009-04-27 06:38:32 +00002032 break;
Douglas Gregorc5046832009-04-27 18:38:38 +00002033
Sebastian Redl539c5062010-08-18 23:57:32 +00002034 case STAT_CACHE: {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002035 ASTStatCache *MyStatCache =
2036 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
Douglas Gregord2eb58a2009-10-16 18:18:30 +00002037 (const unsigned char *)BlobStart,
2038 NumStatHits, NumStatMisses);
2039 FileMgr.addStatCache(MyStatCache);
Sebastian Redl34522812010-07-16 17:50:48 +00002040 F.StatCache = MyStatCache;
Douglas Gregorc5046832009-04-27 18:38:38 +00002041 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00002042 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002043
Sebastian Redl539c5062010-08-18 23:57:32 +00002044 case EXT_VECTOR_DECLS:
Sebastian Redl04f5c312010-07-28 21:38:49 +00002045 // Optimization for the first block.
2046 if (ExtVectorDecls.empty())
2047 ExtVectorDecls.swap(Record);
2048 else
2049 ExtVectorDecls.insert(ExtVectorDecls.end(),
2050 Record.begin(), Record.end());
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002051 break;
2052
Sebastian Redl539c5062010-08-18 23:57:32 +00002053 case VTABLE_USES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002054 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002055 VTableUses.swap(Record);
2056 break;
2057
Sebastian Redl539c5062010-08-18 23:57:32 +00002058 case DYNAMIC_CLASSES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002059 // Optimization for the first block.
2060 if (DynamicClasses.empty())
2061 DynamicClasses.swap(Record);
2062 else
2063 DynamicClasses.insert(DynamicClasses.end(),
2064 Record.begin(), Record.end());
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002065 break;
2066
Sebastian Redl539c5062010-08-18 23:57:32 +00002067 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002068 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002069 break;
2070
Sebastian Redl539c5062010-08-18 23:57:32 +00002071 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002072 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002073 SemaDeclRefs.swap(Record);
2074 break;
2075
Sebastian Redl539c5062010-08-18 23:57:32 +00002076 case ORIGINAL_FILE_NAME:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002077 // The primary AST will be the last to get here, so it will be the one
Sebastian Redlb293a452010-07-20 21:20:32 +00002078 // that's used.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00002079 ActualOriginalFileName.assign(BlobStart, BlobLen);
2080 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002081 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00002082 break;
Mike Stump11289f42009-09-09 15:08:12 +00002083
Sebastian Redl539c5062010-08-18 23:57:32 +00002084 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00002085 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002086 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2087 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2088 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregord54f3a12009-10-05 21:07:28 +00002089 return IgnorePCH;
2090 }
2091 break;
2092 }
Sebastian Redlfa061442010-07-21 20:07:32 +00002093
Sebastian Redl539c5062010-08-18 23:57:32 +00002094 case MACRO_DEFINITION_OFFSETS:
Sebastian Redlfa061442010-07-21 20:07:32 +00002095 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2096 F.NumPreallocatedPreprocessingEntities = Record[0];
2097 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregoraae92242010-03-19 21:51:54 +00002098 break;
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002099
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002100 case DECL_UPDATE_OFFSETS: {
2101 if (Record.size() % 2 != 0) {
2102 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2103 return Failure;
2104 }
2105 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2106 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2107 .push_back(std::make_pair(&F, Record[I+1]));
2108 break;
2109 }
2110
Sebastian Redl539c5062010-08-18 23:57:32 +00002111 case DECL_REPLACEMENTS: {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002112 if (Record.size() % 2 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002113 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002114 return Failure;
2115 }
2116 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl539c5062010-08-18 23:57:32 +00002117 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002118 std::make_pair(&F, Record[I+1]);
2119 break;
2120 }
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002121
2122 case CXX_BASE_SPECIFIER_OFFSETS: {
2123 if (F.LocalNumCXXBaseSpecifiers != 0) {
2124 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2125 return Failure;
2126 }
2127
2128 F.LocalNumCXXBaseSpecifiers = Record[0];
2129 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2130 break;
2131 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002132
2133 case DIAG_USER_MAPPINGS:
2134 if (Record.size() % 2 != 0) {
2135 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2136 return Failure;
2137 }
2138 if (UserDiagMappings.empty())
2139 UserDiagMappings.swap(Record);
2140 else
2141 UserDiagMappings.insert(UserDiagMappings.end(),
2142 Record.begin(), Record.end());
2143 break;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002144 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00002145 First = false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002146 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002147 Error("premature end of bitstream in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002148 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002149}
2150
Sebastian Redl009e7f22010-10-05 16:15:19 +00002151ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2152 ASTFileType Type) {
2153 switch(ReadASTCore(FileName, Type)) {
Sebastian Redl2abc0382010-07-16 20:41:52 +00002154 case Failure: return Failure;
2155 case IgnorePCH: return IgnorePCH;
2156 case Success: break;
2157 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002158
2159 // Here comes stuff that we only do once the entire chain is loaded.
2160
Sebastian Redl96371b42010-09-22 00:42:30 +00002161 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redlfa061442010-07-21 20:07:32 +00002162 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redlada023c2010-08-04 20:40:17 +00002163 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2164 TotalNumSelectors = 0;
Sebastian Redl9e687992010-07-19 22:06:55 +00002165 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl96371b42010-09-22 00:42:30 +00002166 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002167 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002168 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl9e687992010-07-19 22:06:55 +00002169 TotalNumTypes += Chain[I]->LocalNumTypes;
2170 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redlfa061442010-07-21 20:07:32 +00002171 TotalNumPreallocatedPreprocessingEntities +=
2172 Chain[I]->NumPreallocatedPreprocessingEntities;
2173 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redlada023c2010-08-04 20:40:17 +00002174 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl9e687992010-07-19 22:06:55 +00002175 }
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002176 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002177 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl9e687992010-07-19 22:06:55 +00002178 TypesLoaded.resize(TotalNumTypes);
2179 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redlfa061442010-07-21 20:07:32 +00002180 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2181 if (PP) {
2182 if (TotalNumIdentifiers > 0)
2183 PP->getHeaderSearchInfo().SetExternalLookup(this);
2184 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2185 if (!PP->getPreprocessingRecord())
2186 PP->createPreprocessingRecord();
2187 PP->getPreprocessingRecord()->SetExternalSource(*this,
2188 TotalNumPreallocatedPreprocessingEntities);
2189 }
2190 }
Sebastian Redlada023c2010-08-04 20:40:17 +00002191 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl96371b42010-09-22 00:42:30 +00002192 // Preload SLocEntries.
2193 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2194 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2195 if (Result != Success)
2196 return Result;
2197 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002198
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002199 // Check the predefines buffers.
Douglas Gregorce3a8292010-07-27 00:27:13 +00002200 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002201 return IgnorePCH;
2202
2203 if (PP) {
2204 // Initialization of keywords and pragmas occurs before the
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002205 // AST file is read, so there may be some identifiers that were
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002206 // loaded into the IdentifierTable before we intercepted the
2207 // creation of identifiers. Iterate through the list of known
2208 // identifiers and determine whether we have to establish
2209 // preprocessor definitions or top-level identifier declaration
2210 // chains for those identifiers.
2211 //
2212 // We copy the IdentifierInfo pointers to a small vector first,
2213 // since de-serializing declarations or macro definitions can add
2214 // new entries into the identifier table, invalidating the
2215 // iterators.
2216 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2217 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2218 IdEnd = PP->getIdentifierTable().end();
2219 Id != IdEnd; ++Id)
2220 Identifiers.push_back(Id->second);
Sebastian Redlfa061442010-07-21 20:07:32 +00002221 // We need to search the tables in all files.
Sebastian Redlfa061442010-07-21 20:07:32 +00002222 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002223 ASTIdentifierLookupTable *IdTable
2224 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2225 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl5c415f32010-07-22 17:01:13 +00002226 // ones.
2227 if (!IdTable)
2228 continue;
Sebastian Redlfa061442010-07-21 20:07:32 +00002229 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2230 IdentifierInfo *II = Identifiers[I];
2231 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00002232 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redlfa061442010-07-21 20:07:32 +00002233 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002234 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redlb293a452010-07-20 21:20:32 +00002235 if (Pos == IdTable->end())
2236 continue;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002237
Sebastian Redlb293a452010-07-20 21:20:32 +00002238 // Dereferencing the iterator has the effect of populating the
2239 // IdentifierInfo node with the various declarations it needs.
2240 (void)*Pos;
2241 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002242 }
2243 }
2244
2245 if (Context)
2246 InitializeContext(*Context);
2247
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002248 if (DeserializationListener)
2249 DeserializationListener->ReaderInitialized(this);
2250
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002251 return Success;
2252}
2253
Sebastian Redl009e7f22010-10-05 16:15:19 +00002254ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2255 ASTFileType Type) {
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002256 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl009e7f22010-10-05 16:15:19 +00002257 Chain.push_back(new PerFileData(Type));
Sebastian Redl34522812010-07-16 17:50:48 +00002258 PerFileData &F = *Chain.back();
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002259 if (Prev)
2260 Prev->NextInSource = &F;
2261 else
2262 FirstInSource = &F;
2263 F.Loaders.push_back(Prev);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002264
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002265 // Set the AST file name.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002266 F.FileName = FileName;
2267
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002268 // Open the AST file.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002269 //
2270 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2271 std::string ErrStr;
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00002272 if (FileName == "-")
2273 F.Buffer.reset(llvm::MemoryBuffer::getSTDIN(&ErrStr));
2274 else
Chris Lattner5159f612010-11-23 08:35:12 +00002275 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002276 if (!F.Buffer) {
2277 Error(ErrStr.c_str());
2278 return IgnorePCH;
2279 }
2280
2281 // Initialize the stream
2282 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2283 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl34522812010-07-16 17:50:48 +00002284 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002285 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002286 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002287
2288 // Sniff for the signature.
2289 if (Stream.Read(8) != 'C' ||
2290 Stream.Read(8) != 'P' ||
2291 Stream.Read(8) != 'C' ||
2292 Stream.Read(8) != 'H') {
2293 Diag(diag::err_not_a_pch_file) << FileName;
2294 return Failure;
2295 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002296
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002297 while (!Stream.AtEndOfStream()) {
2298 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002299
Douglas Gregor92863e42009-04-10 23:10:45 +00002300 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002301 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002302 return Failure;
2303 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002304
2305 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002306
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002307 // We only know the AST subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002308 switch (BlockID) {
2309 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002310 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002311 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002312 return Failure;
2313 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002314 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00002315 case AST_BLOCK_ID:
Sebastian Redl3e31c722010-08-18 23:56:56 +00002316 switch (ReadASTBlock(F)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002317 case Success:
2318 break;
2319
2320 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00002321 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00002322
2323 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00002324 // FIXME: We could consider reading through to the end of this
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002325 // AST block, skipping subblocks, to see if there are other
2326 // AST blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00002327
2328 // Clear out any preallocated source location entries, so that
2329 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002330 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00002331
2332 // Remove the stat cache.
Sebastian Redl34522812010-07-16 17:50:48 +00002333 if (F.StatCache)
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002334 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00002335
Douglas Gregor92863e42009-04-10 23:10:45 +00002336 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002337 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002338 break;
2339 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002340 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002341 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002342 return Failure;
2343 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002344 break;
2345 }
Mike Stump11289f42009-09-09 15:08:12 +00002346 }
2347
Sebastian Redl2abc0382010-07-16 20:41:52 +00002348 return Success;
2349}
2350
Sebastian Redl2c499f62010-08-18 23:56:43 +00002351void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002352 PP = &pp;
Sebastian Redlfa061442010-07-21 20:07:32 +00002353
2354 unsigned TotalNum = 0;
2355 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2356 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2357 if (TotalNum) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002358 if (!PP->getPreprocessingRecord())
2359 PP->createPreprocessingRecord();
Sebastian Redlfa061442010-07-21 20:07:32 +00002360 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregoraae92242010-03-19 21:51:54 +00002361 }
2362}
2363
Sebastian Redl2c499f62010-08-18 23:56:43 +00002364void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002365 Context = &Ctx;
2366 assert(Context && "Passed null context!");
2367
2368 assert(PP && "Forgot to set Preprocessor ?");
2369 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2370 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00002371 PP->setExternalSource(this);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002372
Douglas Gregoraa433012010-10-01 01:18:02 +00002373 // If we have an update block for the TU waiting, we have to add it before
2374 // deserializing the decl.
2375 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2376 if (DCU != DeclContextOffsets.end()) {
2377 // Insertion could invalidate map, so grab vector.
2378 DeclContextInfos T;
2379 T.swap(DCU->second);
2380 DeclContextOffsets.erase(DCU);
2381 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2382 }
2383
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002384 // Load the translation unit declaration
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00002385 GetTranslationUnitDecl();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002386
2387 // Load the special types.
2388 Context->setBuiltinVaListType(
Sebastian Redl539c5062010-08-18 23:57:32 +00002389 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2390 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002391 Context->setObjCIdType(GetType(Id));
Sebastian Redl539c5062010-08-18 23:57:32 +00002392 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002393 Context->setObjCSelType(GetType(Sel));
Sebastian Redl539c5062010-08-18 23:57:32 +00002394 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002395 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl539c5062010-08-18 23:57:32 +00002396 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002397 Context->setObjCClassType(GetType(Class));
Steve Naroff7cae42b2009-07-10 23:34:53 +00002398
Sebastian Redl539c5062010-08-18 23:57:32 +00002399 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002400 Context->setCFConstantStringType(GetType(String));
Mike Stump11289f42009-09-09 15:08:12 +00002401 if (unsigned FastEnum
Sebastian Redl539c5062010-08-18 23:57:32 +00002402 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002403 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl539c5062010-08-18 23:57:32 +00002404 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregor27821ce2009-07-07 16:35:42 +00002405 QualType FileType = GetType(File);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002406 if (FileType.isNull()) {
2407 Error("FILE type is NULL");
2408 return;
2409 }
John McCall9dd450b2009-09-21 23:43:11 +00002410 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregor27821ce2009-07-07 16:35:42 +00002411 Context->setFILEDecl(Typedef->getDecl());
2412 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002413 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002414 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002415 Error("Invalid FILE type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002416 return;
2417 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002418 Context->setFILEDecl(Tag->getDecl());
2419 }
2420 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002421 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002422 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002423 if (Jmp_bufType.isNull()) {
2424 Error("jmp_bug type is NULL");
2425 return;
2426 }
John McCall9dd450b2009-09-21 23:43:11 +00002427 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002428 Context->setjmp_bufDecl(Typedef->getDecl());
2429 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002430 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002431 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002432 Error("Invalid jmp_buf type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002433 return;
2434 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002435 Context->setjmp_bufDecl(Tag->getDecl());
2436 }
2437 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002438 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002439 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002440 if (Sigjmp_bufType.isNull()) {
2441 Error("sigjmp_buf type is NULL");
2442 return;
2443 }
John McCall9dd450b2009-09-21 23:43:11 +00002444 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002445 Context->setsigjmp_bufDecl(Typedef->getDecl());
2446 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002447 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002448 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stumpa4de80b2009-07-28 02:25:19 +00002449 Context->setsigjmp_bufDecl(Tag->getDecl());
2450 }
2451 }
Mike Stump11289f42009-09-09 15:08:12 +00002452 if (unsigned ObjCIdRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002453 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002454 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump11289f42009-09-09 15:08:12 +00002455 if (unsigned ObjCClassRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002456 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002457 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002458 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpd0153282009-10-20 02:12:22 +00002459 Context->setBlockDescriptorType(GetType(String));
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002460 if (unsigned String
Sebastian Redl539c5062010-08-18 23:57:32 +00002461 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002462 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002463 if (unsigned ObjCSelRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002464 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002465 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002466 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002467 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002468
Sebastian Redl539c5062010-08-18 23:57:32 +00002469 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002470 Context->setInt128Installed();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002471
2472 ReadUserDiagnosticMappings(Context->getDiagnostics());
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002473}
2474
Douglas Gregor45fe0362009-05-12 01:31:05 +00002475/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002476/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00002477/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002478std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00002479 FileManager &FileMgr,
Daniel Dunbar3b951482009-12-03 09:13:06 +00002480 Diagnostic &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002481 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002482 std::string ErrStr;
2483 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner5159f612010-11-23 08:35:12 +00002484 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00002485 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00002486 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002487 return std::string();
2488 }
2489
2490 // Initialize the stream
2491 llvm::BitstreamReader StreamFile;
2492 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00002493 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00002494 (const unsigned char *)Buffer->getBufferEnd());
2495 Stream.init(StreamFile);
2496
2497 // Sniff for the signature.
2498 if (Stream.Read(8) != 'C' ||
2499 Stream.Read(8) != 'P' ||
2500 Stream.Read(8) != 'C' ||
2501 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002502 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002503 return std::string();
2504 }
2505
2506 RecordData Record;
2507 while (!Stream.AtEndOfStream()) {
2508 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002509
Douglas Gregor45fe0362009-05-12 01:31:05 +00002510 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2511 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00002512
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002513 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002514 switch (BlockID) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002515 case AST_BLOCK_ID:
2516 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002517 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002518 return std::string();
2519 }
2520 break;
Mike Stump11289f42009-09-09 15:08:12 +00002521
Douglas Gregor45fe0362009-05-12 01:31:05 +00002522 default:
2523 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002524 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002525 return std::string();
2526 }
2527 break;
2528 }
2529 continue;
2530 }
2531
2532 if (Code == llvm::bitc::END_BLOCK) {
2533 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002534 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002535 return std::string();
2536 }
2537 continue;
2538 }
2539
2540 if (Code == llvm::bitc::DEFINE_ABBREV) {
2541 Stream.ReadAbbrevRecord();
2542 continue;
2543 }
2544
2545 Record.clear();
2546 const char *BlobStart = 0;
2547 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002548 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl539c5062010-08-18 23:57:32 +00002549 == ORIGINAL_FILE_NAME)
Douglas Gregor45fe0362009-05-12 01:31:05 +00002550 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00002551 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00002552
2553 return std::string();
2554}
2555
Douglas Gregor55abb232009-04-10 20:39:37 +00002556/// \brief Parse the record that corresponds to a LangOptions data
2557/// structure.
2558///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002559/// This routine parses the language options from the AST file and then gives
2560/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00002561///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002562/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002563bool ASTReader::ParseLanguageOptions(
Douglas Gregor55abb232009-04-10 20:39:37 +00002564 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002565 if (Listener) {
2566 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00002567
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002568 #define PARSE_LANGOPT(Option) \
2569 LangOpts.Option = Record[Idx]; \
2570 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00002571
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002572 unsigned Idx = 0;
2573 PARSE_LANGOPT(Trigraphs);
2574 PARSE_LANGOPT(BCPLComment);
2575 PARSE_LANGOPT(DollarIdents);
2576 PARSE_LANGOPT(AsmPreprocessor);
2577 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00002578 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002579 PARSE_LANGOPT(ImplicitInt);
2580 PARSE_LANGOPT(Digraphs);
2581 PARSE_LANGOPT(HexFloats);
2582 PARSE_LANGOPT(C99);
2583 PARSE_LANGOPT(Microsoft);
2584 PARSE_LANGOPT(CPlusPlus);
2585 PARSE_LANGOPT(CPlusPlus0x);
2586 PARSE_LANGOPT(CXXOperatorNames);
2587 PARSE_LANGOPT(ObjC1);
2588 PARSE_LANGOPT(ObjC2);
2589 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00002590 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00002591 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002592 PARSE_LANGOPT(PascalStrings);
2593 PARSE_LANGOPT(WritableStrings);
2594 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00002595 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002596 PARSE_LANGOPT(Exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +00002597 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002598 PARSE_LANGOPT(NeXTRuntime);
2599 PARSE_LANGOPT(Freestanding);
2600 PARSE_LANGOPT(NoBuiltin);
2601 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00002602 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002603 PARSE_LANGOPT(Blocks);
2604 PARSE_LANGOPT(EmitAllDecls);
2605 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00002606 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2607 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002608 PARSE_LANGOPT(HeinousExtensions);
2609 PARSE_LANGOPT(Optimize);
2610 PARSE_LANGOPT(OptimizeSize);
2611 PARSE_LANGOPT(Static);
2612 PARSE_LANGOPT(PICLevel);
2613 PARSE_LANGOPT(GNUInline);
2614 PARSE_LANGOPT(NoInline);
2615 PARSE_LANGOPT(AccessControl);
2616 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00002617 PARSE_LANGOPT(ShortWChar);
Chris Lattner51924e512010-06-26 21:25:03 +00002618 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall457a04e2010-10-22 21:05:15 +00002619 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00002620 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00002621 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002622 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00002623 PARSE_LANGOPT(OpenCL);
Mike Stumpd9546382009-12-12 01:27:46 +00002624 PARSE_LANGOPT(CatchUndefined);
2625 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002626 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00002627
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002628 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00002629 }
Douglas Gregor55abb232009-04-10 20:39:37 +00002630
2631 return false;
2632}
2633
Sebastian Redl2c499f62010-08-18 23:56:43 +00002634void ASTReader::ReadPreprocessedEntities() {
Douglas Gregoraae92242010-03-19 21:51:54 +00002635 ReadDefinedMacros();
2636}
2637
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002638void ASTReader::ReadUserDiagnosticMappings(Diagnostic &Diag) {
2639 unsigned Idx = 0;
2640 while (Idx < UserDiagMappings.size()) {
2641 unsigned DiagID = UserDiagMappings[Idx++];
2642 unsigned Map = UserDiagMappings[Idx++];
2643 Diag.setDiagnosticMappingInternal(DiagID, Map, /*isUser=*/true);
2644 }
2645}
2646
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002647/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002648ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002649 PerFileData *F = 0;
2650 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2651 F = Chain[N - I - 1];
2652 if (Index < F->LocalNumTypes)
2653 break;
2654 Index -= F->LocalNumTypes;
2655 }
2656 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redl2c373b92010-10-05 15:59:54 +00002657 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002658}
2659
2660/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002661///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002662/// The index is the type ID, shifted and minus the number of predefs. This
2663/// routine actually reads the record corresponding to the type at the given
2664/// location. It is a helper routine for GetType, which deals with reading type
2665/// IDs.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002666QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002667 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00002668 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00002669
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002670 // Keep track of where we are in the stream, then jump back there
2671 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002672 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002673
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002674 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00002675
Douglas Gregor1342e842009-07-06 18:54:52 +00002676 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00002677 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00002678
Sebastian Redl2c373b92010-10-05 15:59:54 +00002679 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002680 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002681 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00002682 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2683 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002684 if (Record.size() != 2) {
2685 Error("Incorrect encoding of extended qualifier type");
2686 return QualType();
2687 }
Douglas Gregor455b8f42009-04-15 22:00:08 +00002688 QualType Base = GetType(Record[0]);
John McCall8ccfcb52009-09-24 19:53:00 +00002689 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2690 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00002691 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002692
Sebastian Redl539c5062010-08-18 23:57:32 +00002693 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002694 if (Record.size() != 1) {
2695 Error("Incorrect encoding of complex type");
2696 return QualType();
2697 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002698 QualType ElemType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002699 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002700 }
2701
Sebastian Redl539c5062010-08-18 23:57:32 +00002702 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002703 if (Record.size() != 1) {
2704 Error("Incorrect encoding of pointer type");
2705 return QualType();
2706 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002707 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002708 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002709 }
2710
Sebastian Redl539c5062010-08-18 23:57:32 +00002711 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002712 if (Record.size() != 1) {
2713 Error("Incorrect encoding of block pointer type");
2714 return QualType();
2715 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002716 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002717 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002718 }
2719
Sebastian Redl539c5062010-08-18 23:57:32 +00002720 case TYPE_LVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002721 if (Record.size() != 1) {
2722 Error("Incorrect encoding of lvalue reference type");
2723 return QualType();
2724 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002725 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002726 return Context->getLValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002727 }
2728
Sebastian Redl539c5062010-08-18 23:57:32 +00002729 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002730 if (Record.size() != 1) {
2731 Error("Incorrect encoding of rvalue reference type");
2732 return QualType();
2733 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002734 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002735 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002736 }
2737
Sebastian Redl539c5062010-08-18 23:57:32 +00002738 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00002739 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002740 Error("Incorrect encoding of member pointer type");
2741 return QualType();
2742 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002743 QualType PointeeType = GetType(Record[0]);
2744 QualType ClassType = GetType(Record[1]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002745 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002746 }
2747
Sebastian Redl539c5062010-08-18 23:57:32 +00002748 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002749 QualType ElementType = GetType(Record[0]);
2750 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2751 unsigned IndexTypeQuals = Record[2];
2752 unsigned Idx = 3;
2753 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00002754 return Context->getConstantArrayType(ElementType, Size,
2755 ASM, IndexTypeQuals);
2756 }
2757
Sebastian Redl539c5062010-08-18 23:57:32 +00002758 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002759 QualType ElementType = GetType(Record[0]);
2760 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2761 unsigned IndexTypeQuals = Record[2];
Chris Lattner8575daa2009-04-27 21:45:14 +00002762 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002763 }
2764
Sebastian Redl539c5062010-08-18 23:57:32 +00002765 case TYPE_VARIABLE_ARRAY: {
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002766 QualType ElementType = GetType(Record[0]);
2767 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2768 unsigned IndexTypeQuals = Record[2];
Sebastian Redl2c373b92010-10-05 15:59:54 +00002769 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
2770 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
2771 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor04318252009-07-06 15:59:29 +00002772 ASM, IndexTypeQuals,
2773 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002774 }
2775
Sebastian Redl539c5062010-08-18 23:57:32 +00002776 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002777 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002778 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002779 return QualType();
2780 }
2781
2782 QualType ElementType = GetType(Record[0]);
2783 unsigned NumElements = Record[1];
Bob Wilsonaeb56442010-11-10 21:56:12 +00002784 unsigned VecKind = Record[2];
Chris Lattner37141f42010-06-23 06:00:24 +00002785 return Context->getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00002786 (VectorType::VectorKind)VecKind);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002787 }
2788
Sebastian Redl539c5062010-08-18 23:57:32 +00002789 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002790 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002791 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002792 return QualType();
2793 }
2794
2795 QualType ElementType = GetType(Record[0]);
2796 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00002797 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002798 }
2799
Sebastian Redl539c5062010-08-18 23:57:32 +00002800 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002801 if (Record.size() != 4) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002802 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002803 return QualType();
2804 }
2805 QualType ResultType = GetType(Record[0]);
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002806 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00002807 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002808 }
2809
Sebastian Redl539c5062010-08-18 23:57:32 +00002810 case TYPE_FUNCTION_PROTO: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002811 QualType ResultType = GetType(Record[0]);
Douglas Gregordc728752009-12-22 18:11:50 +00002812 bool NoReturn = Record[1];
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002813 unsigned RegParm = Record[2];
2814 CallingConv CallConv = (CallingConv)Record[3];
2815 unsigned Idx = 4;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002816 unsigned NumParams = Record[Idx++];
2817 llvm::SmallVector<QualType, 16> ParamTypes;
2818 for (unsigned I = 0; I != NumParams; ++I)
2819 ParamTypes.push_back(GetType(Record[Idx++]));
2820 bool isVariadic = Record[Idx++];
2821 unsigned Quals = Record[Idx++];
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002822 bool hasExceptionSpec = Record[Idx++];
2823 bool hasAnyExceptionSpec = Record[Idx++];
2824 unsigned NumExceptions = Record[Idx++];
2825 llvm::SmallVector<QualType, 2> Exceptions;
2826 for (unsigned I = 0; I != NumExceptions; ++I)
2827 Exceptions.push_back(GetType(Record[Idx++]));
Jay Foad7d0479f2009-05-21 09:52:38 +00002828 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002829 isVariadic, Quals, hasExceptionSpec,
2830 hasAnyExceptionSpec, NumExceptions,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00002831 Exceptions.data(),
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002832 FunctionType::ExtInfo(NoReturn, RegParm,
2833 CallConv));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002834 }
2835
Sebastian Redl539c5062010-08-18 23:57:32 +00002836 case TYPE_UNRESOLVED_USING:
John McCallb96ec562009-12-04 22:46:56 +00002837 return Context->getTypeDeclType(
2838 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2839
Sebastian Redl539c5062010-08-18 23:57:32 +00002840 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002841 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002842 Error("incorrect encoding of typedef type");
2843 return QualType();
2844 }
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002845 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2846 QualType Canonical = GetType(Record[1]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00002847 if (!Canonical.isNull())
2848 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002849 return Context->getTypedefType(Decl, Canonical);
2850 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002851
Sebastian Redl539c5062010-08-18 23:57:32 +00002852 case TYPE_TYPEOF_EXPR:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002853 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002854
Sebastian Redl539c5062010-08-18 23:57:32 +00002855 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002856 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002857 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002858 return QualType();
2859 }
2860 QualType UnderlyingType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002861 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002862 }
Mike Stump11289f42009-09-09 15:08:12 +00002863
Sebastian Redl539c5062010-08-18 23:57:32 +00002864 case TYPE_DECLTYPE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002865 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson81df7b82009-06-24 19:06:50 +00002866
Sebastian Redl539c5062010-08-18 23:57:32 +00002867 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002868 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002869 Error("incorrect encoding of record type");
2870 return QualType();
2871 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002872 bool IsDependent = Record[0];
2873 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCall25c9d112010-10-14 21:48:26 +00002874 T->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002875 return T;
2876 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002877
Sebastian Redl539c5062010-08-18 23:57:32 +00002878 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002879 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002880 Error("incorrect encoding of enum type");
2881 return QualType();
2882 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002883 bool IsDependent = Record[0];
2884 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCall25c9d112010-10-14 21:48:26 +00002885 T->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002886 return T;
2887 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00002888
Sebastian Redl539c5062010-08-18 23:57:32 +00002889 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002890 unsigned Idx = 0;
2891 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2892 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2893 QualType NamedType = GetType(Record[Idx++]);
2894 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00002895 }
2896
Sebastian Redl539c5062010-08-18 23:57:32 +00002897 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00002898 unsigned Idx = 0;
2899 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00002900 return Context->getObjCInterfaceType(ItfD);
2901 }
2902
Sebastian Redl539c5062010-08-18 23:57:32 +00002903 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00002904 unsigned Idx = 0;
2905 QualType Base = GetType(Record[Idx++]);
Chris Lattner587cbe12009-04-22 06:45:28 +00002906 unsigned NumProtos = Record[Idx++];
2907 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2908 for (unsigned I = 0; I != NumProtos; ++I)
2909 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002910 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00002911 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002912
Sebastian Redl539c5062010-08-18 23:57:32 +00002913 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00002914 unsigned Idx = 0;
John McCall8b07ec22010-05-15 11:32:37 +00002915 QualType Pointee = GetType(Record[Idx++]);
2916 return Context->getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00002917 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00002918
Sebastian Redl539c5062010-08-18 23:57:32 +00002919 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00002920 unsigned Idx = 0;
2921 QualType Parm = GetType(Record[Idx++]);
2922 QualType Replacement = GetType(Record[Idx++]);
2923 return
2924 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2925 Replacement);
2926 }
John McCalle78aac42010-03-10 03:28:59 +00002927
Sebastian Redl539c5062010-08-18 23:57:32 +00002928 case TYPE_INJECTED_CLASS_NAME: {
John McCalle78aac42010-03-10 03:28:59 +00002929 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2930 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00002931 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002932 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00002933 return
2934 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00002935 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002936
Sebastian Redl539c5062010-08-18 23:57:32 +00002937 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002938 unsigned Idx = 0;
2939 unsigned Depth = Record[Idx++];
2940 unsigned Index = Record[Idx++];
2941 bool Pack = Record[Idx++];
2942 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
2943 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
2944 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002945
Sebastian Redl539c5062010-08-18 23:57:32 +00002946 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002947 unsigned Idx = 0;
2948 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2949 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2950 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00002951 QualType Canon = GetType(Record[Idx++]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00002952 if (!Canon.isNull())
2953 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00002954 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002955 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002956
Sebastian Redl539c5062010-08-18 23:57:32 +00002957 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002958 unsigned Idx = 0;
2959 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2960 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2961 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2962 unsigned NumArgs = Record[Idx++];
2963 llvm::SmallVector<TemplateArgument, 8> Args;
2964 Args.reserve(NumArgs);
2965 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00002966 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002967 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
2968 Args.size(), Args.data());
2969 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002970
Sebastian Redl539c5062010-08-18 23:57:32 +00002971 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00002972 unsigned Idx = 0;
2973
2974 // ArrayType
2975 QualType ElementType = GetType(Record[Idx++]);
2976 ArrayType::ArraySizeModifier ASM
2977 = (ArrayType::ArraySizeModifier)Record[Idx++];
2978 unsigned IndexTypeQuals = Record[Idx++];
2979
2980 // DependentSizedArrayType
Sebastian Redl2c373b92010-10-05 15:59:54 +00002981 Expr *NumElts = ReadExpr(*Loc.F);
2982 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00002983
2984 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
2985 IndexTypeQuals, Brackets);
2986 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002987
Sebastian Redl539c5062010-08-18 23:57:32 +00002988 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002989 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002990 bool IsDependent = Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002991 TemplateName Name = ReadTemplateName(Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002992 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redl2c373b92010-10-05 15:59:54 +00002993 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002994 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002995 QualType T;
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002996 if (Canon.isNull())
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002997 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
2998 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002999 else
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003000 T = Context->getTemplateSpecializationType(Name, Args.data(),
3001 Args.size(), Canon);
John McCall25c9d112010-10-14 21:48:26 +00003002 T->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003003 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003004 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003005 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003006 // Suppress a GCC warning
3007 return QualType();
3008}
3009
Sebastian Redl2c373b92010-10-05 15:59:54 +00003010class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00003011 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003012 ASTReader::PerFileData &F;
Sebastian Redlc67764e2010-07-22 22:43:28 +00003013 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redl2c499f62010-08-18 23:56:43 +00003014 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00003015 unsigned &Idx;
3016
Sebastian Redl2c373b92010-10-05 15:59:54 +00003017 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3018 unsigned &I) {
3019 return Reader.ReadSourceLocation(F, R, I);
3020 }
3021
John McCall8f115c62009-10-16 21:56:05 +00003022public:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003023 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl2c499f62010-08-18 23:56:43 +00003024 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003025 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3026 { }
John McCall8f115c62009-10-16 21:56:05 +00003027
John McCall17001972009-10-18 01:05:36 +00003028 // We want compile-time assurance that we've enumerated all of
3029 // these, so unfortunately we have to declare them first, then
3030 // define them out-of-line.
3031#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00003032#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00003033 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00003034#include "clang/AST/TypeLocNodes.def"
3035
John McCall17001972009-10-18 01:05:36 +00003036 void VisitFunctionTypeLoc(FunctionTypeLoc);
3037 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00003038};
3039
John McCall17001972009-10-18 01:05:36 +00003040void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00003041 // nothing to do
3042}
John McCall17001972009-10-18 01:05:36 +00003043void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003044 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorc9b7a592010-01-18 18:04:31 +00003045 if (TL.needsExtraLocalData()) {
3046 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3047 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3048 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3049 TL.setModeAttr(Record[Idx++]);
3050 }
John McCall8f115c62009-10-16 21:56:05 +00003051}
John McCall17001972009-10-18 01:05:36 +00003052void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003053 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003054}
John McCall17001972009-10-18 01:05:36 +00003055void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003056 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003057}
John McCall17001972009-10-18 01:05:36 +00003058void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003059 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003060}
John McCall17001972009-10-18 01:05:36 +00003061void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003062 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003063}
John McCall17001972009-10-18 01:05:36 +00003064void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003065 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003066}
John McCall17001972009-10-18 01:05:36 +00003067void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003068 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003069}
John McCall17001972009-10-18 01:05:36 +00003070void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003071 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3072 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003073 if (Record[Idx++])
Sebastian Redl2c373b92010-10-05 15:59:54 +00003074 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003075 else
John McCall17001972009-10-18 01:05:36 +00003076 TL.setSizeExpr(0);
3077}
3078void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3079 VisitArrayTypeLoc(TL);
3080}
3081void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3082 VisitArrayTypeLoc(TL);
3083}
3084void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3085 VisitArrayTypeLoc(TL);
3086}
3087void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3088 DependentSizedArrayTypeLoc TL) {
3089 VisitArrayTypeLoc(TL);
3090}
3091void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3092 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003093 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003094}
3095void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003096 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003097}
3098void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003099 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003100}
3101void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003102 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3103 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor7fb25412010-10-01 18:44:50 +00003104 TL.setTrailingReturn(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00003105 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCalle6347002009-10-23 01:28:53 +00003106 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall17001972009-10-18 01:05:36 +00003107 }
3108}
3109void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3110 VisitFunctionTypeLoc(TL);
3111}
3112void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3113 VisitFunctionTypeLoc(TL);
3114}
John McCallb96ec562009-12-04 22:46:56 +00003115void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003116 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallb96ec562009-12-04 22:46:56 +00003117}
John McCall17001972009-10-18 01:05:36 +00003118void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003119 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003120}
3121void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003122 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3123 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3124 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003125}
3126void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003127 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3128 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3129 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3130 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003131}
3132void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003133 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003134}
3135void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003136 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003137}
3138void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003139 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003140}
John McCall17001972009-10-18 01:05:36 +00003141void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003142 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003143}
John McCallcebee162009-10-18 09:09:24 +00003144void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3145 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003146 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallcebee162009-10-18 09:09:24 +00003147}
John McCall17001972009-10-18 01:05:36 +00003148void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3149 TemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003150 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3151 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3152 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall0ad16662009-10-29 08:12:44 +00003153 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3154 TL.setArgLocInfo(i,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003155 Reader.GetTemplateArgumentLocInfo(F,
3156 TL.getTypePtr()->getArg(i).getKind(),
3157 Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003158}
Abramo Bagnara6150c882010-05-11 21:36:43 +00003159void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003160 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3161 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003162}
John McCalle78aac42010-03-10 03:28:59 +00003163void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003164 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalle78aac42010-03-10 03:28:59 +00003165}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00003166void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003167 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3168 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3169 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003170}
John McCallc392f372010-06-11 00:33:02 +00003171void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3172 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003173 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3174 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3175 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3176 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3177 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003178 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3179 TL.setArgLocInfo(I,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003180 Reader.GetTemplateArgumentLocInfo(F,
3181 TL.getTypePtr()->getArg(I).getKind(),
3182 Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003183}
John McCall17001972009-10-18 01:05:36 +00003184void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003185 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8b07ec22010-05-15 11:32:37 +00003186}
3187void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3188 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003189 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3190 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003191 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003192 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003193}
John McCallfc93cf92009-10-22 22:37:11 +00003194void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003195 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCallfc93cf92009-10-22 22:37:11 +00003196}
John McCall8f115c62009-10-16 21:56:05 +00003197
Sebastian Redl2c373b92010-10-05 15:59:54 +00003198TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003199 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00003200 unsigned &Idx) {
3201 QualType InfoTy = GetType(Record[Idx++]);
3202 if (InfoTy.isNull())
3203 return 0;
3204
John McCallbcd03502009-12-07 02:54:59 +00003205 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003206 TypeLocReader TLR(*this, F, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00003207 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00003208 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00003209 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00003210}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003211
Sebastian Redl539c5062010-08-18 23:57:32 +00003212QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00003213 unsigned FastQuals = ID & Qualifiers::FastMask;
3214 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003215
Sebastian Redl539c5062010-08-18 23:57:32 +00003216 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003217 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00003218 switch ((PredefinedTypeIDs)Index) {
3219 case PREDEF_TYPE_NULL_ID: return QualType();
3220 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3221 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003222
Sebastian Redl539c5062010-08-18 23:57:32 +00003223 case PREDEF_TYPE_CHAR_U_ID:
3224 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003225 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00003226 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003227 break;
3228
Sebastian Redl539c5062010-08-18 23:57:32 +00003229 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3230 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3231 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3232 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3233 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3234 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3235 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3236 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3237 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3238 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3239 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3240 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3241 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3242 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3243 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3244 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3245 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3246 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3247 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3248 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3249 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3250 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3251 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3252 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003253 }
3254
3255 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00003256 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003257 }
3258
Sebastian Redl539c5062010-08-18 23:57:32 +00003259 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003260 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00003261 if (TypesLoaded[Index].isNull()) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003262 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003263 if (TypesLoaded[Index].isNull())
3264 return QualType();
3265
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003266 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003267 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003268 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003269 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003270 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00003271 }
Mike Stump11289f42009-09-09 15:08:12 +00003272
John McCall8ccfcb52009-09-24 19:53:00 +00003273 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003274}
3275
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003276TypeID ASTReader::GetTypeID(QualType T) const {
3277 return MakeTypeID(T,
3278 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3279}
3280
3281TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3282 if (T.isNull())
3283 return TypeIdx();
3284 assert(!T.getLocalFastQualifiers());
3285
3286 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3287 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3288 // comparing keys of ASTDeclContextNameLookupTable.
3289 // If the type didn't come from the AST file use a specially marked index
3290 // so that any hash/key comparison fail since no such index is stored
3291 // in a AST file.
3292 if (I == TypeIdxs.end())
3293 return TypeIdx(-1);
3294 return I->second;
3295}
3296
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003297unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3298 unsigned Result = 0;
3299 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3300 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3301
3302 return Result;
3303}
3304
John McCall0ad16662009-10-29 08:12:44 +00003305TemplateArgumentLocInfo
Sebastian Redl2c373b92010-10-05 15:59:54 +00003306ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3307 TemplateArgument::ArgKind Kind,
John McCall0ad16662009-10-29 08:12:44 +00003308 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003309 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00003310 switch (Kind) {
3311 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003312 return ReadExpr(F);
John McCall0ad16662009-10-29 08:12:44 +00003313 case TemplateArgument::Type:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003314 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003315 case TemplateArgument::Template: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003316 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3317 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003318 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003319 }
John McCall0ad16662009-10-29 08:12:44 +00003320 case TemplateArgument::Null:
3321 case TemplateArgument::Integral:
3322 case TemplateArgument::Declaration:
3323 case TemplateArgument::Pack:
3324 return TemplateArgumentLocInfo();
3325 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00003326 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00003327 return TemplateArgumentLocInfo();
3328}
3329
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003330TemplateArgumentLoc
Sebastian Redl2c373b92010-10-05 15:59:54 +00003331ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003332 const RecordData &Record, unsigned &Index) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003333 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003334
3335 if (Arg.getKind() == TemplateArgument::Expression) {
3336 if (Record[Index++]) // bool InfoHasSameExpr.
3337 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3338 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00003339 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003340 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003341}
3342
Sebastian Redl2c499f62010-08-18 23:56:43 +00003343Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00003344 return GetDecl(ID);
3345}
3346
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003347uint64_t
3348ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3349 if (ID == 0)
3350 return 0;
3351
3352 --ID;
3353 uint64_t Offset = 0;
3354 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3355 if (ID < Chain[I]->LocalNumCXXBaseSpecifiers)
3356 return Offset + Chain[I]->CXXBaseSpecifiersOffsets[ID];
3357
3358 ID -= Chain[I]->LocalNumCXXBaseSpecifiers;
3359 Offset += Chain[I]->SizeInBits;
3360 }
3361
3362 assert(false && "CXXBaseSpecifiers not found");
3363 return 0;
3364}
3365
3366CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3367 // Figure out which AST file contains this offset.
3368 PerFileData *F = 0;
3369 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3370 if (Offset < Chain[I]->SizeInBits) {
3371 F = Chain[I];
3372 break;
3373 }
3374
3375 Offset -= Chain[I]->SizeInBits;
3376 }
3377
3378 if (!F) {
3379 Error("Malformed AST file: C++ base specifiers at impossible offset");
3380 return 0;
3381 }
3382
3383 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3384 SavedStreamPosition SavedPosition(Cursor);
3385 Cursor.JumpToBit(Offset);
3386 ReadingKindTracker ReadingKind(Read_Decl, *this);
3387 RecordData Record;
3388 unsigned Code = Cursor.ReadCode();
3389 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3390 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3391 Error("Malformed AST file: missing C++ base specifiers");
3392 return 0;
3393 }
3394
3395 unsigned Idx = 0;
3396 unsigned NumBases = Record[Idx++];
3397 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3398 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3399 for (unsigned I = 0; I != NumBases; ++I)
3400 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3401 return Bases;
3402}
3403
Sebastian Redl2c499f62010-08-18 23:56:43 +00003404TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003405 if (!DeclsLoaded[0]) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00003406 ReadDeclRecord(0, 1);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003407 if (DeserializationListener)
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003408 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003409 }
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00003410
3411 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3412}
3413
Sebastian Redl539c5062010-08-18 23:57:32 +00003414Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003415 if (ID == 0)
3416 return 0;
3417
Douglas Gregor745ed142009-04-25 18:35:21 +00003418 if (ID > DeclsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003419 Error("declaration ID out-of-range for AST file");
Douglas Gregor745ed142009-04-25 18:35:21 +00003420 return 0;
3421 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003422
Douglas Gregor745ed142009-04-25 18:35:21 +00003423 unsigned Index = ID - 1;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003424 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003425 ReadDeclRecord(Index, ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003426 if (DeserializationListener)
3427 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3428 }
Douglas Gregor745ed142009-04-25 18:35:21 +00003429
3430 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003431}
3432
Chris Lattner9c28af02009-04-27 05:46:25 +00003433/// \brief Resolve the offset of a statement into a statement.
3434///
3435/// This operation will read a new statement from the external
3436/// source each time it is called, and is meant to be used via a
3437/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00003438Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00003439 // Switch case IDs are per Decl.
3440 ClearSwitchCaseIDs();
3441
Sebastian Redl5c415f32010-07-22 17:01:13 +00003442 // Offset here is a global offset across the entire chain.
3443 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3444 PerFileData &F = *Chain[N - I - 1];
3445 if (Offset < F.SizeInBits) {
3446 // Since we know that this statement is part of a decl, make sure to use
3447 // the decl cursor to read it.
3448 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003449 return ReadStmtFromStream(F);
Sebastian Redl5c415f32010-07-22 17:01:13 +00003450 }
3451 Offset -= F.SizeInBits;
3452 }
3453 llvm_unreachable("Broken chain");
Douglas Gregor3c3aa612009-04-18 00:07:54 +00003454}
3455
Sebastian Redl2c499f62010-08-18 23:56:43 +00003456bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003457 bool (*isKindWeWant)(Decl::Kind),
John McCall75b960e2010-06-01 09:23:16 +00003458 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump11289f42009-09-09 15:08:12 +00003459 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003460 "DeclContext has no lexical decls in storage");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003461
Sebastian Redl5c415f32010-07-22 17:01:13 +00003462 // There might be lexical decls in multiple parts of the chain, for the TU
3463 // at least.
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003464 // DeclContextOffsets might reallocate as we load additional decls below,
3465 // so make a copy of the vector.
3466 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl5c415f32010-07-22 17:01:13 +00003467 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3468 I != E; ++I) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003469 // IDs can be 0 if this context doesn't contain declarations.
3470 if (!I->LexicalDecls)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003471 continue;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003472
3473 // Load all of the declaration IDs
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003474 for (const KindDeclIDPair *ID = I->LexicalDecls,
3475 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3476 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3477 continue;
3478
3479 Decl *D = GetDecl(ID->second);
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003480 assert(D && "Null decl in lexical decls");
3481 Decls.push_back(D);
3482 }
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003483 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003484
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003485 ++NumLexicalDeclContextsRead;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003486 return false;
3487}
3488
John McCall75b960e2010-06-01 09:23:16 +00003489DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00003490ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00003491 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003492 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003493 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003494 if (!Name)
3495 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3496 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003497
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003498 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl471ac2f2010-08-24 00:49:55 +00003499 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003500 // and namespaces. For any given name, the last available results replace
3501 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl5c415f32010-07-22 17:01:13 +00003502 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003503 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl5c415f32010-07-22 17:01:13 +00003504 I != E; ++I) {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003505 if (!I->NameLookupTableData)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003506 continue;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003507
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003508 ASTDeclContextNameLookupTable *LookupTable =
3509 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3510 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3511 if (Pos == LookupTable->end())
Sebastian Redl5c415f32010-07-22 17:01:13 +00003512 continue;
3513
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003514 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3515 for (; Data.first != Data.second; ++Data.first)
3516 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003517 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003518 }
3519
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003520 ++NumVisibleDeclContextsRead;
John McCall75b960e2010-06-01 09:23:16 +00003521
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003522 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00003523 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003524}
3525
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00003526void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3527 assert(DC->hasExternalVisibleStorage() &&
3528 "DeclContext has no visible decls in storage");
3529
3530 llvm::SmallVector<NamedDecl *, 64> Decls;
3531 // There might be visible decls in multiple parts of the chain, for the TU
3532 // and namespaces.
3533 DeclContextInfos &Infos = DeclContextOffsets[DC];
3534 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3535 I != E; ++I) {
3536 if (!I->NameLookupTableData)
3537 continue;
3538
3539 ASTDeclContextNameLookupTable *LookupTable =
3540 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3541 for (ASTDeclContextNameLookupTable::item_iterator
3542 ItemI = LookupTable->item_begin(),
3543 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3544 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3545 = *ItemI;
3546 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3547 Decls.clear();
3548 for (; Data.first != Data.second; ++Data.first)
3549 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3550 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3551 }
3552 }
3553}
3554
Sebastian Redl2c499f62010-08-18 23:56:43 +00003555void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003556 assert(Consumer);
3557 while (!InterestingDecls.empty()) {
3558 DeclGroupRef DG(InterestingDecls.front());
3559 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00003560 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003561 }
3562}
3563
Sebastian Redl2c499f62010-08-18 23:56:43 +00003564void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00003565 this->Consumer = Consumer;
3566
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003567 if (!Consumer)
3568 return;
3569
3570 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003571 // Force deserialization of this decl, which will cause it to be queued for
3572 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00003573 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003574 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00003575
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003576 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003577}
3578
Sebastian Redl2c499f62010-08-18 23:56:43 +00003579void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003580 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003581
Mike Stump11289f42009-09-09 15:08:12 +00003582 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003583 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00003584 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00003585 unsigned NumDeclsLoaded
3586 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3587 (Decl *)0);
3588 unsigned NumIdentifiersLoaded
3589 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3590 IdentifiersLoaded.end(),
3591 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00003592 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003593 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3594 SelectorsLoaded.end(),
3595 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00003596
Douglas Gregorc5046832009-04-27 18:38:38 +00003597 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3598 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor258ae542009-04-27 06:38:32 +00003599 if (TotalNumSLocEntries)
3600 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3601 NumSLocEntriesRead, TotalNumSLocEntries,
3602 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00003603 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003604 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003605 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3606 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3607 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003608 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003609 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3610 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00003611 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003612 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00003613 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3614 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00003615 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003616 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00003617 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3618 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003619 if (TotalNumStatements)
3620 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3621 NumStatementsRead, TotalNumStatements,
3622 ((float)NumStatementsRead/TotalNumStatements * 100));
3623 if (TotalNumMacros)
3624 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3625 NumMacrosRead, TotalNumMacros,
3626 ((float)NumMacrosRead/TotalNumMacros * 100));
3627 if (TotalLexicalDeclContexts)
3628 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3629 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3630 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3631 * 100));
3632 if (TotalVisibleDeclContexts)
3633 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3634 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3635 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3636 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003637 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003638 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003639 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3640 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00003641 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003642 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003643 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003644 std::fprintf(stderr, "\n");
3645}
3646
Sebastian Redl2c499f62010-08-18 23:56:43 +00003647void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00003648 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003649 S.ExternalSource = this;
3650
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003651 // Makes sure any declarations that were deserialized "too early"
3652 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003653 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3654 if (SemaObj->TUScope)
John McCall48871652010-08-21 09:40:31 +00003655 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003656
3657 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003658 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003659 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00003660
3661 // If there were any tentative definitions, deserialize them and add
Sebastian Redl35351a92010-01-31 22:27:38 +00003662 // them to Sema's list of tentative definitions.
Douglas Gregord4df8652009-04-22 22:02:47 +00003663 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3664 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redl35351a92010-01-31 22:27:38 +00003665 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregord4df8652009-04-22 22:02:47 +00003666 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00003667
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003668 // If there were any unused file scoped decls, deserialize them and add to
3669 // Sema's list of unused file scoped decls.
3670 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3671 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3672 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattner90073802010-02-12 00:07:30 +00003673 }
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003674
3675 // If there were any locally-scoped external declarations,
3676 // deserialize them and add them to Sema's table of locally-scoped
3677 // external declarations.
3678 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3679 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3680 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3681 }
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003682
3683 // If there were any ext_vector type declarations, deserialize them
3684 // and add them to Sema's vector of such declarations.
3685 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3686 SemaObj->ExtVectorDecls.push_back(
3687 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003688
3689 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3690 // Can we cut them down before writing them ?
3691
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003692 // If there were any dynamic classes declarations, deserialize them
3693 // and add them to Sema's vector of such declarations.
3694 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3695 SemaObj->DynamicClasses.push_back(
3696 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003697
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003698 // Load the offsets of the declarations that Sema references.
3699 // They will be lazily deserialized when needed.
3700 if (!SemaDeclRefs.empty()) {
3701 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3702 SemaObj->StdNamespace = SemaDeclRefs[0];
3703 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3704 }
3705
Sebastian Redl2c373b92010-10-05 15:59:54 +00003706 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
3707
3708 // If there are @selector references added them to its pool. This is for
3709 // implementation of -Wselector.
3710 if (!F->ReferencedSelectorsData.empty()) {
3711 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
3712 unsigned I = 0;
3713 while (I < DataSize) {
3714 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
3715 SourceLocation SelLoc = ReadSourceLocation(
3716 *F, F->ReferencedSelectorsData, I);
3717 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3718 }
3719 }
3720
3721 // If there were any pending implicit instantiations, deserialize them
3722 // and add them to Sema's queue of such instantiations.
3723 assert(F->PendingInstantiations.size() % 2 == 0 &&
3724 "Expected pairs of entries");
3725 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
3726 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
3727 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
3728 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
3729 }
3730 }
3731
3732 // The two special data sets below always come from the most recent PCH,
3733 // which is at the front of the chain.
3734 PerFileData &F = *Chain.front();
3735
3736 // If there were any weak undeclared identifiers, deserialize them and add to
3737 // Sema's list of weak undeclared identifiers.
3738 if (!WeakUndeclaredIdentifiers.empty()) {
3739 unsigned Idx = 0;
3740 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3741 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3742 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3743 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
3744 bool Used = WeakUndeclaredIdentifiers[Idx++];
3745 Sema::WeakInfo WI(AliasId, Loc);
3746 WI.setUsed(Used);
3747 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3748 }
3749 }
3750
3751 // If there were any VTable uses, deserialize the information and add it
3752 // to Sema's vector and map of VTable uses.
3753 if (!VTableUses.empty()) {
3754 unsigned Idx = 0;
3755 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3756 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3757 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
3758 bool DefinitionRequired = VTableUses[Idx++];
3759 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3760 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003761 }
3762 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00003763}
3764
Sebastian Redl2c499f62010-08-18 23:56:43 +00003765IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redl78f51772010-08-02 18:30:12 +00003766 // Try to find this name within our on-disk hash tables. We start with the
3767 // most recent one, since that one contains the most up-to-date info.
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003768 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003769 ASTIdentifierLookupTable *IdTable
3770 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003771 if (!IdTable)
3772 continue;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003773 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003774 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003775 if (Pos == IdTable->end())
3776 continue;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003777
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003778 // Dereferencing the iterator has the effect of building the
3779 // IdentifierInfo node and populating it with the various
3780 // declarations it needs.
Sebastian Redl78f51772010-08-02 18:30:12 +00003781 return *Pos;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003782 }
Sebastian Redl78f51772010-08-02 18:30:12 +00003783 return 0;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003784}
3785
Douglas Gregor57756ea2010-10-14 22:11:03 +00003786namespace clang {
3787 /// \brief An identifier-lookup iterator that enumerates all of the
3788 /// identifiers stored within a set of AST files.
3789 class ASTIdentifierIterator : public IdentifierIterator {
3790 /// \brief The AST reader whose identifiers are being enumerated.
3791 const ASTReader &Reader;
3792
3793 /// \brief The current index into the chain of AST files stored in
3794 /// the AST reader.
3795 unsigned Index;
3796
3797 /// \brief The current position within the identifier lookup table
3798 /// of the current AST file.
3799 ASTIdentifierLookupTable::key_iterator Current;
3800
3801 /// \brief The end position within the identifier lookup table of
3802 /// the current AST file.
3803 ASTIdentifierLookupTable::key_iterator End;
3804
3805 public:
3806 explicit ASTIdentifierIterator(const ASTReader &Reader);
3807
3808 virtual llvm::StringRef Next();
3809 };
3810}
3811
3812ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
3813 : Reader(Reader), Index(Reader.Chain.size() - 1) {
3814 ASTIdentifierLookupTable *IdTable
3815 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3816 Current = IdTable->key_begin();
3817 End = IdTable->key_end();
3818}
3819
3820llvm::StringRef ASTIdentifierIterator::Next() {
3821 while (Current == End) {
3822 // If we have exhausted all of our AST files, we're done.
3823 if (Index == 0)
3824 return llvm::StringRef();
3825
3826 --Index;
3827 ASTIdentifierLookupTable *IdTable
3828 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3829 Current = IdTable->key_begin();
3830 End = IdTable->key_end();
3831 }
3832
3833 // We have any identifiers remaining in the current AST file; return
3834 // the next one.
3835 std::pair<const char*, unsigned> Key = *Current;
3836 ++Current;
3837 return llvm::StringRef(Key.first, Key.second);
3838}
3839
3840IdentifierIterator *ASTReader::getIdentifiers() const {
3841 return new ASTIdentifierIterator(*this);
3842}
3843
Mike Stump11289f42009-09-09 15:08:12 +00003844std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redl2c499f62010-08-18 23:56:43 +00003845ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redlada023c2010-08-04 20:40:17 +00003846 // Find this selector in a hash table. We want to find the most recent entry.
3847 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3848 PerFileData &F = *Chain[I];
3849 if (!F.SelectorLookupTable)
3850 continue;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003851
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003852 ASTSelectorLookupTable *PoolTable
3853 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3854 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redlada023c2010-08-04 20:40:17 +00003855 if (Pos != PoolTable->end()) {
3856 ++NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003857 // FIXME: Not quite happy with the statistics here. We probably should
3858 // disable this tracking when called via LoadSelector.
3859 // Also, should entries without methods count as misses?
3860 ++NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003861 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redlada023c2010-08-04 20:40:17 +00003862 if (DeserializationListener)
3863 DeserializationListener->SelectorRead(Data.ID, Sel);
3864 return std::make_pair(Data.Instance, Data.Factory);
3865 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003866 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003867
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003868 ++NumMethodPoolMisses;
Sebastian Redlada023c2010-08-04 20:40:17 +00003869 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorc78d3462009-04-24 21:10:55 +00003870}
3871
Sebastian Redl2c499f62010-08-18 23:56:43 +00003872void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003873 // It would be complicated to avoid reading the methods anyway. So don't.
3874 ReadMethodPool(Sel);
3875}
3876
Sebastian Redl2c499f62010-08-18 23:56:43 +00003877void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00003878 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00003879 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00003880 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00003881 if (DeserializationListener)
3882 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003883}
3884
Douglas Gregor1342e842009-07-06 18:54:52 +00003885/// \brief Set the globally-visible declarations associated with the given
3886/// identifier.
3887///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003888/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00003889/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00003890/// them.
3891///
3892/// \param II an IdentifierInfo that refers to one or more globally-visible
3893/// declarations.
3894///
3895/// \param DeclIDs the set of declaration IDs with the name @p II that are
3896/// visible at global scope.
3897///
3898/// \param Nonrecursive should be true to indicate that the caller knows that
3899/// this call is non-recursive, and therefore the globally-visible declarations
3900/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00003901void
Sebastian Redl2c499f62010-08-18 23:56:43 +00003902ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregor1342e842009-07-06 18:54:52 +00003903 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
3904 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003905 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00003906 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
3907 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
3908 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003909 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00003910 return;
3911 }
Mike Stump11289f42009-09-09 15:08:12 +00003912
Douglas Gregor1342e842009-07-06 18:54:52 +00003913 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
3914 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
3915 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00003916 if (SemaObj->TUScope) {
3917 // Introduce this declaration into the translation-unit scope
3918 // and add it to the declaration chain for this identifier, so
3919 // that (unqualified) name lookup will find it.
John McCall48871652010-08-21 09:40:31 +00003920 SemaObj->TUScope->AddDecl(D);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00003921 }
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003922 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregor1342e842009-07-06 18:54:52 +00003923 } else {
3924 // Queue this declaration so that it will be added to the
3925 // translation unit scope and identifier's declaration chain
3926 // once a Sema object is known.
3927 PreloadedDecls.push_back(D);
3928 }
3929 }
3930}
3931
Sebastian Redl2c499f62010-08-18 23:56:43 +00003932IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003933 if (ID == 0)
3934 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00003935
Sebastian Redlc713b962010-07-21 00:46:22 +00003936 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003937 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003938 return 0;
3939 }
Mike Stump11289f42009-09-09 15:08:12 +00003940
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003941 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc713b962010-07-21 00:46:22 +00003942 ID -= 1;
3943 if (!IdentifiersLoaded[ID]) {
3944 unsigned Index = ID;
3945 const char *Str = 0;
3946 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3947 PerFileData *F = Chain[N - I - 1];
3948 if (Index < F->LocalNumIdentifiers) {
3949 uint32_t Offset = F->IdentifierOffsets[Index];
3950 Str = F->IdentifierTableData + Offset;
3951 break;
3952 }
3953 Index -= F->LocalNumIdentifiers;
3954 }
3955 assert(Str && "Broken Chain");
Douglas Gregor5287b4e2009-04-25 21:04:17 +00003956
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003957 // All of the strings in the AST file are preceded by a 16-bit length.
3958 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00003959 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
3960 // unsigned integers. This is important to avoid integer overflow when
3961 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00003962 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00003963 unsigned StrLen = (((unsigned) StrLenPtr[0])
3964 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00003965 IdentifiersLoaded[ID]
Kovarththanan Rajaratnama3b09592010-03-12 10:32:27 +00003966 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003967 if (DeserializationListener)
3968 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003969 }
Mike Stump11289f42009-09-09 15:08:12 +00003970
Sebastian Redlc713b962010-07-21 00:46:22 +00003971 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003972}
3973
Sebastian Redl2c499f62010-08-18 23:56:43 +00003974void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00003975 ReadSLocEntryRecord(ID);
3976}
3977
Sebastian Redl2c499f62010-08-18 23:56:43 +00003978Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00003979 if (ID == 0)
3980 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00003981
Sebastian Redlada023c2010-08-04 20:40:17 +00003982 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003983 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00003984 return Selector();
3985 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003986
Sebastian Redlada023c2010-08-04 20:40:17 +00003987 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003988 // Load this selector from the selector table.
Sebastian Redlada023c2010-08-04 20:40:17 +00003989 unsigned Idx = ID - 1;
3990 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3991 PerFileData &F = *Chain[N - I - 1];
3992 if (Idx < F.LocalNumSelectors) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003993 ASTSelectorLookupTrait Trait(*this);
Sebastian Redlada023c2010-08-04 20:40:17 +00003994 SelectorsLoaded[ID - 1] =
3995 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
3996 if (DeserializationListener)
3997 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
3998 break;
3999 }
4000 Idx -= F.LocalNumSelectors;
4001 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004002 }
4003
Sebastian Redlada023c2010-08-04 20:40:17 +00004004 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00004005}
4006
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004007Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00004008 return DecodeSelector(ID);
4009}
4010
Sebastian Redl2c499f62010-08-18 23:56:43 +00004011uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00004012 // ID 0 (the null selector) is considered an external selector.
4013 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00004014}
4015
Mike Stump11289f42009-09-09 15:08:12 +00004016DeclarationName
Sebastian Redl2c499f62010-08-18 23:56:43 +00004017ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004018 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4019 switch (Kind) {
4020 case DeclarationName::Identifier:
4021 return DeclarationName(GetIdentifierInfo(Record, Idx));
4022
4023 case DeclarationName::ObjCZeroArgSelector:
4024 case DeclarationName::ObjCOneArgSelector:
4025 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff3c301dc2009-04-23 15:15:40 +00004026 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004027
4028 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004029 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004030 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004031
4032 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004033 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004034 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004035
4036 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004037 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004038 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004039
4040 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004041 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004042 (OverloadedOperatorKind)Record[Idx++]);
4043
Alexis Hunt3d221f22009-11-29 07:34:05 +00004044 case DeclarationName::CXXLiteralOperatorName:
4045 return Context->DeclarationNames.getCXXLiteralOperatorName(
4046 GetIdentifierInfo(Record, Idx));
4047
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004048 case DeclarationName::CXXUsingDirective:
4049 return DeclarationName::getUsingDirectiveName();
4050 }
4051
4052 // Required to silence GCC warning
4053 return DeclarationName();
4054}
Douglas Gregor55abb232009-04-10 20:39:37 +00004055
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004056void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4057 DeclarationNameLoc &DNLoc,
4058 DeclarationName Name,
4059 const RecordData &Record, unsigned &Idx) {
4060 switch (Name.getNameKind()) {
4061 case DeclarationName::CXXConstructorName:
4062 case DeclarationName::CXXDestructorName:
4063 case DeclarationName::CXXConversionFunctionName:
4064 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4065 break;
4066
4067 case DeclarationName::CXXOperatorName:
4068 DNLoc.CXXOperatorName.BeginOpNameLoc
4069 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4070 DNLoc.CXXOperatorName.EndOpNameLoc
4071 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4072 break;
4073
4074 case DeclarationName::CXXLiteralOperatorName:
4075 DNLoc.CXXLiteralOperatorName.OpNameLoc
4076 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4077 break;
4078
4079 case DeclarationName::Identifier:
4080 case DeclarationName::ObjCZeroArgSelector:
4081 case DeclarationName::ObjCOneArgSelector:
4082 case DeclarationName::ObjCMultiArgSelector:
4083 case DeclarationName::CXXUsingDirective:
4084 break;
4085 }
4086}
4087
4088void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4089 DeclarationNameInfo &NameInfo,
4090 const RecordData &Record, unsigned &Idx) {
4091 NameInfo.setName(ReadDeclarationName(Record, Idx));
4092 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4093 DeclarationNameLoc DNLoc;
4094 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4095 NameInfo.setInfo(DNLoc);
4096}
4097
4098void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4099 const RecordData &Record, unsigned &Idx) {
4100 Info.NNS = ReadNestedNameSpecifier(Record, Idx);
4101 Info.NNSRange = ReadSourceRange(F, Record, Idx);
4102 unsigned NumTPLists = Record[Idx++];
4103 Info.NumTemplParamLists = NumTPLists;
4104 if (NumTPLists) {
4105 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4106 for (unsigned i=0; i != NumTPLists; ++i)
4107 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4108 }
4109}
4110
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004111TemplateName
Sebastian Redl2c499f62010-08-18 23:56:43 +00004112ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004113 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004114 switch (Kind) {
4115 case TemplateName::Template:
4116 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4117
4118 case TemplateName::OverloadedTemplate: {
4119 unsigned size = Record[Idx++];
4120 UnresolvedSet<8> Decls;
4121 while (size--)
4122 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4123
4124 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4125 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004126
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004127 case TemplateName::QualifiedTemplate: {
4128 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4129 bool hasTemplKeyword = Record[Idx++];
4130 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4131 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4132 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004133
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004134 case TemplateName::DependentTemplate: {
4135 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4136 if (Record[Idx++]) // isIdentifier
4137 return Context->getDependentTemplateName(NNS,
4138 GetIdentifierInfo(Record, Idx));
4139 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004140 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004141 }
4142 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004143
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004144 assert(0 && "Unhandled template name kind!");
4145 return TemplateName();
4146}
4147
4148TemplateArgument
Sebastian Redl2c373b92010-10-05 15:59:54 +00004149ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00004150 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004151 switch ((TemplateArgument::ArgKind)Record[Idx++]) {
4152 case TemplateArgument::Null:
4153 return TemplateArgument();
4154 case TemplateArgument::Type:
4155 return TemplateArgument(GetType(Record[Idx++]));
4156 case TemplateArgument::Declaration:
4157 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00004158 case TemplateArgument::Integral: {
4159 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4160 QualType T = GetType(Record[Idx++]);
4161 return TemplateArgument(Value, T);
4162 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004163 case TemplateArgument::Template:
4164 return TemplateArgument(ReadTemplateName(Record, Idx));
4165 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004166 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004167 case TemplateArgument::Pack: {
4168 unsigned NumArgs = Record[Idx++];
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004169 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4170 for (unsigned I = 0; I != NumArgs; ++I)
4171 Args[I] = ReadTemplateArgument(F, Record, Idx);
4172 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004173 }
4174 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004175
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004176 assert(0 && "Unhandled template argument kind!");
4177 return TemplateArgument();
4178}
4179
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004180TemplateParameterList *
Sebastian Redl2c373b92010-10-05 15:59:54 +00004181ASTReader::ReadTemplateParameterList(PerFileData &F,
4182 const RecordData &Record, unsigned &Idx) {
4183 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4184 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4185 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004186
4187 unsigned NumParams = Record[Idx++];
4188 llvm::SmallVector<NamedDecl *, 16> Params;
4189 Params.reserve(NumParams);
4190 while (NumParams--)
4191 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004192
4193 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004194 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4195 Params.data(), Params.size(), RAngleLoc);
4196 return TemplateParams;
4197}
4198
4199void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004200ASTReader::
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004201ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004202 PerFileData &F, const RecordData &Record,
4203 unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004204 unsigned NumTemplateArgs = Record[Idx++];
4205 TemplArgs.reserve(NumTemplateArgs);
4206 while (NumTemplateArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004207 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004208}
4209
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004210/// \brief Read a UnresolvedSet structure.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004211void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004212 const RecordData &Record, unsigned &Idx) {
4213 unsigned NumDecls = Record[Idx++];
4214 while (NumDecls--) {
4215 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4216 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4217 Set.addDecl(D, AS);
4218 }
4219}
4220
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004221CXXBaseSpecifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00004222ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky19b9f952010-07-26 16:56:01 +00004223 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004224 bool isVirtual = static_cast<bool>(Record[Idx++]);
4225 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4226 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004227 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4228 SourceRange Range = ReadSourceRange(F, Record, Idx);
Nick Lewycky19b9f952010-07-26 16:56:01 +00004229 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004230}
4231
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004232std::pair<CXXBaseOrMemberInitializer **, unsigned>
Sebastian Redl2c373b92010-10-05 15:59:54 +00004233ASTReader::ReadCXXBaseOrMemberInitializers(PerFileData &F,
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004234 const RecordData &Record,
4235 unsigned &Idx) {
4236 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0;
4237 unsigned NumInitializers = Record[Idx++];
4238 if (NumInitializers) {
4239 ASTContext &C = *getContext();
4240
4241 BaseOrMemberInitializers
4242 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
4243 for (unsigned i=0; i != NumInitializers; ++i) {
4244 TypeSourceInfo *BaseClassInfo = 0;
4245 bool IsBaseVirtual = false;
4246 FieldDecl *Member = 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004247
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004248 bool IsBaseInitializer = Record[Idx++];
4249 if (IsBaseInitializer) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004250 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004251 IsBaseVirtual = Record[Idx++];
4252 } else {
4253 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
4254 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00004255 SourceLocation MemberLoc = ReadSourceLocation(F, Record, Idx);
4256 Expr *Init = ReadExpr(F);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004257 FieldDecl *AnonUnionMember
4258 = cast_or_null<FieldDecl>(GetDecl(Record[Idx++]));
Sebastian Redl2c373b92010-10-05 15:59:54 +00004259 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4260 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004261 bool IsWritten = Record[Idx++];
4262 unsigned SourceOrderOrNumArrayIndices;
4263 llvm::SmallVector<VarDecl *, 8> Indices;
4264 if (IsWritten) {
4265 SourceOrderOrNumArrayIndices = Record[Idx++];
4266 } else {
4267 SourceOrderOrNumArrayIndices = Record[Idx++];
4268 Indices.reserve(SourceOrderOrNumArrayIndices);
4269 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4270 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4271 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004272
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004273 CXXBaseOrMemberInitializer *BOMInit;
4274 if (IsBaseInitializer) {
4275 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
4276 IsBaseVirtual, LParenLoc,
4277 Init, RParenLoc);
4278 } else if (IsWritten) {
4279 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc,
4280 LParenLoc, Init, RParenLoc);
4281 } else {
4282 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc,
4283 LParenLoc, Init, RParenLoc,
4284 Indices.data(),
4285 Indices.size());
4286 }
4287
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00004288 if (IsWritten)
4289 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004290 BOMInit->setAnonUnionMember(AnonUnionMember);
4291 BaseOrMemberInitializers[i] = BOMInit;
4292 }
4293 }
4294
4295 return std::make_pair(BaseOrMemberInitializers, NumInitializers);
4296}
4297
Chris Lattnerca025db2010-05-07 21:43:38 +00004298NestedNameSpecifier *
Sebastian Redl2c499f62010-08-18 23:56:43 +00004299ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004300 unsigned N = Record[Idx++];
4301 NestedNameSpecifier *NNS = 0, *Prev = 0;
4302 for (unsigned I = 0; I != N; ++I) {
4303 NestedNameSpecifier::SpecifierKind Kind
4304 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4305 switch (Kind) {
4306 case NestedNameSpecifier::Identifier: {
4307 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4308 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4309 break;
4310 }
4311
4312 case NestedNameSpecifier::Namespace: {
4313 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4314 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4315 break;
4316 }
4317
4318 case NestedNameSpecifier::TypeSpec:
4319 case NestedNameSpecifier::TypeSpecWithTemplate: {
4320 Type *T = GetType(Record[Idx++]).getTypePtr();
4321 bool Template = Record[Idx++];
4322 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4323 break;
4324 }
4325
4326 case NestedNameSpecifier::Global: {
4327 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4328 // No associated value, and there can't be a prefix.
4329 break;
4330 }
Chris Lattnerca025db2010-05-07 21:43:38 +00004331 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00004332 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00004333 }
4334 return NNS;
4335}
4336
4337SourceRange
Sebastian Redl2c373b92010-10-05 15:59:54 +00004338ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4339 unsigned &Idx) {
4340 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4341 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00004342 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00004343}
4344
Douglas Gregor1daeb692009-04-13 18:14:40 +00004345/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004346llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004347 unsigned BitWidth = Record[Idx++];
4348 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4349 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4350 Idx += NumWords;
4351 return Result;
4352}
4353
4354/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004355llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004356 bool isUnsigned = Record[Idx++];
4357 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4358}
4359
Douglas Gregore0a3a512009-04-14 21:55:33 +00004360/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004361llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004362 return llvm::APFloat(ReadAPInt(Record, Idx));
4363}
4364
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004365// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00004366std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004367 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00004368 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004369 Idx += Len;
4370 return Result;
4371}
4372
Sebastian Redl2c499f62010-08-18 23:56:43 +00004373CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00004374 unsigned &Idx) {
4375 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4376 return CXXTemporary::Create(*Context, Decl);
4377}
4378
Sebastian Redl2c499f62010-08-18 23:56:43 +00004379DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00004380 return Diag(SourceLocation(), DiagID);
4381}
4382
Sebastian Redl2c499f62010-08-18 23:56:43 +00004383DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004384 return Diags.Report(Loc, DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00004385}
Douglas Gregora9af1d12009-04-17 00:04:06 +00004386
Douglas Gregora868bbd2009-04-21 22:25:48 +00004387/// \brief Retrieve the identifier table associated with the
4388/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004389IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004390 assert(PP && "Forgot to set Preprocessor ?");
4391 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00004392}
4393
Douglas Gregora9af1d12009-04-17 00:04:06 +00004394/// \brief Record that the given ID maps to the given switch-case
4395/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004396void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004397 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4398 SwitchCaseStmts[ID] = SC;
4399}
4400
4401/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004402SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004403 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4404 return SwitchCaseStmts[ID];
4405}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004406
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00004407void ASTReader::ClearSwitchCaseIDs() {
4408 SwitchCaseStmts.clear();
4409}
4410
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004411/// \brief Record that the given label statement has been
4412/// deserialized and has the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004413void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump11289f42009-09-09 15:08:12 +00004414 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004415 "Deserialized label twice");
4416 LabelStmts[ID] = S;
4417
4418 // If we've already seen any goto statements that point to this
4419 // label, resolve them now.
4420 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
4421 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
4422 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
4423 Goto->second->setLabel(S);
4424 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor779d8652009-04-17 18:58:21 +00004425
4426 // If we've already seen any address-label statements that point to
4427 // this label, resolve them now.
4428 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump11289f42009-09-09 15:08:12 +00004429 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor779d8652009-04-17 18:58:21 +00004430 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump11289f42009-09-09 15:08:12 +00004431 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor779d8652009-04-17 18:58:21 +00004432 AddrLabel != AddrLabels.second; ++AddrLabel)
4433 AddrLabel->second->setLabel(S);
4434 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004435}
4436
4437/// \brief Set the label of the given statement to the label
4438/// identified by ID.
4439///
4440/// Depending on the order in which the label and other statements
4441/// referencing that label occur, this operation may complete
4442/// immediately (updating the statement) or it may queue the
4443/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004444void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004445 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4446 if (Label != LabelStmts.end()) {
4447 // We've already seen this label, so set the label of the goto and
4448 // we're done.
4449 S->setLabel(Label->second);
4450 } else {
4451 // We haven't seen this label yet, so add this goto to the set of
4452 // unresolved goto statements.
4453 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
4454 }
4455}
Douglas Gregor779d8652009-04-17 18:58:21 +00004456
4457/// \brief Set the label of the given expression to the label
4458/// identified by ID.
4459///
4460/// Depending on the order in which the label and other statements
4461/// referencing that label occur, this operation may complete
4462/// immediately (updating the statement) or it may queue the
4463/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004464void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor779d8652009-04-17 18:58:21 +00004465 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4466 if (Label != LabelStmts.end()) {
4467 // We've already seen this label, so set the label of the
4468 // label-address expression and we're done.
4469 S->setLabel(Label->second);
4470 } else {
4471 // We haven't seen this label yet, so add this label-address
4472 // expression to the set of unresolved label-address expressions.
4473 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
4474 }
4475}
Douglas Gregor1342e842009-07-06 18:54:52 +00004476
Sebastian Redl2c499f62010-08-18 23:56:43 +00004477void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004478 assert(NumCurrentElementsDeserializing &&
4479 "FinishedDeserializing not paired with StartedDeserializing");
4480 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004481 // If any identifiers with corresponding top-level declarations have
4482 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004483 while (!PendingIdentifierInfos.empty()) {
4484 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4485 PendingIdentifierInfos.front().DeclIDs, true);
4486 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00004487 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004488
4489 // We are not in recursive loading, so it's safe to pass the "interesting"
4490 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004491 if (Consumer)
4492 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidisad5f95c2010-10-24 17:26:31 +00004493
4494 assert(PendingForwardRefs.size() == 0 &&
4495 "Some forward refs did not get linked to the definition!");
Douglas Gregor1342e842009-07-06 18:54:52 +00004496 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004497 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00004498}
Douglas Gregorb473b072010-08-19 00:28:17 +00004499
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004500ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
4501 const char *isysroot, bool DisableValidation)
4502 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4503 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4504 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4505 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
4506 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00004507 TotalNumSLocEntries(0), NextSLocOffset(0), NumStatementsRead(0),
4508 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
4509 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004510 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4511 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4512 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
4513 RelocatablePCH = false;
4514}
4515
4516ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4517 Diagnostic &Diags, const char *isysroot,
4518 bool DisableValidation)
4519 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4520 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
4521 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
4522 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00004523 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4524 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4525 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4526 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4527 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4528 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004529 RelocatablePCH = false;
4530}
4531
4532ASTReader::~ASTReader() {
4533 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4534 delete Chain[e - i - 1];
4535 // Delete all visible decl lookup tables
4536 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4537 E = DeclContextOffsets.end();
4538 I != E; ++I) {
4539 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4540 J != F; ++J) {
4541 if (J->NameLookupTableData)
4542 delete static_cast<ASTDeclContextNameLookupTable*>(
4543 J->NameLookupTableData);
4544 }
4545 }
4546 for (DeclContextVisibleUpdatesPending::iterator
4547 I = PendingVisibleUpdates.begin(),
4548 E = PendingVisibleUpdates.end();
4549 I != E; ++I) {
4550 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4551 F = I->second.end();
4552 J != F; ++J)
4553 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4554 }
4555}
4556
Sebastian Redl009e7f22010-10-05 16:15:19 +00004557ASTReader::PerFileData::PerFileData(ASTFileType Ty)
4558 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00004559 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
4560 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
4561 MacroDefinitionOffsets(0), LocalNumSelectors(0), SelectorOffsets(0),
4562 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004563 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
4564 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redl3f6b7532010-10-01 19:59:12 +00004565 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregorb473b072010-08-19 00:28:17 +00004566{}
4567
4568ASTReader::PerFileData::~PerFileData() {
4569 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
4570 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4571}
4572