blob: adb233ac144a24d3b1e18bcc8ba2f542ee260ec7 [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"
Douglas Gregorbfbde532009-04-10 21:16:55 +000036#include "clang/Basic/TargetInfo.h"
Douglas Gregord54f3a12009-10-05 21:07:28 +000037#include "clang/Basic/Version.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000038#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000039#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000040#include "llvm/Support/MemoryBuffer.h"
John McCall0ad16662009-10-29 08:12:44 +000041#include "llvm/Support/ErrorHandling.h"
Daniel Dunbarf2ce9a22009-11-18 19:50:41 +000042#include "llvm/System/Path.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000043#include <algorithm>
Douglas Gregorc379c072009-04-28 18:58:38 +000044#include <iterator>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000045#include <cstdio>
Douglas Gregorc5046832009-04-27 18:38:38 +000046#include <sys/stat.h>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000047using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000048using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000049
50//===----------------------------------------------------------------------===//
Sebastian Redld44cd6a2010-08-18 23:57:06 +000051// PCH validator implementation
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000052//===----------------------------------------------------------------------===//
53
Sebastian Redl3e31c722010-08-18 23:56:56 +000054ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000055
56bool
57PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
58 const LangOptions &PPLangOpts = PP.getLangOptions();
59#define PARSE_LANGOPT_BENIGN(Option)
60#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
61 if (PPLangOpts.Option != LangOpts.Option) { \
62 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
63 return true; \
64 }
65
66 PARSE_LANGOPT_BENIGN(Trigraphs);
67 PARSE_LANGOPT_BENIGN(BCPLComment);
68 PARSE_LANGOPT_BENIGN(DollarIdents);
69 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
70 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carruthe03aa552010-04-17 20:17:31 +000071 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000072 PARSE_LANGOPT_BENIGN(ImplicitInt);
73 PARSE_LANGOPT_BENIGN(Digraphs);
74 PARSE_LANGOPT_BENIGN(HexFloats);
75 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
76 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
77 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
78 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
79 PARSE_LANGOPT_BENIGN(CXXOperatorName);
80 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
81 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
82 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian45878032010-02-09 19:31:38 +000083 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanian62c56022010-04-22 21:01:59 +000084 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
85 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000086 PARSE_LANGOPT_BENIGN(PascalStrings);
87 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump11289f42009-09-09 15:08:12 +000088 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000089 diag::warn_pch_lax_vector_conversions);
Nate Begeman9d905792009-06-25 22:57:40 +000090 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000091 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +000092 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000093 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
94 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
95 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump11289f42009-09-09 15:08:12 +000096 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000097 diag::warn_pch_thread_safe_statics);
Daniel Dunbara77eaeb2009-09-03 04:54:28 +000098 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000099 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
100 PARSE_LANGOPT_BENIGN(EmitAllDecls);
101 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattner51924e512010-06-26 21:25:03 +0000102 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump11289f42009-09-09 15:08:12 +0000103 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000104 diag::warn_pch_heinous_extensions);
105 // FIXME: Most of the options below are benign if the macro wasn't
106 // used. Unfortunately, this means that a PCH compiled without
107 // optimization can't be used with optimization turned on, even
108 // though the only thing that changes is whether __OPTIMIZE__ was
109 // defined... but if __OPTIMIZE__ never showed up in the header, it
110 // doesn't matter. We could consider making this some special kind
111 // of check.
112 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
113 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
114 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
115 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
116 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
117 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
118 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
119 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsoned4e2952009-11-05 20:14:16 +0000120 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000121 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump11289f42009-09-09 15:08:12 +0000122 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000123 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
124 return true;
125 }
126 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000127 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
128 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000129 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman9d905792009-06-25 22:57:40 +0000130 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Mike Stumpd9546382009-12-12 01:27:46 +0000131 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbar143021e2009-09-21 04:16:19 +0000132 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000133 PARSE_LANGOPT_BENIGN(SpellChecking);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +0000134#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000135#undef PARSE_LANGOPT_BENIGN
136
137 return false;
138}
139
Daniel Dunbar20a682d2009-11-11 00:52:11 +0000140bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
141 if (Triple == PP.getTargetInfo().getTriple().str())
142 return false;
143
144 Reader.Diag(diag::warn_pch_target_triple)
145 << Triple << PP.getTargetInfo().getTriple().str();
146 return true;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000147}
148
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000149struct EmptyStringRef {
Benjamin Kramer8d5609b2010-07-14 23:19:41 +0000150 bool operator ()(llvm::StringRef r) const { return r.empty(); }
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000151};
152struct EmptyBlock {
153 bool operator ()(const PCHPredefinesBlock &r) const { return r.Data.empty(); }
154};
155
156static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
157 PCHPredefinesBlocks R) {
158 // First, sum up the lengths.
159 unsigned LL = 0, RL = 0;
160 for (unsigned I = 0, N = L.size(); I != N; ++I) {
161 LL += L[I].size();
162 }
163 for (unsigned I = 0, N = R.size(); I != N; ++I) {
164 RL += R[I].Data.size();
165 }
166 if (LL != RL)
167 return false;
168 if (LL == 0 && RL == 0)
169 return true;
170
171 // Kick out empty parts, they confuse the algorithm below.
172 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
173 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
174
175 // Do it the hard way. At this point, both vectors must be non-empty.
176 llvm::StringRef LR = L[0], RR = R[0].Data;
177 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbar01ad0a72010-07-16 00:00:11 +0000178 (void) RN;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000179 for (;;) {
180 // Compare the current pieces.
181 if (LR.size() == RR.size()) {
182 // If they're the same length, it's pretty easy.
183 if (LR != RR)
184 return false;
185 // Both pieces are done, advance.
186 ++LI;
187 ++RI;
188 // If either string is done, they're both done, since they're the same
189 // length.
190 if (LI == LN) {
191 assert(RI == RN && "Strings not the same length after all?");
192 return true;
193 }
194 LR = L[LI];
195 RR = R[RI].Data;
196 } else if (LR.size() < RR.size()) {
197 // Right piece is longer.
198 if (!RR.startswith(LR))
199 return false;
200 ++LI;
201 assert(LI != LN && "Strings not the same length after all?");
202 RR = RR.substr(LR.size());
203 LR = L[LI];
204 } else {
205 // Left piece is longer.
206 if (!LR.startswith(RR))
207 return false;
208 ++RI;
209 assert(RI != RN && "Strings not the same length after all?");
210 LR = LR.substr(RR.size());
211 RR = R[RI].Data;
212 }
213 }
214}
215
216static std::pair<FileID, llvm::StringRef::size_type>
217FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
218 std::pair<FileID, llvm::StringRef::size_type> Res;
219 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
220 Res.second = Buffers[I].Data.find(MacroDef);
221 if (Res.second != llvm::StringRef::npos) {
222 Res.first = Buffers[I].BufferID;
223 break;
224 }
225 }
226 return Res;
227}
228
229bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000230 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000231 std::string &SuggestedPredefines) {
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000232 // We are in the context of an implicit include, so the predefines buffer will
233 // have a #include entry for the PCH file itself (as normalized by the
234 // preprocessor initialization). Find it and skip over it in the checking
235 // below.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000236 llvm::SmallString<256> PCHInclude;
237 PCHInclude += "#include \"";
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000238 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000239 PCHInclude += "\"\n";
240 std::pair<llvm::StringRef,llvm::StringRef> Split =
241 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
242 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000243 if (Left == PP.getPredefines()) {
244 Error("Missing PCH include entry!");
245 return true;
246 }
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000247
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000248 // If the concatenation of all the PCH buffers is equal to the adjusted
249 // command line, we're done.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000250 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
251 CommandLine.push_back(Left);
252 CommandLine.push_back(Right);
253 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000254 return false;
255
256 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000257
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000258 // The predefines buffers are different. Determine what the differences are,
259 // and whether they require us to reject the PCH file.
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000260 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000261 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
262 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000263
264 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
265 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000266
267 // Pick out implicit #includes after the PCH and don't consider them for
268 // validation; we will insert them into SuggestedPredefines so that the
269 // preprocessor includes them.
270 std::string IncludesAfterPCH;
271 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
272 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
273 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
274 if (AfterPCHLines[i].startswith("#include ")) {
275 IncludesAfterPCH += AfterPCHLines[i];
276 IncludesAfterPCH += '\n';
277 } else {
278 CmdLineLines.push_back(AfterPCHLines[i]);
279 }
280 }
281
282 // Make sure we add the includes last into SuggestedPredefines before we
283 // exit this function.
284 struct AddIncludesRAII {
285 std::string &SuggestedPredefines;
286 std::string &IncludesAfterPCH;
287
288 AddIncludesRAII(std::string &SuggestedPredefines,
289 std::string &IncludesAfterPCH)
290 : SuggestedPredefines(SuggestedPredefines),
291 IncludesAfterPCH(IncludesAfterPCH) { }
292 ~AddIncludesRAII() {
293 SuggestedPredefines += IncludesAfterPCH;
294 }
295 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000296
Daniel Dunbar499baed2009-11-11 05:26:28 +0000297 // Sort both sets of predefined buffer lines, since we allow some extra
298 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000299 std::sort(CmdLineLines.begin(), CmdLineLines.end());
300 std::sort(PCHLines.begin(), PCHLines.end());
301
Daniel Dunbar499baed2009-11-11 05:26:28 +0000302 // Determine which predefines that were used to build the PCH file are missing
303 // from the command line.
304 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000305 std::set_difference(PCHLines.begin(), PCHLines.end(),
306 CmdLineLines.begin(), CmdLineLines.end(),
307 std::back_inserter(MissingPredefines));
308
309 bool MissingDefines = false;
310 bool ConflictingDefines = false;
311 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000312 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000313 if (Missing.startswith("#include ")) {
314 // An -include was specified when generating the PCH; it is included in
315 // the PCH, just ignore it.
316 continue;
317 }
Daniel Dunbar499baed2009-11-11 05:26:28 +0000318 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000319 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
320 return true;
321 }
Mike Stump11289f42009-09-09 15:08:12 +0000322
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000323 // This is a macro definition. Determine the name of the macro we're
324 // defining.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000325 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000326 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000327 = Missing.find_first_of("( \n\r", StartOfMacroName);
328 assert(EndOfMacroName != std::string::npos &&
329 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000330 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000331
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000332 // Determine whether this macro was given a different definition on the
333 // command line.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000334 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000335 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000336 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000337 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
338 MacroDefStart);
339 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000340 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000341 // Different macro; we're done.
342 ConflictPos = CmdLineLines.end();
Mike Stump11289f42009-09-09 15:08:12 +0000343 break;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000344 }
Mike Stump11289f42009-09-09 15:08:12 +0000345
346 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000347 "Invalid #define in predefines buffer?");
Mike Stump11289f42009-09-09 15:08:12 +0000348 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000349 (*ConflictPos)[MacroDefLen] != '(')
350 continue; // Longer macro name; keep trying.
Mike Stump11289f42009-09-09 15:08:12 +0000351
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000352 // We found a conflicting macro definition.
353 break;
354 }
Mike Stump11289f42009-09-09 15:08:12 +0000355
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000356 if (ConflictPos != CmdLineLines.end()) {
357 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
358 << MacroName;
359
360 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000361 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
362 FindMacro(Buffers, Missing);
363 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
364 SourceLocation PCHMissingLoc =
365 SourceMgr.getLocForStartOfFile(MacroLoc.first)
366 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar499baed2009-11-11 05:26:28 +0000367 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000368
369 ConflictingDefines = true;
370 continue;
371 }
Mike Stump11289f42009-09-09 15:08:12 +0000372
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000373 // If the macro doesn't conflict, then we'll just pick up the macro
374 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000375 if (ConflictingDefines)
376 continue; // Don't complain if there are already conflicting defs
Mike Stump11289f42009-09-09 15:08:12 +0000377
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000378 if (!MissingDefines) {
379 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
380 MissingDefines = true;
381 }
382
383 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000384 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
385 FindMacro(Buffers, Missing);
386 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
387 SourceLocation PCHMissingLoc =
388 SourceMgr.getLocForStartOfFile(MacroLoc.first)
389 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000390 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
391 }
Mike Stump11289f42009-09-09 15:08:12 +0000392
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000393 if (ConflictingDefines)
394 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000395
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000396 // Determine what predefines were introduced based on command-line
397 // parameters that were not present when building the PCH
398 // file. Extra #defines are okay, so long as the identifiers being
399 // defined were not used within the precompiled header.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000400 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000401 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
402 PCHLines.begin(), PCHLines.end(),
Mike Stump11289f42009-09-09 15:08:12 +0000403 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000404 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000405 llvm::StringRef &Extra = ExtraPredefines[I];
406 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000407 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
408 return true;
409 }
410
411 // This is an extra macro definition. Determine the name of the
412 // macro we're defining.
413 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000414 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000415 = Extra.find_first_of("( \n\r", StartOfMacroName);
416 assert(EndOfMacroName != std::string::npos &&
417 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000418 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000419
420 // Check whether this name was used somewhere in the PCH file. If
421 // so, defining it as a macro could change behavior, so we reject
422 // the PCH file.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000423 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar045c92f2009-11-11 00:52:00 +0000424 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000425 return true;
426 }
427
428 // Add this definition to the suggested predefines buffer.
429 SuggestedPredefines += Extra;
430 SuggestedPredefines += '\n';
431 }
432
433 // If we get here, it's because the predefines buffer had compatible
434 // contents. Accept the PCH file.
435 return false;
436}
437
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000438void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
439 unsigned ID) {
440 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
441 ++NumHeaderInfos;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000442}
443
444void PCHValidator::ReadCounter(unsigned Value) {
445 PP.setCounterValue(Value);
446}
447
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000448//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +0000449// AST reader implementation
Douglas Gregora868bbd2009-04-21 22:25:48 +0000450//===----------------------------------------------------------------------===//
451
Sebastian Redl07a89a82010-07-30 00:29:29 +0000452void
Sebastian Redl3e31c722010-08-18 23:56:56 +0000453ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redl07a89a82010-07-30 00:29:29 +0000454 DeserializationListener = Listener;
455 if (DeserializationListener)
456 DeserializationListener->SetReader(this);
457}
458
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000459
Douglas Gregora868bbd2009-04-21 22:25:48 +0000460namespace {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000461class ASTSelectorLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000462 ASTReader &Reader;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000463
464public:
Sebastian Redl834bb972010-08-04 17:20:04 +0000465 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +0000466 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +0000467 ObjCMethodList Instance, Factory;
468 };
Douglas Gregorc78d3462009-04-24 21:10:55 +0000469
470 typedef Selector external_key_type;
471 typedef external_key_type internal_key_type;
472
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000473 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump11289f42009-09-09 15:08:12 +0000474
Douglas Gregorc78d3462009-04-24 21:10:55 +0000475 static bool EqualKey(const internal_key_type& a,
476 const internal_key_type& b) {
477 return a == b;
478 }
Mike Stump11289f42009-09-09 15:08:12 +0000479
Douglas Gregorc78d3462009-04-24 21:10:55 +0000480 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +0000481 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000482 }
Mike Stump11289f42009-09-09 15:08:12 +0000483
Douglas Gregorc78d3462009-04-24 21:10:55 +0000484 // This hopefully will just get inlined and removed by the optimizer.
485 static const internal_key_type&
486 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000487
Douglas Gregorc78d3462009-04-24 21:10:55 +0000488 static std::pair<unsigned, unsigned>
489 ReadKeyDataLength(const unsigned char*& d) {
490 using namespace clang::io;
491 unsigned KeyLen = ReadUnalignedLE16(d);
492 unsigned DataLen = ReadUnalignedLE16(d);
493 return std::make_pair(KeyLen, DataLen);
494 }
Mike Stump11289f42009-09-09 15:08:12 +0000495
Douglas Gregor95c13f52009-04-25 17:48:32 +0000496 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000497 using namespace clang::io;
Chris Lattner8575daa2009-04-27 21:45:14 +0000498 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000499 unsigned N = ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000500 IdentifierInfo *FirstII
Douglas Gregorc78d3462009-04-24 21:10:55 +0000501 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
502 if (N == 0)
503 return SelTable.getNullarySelector(FirstII);
504 else if (N == 1)
505 return SelTable.getUnarySelector(FirstII);
506
507 llvm::SmallVector<IdentifierInfo *, 16> Args;
508 Args.push_back(FirstII);
509 for (unsigned I = 1; I != N; ++I)
510 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
511
Douglas Gregor038c3382009-05-22 22:45:36 +0000512 return SelTable.getSelector(N, Args.data());
Douglas Gregorc78d3462009-04-24 21:10:55 +0000513 }
Mike Stump11289f42009-09-09 15:08:12 +0000514
Douglas Gregorc78d3462009-04-24 21:10:55 +0000515 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
516 using namespace clang::io;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000517
518 data_type Result;
519
Sebastian Redl834bb972010-08-04 17:20:04 +0000520 Result.ID = ReadUnalignedLE32(d);
521 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
522 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
523
Douglas Gregorc78d3462009-04-24 21:10:55 +0000524 // Load instance methods
525 ObjCMethodList *Prev = 0;
526 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000527 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000528 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000529 if (!Result.Instance.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000530 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000531 Result.Instance.Method = Method;
532 Prev = &Result.Instance;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000533 continue;
534 }
535
Ted Kremenekda4abf12010-02-11 00:53:01 +0000536 ObjCMethodList *Mem =
537 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
538 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000539 Prev = Prev->Next;
540 }
541
542 // Load factory methods
543 Prev = 0;
544 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000545 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000546 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000547 if (!Result.Factory.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000548 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000549 Result.Factory.Method = Method;
550 Prev = &Result.Factory;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000551 continue;
552 }
553
Ted Kremenekda4abf12010-02-11 00:53:01 +0000554 ObjCMethodList *Mem =
555 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
556 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000557 Prev = Prev->Next;
558 }
559
560 return Result;
561 }
562};
Mike Stump11289f42009-09-09 15:08:12 +0000563
564} // end anonymous namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +0000565
566/// \brief The on-disk hash table used for the global method pool.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000567typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
568 ASTSelectorLookupTable;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000569
570namespace {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000571class ASTIdentifierLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000572 ASTReader &Reader;
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000573 llvm::BitstreamCursor &Stream;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000574
575 // If we know the IdentifierInfo in advance, it is here and we will
576 // not build a new one. Used when deserializing information about an
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000577 // identifier that was constructed before the AST file was read.
Douglas Gregora868bbd2009-04-21 22:25:48 +0000578 IdentifierInfo *KnownII;
579
580public:
581 typedef IdentifierInfo * data_type;
582
583 typedef const std::pair<const char*, unsigned> external_key_type;
584
585 typedef external_key_type internal_key_type;
586
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000587 ASTIdentifierLookupTrait(ASTReader &Reader, llvm::BitstreamCursor &Stream,
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000588 IdentifierInfo *II = 0)
589 : Reader(Reader), Stream(Stream), KnownII(II) { }
Mike Stump11289f42009-09-09 15:08:12 +0000590
Douglas Gregora868bbd2009-04-21 22:25:48 +0000591 static bool EqualKey(const internal_key_type& a,
592 const internal_key_type& b) {
593 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
594 : false;
595 }
Mike Stump11289f42009-09-09 15:08:12 +0000596
Douglas Gregora868bbd2009-04-21 22:25:48 +0000597 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000598 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregora868bbd2009-04-21 22:25:48 +0000599 }
Mike Stump11289f42009-09-09 15:08:12 +0000600
Douglas Gregora868bbd2009-04-21 22:25:48 +0000601 // This hopefully will just get inlined and removed by the optimizer.
602 static const internal_key_type&
603 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000604
Douglas Gregora868bbd2009-04-21 22:25:48 +0000605 static std::pair<unsigned, unsigned>
606 ReadKeyDataLength(const unsigned char*& d) {
607 using namespace clang::io;
Douglas Gregor6b7bf5a2009-04-25 20:26:24 +0000608 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor5287b4e2009-04-25 21:04:17 +0000609 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000610 return std::make_pair(KeyLen, DataLen);
611 }
Mike Stump11289f42009-09-09 15:08:12 +0000612
Douglas Gregora868bbd2009-04-21 22:25:48 +0000613 static std::pair<const char*, unsigned>
614 ReadKey(const unsigned char* d, unsigned n) {
615 assert(n >= 2 && d[n-1] == '\0');
616 return std::make_pair((const char*) d, n-1);
617 }
Mike Stump11289f42009-09-09 15:08:12 +0000618
619 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregora868bbd2009-04-21 22:25:48 +0000620 const unsigned char* d,
621 unsigned DataLen) {
622 using namespace clang::io;
Sebastian Redl539c5062010-08-18 23:57:32 +0000623 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000624 bool IsInteresting = ID & 0x01;
625
626 // Wipe out the "is interesting" bit.
627 ID = ID >> 1;
628
629 if (!IsInteresting) {
Sebastian Redl98912122010-07-27 23:01:28 +0000630 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregor1d583f22009-04-28 21:18:29 +0000631 // and associate it with the persistent ID.
632 IdentifierInfo *II = KnownII;
633 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000634 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000635 Reader.SetIdentifierInfo(ID, II);
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000636 II->setIsFromAST();
Douglas Gregor1d583f22009-04-28 21:18:29 +0000637 return II;
638 }
639
Douglas Gregorb9256522009-04-28 21:32:13 +0000640 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000641 bool CPlusPlusOperatorKeyword = Bits & 0x01;
642 Bits >>= 1;
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000643 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
644 Bits >>= 1;
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000645 bool Poisoned = Bits & 0x01;
646 Bits >>= 1;
647 bool ExtensionToken = Bits & 0x01;
648 Bits >>= 1;
649 bool hasMacroDefinition = Bits & 0x01;
650 Bits >>= 1;
651 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
652 Bits >>= 10;
Mike Stump11289f42009-09-09 15:08:12 +0000653
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000654 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorb9256522009-04-28 21:32:13 +0000655 DataLen -= 6;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000656
657 // Build the IdentifierInfo itself and link the identifier ID with
658 // the new IdentifierInfo.
659 IdentifierInfo *II = KnownII;
660 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000661 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000662 Reader.SetIdentifierInfo(ID, II);
663
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000664 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000665 // Token IDs are read-only.
666 if (HasRevertedTokenIDToIdentifier)
667 II->RevertTokenIDToIdentifier();
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000668 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump11289f42009-09-09 15:08:12 +0000669 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000670 "Incorrect extension token flag");
671 (void)ExtensionToken;
672 II->setIsPoisoned(Poisoned);
673 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
674 "Incorrect C++ operator keyword flag");
675 (void)CPlusPlusOperatorKeyword;
676
Douglas Gregorc3366a52009-04-21 23:56:24 +0000677 // If this identifier is a macro, deserialize the macro
678 // definition.
679 if (hasMacroDefinition) {
Douglas Gregorb9256522009-04-28 21:32:13 +0000680 uint32_t Offset = ReadUnalignedLE32(d);
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000681 Reader.ReadMacroRecord(Stream, Offset);
Douglas Gregorb9256522009-04-28 21:32:13 +0000682 DataLen -= 4;
Douglas Gregorc3366a52009-04-21 23:56:24 +0000683 }
Douglas Gregora868bbd2009-04-21 22:25:48 +0000684
685 // Read all of the declarations visible at global scope with this
686 // name.
Chris Lattner1d728882009-04-27 22:17:41 +0000687 if (Reader.getContext() == 0) return II;
Douglas Gregor1342e842009-07-06 18:54:52 +0000688 if (DataLen > 0) {
689 llvm::SmallVector<uint32_t, 4> DeclIDs;
690 for (; DataLen > 0; DataLen -= 4)
691 DeclIDs.push_back(ReadUnalignedLE32(d));
692 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000693 }
Mike Stump11289f42009-09-09 15:08:12 +0000694
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000695 II->setIsFromAST();
Douglas Gregora868bbd2009-04-21 22:25:48 +0000696 return II;
697 }
698};
Mike Stump11289f42009-09-09 15:08:12 +0000699
700} // end anonymous namespace
Douglas Gregora868bbd2009-04-21 22:25:48 +0000701
702/// \brief The on-disk hash table used to contain information about
703/// all of the identifiers in the program.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000704typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
705 ASTIdentifierLookupTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000706
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000707namespace {
708class ASTDeclContextNameLookupTrait {
709 ASTReader &Reader;
710
711public:
712 /// \brief Pair of begin/end iterators for DeclIDs.
713 typedef std::pair<DeclID *, DeclID *> data_type;
714
715 /// \brief Special internal key for declaration names.
716 /// The hash table creates keys for comparison; we do not create
717 /// a DeclarationName for the internal key to avoid deserializing types.
718 struct DeclNameKey {
719 DeclarationName::NameKind Kind;
720 uint64_t Data;
721 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
722 };
723
724 typedef DeclarationName external_key_type;
725 typedef DeclNameKey internal_key_type;
726
727 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
728
729 static bool EqualKey(const internal_key_type& a,
730 const internal_key_type& b) {
731 return a.Kind == b.Kind && a.Data == b.Data;
732 }
733
734 unsigned ComputeHash(const DeclNameKey &Key) const {
735 llvm::FoldingSetNodeID ID;
736 ID.AddInteger(Key.Kind);
737
738 switch (Key.Kind) {
739 case DeclarationName::Identifier:
740 case DeclarationName::CXXLiteralOperatorName:
741 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
742 break;
743 case DeclarationName::ObjCZeroArgSelector:
744 case DeclarationName::ObjCOneArgSelector:
745 case DeclarationName::ObjCMultiArgSelector:
746 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
747 break;
748 case DeclarationName::CXXConstructorName:
749 case DeclarationName::CXXDestructorName:
750 case DeclarationName::CXXConversionFunctionName:
751 ID.AddInteger((TypeID)Key.Data);
752 break;
753 case DeclarationName::CXXOperatorName:
754 ID.AddInteger((OverloadedOperatorKind)Key.Data);
755 break;
756 case DeclarationName::CXXUsingDirective:
757 break;
758 }
759
760 return ID.ComputeHash();
761 }
762
763 internal_key_type GetInternalKey(const external_key_type& Name) const {
764 DeclNameKey Key;
765 Key.Kind = Name.getNameKind();
766 switch (Name.getNameKind()) {
767 case DeclarationName::Identifier:
768 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
769 break;
770 case DeclarationName::ObjCZeroArgSelector:
771 case DeclarationName::ObjCOneArgSelector:
772 case DeclarationName::ObjCMultiArgSelector:
773 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
774 break;
775 case DeclarationName::CXXConstructorName:
776 case DeclarationName::CXXDestructorName:
777 case DeclarationName::CXXConversionFunctionName:
778 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
779 break;
780 case DeclarationName::CXXOperatorName:
781 Key.Data = Name.getCXXOverloadedOperator();
782 break;
783 case DeclarationName::CXXLiteralOperatorName:
784 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
785 break;
786 case DeclarationName::CXXUsingDirective:
787 break;
788 }
789
790 return Key;
791 }
792
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000793 external_key_type GetExternalKey(const internal_key_type& Key) const {
794 ASTContext *Context = Reader.getContext();
795 switch (Key.Kind) {
796 case DeclarationName::Identifier:
797 return DeclarationName((IdentifierInfo*)Key.Data);
798
799 case DeclarationName::ObjCZeroArgSelector:
800 case DeclarationName::ObjCOneArgSelector:
801 case DeclarationName::ObjCMultiArgSelector:
802 return DeclarationName(Selector(Key.Data));
803
804 case DeclarationName::CXXConstructorName:
805 return Context->DeclarationNames.getCXXConstructorName(
806 Context->getCanonicalType(Reader.GetType(Key.Data)));
807
808 case DeclarationName::CXXDestructorName:
809 return Context->DeclarationNames.getCXXDestructorName(
810 Context->getCanonicalType(Reader.GetType(Key.Data)));
811
812 case DeclarationName::CXXConversionFunctionName:
813 return Context->DeclarationNames.getCXXConversionFunctionName(
814 Context->getCanonicalType(Reader.GetType(Key.Data)));
815
816 case DeclarationName::CXXOperatorName:
817 return Context->DeclarationNames.getCXXOperatorName(
818 (OverloadedOperatorKind)Key.Data);
819
820 case DeclarationName::CXXLiteralOperatorName:
821 return Context->DeclarationNames.getCXXLiteralOperatorName(
822 (IdentifierInfo*)Key.Data);
823
824 case DeclarationName::CXXUsingDirective:
825 return DeclarationName::getUsingDirectiveName();
826 }
827
828 llvm_unreachable("Invalid Name Kind ?");
829 }
830
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000831 static std::pair<unsigned, unsigned>
832 ReadKeyDataLength(const unsigned char*& d) {
833 using namespace clang::io;
834 unsigned KeyLen = ReadUnalignedLE16(d);
835 unsigned DataLen = ReadUnalignedLE16(d);
836 return std::make_pair(KeyLen, DataLen);
837 }
838
839 internal_key_type ReadKey(const unsigned char* d, unsigned) {
840 using namespace clang::io;
841
842 DeclNameKey Key;
843 Key.Kind = (DeclarationName::NameKind)*d++;
844 switch (Key.Kind) {
845 case DeclarationName::Identifier:
846 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
847 break;
848 case DeclarationName::ObjCZeroArgSelector:
849 case DeclarationName::ObjCOneArgSelector:
850 case DeclarationName::ObjCMultiArgSelector:
851 Key.Data =
852 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
853 break;
854 case DeclarationName::CXXConstructorName:
855 case DeclarationName::CXXDestructorName:
856 case DeclarationName::CXXConversionFunctionName:
857 Key.Data = ReadUnalignedLE32(d); // TypeID
858 break;
859 case DeclarationName::CXXOperatorName:
860 Key.Data = *d++; // OverloadedOperatorKind
861 break;
862 case DeclarationName::CXXLiteralOperatorName:
863 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
864 break;
865 case DeclarationName::CXXUsingDirective:
866 break;
867 }
868
869 return Key;
870 }
871
872 data_type ReadData(internal_key_type, const unsigned char* d,
873 unsigned DataLen) {
874 using namespace clang::io;
875 unsigned NumDecls = ReadUnalignedLE16(d);
876 DeclID *Start = (DeclID *)d;
877 return std::make_pair(Start, Start + NumDecls);
878 }
879};
880
881} // end anonymous namespace
882
883/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
884typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
885 ASTDeclContextNameLookupTable;
886
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000887bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
888 const std::pair<uint64_t, uint64_t> &Offsets,
889 DeclContextInfo &Info) {
890 SavedStreamPosition SavedPosition(Cursor);
891 // First the lexical decls.
892 if (Offsets.first != 0) {
893 Cursor.JumpToBit(Offsets.first);
894
895 RecordData Record;
896 const char *Blob;
897 unsigned BlobLen;
898 unsigned Code = Cursor.ReadCode();
899 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
900 if (RecCode != DECL_CONTEXT_LEXICAL) {
901 Error("Expected lexical block");
902 return true;
903 }
904
905 Info.LexicalDecls = reinterpret_cast<const DeclID*>(Blob);
906 Info.NumLexicalDecls = BlobLen / sizeof(DeclID);
907 } else {
908 Info.LexicalDecls = 0;
909 Info.NumLexicalDecls = 0;
910 }
911
912 // Now the lookup table.
913 if (Offsets.second != 0) {
914 Cursor.JumpToBit(Offsets.second);
915
916 RecordData Record;
917 const char *Blob;
918 unsigned BlobLen;
919 unsigned Code = Cursor.ReadCode();
920 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
921 if (RecCode != DECL_CONTEXT_VISIBLE) {
922 Error("Expected visible lookup table block");
923 return true;
924 }
925 Info.NameLookupTableData
926 = ASTDeclContextNameLookupTable::Create(
927 (const unsigned char *)Blob + Record[0],
928 (const unsigned char *)Blob,
929 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl9d8f58b2010-08-24 00:50:00 +0000930 } else {
931 Info.NameLookupTableData = 0;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000932 }
933
934 return false;
935}
936
Sebastian Redl2c499f62010-08-18 23:56:43 +0000937void ASTReader::Error(const char *Msg) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000938 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000939}
940
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000941/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000942bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000943 if (Listener)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000944 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000945 ActualOriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000946 SuggestedPredefines);
Douglas Gregorc379c072009-04-28 18:58:38 +0000947 return false;
Douglas Gregor92863e42009-04-10 23:10:45 +0000948}
949
Douglas Gregorc5046832009-04-27 18:38:38 +0000950//===----------------------------------------------------------------------===//
951// Source Manager Deserialization
952//===----------------------------------------------------------------------===//
953
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000954/// \brief Read the line table in the source manager block.
955/// \returns true if ther was an error.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000956bool ASTReader::ParseLineTable(llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000957 unsigned Idx = 0;
958 LineTableInfo &LineTable = SourceMgr.getLineTable();
959
960 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +0000961 std::map<int, int> FileIDs;
962 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000963 // Extract the file name
964 unsigned FilenameLen = Record[Idx++];
965 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
966 Idx += FilenameLen;
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000967 MaybeAddSystemRootToFilename(Filename);
Mike Stump11289f42009-09-09 15:08:12 +0000968 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregora8854652009-04-13 17:12:42 +0000969 Filename.size());
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000970 }
971
972 // Parse the line entries
973 std::vector<LineEntry> Entries;
974 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000975 int FID = Record[Idx++];
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000976
977 // Extract the line entries
978 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000979 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000980 Entries.clear();
981 Entries.reserve(NumEntries);
982 for (unsigned I = 0; I != NumEntries; ++I) {
983 unsigned FileOffset = Record[Idx++];
984 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000985 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +0000986 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000987 = (SrcMgr::CharacteristicKind)Record[Idx++];
988 unsigned IncludeOffset = Record[Idx++];
989 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
990 FileKind, IncludeOffset));
991 }
992 LineTable.AddEntry(FID, Entries);
993 }
994
995 return false;
996}
997
Douglas Gregorc5046832009-04-27 18:38:38 +0000998namespace {
999
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001000class ASTStatData {
Douglas Gregorc5046832009-04-27 18:38:38 +00001001public:
1002 const bool hasStat;
1003 const ino_t ino;
1004 const dev_t dev;
1005 const mode_t mode;
1006 const time_t mtime;
1007 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +00001008
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001009 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump11289f42009-09-09 15:08:12 +00001010 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
1011
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001012 ASTStatData()
Douglas Gregorc5046832009-04-27 18:38:38 +00001013 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
1014};
1015
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001016class ASTStatLookupTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001017 public:
1018 typedef const char *external_key_type;
1019 typedef const char *internal_key_type;
1020
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001021 typedef ASTStatData data_type;
Douglas Gregorc5046832009-04-27 18:38:38 +00001022
1023 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001024 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001025 }
1026
1027 static internal_key_type GetInternalKey(const char *path) { return path; }
1028
1029 static bool EqualKey(internal_key_type a, internal_key_type b) {
1030 return strcmp(a, b) == 0;
1031 }
1032
1033 static std::pair<unsigned, unsigned>
1034 ReadKeyDataLength(const unsigned char*& d) {
1035 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1036 unsigned DataLen = (unsigned) *d++;
1037 return std::make_pair(KeyLen + 1, DataLen);
1038 }
1039
1040 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1041 return (const char *)d;
1042 }
1043
1044 static data_type ReadData(const internal_key_type, const unsigned char *d,
1045 unsigned /*DataLen*/) {
1046 using namespace clang::io;
1047
1048 if (*d++ == 1)
1049 return data_type();
1050
1051 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1052 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1053 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +00001054 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +00001055 off_t size = (off_t) ReadUnalignedLE64(d);
1056 return data_type(ino, dev, mode, mtime, size);
1057 }
1058};
1059
1060/// \brief stat() cache for precompiled headers.
1061///
1062/// This cache is very similar to the stat cache used by pretokenized
1063/// headers.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001064class ASTStatCache : public StatSysCallCache {
1065 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregorc5046832009-04-27 18:38:38 +00001066 CacheTy *Cache;
1067
1068 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +00001069public:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001070 ASTStatCache(const unsigned char *Buckets,
Douglas Gregorc5046832009-04-27 18:38:38 +00001071 const unsigned char *Base,
1072 unsigned &NumStatHits,
Mike Stump11289f42009-09-09 15:08:12 +00001073 unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +00001074 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1075 Cache = CacheTy::Create(Buckets, Base);
1076 }
1077
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001078 ~ASTStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +00001079
Douglas Gregorc5046832009-04-27 18:38:38 +00001080 int stat(const char *path, struct stat *buf) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001081 // Do the lookup for the file's data in the AST file.
Douglas Gregorc5046832009-04-27 18:38:38 +00001082 CacheTy::iterator I = Cache->find(path);
1083
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001084 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregorc5046832009-04-27 18:38:38 +00001085 if (I == Cache->end()) {
1086 ++NumStatMisses;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001087 return StatSysCallCache::stat(path, buf);
Douglas Gregorc5046832009-04-27 18:38:38 +00001088 }
Mike Stump11289f42009-09-09 15:08:12 +00001089
Douglas Gregorc5046832009-04-27 18:38:38 +00001090 ++NumStatHits;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001091 ASTStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001092
Douglas Gregorc5046832009-04-27 18:38:38 +00001093 if (!Data.hasStat)
1094 return 1;
1095
1096 buf->st_ino = Data.ino;
1097 buf->st_dev = Data.dev;
1098 buf->st_mtime = Data.mtime;
1099 buf->st_mode = Data.mode;
1100 buf->st_size = Data.size;
1101 return 0;
1102 }
1103};
1104} // end anonymous namespace
1105
1106
Sebastian Redl393f8b72010-07-19 20:52:06 +00001107/// \brief Read a source manager block
Sebastian Redl2c499f62010-08-18 23:56:43 +00001108ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001109 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +00001110
Sebastian Redl393f8b72010-07-19 20:52:06 +00001111 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001112
Douglas Gregor258ae542009-04-27 06:38:32 +00001113 // Set the source-location entry cursor to the current position in
1114 // the stream. This cursor will be used to read the contents of the
1115 // source manager block initially, and then lazily read
1116 // source-location entries as needed.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001117 SLocEntryCursor = F.Stream;
Douglas Gregor258ae542009-04-27 06:38:32 +00001118
1119 // The stream itself is going to skip over the source manager block.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001120 if (F.Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001121 Error("malformed block record in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001122 return Failure;
1123 }
1124
1125 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001126 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001127 Error("malformed source manager block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001128 return Failure;
1129 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001130
Douglas Gregora7f71a92009-04-10 03:52:48 +00001131 RecordData Record;
1132 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001133 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001134 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001135 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001136 Error("error at end of Source Manager block in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001137 return Failure;
1138 }
Douglas Gregor92863e42009-04-10 23:10:45 +00001139 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001140 }
Mike Stump11289f42009-09-09 15:08:12 +00001141
Douglas Gregora7f71a92009-04-10 03:52:48 +00001142 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1143 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +00001144 SLocEntryCursor.ReadSubBlockID();
1145 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001146 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001147 return Failure;
1148 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001149 continue;
1150 }
Mike Stump11289f42009-09-09 15:08:12 +00001151
Douglas Gregora7f71a92009-04-10 03:52:48 +00001152 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001153 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001154 continue;
1155 }
Mike Stump11289f42009-09-09 15:08:12 +00001156
Douglas Gregora7f71a92009-04-10 03:52:48 +00001157 // Read a record.
1158 const char *BlobStart;
1159 unsigned BlobLen;
1160 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +00001161 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001162 default: // Default behavior: ignore.
1163 break;
1164
Sebastian Redl539c5062010-08-18 23:57:32 +00001165 case SM_LINE_TABLE:
Sebastian Redlb293a452010-07-20 21:20:32 +00001166 if (ParseLineTable(Record))
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001167 return Failure;
Chris Lattner184e65d2009-04-14 23:22:57 +00001168 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00001169
Sebastian Redl539c5062010-08-18 23:57:32 +00001170 case SM_SLOC_FILE_ENTRY:
1171 case SM_SLOC_BUFFER_ENTRY:
1172 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor258ae542009-04-27 06:38:32 +00001173 // Once we hit one of the source location entries, we're done.
1174 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001175 }
1176 }
1177}
1178
Sebastian Redl06750302010-07-20 21:50:20 +00001179/// \brief Get a cursor that's correctly positioned for reading the source
1180/// location entry with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001181llvm::BitstreamCursor &ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl06750302010-07-20 21:50:20 +00001182 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1183 "SLocCursorForID should only be called for real IDs.");
1184
1185 ID -= 1;
1186 PerFileData *F = 0;
1187 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1188 F = Chain[N - I - 1];
1189 if (ID < F->LocalNumSLocEntries)
1190 break;
1191 ID -= F->LocalNumSLocEntries;
1192 }
1193 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1194
1195 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
1196 return F->SLocEntryCursor;
1197}
1198
Douglas Gregor258ae542009-04-27 06:38:32 +00001199/// \brief Read in the source location entry with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001200ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001201 if (ID == 0)
1202 return Success;
1203
1204 if (ID > TotalNumSLocEntries) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001205 Error("source location entry ID out-of-range for AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001206 return Failure;
1207 }
1208
Sebastian Redl06750302010-07-20 21:50:20 +00001209 llvm::BitstreamCursor &SLocEntryCursor = SLocCursorForID(ID);
Sebastian Redl34522812010-07-16 17:50:48 +00001210
Douglas Gregor258ae542009-04-27 06:38:32 +00001211 ++NumSLocEntriesRead;
Douglas Gregor258ae542009-04-27 06:38:32 +00001212 unsigned Code = SLocEntryCursor.ReadCode();
1213 if (Code == llvm::bitc::END_BLOCK ||
1214 Code == llvm::bitc::ENTER_SUBBLOCK ||
1215 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001216 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001217 return Failure;
1218 }
1219
Douglas Gregor258ae542009-04-27 06:38:32 +00001220 RecordData Record;
1221 const char *BlobStart;
1222 unsigned BlobLen;
1223 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1224 default:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001225 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001226 return Failure;
1227
Sebastian Redl539c5062010-08-18 23:57:32 +00001228 case SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001229 std::string Filename(BlobStart, BlobStart + BlobLen);
1230 MaybeAddSystemRootToFilename(Filename);
1231 const FileEntry *File = FileMgr.getFile(Filename);
Chris Lattnerd20dc872009-06-15 04:35:16 +00001232 if (File == 0) {
1233 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001234 ErrorStr += Filename;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001235 ErrorStr += "' referenced by AST file";
Chris Lattnerd20dc872009-06-15 04:35:16 +00001236 Error(ErrorStr.c_str());
1237 return Failure;
1238 }
Mike Stump11289f42009-09-09 15:08:12 +00001239
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001240 if (Record.size() < 10) {
Ted Kremenekabb1ddd2010-03-18 21:23:05 +00001241 Error("source location entry is incorrect");
1242 return Failure;
1243 }
1244
Douglas Gregorce3a8292010-07-27 00:27:13 +00001245 if (!DisableValidation &&
1246 ((off_t)Record[4] != File->getSize()
Douglas Gregor08288f22010-04-09 15:54:22 +00001247#if !defined(LLVM_ON_WIN32)
1248 // In our regression testing, the Windows file system seems to
1249 // have inconsistent modification times that sometimes
1250 // erroneously trigger this error-handling path.
Douglas Gregorce3a8292010-07-27 00:27:13 +00001251 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor08288f22010-04-09 15:54:22 +00001252#endif
Douglas Gregorce3a8292010-07-27 00:27:13 +00001253 )) {
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001254 Diag(diag::err_fe_pch_file_modified)
1255 << Filename;
1256 return Failure;
1257 }
1258
Douglas Gregor258ae542009-04-27 06:38:32 +00001259 FileID FID = SourceMgr.createFileID(File,
1260 SourceLocation::getFromRawEncoding(Record[1]),
1261 (SrcMgr::CharacteristicKind)Record[2],
1262 ID, Record[0]);
1263 if (Record[3])
1264 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1265 .setHasLineDirectives();
1266
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001267 // Reconstruct header-search information for this file.
1268 HeaderFileInfo HFI;
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001269 HFI.isImport = Record[6];
1270 HFI.DirInfo = Record[7];
1271 HFI.NumIncludes = Record[8];
1272 HFI.ControllingMacroID = Record[9];
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001273 if (Listener)
1274 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor258ae542009-04-27 06:38:32 +00001275 break;
1276 }
1277
Sebastian Redl539c5062010-08-18 23:57:32 +00001278 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor258ae542009-04-27 06:38:32 +00001279 const char *Name = BlobStart;
1280 unsigned Offset = Record[0];
1281 unsigned Code = SLocEntryCursor.ReadCode();
1282 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001283 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +00001284 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001285
Sebastian Redl539c5062010-08-18 23:57:32 +00001286 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001287 Error("AST record has invalid code");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001288 return Failure;
1289 }
1290
Douglas Gregor258ae542009-04-27 06:38:32 +00001291 llvm::MemoryBuffer *Buffer
Chris Lattner58c79342010-04-05 22:42:27 +00001292 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1293 Name);
Douglas Gregor258ae542009-04-27 06:38:32 +00001294 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump11289f42009-09-09 15:08:12 +00001295
Douglas Gregore6648fb2009-04-28 20:33:11 +00001296 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001297 PCHPredefinesBlock Block = {
1298 BufferID,
1299 llvm::StringRef(BlobStart, BlobLen - 1)
1300 };
1301 PCHPredefinesBuffers.push_back(Block);
Douglas Gregore6648fb2009-04-28 20:33:11 +00001302 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001303
1304 break;
1305 }
1306
Sebastian Redl539c5062010-08-18 23:57:32 +00001307 case SM_SLOC_INSTANTIATION_ENTRY: {
Mike Stump11289f42009-09-09 15:08:12 +00001308 SourceLocation SpellingLoc
Douglas Gregor258ae542009-04-27 06:38:32 +00001309 = SourceLocation::getFromRawEncoding(Record[1]);
1310 SourceMgr.createInstantiationLoc(SpellingLoc,
1311 SourceLocation::getFromRawEncoding(Record[2]),
1312 SourceLocation::getFromRawEncoding(Record[3]),
1313 Record[4],
1314 ID,
1315 Record[0]);
1316 break;
Mike Stump11289f42009-09-09 15:08:12 +00001317 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001318 }
1319
1320 return Success;
1321}
1322
Chris Lattnere78a6be2009-04-27 01:05:14 +00001323/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1324/// specified cursor. Read the abbreviations that are at the top of the block
1325/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001326bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001327 unsigned BlockID) {
1328 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001329 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001330 return Failure;
1331 }
Mike Stump11289f42009-09-09 15:08:12 +00001332
Chris Lattnere78a6be2009-04-27 01:05:14 +00001333 while (true) {
1334 unsigned Code = Cursor.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00001335
Chris Lattnere78a6be2009-04-27 01:05:14 +00001336 // We expect all abbrevs to be at the start of the block.
1337 if (Code != llvm::bitc::DEFINE_ABBREV)
1338 return false;
1339 Cursor.ReadAbbrevRecord();
1340 }
1341}
1342
Sebastian Redl2c499f62010-08-18 23:56:43 +00001343void ASTReader::ReadMacroRecord(llvm::BitstreamCursor &Stream, uint64_t Offset){
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001344 assert(PP && "Forgot to set Preprocessor ?");
Mike Stump11289f42009-09-09 15:08:12 +00001345
Douglas Gregorc3366a52009-04-21 23:56:24 +00001346 // Keep track of where we are in the stream, then jump back there
1347 // after reading this macro.
1348 SavedStreamPosition SavedPosition(Stream);
1349
1350 Stream.JumpToBit(Offset);
1351 RecordData Record;
1352 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1353 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001354
Douglas Gregorc3366a52009-04-21 23:56:24 +00001355 while (true) {
1356 unsigned Code = Stream.ReadCode();
1357 switch (Code) {
1358 case llvm::bitc::END_BLOCK:
1359 return;
1360
1361 case llvm::bitc::ENTER_SUBBLOCK:
1362 // No known subblocks, always skip them.
1363 Stream.ReadSubBlockID();
1364 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001365 Error("malformed block record in AST file");
Douglas Gregorc3366a52009-04-21 23:56:24 +00001366 return;
1367 }
1368 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001369
Douglas Gregorc3366a52009-04-21 23:56:24 +00001370 case llvm::bitc::DEFINE_ABBREV:
1371 Stream.ReadAbbrevRecord();
1372 continue;
1373 default: break;
1374 }
1375
1376 // Read a record.
1377 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001378 PreprocessorRecordTypes RecType =
1379 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001380 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001381 case PP_MACRO_OBJECT_LIKE:
1382 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001383 // If we already have a macro, that means that we've hit the end
1384 // of the definition of the macro we were looking for. We're
1385 // done.
1386 if (Macro)
1387 return;
1388
1389 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1390 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001391 Error("macro must have a name in AST file");
Douglas Gregorc3366a52009-04-21 23:56:24 +00001392 return;
1393 }
1394 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1395 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001396
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001397 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001398 MI->setIsUsed(isUsed);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001399 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001400
Douglas Gregoraae92242010-03-19 21:51:54 +00001401 unsigned NextIndex = 3;
Sebastian Redl539c5062010-08-18 23:57:32 +00001402 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001403 // Decode function-like macro info.
1404 bool isC99VarArgs = Record[3];
1405 bool isGNUVarArgs = Record[4];
1406 MacroArgs.clear();
1407 unsigned NumArgs = Record[5];
Douglas Gregoraae92242010-03-19 21:51:54 +00001408 NextIndex = 6 + NumArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001409 for (unsigned i = 0; i != NumArgs; ++i)
1410 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1411
1412 // Install function-like macro info.
1413 MI->setIsFunctionLike();
1414 if (isC99VarArgs) MI->setIsC99Varargs();
1415 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001416 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001417 PP->getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001418 }
1419
1420 // Finally, install the macro.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001421 PP->setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001422
1423 // Remember that we saw this macro last so that we add the tokens that
1424 // form its body to it.
1425 Macro = MI;
Douglas Gregoraae92242010-03-19 21:51:54 +00001426
1427 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1428 // We have a macro definition. Load it now.
1429 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1430 getMacroDefinition(Record[NextIndex]));
1431 }
1432
Douglas Gregorc3366a52009-04-21 23:56:24 +00001433 ++NumMacrosRead;
1434 break;
1435 }
Mike Stump11289f42009-09-09 15:08:12 +00001436
Sebastian Redl539c5062010-08-18 23:57:32 +00001437 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001438 // If we see a TOKEN before a PP_MACRO_*, then the file is
1439 // erroneous, just pretend we didn't see this.
1440 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001441
Douglas Gregorc3366a52009-04-21 23:56:24 +00001442 Token Tok;
1443 Tok.startToken();
1444 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1445 Tok.setLength(Record[1]);
1446 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1447 Tok.setIdentifierInfo(II);
1448 Tok.setKind((tok::TokenKind)Record[3]);
1449 Tok.setFlag((Token::TokenFlags)Record[4]);
1450 Macro->AddTokenToBody(Tok);
1451 break;
1452 }
Douglas Gregoraae92242010-03-19 21:51:54 +00001453
Sebastian Redl539c5062010-08-18 23:57:32 +00001454 case PP_MACRO_INSTANTIATION: {
Douglas Gregoraae92242010-03-19 21:51:54 +00001455 // If we already have a macro, that means that we've hit the end
1456 // of the definition of the macro we were looking for. We're
1457 // done.
1458 if (Macro)
1459 return;
1460
1461 if (!PP->getPreprocessingRecord()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001462 Error("missing preprocessing record in AST file");
Douglas Gregoraae92242010-03-19 21:51:54 +00001463 return;
1464 }
1465
1466 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1467 if (PPRec.getPreprocessedEntity(Record[0]))
1468 return;
1469
1470 MacroInstantiation *MI
1471 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1472 SourceRange(
1473 SourceLocation::getFromRawEncoding(Record[1]),
1474 SourceLocation::getFromRawEncoding(Record[2])),
1475 getMacroDefinition(Record[4]));
1476 PPRec.SetPreallocatedEntity(Record[0], MI);
1477 return;
1478 }
1479
Sebastian Redl539c5062010-08-18 23:57:32 +00001480 case PP_MACRO_DEFINITION: {
Douglas Gregoraae92242010-03-19 21:51:54 +00001481 // If we already have a macro, that means that we've hit the end
1482 // of the definition of the macro we were looking for. We're
1483 // done.
1484 if (Macro)
1485 return;
1486
1487 if (!PP->getPreprocessingRecord()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001488 Error("missing preprocessing record in AST file");
Douglas Gregoraae92242010-03-19 21:51:54 +00001489 return;
1490 }
1491
1492 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1493 if (PPRec.getPreprocessedEntity(Record[0]))
1494 return;
1495
1496 if (Record[1] >= MacroDefinitionsLoaded.size()) {
1497 Error("out-of-bounds macro definition record");
1498 return;
1499 }
1500
1501 MacroDefinition *MD
1502 = new (PPRec) MacroDefinition(DecodeIdentifierInfo(Record[4]),
1503 SourceLocation::getFromRawEncoding(Record[5]),
1504 SourceRange(
1505 SourceLocation::getFromRawEncoding(Record[2]),
1506 SourceLocation::getFromRawEncoding(Record[3])));
1507 PPRec.SetPreallocatedEntity(Record[0], MD);
1508 MacroDefinitionsLoaded[Record[1]] = MD;
1509 return;
1510 }
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001511 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001512 }
1513}
1514
Sebastian Redl2c499f62010-08-18 23:56:43 +00001515void ASTReader::ReadDefinedMacros() {
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001516 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1517 llvm::BitstreamCursor &MacroCursor = Chain[N - I - 1]->MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001518
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001519 // If there was no preprocessor block, skip this file.
1520 if (!MacroCursor.getBitStreamReader())
1521 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001522
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001523 llvm::BitstreamCursor Cursor = MacroCursor;
Sebastian Redl539c5062010-08-18 23:57:32 +00001524 if (Cursor.EnterSubBlock(PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001525 Error("malformed preprocessor block record in AST file");
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001526 return;
1527 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001528
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001529 RecordData Record;
1530 while (true) {
Sebastian Redl4102dd52010-09-28 02:55:49 +00001531 uint64_t Offset = Cursor.GetCurrentBitNo();
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001532 unsigned Code = Cursor.ReadCode();
1533 if (Code == llvm::bitc::END_BLOCK) {
1534 if (Cursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001535 Error("error at end of preprocessor block in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001536 return;
1537 }
1538 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001539 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001540
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001541 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1542 // No known subblocks, always skip them.
1543 Cursor.ReadSubBlockID();
1544 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001545 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001546 return;
1547 }
1548 continue;
1549 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001550
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001551 if (Code == llvm::bitc::DEFINE_ABBREV) {
1552 Cursor.ReadAbbrevRecord();
1553 continue;
1554 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001555
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001556 // Read a record.
1557 const char *BlobStart;
1558 unsigned BlobLen;
1559 Record.clear();
1560 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1561 default: // Default behavior: ignore.
1562 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001563
Sebastian Redl539c5062010-08-18 23:57:32 +00001564 case PP_MACRO_OBJECT_LIKE:
1565 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001566 DecodeIdentifierInfo(Record[0]);
1567 break;
1568
Sebastian Redl539c5062010-08-18 23:57:32 +00001569 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001570 // Ignore tokens.
1571 break;
Douglas Gregoraae92242010-03-19 21:51:54 +00001572
Sebastian Redl539c5062010-08-18 23:57:32 +00001573 case PP_MACRO_INSTANTIATION:
1574 case PP_MACRO_DEFINITION:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001575 // Read the macro record.
Sebastian Redl4102dd52010-09-28 02:55:49 +00001576 // FIXME: That's a stupid way to do this. We should reuse this cursor.
1577 ReadMacroRecord(Chain[N - I - 1]->Stream, Offset);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001578 break;
1579 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001580 }
1581 }
1582}
1583
Sebastian Redl50e26582010-09-15 19:54:06 +00001584MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregoraae92242010-03-19 21:51:54 +00001585 if (ID == 0 || ID >= MacroDefinitionsLoaded.size())
1586 return 0;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001587
1588 if (!MacroDefinitionsLoaded[ID]) {
1589 unsigned Index = ID;
1590 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1591 PerFileData &F = *Chain[N - I - 1];
1592 if (Index < F.LocalNumMacroDefinitions) {
1593 ReadMacroRecord(F.Stream, F.MacroDefinitionOffsets[Index]);
1594 break;
1595 }
1596 Index -= F.LocalNumMacroDefinitions;
1597 }
1598 assert(MacroDefinitionsLoaded[ID] && "Broken chain");
1599 }
1600
Douglas Gregoraae92242010-03-19 21:51:54 +00001601 return MacroDefinitionsLoaded[ID];
1602}
1603
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001604/// \brief If we are loading a relocatable PCH file, and the filename is
1605/// not an absolute path, add the system root to the beginning of the file
1606/// name.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001607void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001608 // If this is not a relocatable PCH file, there's nothing to do.
1609 if (!RelocatablePCH)
1610 return;
Mike Stump11289f42009-09-09 15:08:12 +00001611
Daniel Dunbarf2ce9a22009-11-18 19:50:41 +00001612 if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001613 return;
1614
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001615 if (isysroot == 0) {
1616 // If no system root was given, default to '/'
1617 Filename.insert(Filename.begin(), '/');
1618 return;
1619 }
Mike Stump11289f42009-09-09 15:08:12 +00001620
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001621 unsigned Length = strlen(isysroot);
1622 if (isysroot[Length - 1] != '/')
1623 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001624
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001625 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1626}
1627
Sebastian Redl2c499f62010-08-18 23:56:43 +00001628ASTReader::ASTReadResult
Sebastian Redl3e31c722010-08-18 23:56:56 +00001629ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001630 llvm::BitstreamCursor &Stream = F.Stream;
1631
Sebastian Redl539c5062010-08-18 23:57:32 +00001632 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001633 Error("malformed block record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001634 return Failure;
1635 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001636
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001637 // Read all of the records and blocks for the ASt file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001638 RecordData Record;
Sebastian Redl393f8b72010-07-19 20:52:06 +00001639 bool First = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001640 while (!Stream.AtEndOfStream()) {
1641 unsigned Code = Stream.ReadCode();
1642 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001643 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001644 Error("error at end of module block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001645 return Failure;
1646 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001647
Douglas Gregor55abb232009-04-10 20:39:37 +00001648 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001649 }
1650
1651 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1652 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001653 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001654 // We lazily load the decls block, but we want to set up the
1655 // DeclsCursor cursor to point into it. Clone our current bitcode
1656 // cursor to it, enter the block and read the abbrevs in that block.
1657 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001658 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001659 if (Stream.SkipBlock() || // Skip with the main cursor.
1660 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001661 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001662 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001663 return Failure;
1664 }
1665 break;
Mike Stump11289f42009-09-09 15:08:12 +00001666
Sebastian Redl539c5062010-08-18 23:57:32 +00001667 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001668 F.MacroCursor = Stream;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001669 if (PP)
1670 PP->setExternalSource(this);
1671
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001672 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001673 Error("malformed block record in AST file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001674 return Failure;
1675 }
1676 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001677
Sebastian Redl539c5062010-08-18 23:57:32 +00001678 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001679 switch (ReadSourceManagerBlock(F)) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001680 case Success:
1681 break;
1682
1683 case Failure:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001684 Error("malformed source manager block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001685 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001686
1687 case IgnorePCH:
1688 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001689 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001690 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001691 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00001692 First = false;
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001693 continue;
1694 }
1695
1696 if (Code == llvm::bitc::DEFINE_ABBREV) {
1697 Stream.ReadAbbrevRecord();
1698 continue;
1699 }
1700
1701 // Read and process a record.
1702 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001703 const char *BlobStart = 0;
1704 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001705 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Douglas Gregorbfbde532009-04-10 21:16:55 +00001706 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001707 default: // Default behavior: ignore.
1708 break;
1709
Sebastian Redl539c5062010-08-18 23:57:32 +00001710 case METADATA: {
1711 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1712 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001713 : diag::warn_pch_version_too_new);
1714 return IgnorePCH;
1715 }
1716
1717 RelocatablePCH = Record[4];
1718 if (Listener) {
1719 std::string TargetTriple(BlobStart, BlobLen);
1720 if (Listener->ReadTargetTriple(TargetTriple))
1721 return IgnorePCH;
1722 }
1723 break;
1724 }
1725
Sebastian Redl539c5062010-08-18 23:57:32 +00001726 case CHAINED_METADATA: {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001727 if (!First) {
1728 Error("CHAINED_METADATA is not first record in block");
1729 return Failure;
1730 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001731 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1732 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001733 : diag::warn_pch_version_too_new);
1734 return IgnorePCH;
1735 }
1736
1737 // Load the chained file.
Sebastian Redl3e31c722010-08-18 23:56:56 +00001738 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen))) {
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001739 case Failure: return Failure;
1740 // If we have to ignore the dependency, we'll have to ignore this too.
1741 case IgnorePCH: return IgnorePCH;
1742 case Success: break;
1743 }
1744 break;
1745 }
1746
Sebastian Redl539c5062010-08-18 23:57:32 +00001747 case TYPE_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001748 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001749 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001750 return Failure;
1751 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001752 F.TypeOffsets = (const uint32_t *)BlobStart;
1753 F.LocalNumTypes = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001754 break;
1755
Sebastian Redl539c5062010-08-18 23:57:32 +00001756 case DECL_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001757 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001758 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001759 return Failure;
1760 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001761 F.DeclOffsets = (const uint32_t *)BlobStart;
1762 F.LocalNumDecls = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001763 break;
Douglas Gregor55abb232009-04-10 20:39:37 +00001764
Sebastian Redl539c5062010-08-18 23:57:32 +00001765 case TU_UPDATE_LEXICAL: {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001766 DeclContextInfo Info = {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001767 /* No visible information */ 0,
Sebastian Redl539c5062010-08-18 23:57:32 +00001768 reinterpret_cast<const DeclID *>(BlobStart),
1769 BlobLen / sizeof(DeclID)
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001770 };
Douglas Gregoraa433012010-10-01 01:18:02 +00001771 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
1772 .push_back(Info);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001773 break;
1774 }
1775
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001776 case UPDATE_VISIBLE: {
1777 serialization::DeclID ID = Record[0];
1778 void *Table = ASTDeclContextNameLookupTable::Create(
1779 (const unsigned char *)BlobStart + Record[1],
1780 (const unsigned char *)BlobStart,
1781 ASTDeclContextNameLookupTrait(*this));
Douglas Gregoraa433012010-10-01 01:18:02 +00001782 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001783 DeclContextInfo Info = {
1784 Table, /* No lexical inforamtion */ 0, 0
1785 };
1786 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1787 } else
1788 PendingVisibleUpdates[ID].push_back(Table);
1789 break;
1790 }
1791
Sebastian Redl539c5062010-08-18 23:57:32 +00001792 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001793 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1794 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001795 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001796 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1797 Latest > FirstLatestDeclIDs[First]) &&
1798 "The new latest is supposed to come after the previous latest");
1799 FirstLatestDeclIDs[First] = Latest;
1800 }
1801 break;
1802 }
1803
Sebastian Redl539c5062010-08-18 23:57:32 +00001804 case LANGUAGE_OPTIONS:
Douglas Gregorce3a8292010-07-27 00:27:13 +00001805 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor55abb232009-04-10 20:39:37 +00001806 return IgnorePCH;
1807 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00001808
Sebastian Redl539c5062010-08-18 23:57:32 +00001809 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001810 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00001811 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001812 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001813 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00001814 (const unsigned char *)F.IdentifierTableData + Record[0],
1815 (const unsigned char *)F.IdentifierTableData,
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001816 ASTIdentifierLookupTrait(*this, F.Stream));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001817 if (PP)
1818 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00001819 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001820 break;
1821
Sebastian Redl539c5062010-08-18 23:57:32 +00001822 case IDENTIFIER_OFFSET:
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001823 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001824 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001825 return Failure;
1826 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001827 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1828 F.LocalNumIdentifiers = Record[0];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001829 break;
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001830
Sebastian Redl539c5062010-08-18 23:57:32 +00001831 case EXTERNAL_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001832 // Optimization for the first block.
1833 if (ExternalDefinitions.empty())
1834 ExternalDefinitions.swap(Record);
1835 else
1836 ExternalDefinitions.insert(ExternalDefinitions.end(),
1837 Record.begin(), Record.end());
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001838 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00001839
Sebastian Redl539c5062010-08-18 23:57:32 +00001840 case SPECIAL_TYPES:
Sebastian Redlb293a452010-07-20 21:20:32 +00001841 // Optimization for the first block
1842 if (SpecialTypes.empty())
1843 SpecialTypes.swap(Record);
1844 else
1845 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregor652d82a2009-04-18 05:55:16 +00001846 break;
1847
Sebastian Redl539c5062010-08-18 23:57:32 +00001848 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001849 TotalNumStatements += Record[0];
1850 TotalNumMacros += Record[1];
1851 TotalLexicalDeclContexts += Record[2];
1852 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00001853 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00001854
Sebastian Redl539c5062010-08-18 23:57:32 +00001855 case TENTATIVE_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001856 // Optimization for the first block.
1857 if (TentativeDefinitions.empty())
1858 TentativeDefinitions.swap(Record);
1859 else
1860 TentativeDefinitions.insert(TentativeDefinitions.end(),
1861 Record.begin(), Record.end());
Douglas Gregord4df8652009-04-22 22:02:47 +00001862 break;
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001863
Sebastian Redl539c5062010-08-18 23:57:32 +00001864 case UNUSED_FILESCOPED_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001865 // Optimization for the first block.
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00001866 if (UnusedFileScopedDecls.empty())
1867 UnusedFileScopedDecls.swap(Record);
Sebastian Redlb293a452010-07-20 21:20:32 +00001868 else
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00001869 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1870 Record.begin(), Record.end());
Tanya Lattner90073802010-02-12 00:07:30 +00001871 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001872
Sebastian Redl539c5062010-08-18 23:57:32 +00001873 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001874 // Later blocks overwrite earlier ones.
1875 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00001876 break;
1877
Sebastian Redl539c5062010-08-18 23:57:32 +00001878 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001879 // Optimization for the first block.
1880 if (LocallyScopedExternalDecls.empty())
1881 LocallyScopedExternalDecls.swap(Record);
1882 else
1883 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1884 Record.begin(), Record.end());
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001885 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001886
Sebastian Redl539c5062010-08-18 23:57:32 +00001887 case SELECTOR_OFFSETS:
Sebastian Redla19a67f2010-08-03 21:58:15 +00001888 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00001889 F.LocalNumSelectors = Record[0];
Douglas Gregor95c13f52009-04-25 17:48:32 +00001890 break;
1891
Sebastian Redl539c5062010-08-18 23:57:32 +00001892 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00001893 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001894 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00001895 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001896 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00001897 F.SelectorLookupTableData + Record[0],
1898 F.SelectorLookupTableData,
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001899 ASTSelectorLookupTrait(*this));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00001900 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00001901 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00001902
Sebastian Redl96371b42010-09-22 00:42:30 +00001903 case REFERENCED_SELECTOR_POOL:
1904 if (ReferencedSelectorsData.empty())
1905 ReferencedSelectorsData.swap(Record);
1906 else
1907 ReferencedSelectorsData.insert(ReferencedSelectorsData.end(),
1908 Record.begin(), Record.end());
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001909 break;
1910
Sebastian Redl539c5062010-08-18 23:57:32 +00001911 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001912 if (!Record.empty() && Listener)
1913 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00001914 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00001915
Sebastian Redl539c5062010-08-18 23:57:32 +00001916 case SOURCE_LOCATION_OFFSETS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001917 F.SLocOffsets = (const uint32_t *)BlobStart;
1918 F.LocalNumSLocEntries = Record[0];
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001919 F.LocalSLocSize = Record[1];
Douglas Gregor258ae542009-04-27 06:38:32 +00001920 break;
1921
Sebastian Redl539c5062010-08-18 23:57:32 +00001922 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl96371b42010-09-22 00:42:30 +00001923 if (PreloadSLocEntries.empty())
1924 PreloadSLocEntries.swap(Record);
1925 else
1926 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
1927 Record.begin(), Record.end());
Douglas Gregor258ae542009-04-27 06:38:32 +00001928 break;
Douglas Gregorc5046832009-04-27 18:38:38 +00001929
Sebastian Redl539c5062010-08-18 23:57:32 +00001930 case STAT_CACHE: {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001931 ASTStatCache *MyStatCache =
1932 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001933 (const unsigned char *)BlobStart,
1934 NumStatHits, NumStatMisses);
1935 FileMgr.addStatCache(MyStatCache);
Sebastian Redl34522812010-07-16 17:50:48 +00001936 F.StatCache = MyStatCache;
Douglas Gregorc5046832009-04-27 18:38:38 +00001937 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001938 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001939
Sebastian Redl539c5062010-08-18 23:57:32 +00001940 case EXT_VECTOR_DECLS:
Sebastian Redl04f5c312010-07-28 21:38:49 +00001941 // Optimization for the first block.
1942 if (ExtVectorDecls.empty())
1943 ExtVectorDecls.swap(Record);
1944 else
1945 ExtVectorDecls.insert(ExtVectorDecls.end(),
1946 Record.begin(), Record.end());
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001947 break;
1948
Sebastian Redl539c5062010-08-18 23:57:32 +00001949 case VTABLE_USES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001950 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00001951 VTableUses.swap(Record);
1952 break;
1953
Sebastian Redl539c5062010-08-18 23:57:32 +00001954 case DYNAMIC_CLASSES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001955 // Optimization for the first block.
1956 if (DynamicClasses.empty())
1957 DynamicClasses.swap(Record);
1958 else
1959 DynamicClasses.insert(DynamicClasses.end(),
1960 Record.begin(), Record.end());
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00001961 break;
1962
Sebastian Redl539c5062010-08-18 23:57:32 +00001963 case PENDING_IMPLICIT_INSTANTIATIONS:
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00001964 // Optimization for the first block.
Chandler Carruth54080172010-08-25 08:44:16 +00001965 if (PendingInstantiations.empty())
1966 PendingInstantiations.swap(Record);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00001967 else
Chandler Carruth54080172010-08-25 08:44:16 +00001968 PendingInstantiations.insert(PendingInstantiations.end(),
1969 Record.begin(), Record.end());
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00001970 break;
1971
Sebastian Redl539c5062010-08-18 23:57:32 +00001972 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001973 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00001974 SemaDeclRefs.swap(Record);
1975 break;
1976
Sebastian Redl539c5062010-08-18 23:57:32 +00001977 case ORIGINAL_FILE_NAME:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001978 // The primary AST will be the last to get here, so it will be the one
Sebastian Redlb293a452010-07-20 21:20:32 +00001979 // that's used.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00001980 ActualOriginalFileName.assign(BlobStart, BlobLen);
1981 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001982 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001983 break;
Mike Stump11289f42009-09-09 15:08:12 +00001984
Sebastian Redl539c5062010-08-18 23:57:32 +00001985 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00001986 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001987 llvm::StringRef ASTBranch(BlobStart, BlobLen);
1988 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1989 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregord54f3a12009-10-05 21:07:28 +00001990 return IgnorePCH;
1991 }
1992 break;
1993 }
Sebastian Redlfa061442010-07-21 20:07:32 +00001994
Sebastian Redl539c5062010-08-18 23:57:32 +00001995 case MACRO_DEFINITION_OFFSETS:
Sebastian Redlfa061442010-07-21 20:07:32 +00001996 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
1997 F.NumPreallocatedPreprocessingEntities = Record[0];
1998 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001999 break;
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002000
Sebastian Redl539c5062010-08-18 23:57:32 +00002001 case DECL_REPLACEMENTS: {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002002 if (Record.size() % 2 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002003 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002004 return Failure;
2005 }
2006 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl539c5062010-08-18 23:57:32 +00002007 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002008 std::make_pair(&F, Record[I+1]);
2009 break;
2010 }
Sebastian Redlaba202b2010-08-24 22:50:19 +00002011
2012 case ADDITIONAL_TEMPLATE_SPECIALIZATIONS: {
2013 AdditionalTemplateSpecializations &ATS =
2014 AdditionalTemplateSpecializationsPending[Record[0]];
2015 ATS.insert(ATS.end(), Record.begin()+1, Record.end());
2016 break;
2017 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002018 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00002019 First = false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002020 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002021 Error("premature end of bitstream in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002022 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002023}
2024
Sebastian Redl3e31c722010-08-18 23:56:56 +00002025ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName) {
2026 switch(ReadASTCore(FileName)) {
Sebastian Redl2abc0382010-07-16 20:41:52 +00002027 case Failure: return Failure;
2028 case IgnorePCH: return IgnorePCH;
2029 case Success: break;
2030 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002031
2032 // Here comes stuff that we only do once the entire chain is loaded.
2033
Sebastian Redl96371b42010-09-22 00:42:30 +00002034 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redlfa061442010-07-21 20:07:32 +00002035 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redlada023c2010-08-04 20:40:17 +00002036 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2037 TotalNumSelectors = 0;
Sebastian Redl9e687992010-07-19 22:06:55 +00002038 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl96371b42010-09-22 00:42:30 +00002039 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002040 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002041 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl9e687992010-07-19 22:06:55 +00002042 TotalNumTypes += Chain[I]->LocalNumTypes;
2043 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redlfa061442010-07-21 20:07:32 +00002044 TotalNumPreallocatedPreprocessingEntities +=
2045 Chain[I]->NumPreallocatedPreprocessingEntities;
2046 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redlada023c2010-08-04 20:40:17 +00002047 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl9e687992010-07-19 22:06:55 +00002048 }
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002049 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002050 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl9e687992010-07-19 22:06:55 +00002051 TypesLoaded.resize(TotalNumTypes);
2052 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redlfa061442010-07-21 20:07:32 +00002053 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2054 if (PP) {
2055 if (TotalNumIdentifiers > 0)
2056 PP->getHeaderSearchInfo().SetExternalLookup(this);
2057 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2058 if (!PP->getPreprocessingRecord())
2059 PP->createPreprocessingRecord();
2060 PP->getPreprocessingRecord()->SetExternalSource(*this,
2061 TotalNumPreallocatedPreprocessingEntities);
2062 }
2063 }
Sebastian Redlada023c2010-08-04 20:40:17 +00002064 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl96371b42010-09-22 00:42:30 +00002065 // Preload SLocEntries.
2066 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2067 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2068 if (Result != Success)
2069 return Result;
2070 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002071
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002072 // Check the predefines buffers.
Douglas Gregorce3a8292010-07-27 00:27:13 +00002073 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002074 return IgnorePCH;
2075
2076 if (PP) {
2077 // Initialization of keywords and pragmas occurs before the
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002078 // AST file is read, so there may be some identifiers that were
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002079 // loaded into the IdentifierTable before we intercepted the
2080 // creation of identifiers. Iterate through the list of known
2081 // identifiers and determine whether we have to establish
2082 // preprocessor definitions or top-level identifier declaration
2083 // chains for those identifiers.
2084 //
2085 // We copy the IdentifierInfo pointers to a small vector first,
2086 // since de-serializing declarations or macro definitions can add
2087 // new entries into the identifier table, invalidating the
2088 // iterators.
2089 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2090 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2091 IdEnd = PP->getIdentifierTable().end();
2092 Id != IdEnd; ++Id)
2093 Identifiers.push_back(Id->second);
Sebastian Redlfa061442010-07-21 20:07:32 +00002094 // We need to search the tables in all files.
Sebastian Redlfa061442010-07-21 20:07:32 +00002095 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002096 ASTIdentifierLookupTable *IdTable
2097 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2098 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl5c415f32010-07-22 17:01:13 +00002099 // ones.
2100 if (!IdTable)
2101 continue;
Sebastian Redlfa061442010-07-21 20:07:32 +00002102 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2103 IdentifierInfo *II = Identifiers[I];
2104 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002105 ASTIdentifierLookupTrait Info(*this, Chain[J]->Stream, II);
Sebastian Redlfa061442010-07-21 20:07:32 +00002106 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002107 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redlb293a452010-07-20 21:20:32 +00002108 if (Pos == IdTable->end())
2109 continue;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002110
Sebastian Redlb293a452010-07-20 21:20:32 +00002111 // Dereferencing the iterator has the effect of populating the
2112 // IdentifierInfo node with the various declarations it needs.
2113 (void)*Pos;
2114 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002115 }
2116 }
2117
2118 if (Context)
2119 InitializeContext(*Context);
2120
2121 return Success;
2122}
2123
Sebastian Redl3e31c722010-08-18 23:56:56 +00002124ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName) {
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002125 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002126 Chain.push_back(new PerFileData());
Sebastian Redl34522812010-07-16 17:50:48 +00002127 PerFileData &F = *Chain.back();
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002128 if (Prev)
2129 Prev->NextInSource = &F;
2130 else
2131 FirstInSource = &F;
2132 F.Loaders.push_back(Prev);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002133
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002134 // Set the AST file name.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002135 F.FileName = FileName;
2136
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002137 // Open the AST file.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002138 //
2139 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2140 std::string ErrStr;
2141 F.Buffer.reset(llvm::MemoryBuffer::getFileOrSTDIN(FileName, &ErrStr));
2142 if (!F.Buffer) {
2143 Error(ErrStr.c_str());
2144 return IgnorePCH;
2145 }
2146
2147 // Initialize the stream
2148 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2149 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl34522812010-07-16 17:50:48 +00002150 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002151 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002152 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002153
2154 // Sniff for the signature.
2155 if (Stream.Read(8) != 'C' ||
2156 Stream.Read(8) != 'P' ||
2157 Stream.Read(8) != 'C' ||
2158 Stream.Read(8) != 'H') {
2159 Diag(diag::err_not_a_pch_file) << FileName;
2160 return Failure;
2161 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002162
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002163 while (!Stream.AtEndOfStream()) {
2164 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002165
Douglas Gregor92863e42009-04-10 23:10:45 +00002166 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002167 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002168 return Failure;
2169 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002170
2171 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002172
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002173 // We only know the AST subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002174 switch (BlockID) {
2175 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002176 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002177 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002178 return Failure;
2179 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002180 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00002181 case AST_BLOCK_ID:
Sebastian Redl3e31c722010-08-18 23:56:56 +00002182 switch (ReadASTBlock(F)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002183 case Success:
2184 break;
2185
2186 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00002187 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00002188
2189 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00002190 // FIXME: We could consider reading through to the end of this
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002191 // AST block, skipping subblocks, to see if there are other
2192 // AST blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00002193
2194 // Clear out any preallocated source location entries, so that
2195 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002196 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00002197
2198 // Remove the stat cache.
Sebastian Redl34522812010-07-16 17:50:48 +00002199 if (F.StatCache)
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002200 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00002201
Douglas Gregor92863e42009-04-10 23:10:45 +00002202 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002203 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002204 break;
2205 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002206 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002207 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002208 return Failure;
2209 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002210 break;
2211 }
Mike Stump11289f42009-09-09 15:08:12 +00002212 }
2213
Sebastian Redl2abc0382010-07-16 20:41:52 +00002214 return Success;
2215}
2216
Sebastian Redl2c499f62010-08-18 23:56:43 +00002217void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002218 PP = &pp;
Sebastian Redlfa061442010-07-21 20:07:32 +00002219
2220 unsigned TotalNum = 0;
2221 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2222 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2223 if (TotalNum) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002224 if (!PP->getPreprocessingRecord())
2225 PP->createPreprocessingRecord();
Sebastian Redlfa061442010-07-21 20:07:32 +00002226 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregoraae92242010-03-19 21:51:54 +00002227 }
2228}
2229
Sebastian Redl2c499f62010-08-18 23:56:43 +00002230void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002231 Context = &Ctx;
2232 assert(Context && "Passed null context!");
2233
2234 assert(PP && "Forgot to set Preprocessor ?");
2235 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2236 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00002237 PP->setExternalSource(this);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002238
Douglas Gregoraa433012010-10-01 01:18:02 +00002239 // If we have an update block for the TU waiting, we have to add it before
2240 // deserializing the decl.
2241 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2242 if (DCU != DeclContextOffsets.end()) {
2243 // Insertion could invalidate map, so grab vector.
2244 DeclContextInfos T;
2245 T.swap(DCU->second);
2246 DeclContextOffsets.erase(DCU);
2247 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2248 }
2249
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002250 // Load the translation unit declaration
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00002251 GetTranslationUnitDecl();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002252
2253 // Load the special types.
2254 Context->setBuiltinVaListType(
Sebastian Redl539c5062010-08-18 23:57:32 +00002255 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2256 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002257 Context->setObjCIdType(GetType(Id));
Sebastian Redl539c5062010-08-18 23:57:32 +00002258 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002259 Context->setObjCSelType(GetType(Sel));
Sebastian Redl539c5062010-08-18 23:57:32 +00002260 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002261 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl539c5062010-08-18 23:57:32 +00002262 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002263 Context->setObjCClassType(GetType(Class));
Steve Naroff7cae42b2009-07-10 23:34:53 +00002264
Sebastian Redl539c5062010-08-18 23:57:32 +00002265 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002266 Context->setCFConstantStringType(GetType(String));
Mike Stump11289f42009-09-09 15:08:12 +00002267 if (unsigned FastEnum
Sebastian Redl539c5062010-08-18 23:57:32 +00002268 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002269 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl539c5062010-08-18 23:57:32 +00002270 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregor27821ce2009-07-07 16:35:42 +00002271 QualType FileType = GetType(File);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002272 if (FileType.isNull()) {
2273 Error("FILE type is NULL");
2274 return;
2275 }
John McCall9dd450b2009-09-21 23:43:11 +00002276 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregor27821ce2009-07-07 16:35:42 +00002277 Context->setFILEDecl(Typedef->getDecl());
2278 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002279 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002280 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002281 Error("Invalid FILE type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002282 return;
2283 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002284 Context->setFILEDecl(Tag->getDecl());
2285 }
2286 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002287 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002288 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002289 if (Jmp_bufType.isNull()) {
2290 Error("jmp_bug type is NULL");
2291 return;
2292 }
John McCall9dd450b2009-09-21 23:43:11 +00002293 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002294 Context->setjmp_bufDecl(Typedef->getDecl());
2295 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002296 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002297 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002298 Error("Invalid jmp_buf type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002299 return;
2300 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002301 Context->setjmp_bufDecl(Tag->getDecl());
2302 }
2303 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002304 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002305 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002306 if (Sigjmp_bufType.isNull()) {
2307 Error("sigjmp_buf type is NULL");
2308 return;
2309 }
John McCall9dd450b2009-09-21 23:43:11 +00002310 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002311 Context->setsigjmp_bufDecl(Typedef->getDecl());
2312 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002313 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002314 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stumpa4de80b2009-07-28 02:25:19 +00002315 Context->setsigjmp_bufDecl(Tag->getDecl());
2316 }
2317 }
Mike Stump11289f42009-09-09 15:08:12 +00002318 if (unsigned ObjCIdRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002319 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002320 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump11289f42009-09-09 15:08:12 +00002321 if (unsigned ObjCClassRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002322 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002323 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002324 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpd0153282009-10-20 02:12:22 +00002325 Context->setBlockDescriptorType(GetType(String));
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002326 if (unsigned String
Sebastian Redl539c5062010-08-18 23:57:32 +00002327 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002328 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002329 if (unsigned ObjCSelRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002330 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002331 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002332 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002333 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002334
Sebastian Redl539c5062010-08-18 23:57:32 +00002335 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002336 Context->setInt128Installed();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002337}
2338
Douglas Gregor45fe0362009-05-12 01:31:05 +00002339/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002340/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00002341/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002342std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Daniel Dunbar3b951482009-12-03 09:13:06 +00002343 Diagnostic &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002344 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002345 std::string ErrStr;
2346 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002347 Buffer.reset(llvm::MemoryBuffer::getFile(ASTFileName.c_str(), &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00002348 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00002349 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002350 return std::string();
2351 }
2352
2353 // Initialize the stream
2354 llvm::BitstreamReader StreamFile;
2355 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00002356 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00002357 (const unsigned char *)Buffer->getBufferEnd());
2358 Stream.init(StreamFile);
2359
2360 // Sniff for the signature.
2361 if (Stream.Read(8) != 'C' ||
2362 Stream.Read(8) != 'P' ||
2363 Stream.Read(8) != 'C' ||
2364 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002365 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002366 return std::string();
2367 }
2368
2369 RecordData Record;
2370 while (!Stream.AtEndOfStream()) {
2371 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002372
Douglas Gregor45fe0362009-05-12 01:31:05 +00002373 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2374 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00002375
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002376 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002377 switch (BlockID) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002378 case AST_BLOCK_ID:
2379 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002380 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002381 return std::string();
2382 }
2383 break;
Mike Stump11289f42009-09-09 15:08:12 +00002384
Douglas Gregor45fe0362009-05-12 01:31:05 +00002385 default:
2386 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002387 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002388 return std::string();
2389 }
2390 break;
2391 }
2392 continue;
2393 }
2394
2395 if (Code == llvm::bitc::END_BLOCK) {
2396 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002397 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002398 return std::string();
2399 }
2400 continue;
2401 }
2402
2403 if (Code == llvm::bitc::DEFINE_ABBREV) {
2404 Stream.ReadAbbrevRecord();
2405 continue;
2406 }
2407
2408 Record.clear();
2409 const char *BlobStart = 0;
2410 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002411 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl539c5062010-08-18 23:57:32 +00002412 == ORIGINAL_FILE_NAME)
Douglas Gregor45fe0362009-05-12 01:31:05 +00002413 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00002414 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00002415
2416 return std::string();
2417}
2418
Douglas Gregor55abb232009-04-10 20:39:37 +00002419/// \brief Parse the record that corresponds to a LangOptions data
2420/// structure.
2421///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002422/// This routine parses the language options from the AST file and then gives
2423/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00002424///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002425/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002426bool ASTReader::ParseLanguageOptions(
Douglas Gregor55abb232009-04-10 20:39:37 +00002427 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002428 if (Listener) {
2429 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00002430
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002431 #define PARSE_LANGOPT(Option) \
2432 LangOpts.Option = Record[Idx]; \
2433 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00002434
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002435 unsigned Idx = 0;
2436 PARSE_LANGOPT(Trigraphs);
2437 PARSE_LANGOPT(BCPLComment);
2438 PARSE_LANGOPT(DollarIdents);
2439 PARSE_LANGOPT(AsmPreprocessor);
2440 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00002441 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002442 PARSE_LANGOPT(ImplicitInt);
2443 PARSE_LANGOPT(Digraphs);
2444 PARSE_LANGOPT(HexFloats);
2445 PARSE_LANGOPT(C99);
2446 PARSE_LANGOPT(Microsoft);
2447 PARSE_LANGOPT(CPlusPlus);
2448 PARSE_LANGOPT(CPlusPlus0x);
2449 PARSE_LANGOPT(CXXOperatorNames);
2450 PARSE_LANGOPT(ObjC1);
2451 PARSE_LANGOPT(ObjC2);
2452 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00002453 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00002454 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002455 PARSE_LANGOPT(PascalStrings);
2456 PARSE_LANGOPT(WritableStrings);
2457 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00002458 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002459 PARSE_LANGOPT(Exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +00002460 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002461 PARSE_LANGOPT(NeXTRuntime);
2462 PARSE_LANGOPT(Freestanding);
2463 PARSE_LANGOPT(NoBuiltin);
2464 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00002465 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002466 PARSE_LANGOPT(Blocks);
2467 PARSE_LANGOPT(EmitAllDecls);
2468 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00002469 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2470 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002471 PARSE_LANGOPT(HeinousExtensions);
2472 PARSE_LANGOPT(Optimize);
2473 PARSE_LANGOPT(OptimizeSize);
2474 PARSE_LANGOPT(Static);
2475 PARSE_LANGOPT(PICLevel);
2476 PARSE_LANGOPT(GNUInline);
2477 PARSE_LANGOPT(NoInline);
2478 PARSE_LANGOPT(AccessControl);
2479 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00002480 PARSE_LANGOPT(ShortWChar);
Chris Lattner51924e512010-06-26 21:25:03 +00002481 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
2482 LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00002483 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00002484 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002485 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00002486 PARSE_LANGOPT(OpenCL);
Mike Stumpd9546382009-12-12 01:27:46 +00002487 PARSE_LANGOPT(CatchUndefined);
2488 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002489 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00002490
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002491 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00002492 }
Douglas Gregor55abb232009-04-10 20:39:37 +00002493
2494 return false;
2495}
2496
Sebastian Redl2c499f62010-08-18 23:56:43 +00002497void ASTReader::ReadPreprocessedEntities() {
Douglas Gregoraae92242010-03-19 21:51:54 +00002498 ReadDefinedMacros();
2499}
2500
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002501/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002502ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002503 PerFileData *F = 0;
2504 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2505 F = Chain[N - I - 1];
2506 if (Index < F->LocalNumTypes)
2507 break;
2508 Index -= F->LocalNumTypes;
2509 }
2510 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redlb2831db2010-07-20 22:55:31 +00002511 return RecordLocation(&F->DeclsCursor, F->TypeOffsets[Index]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002512}
2513
2514/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002515///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002516/// The index is the type ID, shifted and minus the number of predefs. This
2517/// routine actually reads the record corresponding to the type at the given
2518/// location. It is a helper routine for GetType, which deals with reading type
2519/// IDs.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002520QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002521 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlb2831db2010-07-20 22:55:31 +00002522 llvm::BitstreamCursor &DeclsCursor = *Loc.first;
Sebastian Redl34522812010-07-16 17:50:48 +00002523
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002524 // Keep track of where we are in the stream, then jump back there
2525 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002526 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002527
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002528 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00002529
Douglas Gregor1342e842009-07-06 18:54:52 +00002530 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00002531 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00002532
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002533 DeclsCursor.JumpToBit(Loc.second);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002534 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002535 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00002536 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2537 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002538 if (Record.size() != 2) {
2539 Error("Incorrect encoding of extended qualifier type");
2540 return QualType();
2541 }
Douglas Gregor455b8f42009-04-15 22:00:08 +00002542 QualType Base = GetType(Record[0]);
John McCall8ccfcb52009-09-24 19:53:00 +00002543 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2544 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00002545 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002546
Sebastian Redl539c5062010-08-18 23:57:32 +00002547 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002548 if (Record.size() != 1) {
2549 Error("Incorrect encoding of complex type");
2550 return QualType();
2551 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002552 QualType ElemType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002553 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002554 }
2555
Sebastian Redl539c5062010-08-18 23:57:32 +00002556 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002557 if (Record.size() != 1) {
2558 Error("Incorrect encoding of pointer type");
2559 return QualType();
2560 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002561 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002562 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002563 }
2564
Sebastian Redl539c5062010-08-18 23:57:32 +00002565 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002566 if (Record.size() != 1) {
2567 Error("Incorrect encoding of block pointer type");
2568 return QualType();
2569 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002570 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002571 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002572 }
2573
Sebastian Redl539c5062010-08-18 23:57:32 +00002574 case TYPE_LVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002575 if (Record.size() != 1) {
2576 Error("Incorrect encoding of lvalue reference type");
2577 return QualType();
2578 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002579 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002580 return Context->getLValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002581 }
2582
Sebastian Redl539c5062010-08-18 23:57:32 +00002583 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002584 if (Record.size() != 1) {
2585 Error("Incorrect encoding of rvalue reference type");
2586 return QualType();
2587 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002588 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002589 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002590 }
2591
Sebastian Redl539c5062010-08-18 23:57:32 +00002592 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00002593 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002594 Error("Incorrect encoding of member pointer type");
2595 return QualType();
2596 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002597 QualType PointeeType = GetType(Record[0]);
2598 QualType ClassType = GetType(Record[1]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002599 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002600 }
2601
Sebastian Redl539c5062010-08-18 23:57:32 +00002602 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002603 QualType ElementType = GetType(Record[0]);
2604 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2605 unsigned IndexTypeQuals = Record[2];
2606 unsigned Idx = 3;
2607 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00002608 return Context->getConstantArrayType(ElementType, Size,
2609 ASM, IndexTypeQuals);
2610 }
2611
Sebastian Redl539c5062010-08-18 23:57:32 +00002612 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002613 QualType ElementType = GetType(Record[0]);
2614 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2615 unsigned IndexTypeQuals = Record[2];
Chris Lattner8575daa2009-04-27 21:45:14 +00002616 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002617 }
2618
Sebastian Redl539c5062010-08-18 23:57:32 +00002619 case TYPE_VARIABLE_ARRAY: {
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002620 QualType ElementType = GetType(Record[0]);
2621 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2622 unsigned IndexTypeQuals = Record[2];
Douglas Gregor04318252009-07-06 15:59:29 +00002623 SourceLocation LBLoc = SourceLocation::getFromRawEncoding(Record[3]);
2624 SourceLocation RBLoc = SourceLocation::getFromRawEncoding(Record[4]);
Sebastian Redlc67764e2010-07-22 22:43:28 +00002625 return Context->getVariableArrayType(ElementType, ReadExpr(DeclsCursor),
Douglas Gregor04318252009-07-06 15:59:29 +00002626 ASM, IndexTypeQuals,
2627 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002628 }
2629
Sebastian Redl539c5062010-08-18 23:57:32 +00002630 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002631 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002632 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002633 return QualType();
2634 }
2635
2636 QualType ElementType = GetType(Record[0]);
2637 unsigned NumElements = Record[1];
Chris Lattner37141f42010-06-23 06:00:24 +00002638 unsigned AltiVecSpec = Record[2];
2639 return Context->getVectorType(ElementType, NumElements,
2640 (VectorType::AltiVecSpecific)AltiVecSpec);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002641 }
2642
Sebastian Redl539c5062010-08-18 23:57:32 +00002643 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002644 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002645 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002646 return QualType();
2647 }
2648
2649 QualType ElementType = GetType(Record[0]);
2650 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00002651 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002652 }
2653
Sebastian Redl539c5062010-08-18 23:57:32 +00002654 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002655 if (Record.size() != 4) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002656 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002657 return QualType();
2658 }
2659 QualType ResultType = GetType(Record[0]);
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002660 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00002661 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002662 }
2663
Sebastian Redl539c5062010-08-18 23:57:32 +00002664 case TYPE_FUNCTION_PROTO: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002665 QualType ResultType = GetType(Record[0]);
Douglas Gregordc728752009-12-22 18:11:50 +00002666 bool NoReturn = Record[1];
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002667 unsigned RegParm = Record[2];
2668 CallingConv CallConv = (CallingConv)Record[3];
2669 unsigned Idx = 4;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002670 unsigned NumParams = Record[Idx++];
2671 llvm::SmallVector<QualType, 16> ParamTypes;
2672 for (unsigned I = 0; I != NumParams; ++I)
2673 ParamTypes.push_back(GetType(Record[Idx++]));
2674 bool isVariadic = Record[Idx++];
2675 unsigned Quals = Record[Idx++];
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002676 bool hasExceptionSpec = Record[Idx++];
2677 bool hasAnyExceptionSpec = Record[Idx++];
2678 unsigned NumExceptions = Record[Idx++];
2679 llvm::SmallVector<QualType, 2> Exceptions;
2680 for (unsigned I = 0; I != NumExceptions; ++I)
2681 Exceptions.push_back(GetType(Record[Idx++]));
Jay Foad7d0479f2009-05-21 09:52:38 +00002682 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002683 isVariadic, Quals, hasExceptionSpec,
2684 hasAnyExceptionSpec, NumExceptions,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00002685 Exceptions.data(),
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002686 FunctionType::ExtInfo(NoReturn, RegParm,
2687 CallConv));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002688 }
2689
Sebastian Redl539c5062010-08-18 23:57:32 +00002690 case TYPE_UNRESOLVED_USING:
John McCallb96ec562009-12-04 22:46:56 +00002691 return Context->getTypeDeclType(
2692 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2693
Sebastian Redl539c5062010-08-18 23:57:32 +00002694 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002695 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002696 Error("incorrect encoding of typedef type");
2697 return QualType();
2698 }
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002699 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2700 QualType Canonical = GetType(Record[1]);
2701 return Context->getTypedefType(Decl, Canonical);
2702 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002703
Sebastian Redl539c5062010-08-18 23:57:32 +00002704 case TYPE_TYPEOF_EXPR:
Sebastian Redlc67764e2010-07-22 22:43:28 +00002705 return Context->getTypeOfExprType(ReadExpr(DeclsCursor));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002706
Sebastian Redl539c5062010-08-18 23:57:32 +00002707 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002708 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002709 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002710 return QualType();
2711 }
2712 QualType UnderlyingType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002713 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002714 }
Mike Stump11289f42009-09-09 15:08:12 +00002715
Sebastian Redl539c5062010-08-18 23:57:32 +00002716 case TYPE_DECLTYPE:
Sebastian Redlc67764e2010-07-22 22:43:28 +00002717 return Context->getDecltypeType(ReadExpr(DeclsCursor));
Anders Carlsson81df7b82009-06-24 19:06:50 +00002718
Sebastian Redl539c5062010-08-18 23:57:32 +00002719 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002720 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002721 Error("incorrect encoding of record type");
2722 return QualType();
2723 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002724 bool IsDependent = Record[0];
2725 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
2726 T->Dependent = IsDependent;
2727 return T;
2728 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002729
Sebastian Redl539c5062010-08-18 23:57:32 +00002730 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002731 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002732 Error("incorrect encoding of enum type");
2733 return QualType();
2734 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002735 bool IsDependent = Record[0];
2736 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
2737 T->Dependent = IsDependent;
2738 return T;
2739 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00002740
Sebastian Redl539c5062010-08-18 23:57:32 +00002741 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002742 unsigned Idx = 0;
2743 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2744 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2745 QualType NamedType = GetType(Record[Idx++]);
2746 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00002747 }
2748
Sebastian Redl539c5062010-08-18 23:57:32 +00002749 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00002750 unsigned Idx = 0;
2751 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00002752 return Context->getObjCInterfaceType(ItfD);
2753 }
2754
Sebastian Redl539c5062010-08-18 23:57:32 +00002755 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00002756 unsigned Idx = 0;
2757 QualType Base = GetType(Record[Idx++]);
Chris Lattner587cbe12009-04-22 06:45:28 +00002758 unsigned NumProtos = Record[Idx++];
2759 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2760 for (unsigned I = 0; I != NumProtos; ++I)
2761 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
John McCall8b07ec22010-05-15 11:32:37 +00002762 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00002763 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002764
Sebastian Redl539c5062010-08-18 23:57:32 +00002765 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00002766 unsigned Idx = 0;
John McCall8b07ec22010-05-15 11:32:37 +00002767 QualType Pointee = GetType(Record[Idx++]);
2768 return Context->getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00002769 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00002770
Sebastian Redl539c5062010-08-18 23:57:32 +00002771 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00002772 unsigned Idx = 0;
2773 QualType Parm = GetType(Record[Idx++]);
2774 QualType Replacement = GetType(Record[Idx++]);
2775 return
2776 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2777 Replacement);
2778 }
John McCalle78aac42010-03-10 03:28:59 +00002779
Sebastian Redl539c5062010-08-18 23:57:32 +00002780 case TYPE_INJECTED_CLASS_NAME: {
John McCalle78aac42010-03-10 03:28:59 +00002781 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2782 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00002783 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002784 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00002785 return
2786 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00002787 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002788
Sebastian Redl539c5062010-08-18 23:57:32 +00002789 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002790 unsigned Idx = 0;
2791 unsigned Depth = Record[Idx++];
2792 unsigned Index = Record[Idx++];
2793 bool Pack = Record[Idx++];
2794 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
2795 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
2796 }
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002797
Sebastian Redl539c5062010-08-18 23:57:32 +00002798 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002799 unsigned Idx = 0;
2800 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2801 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2802 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00002803 QualType Canon = GetType(Record[Idx++]);
2804 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002805 }
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002806
Sebastian Redl539c5062010-08-18 23:57:32 +00002807 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002808 unsigned Idx = 0;
2809 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2810 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2811 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2812 unsigned NumArgs = Record[Idx++];
2813 llvm::SmallVector<TemplateArgument, 8> Args;
2814 Args.reserve(NumArgs);
2815 while (NumArgs--)
Sebastian Redlc67764e2010-07-22 22:43:28 +00002816 Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002817 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
2818 Args.size(), Args.data());
2819 }
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00002820
Sebastian Redl539c5062010-08-18 23:57:32 +00002821 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00002822 unsigned Idx = 0;
2823
2824 // ArrayType
2825 QualType ElementType = GetType(Record[Idx++]);
2826 ArrayType::ArraySizeModifier ASM
2827 = (ArrayType::ArraySizeModifier)Record[Idx++];
2828 unsigned IndexTypeQuals = Record[Idx++];
2829
2830 // DependentSizedArrayType
Sebastian Redlc67764e2010-07-22 22:43:28 +00002831 Expr *NumElts = ReadExpr(DeclsCursor);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00002832 SourceRange Brackets = ReadSourceRange(Record, Idx);
2833
2834 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
2835 IndexTypeQuals, Brackets);
2836 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002837
Sebastian Redl539c5062010-08-18 23:57:32 +00002838 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002839 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002840 bool IsDependent = Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002841 TemplateName Name = ReadTemplateName(Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002842 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc67764e2010-07-22 22:43:28 +00002843 ReadTemplateArgumentList(Args, DeclsCursor, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002844 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002845 QualType T;
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002846 if (Canon.isNull())
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002847 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
2848 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002849 else
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002850 T = Context->getTemplateSpecializationType(Name, Args.data(),
2851 Args.size(), Canon);
2852 T->Dependent = IsDependent;
2853 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002854 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002855 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002856 // Suppress a GCC warning
2857 return QualType();
2858}
2859
John McCall8f115c62009-10-16 21:56:05 +00002860namespace {
2861
2862class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00002863 ASTReader &Reader;
Sebastian Redlc67764e2010-07-22 22:43:28 +00002864 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redl2c499f62010-08-18 23:56:43 +00002865 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00002866 unsigned &Idx;
2867
2868public:
Sebastian Redl2c499f62010-08-18 23:56:43 +00002869 TypeLocReader(ASTReader &Reader, llvm::BitstreamCursor &Cursor,
2870 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redlc67764e2010-07-22 22:43:28 +00002871 : Reader(Reader), DeclsCursor(Cursor), Record(Record), Idx(Idx) { }
John McCall8f115c62009-10-16 21:56:05 +00002872
John McCall17001972009-10-18 01:05:36 +00002873 // We want compile-time assurance that we've enumerated all of
2874 // these, so unfortunately we have to declare them first, then
2875 // define them out-of-line.
2876#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00002877#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00002878 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00002879#include "clang/AST/TypeLocNodes.def"
2880
John McCall17001972009-10-18 01:05:36 +00002881 void VisitFunctionTypeLoc(FunctionTypeLoc);
2882 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00002883};
2884
2885}
2886
John McCall17001972009-10-18 01:05:36 +00002887void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00002888 // nothing to do
2889}
John McCall17001972009-10-18 01:05:36 +00002890void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +00002891 TL.setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2892 if (TL.needsExtraLocalData()) {
2893 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
2894 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
2895 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
2896 TL.setModeAttr(Record[Idx++]);
2897 }
John McCall8f115c62009-10-16 21:56:05 +00002898}
John McCall17001972009-10-18 01:05:36 +00002899void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
2900 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002901}
John McCall17001972009-10-18 01:05:36 +00002902void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
2903 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002904}
John McCall17001972009-10-18 01:05:36 +00002905void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
2906 TL.setCaretLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002907}
John McCall17001972009-10-18 01:05:36 +00002908void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
2909 TL.setAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002910}
John McCall17001972009-10-18 01:05:36 +00002911void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
2912 TL.setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002913}
John McCall17001972009-10-18 01:05:36 +00002914void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
2915 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002916}
John McCall17001972009-10-18 01:05:36 +00002917void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
2918 TL.setLBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2919 TL.setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002920 if (Record[Idx++])
Sebastian Redlc67764e2010-07-22 22:43:28 +00002921 TL.setSizeExpr(Reader.ReadExpr(DeclsCursor));
Douglas Gregor12bfa382009-10-17 00:13:19 +00002922 else
John McCall17001972009-10-18 01:05:36 +00002923 TL.setSizeExpr(0);
2924}
2925void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
2926 VisitArrayTypeLoc(TL);
2927}
2928void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
2929 VisitArrayTypeLoc(TL);
2930}
2931void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
2932 VisitArrayTypeLoc(TL);
2933}
2934void TypeLocReader::VisitDependentSizedArrayTypeLoc(
2935 DependentSizedArrayTypeLoc TL) {
2936 VisitArrayTypeLoc(TL);
2937}
2938void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
2939 DependentSizedExtVectorTypeLoc TL) {
2940 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2941}
2942void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
2943 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2944}
2945void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
2946 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2947}
2948void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
2949 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2950 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor7fb25412010-10-01 18:44:50 +00002951 TL.setTrailingReturn(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00002952 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCalle6347002009-10-23 01:28:53 +00002953 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall17001972009-10-18 01:05:36 +00002954 }
2955}
2956void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
2957 VisitFunctionTypeLoc(TL);
2958}
2959void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
2960 VisitFunctionTypeLoc(TL);
2961}
John McCallb96ec562009-12-04 22:46:56 +00002962void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
2963 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2964}
John McCall17001972009-10-18 01:05:36 +00002965void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
2966 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2967}
2968void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +00002969 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2970 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2971 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall17001972009-10-18 01:05:36 +00002972}
2973void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +00002974 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2975 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2976 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Sebastian Redlc67764e2010-07-22 22:43:28 +00002977 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(DeclsCursor, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002978}
2979void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
2980 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2981}
2982void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
2983 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2984}
2985void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
2986 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2987}
John McCall17001972009-10-18 01:05:36 +00002988void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
2989 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2990}
John McCallcebee162009-10-18 09:09:24 +00002991void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
2992 SubstTemplateTypeParmTypeLoc TL) {
2993 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2994}
John McCall17001972009-10-18 01:05:36 +00002995void TypeLocReader::VisitTemplateSpecializationTypeLoc(
2996 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +00002997 TL.setTemplateNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2998 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2999 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
3000 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3001 TL.setArgLocInfo(i,
3002 Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(i).getKind(),
Sebastian Redlc67764e2010-07-22 22:43:28 +00003003 DeclsCursor, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003004}
Abramo Bagnara6150c882010-05-11 21:36:43 +00003005void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +00003006 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
3007 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003008}
John McCalle78aac42010-03-10 03:28:59 +00003009void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
3010 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
3011}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00003012void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +00003013 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
3014 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003015 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
3016}
John McCallc392f372010-06-11 00:33:02 +00003017void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3018 DependentTemplateSpecializationTypeLoc TL) {
3019 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
3020 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
3021 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
3022 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
3023 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
3024 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3025 TL.setArgLocInfo(I,
3026 Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(I).getKind(),
Sebastian Redlc67764e2010-07-22 22:43:28 +00003027 DeclsCursor, Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003028}
John McCall17001972009-10-18 01:05:36 +00003029void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
3030 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00003031}
3032void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3033 TL.setHasBaseTypeAsWritten(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00003034 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
3035 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
3036 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
3037 TL.setProtocolLoc(i, SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00003038}
John McCallfc93cf92009-10-22 22:37:11 +00003039void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
3040 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCallfc93cf92009-10-22 22:37:11 +00003041}
John McCall8f115c62009-10-16 21:56:05 +00003042
Sebastian Redl2c499f62010-08-18 23:56:43 +00003043TypeSourceInfo *ASTReader::GetTypeSourceInfo(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003044 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00003045 unsigned &Idx) {
3046 QualType InfoTy = GetType(Record[Idx++]);
3047 if (InfoTy.isNull())
3048 return 0;
3049
John McCallbcd03502009-12-07 02:54:59 +00003050 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redlc67764e2010-07-22 22:43:28 +00003051 TypeLocReader TLR(*this, DeclsCursor, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00003052 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00003053 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00003054 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00003055}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003056
Sebastian Redl539c5062010-08-18 23:57:32 +00003057QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00003058 unsigned FastQuals = ID & Qualifiers::FastMask;
3059 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003060
Sebastian Redl539c5062010-08-18 23:57:32 +00003061 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003062 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00003063 switch ((PredefinedTypeIDs)Index) {
3064 case PREDEF_TYPE_NULL_ID: return QualType();
3065 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3066 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003067
Sebastian Redl539c5062010-08-18 23:57:32 +00003068 case PREDEF_TYPE_CHAR_U_ID:
3069 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003070 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00003071 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003072 break;
3073
Sebastian Redl539c5062010-08-18 23:57:32 +00003074 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3075 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3076 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3077 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3078 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3079 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3080 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3081 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3082 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3083 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3084 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3085 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3086 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3087 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3088 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3089 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3090 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3091 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3092 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3093 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3094 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3095 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3096 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3097 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003098 }
3099
3100 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00003101 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003102 }
3103
Sebastian Redl539c5062010-08-18 23:57:32 +00003104 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003105 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00003106 if (TypesLoaded[Index].isNull()) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003107 TypesLoaded[Index] = ReadTypeRecord(Index);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003108 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003109 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003110 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003111 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003112 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00003113 }
Mike Stump11289f42009-09-09 15:08:12 +00003114
John McCall8ccfcb52009-09-24 19:53:00 +00003115 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003116}
3117
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003118TypeID ASTReader::GetTypeID(QualType T) const {
3119 return MakeTypeID(T,
3120 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3121}
3122
3123TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3124 if (T.isNull())
3125 return TypeIdx();
3126 assert(!T.getLocalFastQualifiers());
3127
3128 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3129 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3130 // comparing keys of ASTDeclContextNameLookupTable.
3131 // If the type didn't come from the AST file use a specially marked index
3132 // so that any hash/key comparison fail since no such index is stored
3133 // in a AST file.
3134 if (I == TypeIdxs.end())
3135 return TypeIdx(-1);
3136 return I->second;
3137}
3138
John McCall0ad16662009-10-29 08:12:44 +00003139TemplateArgumentLocInfo
Sebastian Redl2c499f62010-08-18 23:56:43 +00003140ASTReader::GetTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003141 llvm::BitstreamCursor &DeclsCursor,
John McCall0ad16662009-10-29 08:12:44 +00003142 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003143 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00003144 switch (Kind) {
3145 case TemplateArgument::Expression:
Sebastian Redlc67764e2010-07-22 22:43:28 +00003146 return ReadExpr(DeclsCursor);
John McCall0ad16662009-10-29 08:12:44 +00003147 case TemplateArgument::Type:
Sebastian Redlc67764e2010-07-22 22:43:28 +00003148 return GetTypeSourceInfo(DeclsCursor, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003149 case TemplateArgument::Template: {
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003150 SourceRange QualifierRange = ReadSourceRange(Record, Index);
3151 SourceLocation TemplateNameLoc = ReadSourceLocation(Record, Index);
3152 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003153 }
John McCall0ad16662009-10-29 08:12:44 +00003154 case TemplateArgument::Null:
3155 case TemplateArgument::Integral:
3156 case TemplateArgument::Declaration:
3157 case TemplateArgument::Pack:
3158 return TemplateArgumentLocInfo();
3159 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00003160 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00003161 return TemplateArgumentLocInfo();
3162}
3163
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003164TemplateArgumentLoc
Sebastian Redl2c499f62010-08-18 23:56:43 +00003165ASTReader::ReadTemplateArgumentLoc(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003166 const RecordData &Record, unsigned &Index) {
3167 TemplateArgument Arg = ReadTemplateArgument(DeclsCursor, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003168
3169 if (Arg.getKind() == TemplateArgument::Expression) {
3170 if (Record[Index++]) // bool InfoHasSameExpr.
3171 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3172 }
3173 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(Arg.getKind(),
Sebastian Redlc67764e2010-07-22 22:43:28 +00003174 DeclsCursor,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003175 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003176}
3177
Sebastian Redl2c499f62010-08-18 23:56:43 +00003178Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00003179 return GetDecl(ID);
3180}
3181
Sebastian Redl2c499f62010-08-18 23:56:43 +00003182TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003183 if (!DeclsLoaded[0]) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00003184 ReadDeclRecord(0, 1);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003185 if (DeserializationListener)
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003186 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003187 }
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00003188
3189 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3190}
3191
Sebastian Redl539c5062010-08-18 23:57:32 +00003192Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003193 if (ID == 0)
3194 return 0;
3195
Douglas Gregor745ed142009-04-25 18:35:21 +00003196 if (ID > DeclsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003197 Error("declaration ID out-of-range for AST file");
Douglas Gregor745ed142009-04-25 18:35:21 +00003198 return 0;
3199 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003200
Douglas Gregor745ed142009-04-25 18:35:21 +00003201 unsigned Index = ID - 1;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003202 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003203 ReadDeclRecord(Index, ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003204 if (DeserializationListener)
3205 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3206 }
Douglas Gregor745ed142009-04-25 18:35:21 +00003207
3208 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003209}
3210
Chris Lattner9c28af02009-04-27 05:46:25 +00003211/// \brief Resolve the offset of a statement into a statement.
3212///
3213/// This operation will read a new statement from the external
3214/// source each time it is called, and is meant to be used via a
3215/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00003216Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Sebastian Redl5c415f32010-07-22 17:01:13 +00003217 // Offset here is a global offset across the entire chain.
3218 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3219 PerFileData &F = *Chain[N - I - 1];
3220 if (Offset < F.SizeInBits) {
3221 // Since we know that this statement is part of a decl, make sure to use
3222 // the decl cursor to read it.
3223 F.DeclsCursor.JumpToBit(Offset);
3224 return ReadStmtFromStream(F.DeclsCursor);
3225 }
3226 Offset -= F.SizeInBits;
3227 }
3228 llvm_unreachable("Broken chain");
Douglas Gregor3c3aa612009-04-18 00:07:54 +00003229}
3230
Sebastian Redl2c499f62010-08-18 23:56:43 +00003231bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00003232 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump11289f42009-09-09 15:08:12 +00003233 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003234 "DeclContext has no lexical decls in storage");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003235
Sebastian Redl5c415f32010-07-22 17:01:13 +00003236 // There might be lexical decls in multiple parts of the chain, for the TU
3237 // at least.
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003238 // DeclContextOffsets might reallocate as we load additional decls below,
3239 // so make a copy of the vector.
3240 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl5c415f32010-07-22 17:01:13 +00003241 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3242 I != E; ++I) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003243 // IDs can be 0 if this context doesn't contain declarations.
3244 if (!I->LexicalDecls)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003245 continue;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003246
3247 // Load all of the declaration IDs
Sebastian Redl4102dd52010-09-28 02:55:49 +00003248 for (const DeclID *ID = I->LexicalDecls, *IDE = ID + I->NumLexicalDecls;
3249 ID != IDE; ++ID) {
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003250 Decl *D = GetDecl(*ID);
3251 assert(D && "Null decl in lexical decls");
3252 Decls.push_back(D);
3253 }
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003254 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003255
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003256 ++NumLexicalDeclContextsRead;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003257 return false;
3258}
3259
John McCall75b960e2010-06-01 09:23:16 +00003260DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00003261ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00003262 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003263 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003264 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003265 if (!Name)
3266 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3267 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003268
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003269 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl471ac2f2010-08-24 00:49:55 +00003270 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003271 // and namespaces. For any given name, the last available results replace
3272 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl5c415f32010-07-22 17:01:13 +00003273 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003274 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl5c415f32010-07-22 17:01:13 +00003275 I != E; ++I) {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003276 if (!I->NameLookupTableData)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003277 continue;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003278
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003279 ASTDeclContextNameLookupTable *LookupTable =
3280 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3281 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3282 if (Pos == LookupTable->end())
Sebastian Redl5c415f32010-07-22 17:01:13 +00003283 continue;
3284
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003285 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3286 for (; Data.first != Data.second; ++Data.first)
3287 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003288 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003289 }
3290
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003291 ++NumVisibleDeclContextsRead;
John McCall75b960e2010-06-01 09:23:16 +00003292
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003293 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00003294 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003295}
3296
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00003297void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3298 assert(DC->hasExternalVisibleStorage() &&
3299 "DeclContext has no visible decls in storage");
3300
3301 llvm::SmallVector<NamedDecl *, 64> Decls;
3302 // There might be visible decls in multiple parts of the chain, for the TU
3303 // and namespaces.
3304 DeclContextInfos &Infos = DeclContextOffsets[DC];
3305 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3306 I != E; ++I) {
3307 if (!I->NameLookupTableData)
3308 continue;
3309
3310 ASTDeclContextNameLookupTable *LookupTable =
3311 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3312 for (ASTDeclContextNameLookupTable::item_iterator
3313 ItemI = LookupTable->item_begin(),
3314 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3315 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3316 = *ItemI;
3317 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3318 Decls.clear();
3319 for (; Data.first != Data.second; ++Data.first)
3320 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3321 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3322 }
3323 }
3324}
3325
Sebastian Redl2c499f62010-08-18 23:56:43 +00003326void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003327 assert(Consumer);
3328 while (!InterestingDecls.empty()) {
3329 DeclGroupRef DG(InterestingDecls.front());
3330 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00003331 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003332 }
3333}
3334
Sebastian Redl2c499f62010-08-18 23:56:43 +00003335void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00003336 this->Consumer = Consumer;
3337
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003338 if (!Consumer)
3339 return;
3340
3341 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003342 // Force deserialization of this decl, which will cause it to be queued for
3343 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00003344 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003345 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00003346
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003347 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003348}
3349
Sebastian Redl2c499f62010-08-18 23:56:43 +00003350void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003351 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003352
Mike Stump11289f42009-09-09 15:08:12 +00003353 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003354 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00003355 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00003356 unsigned NumDeclsLoaded
3357 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3358 (Decl *)0);
3359 unsigned NumIdentifiersLoaded
3360 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3361 IdentifiersLoaded.end(),
3362 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00003363 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003364 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3365 SelectorsLoaded.end(),
3366 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00003367
Douglas Gregorc5046832009-04-27 18:38:38 +00003368 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3369 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor258ae542009-04-27 06:38:32 +00003370 if (TotalNumSLocEntries)
3371 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3372 NumSLocEntriesRead, TotalNumSLocEntries,
3373 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00003374 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003375 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003376 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3377 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3378 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003379 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003380 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3381 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00003382 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003383 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00003384 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3385 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00003386 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003387 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00003388 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3389 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003390 if (TotalNumStatements)
3391 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3392 NumStatementsRead, TotalNumStatements,
3393 ((float)NumStatementsRead/TotalNumStatements * 100));
3394 if (TotalNumMacros)
3395 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3396 NumMacrosRead, TotalNumMacros,
3397 ((float)NumMacrosRead/TotalNumMacros * 100));
3398 if (TotalLexicalDeclContexts)
3399 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3400 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3401 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3402 * 100));
3403 if (TotalVisibleDeclContexts)
3404 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3405 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3406 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3407 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003408 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003409 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003410 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3411 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00003412 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003413 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003414 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003415 std::fprintf(stderr, "\n");
3416}
3417
Sebastian Redl2c499f62010-08-18 23:56:43 +00003418void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00003419 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003420 S.ExternalSource = this;
3421
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003422 // Makes sure any declarations that were deserialized "too early"
3423 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003424 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3425 if (SemaObj->TUScope)
John McCall48871652010-08-21 09:40:31 +00003426 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003427
3428 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003429 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003430 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00003431
3432 // If there were any tentative definitions, deserialize them and add
Sebastian Redl35351a92010-01-31 22:27:38 +00003433 // them to Sema's list of tentative definitions.
Douglas Gregord4df8652009-04-22 22:02:47 +00003434 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3435 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redl35351a92010-01-31 22:27:38 +00003436 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregord4df8652009-04-22 22:02:47 +00003437 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00003438
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003439 // If there were any unused file scoped decls, deserialize them and add to
3440 // Sema's list of unused file scoped decls.
3441 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3442 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3443 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattner90073802010-02-12 00:07:30 +00003444 }
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003445
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003446 // If there were any weak undeclared identifiers, deserialize them and add to
3447 // Sema's list of weak undeclared identifiers.
3448 if (!WeakUndeclaredIdentifiers.empty()) {
3449 unsigned Idx = 0;
3450 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3451 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3452 IdentifierInfo *AliasId=GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3453 SourceLocation Loc = ReadSourceLocation(WeakUndeclaredIdentifiers, Idx);
3454 bool Used = WeakUndeclaredIdentifiers[Idx++];
3455 Sema::WeakInfo WI(AliasId, Loc);
3456 WI.setUsed(Used);
3457 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3458 }
3459 }
3460
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003461 // If there were any locally-scoped external declarations,
3462 // deserialize them and add them to Sema's table of locally-scoped
3463 // external declarations.
3464 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3465 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3466 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3467 }
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003468
3469 // If there were any ext_vector type declarations, deserialize them
3470 // and add them to Sema's vector of such declarations.
3471 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3472 SemaObj->ExtVectorDecls.push_back(
3473 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003474
3475 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3476 // Can we cut them down before writing them ?
3477
3478 // If there were any VTable uses, deserialize the information and add it
3479 // to Sema's vector and map of VTable uses.
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003480 if (!VTableUses.empty()) {
3481 unsigned Idx = 0;
3482 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3483 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3484 SourceLocation Loc = ReadSourceLocation(VTableUses, Idx);
3485 bool DefinitionRequired = VTableUses[Idx++];
3486 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3487 SemaObj->VTablesUsed[Class] = DefinitionRequired;
3488 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003489 }
3490
3491 // If there were any dynamic classes declarations, deserialize them
3492 // and add them to Sema's vector of such declarations.
3493 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3494 SemaObj->DynamicClasses.push_back(
3495 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003496
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003497 // If there were any pending implicit instantiations, deserialize them
3498 // and add them to Sema's queue of such instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003499 assert(PendingInstantiations.size() % 2 == 0 && "Expected pairs of entries");
3500 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
3501 ValueDecl *D=cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
3502 SourceLocation Loc = ReadSourceLocation(PendingInstantiations, Idx);
3503 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003504 }
3505
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003506 // Load the offsets of the declarations that Sema references.
3507 // They will be lazily deserialized when needed.
3508 if (!SemaDeclRefs.empty()) {
3509 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3510 SemaObj->StdNamespace = SemaDeclRefs[0];
3511 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3512 }
3513
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003514 // If there are @selector references added them to its pool. This is for
3515 // implementation of -Wselector.
Sebastian Redlada023c2010-08-04 20:40:17 +00003516 if (!ReferencedSelectorsData.empty()) {
3517 unsigned int DataSize = ReferencedSelectorsData.size()-1;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003518 unsigned I = 0;
3519 while (I < DataSize) {
Sebastian Redlada023c2010-08-04 20:40:17 +00003520 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003521 SourceLocation SelLoc =
Sebastian Redlada023c2010-08-04 20:40:17 +00003522 SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003523 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3524 }
3525 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00003526}
3527
Sebastian Redl2c499f62010-08-18 23:56:43 +00003528IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redl78f51772010-08-02 18:30:12 +00003529 // Try to find this name within our on-disk hash tables. We start with the
3530 // most recent one, since that one contains the most up-to-date info.
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003531 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003532 ASTIdentifierLookupTable *IdTable
3533 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003534 if (!IdTable)
3535 continue;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003536 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003537 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003538 if (Pos == IdTable->end())
3539 continue;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003540
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003541 // Dereferencing the iterator has the effect of building the
3542 // IdentifierInfo node and populating it with the various
3543 // declarations it needs.
Sebastian Redl78f51772010-08-02 18:30:12 +00003544 return *Pos;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003545 }
Sebastian Redl78f51772010-08-02 18:30:12 +00003546 return 0;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003547}
3548
Mike Stump11289f42009-09-09 15:08:12 +00003549std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redl2c499f62010-08-18 23:56:43 +00003550ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redlada023c2010-08-04 20:40:17 +00003551 // Find this selector in a hash table. We want to find the most recent entry.
3552 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3553 PerFileData &F = *Chain[I];
3554 if (!F.SelectorLookupTable)
3555 continue;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003556
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003557 ASTSelectorLookupTable *PoolTable
3558 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3559 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redlada023c2010-08-04 20:40:17 +00003560 if (Pos != PoolTable->end()) {
3561 ++NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003562 // FIXME: Not quite happy with the statistics here. We probably should
3563 // disable this tracking when called via LoadSelector.
3564 // Also, should entries without methods count as misses?
3565 ++NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003566 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redlada023c2010-08-04 20:40:17 +00003567 if (DeserializationListener)
3568 DeserializationListener->SelectorRead(Data.ID, Sel);
3569 return std::make_pair(Data.Instance, Data.Factory);
3570 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003571 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003572
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003573 ++NumMethodPoolMisses;
Sebastian Redlada023c2010-08-04 20:40:17 +00003574 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorc78d3462009-04-24 21:10:55 +00003575}
3576
Sebastian Redl2c499f62010-08-18 23:56:43 +00003577void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003578 // It would be complicated to avoid reading the methods anyway. So don't.
3579 ReadMethodPool(Sel);
3580}
3581
Sebastian Redl2c499f62010-08-18 23:56:43 +00003582void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00003583 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00003584 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00003585 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00003586 if (DeserializationListener)
3587 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003588}
3589
Douglas Gregor1342e842009-07-06 18:54:52 +00003590/// \brief Set the globally-visible declarations associated with the given
3591/// identifier.
3592///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003593/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00003594/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00003595/// them.
3596///
3597/// \param II an IdentifierInfo that refers to one or more globally-visible
3598/// declarations.
3599///
3600/// \param DeclIDs the set of declaration IDs with the name @p II that are
3601/// visible at global scope.
3602///
3603/// \param Nonrecursive should be true to indicate that the caller knows that
3604/// this call is non-recursive, and therefore the globally-visible declarations
3605/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00003606void
Sebastian Redl2c499f62010-08-18 23:56:43 +00003607ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregor1342e842009-07-06 18:54:52 +00003608 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
3609 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003610 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00003611 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
3612 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
3613 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003614 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00003615 return;
3616 }
Mike Stump11289f42009-09-09 15:08:12 +00003617
Douglas Gregor1342e842009-07-06 18:54:52 +00003618 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
3619 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
3620 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00003621 if (SemaObj->TUScope) {
3622 // Introduce this declaration into the translation-unit scope
3623 // and add it to the declaration chain for this identifier, so
3624 // that (unqualified) name lookup will find it.
John McCall48871652010-08-21 09:40:31 +00003625 SemaObj->TUScope->AddDecl(D);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00003626 }
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003627 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregor1342e842009-07-06 18:54:52 +00003628 } else {
3629 // Queue this declaration so that it will be added to the
3630 // translation unit scope and identifier's declaration chain
3631 // once a Sema object is known.
3632 PreloadedDecls.push_back(D);
3633 }
3634 }
3635}
3636
Sebastian Redl2c499f62010-08-18 23:56:43 +00003637IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003638 if (ID == 0)
3639 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00003640
Sebastian Redlc713b962010-07-21 00:46:22 +00003641 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003642 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003643 return 0;
3644 }
Mike Stump11289f42009-09-09 15:08:12 +00003645
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003646 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc713b962010-07-21 00:46:22 +00003647 ID -= 1;
3648 if (!IdentifiersLoaded[ID]) {
3649 unsigned Index = ID;
3650 const char *Str = 0;
3651 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3652 PerFileData *F = Chain[N - I - 1];
3653 if (Index < F->LocalNumIdentifiers) {
3654 uint32_t Offset = F->IdentifierOffsets[Index];
3655 Str = F->IdentifierTableData + Offset;
3656 break;
3657 }
3658 Index -= F->LocalNumIdentifiers;
3659 }
3660 assert(Str && "Broken Chain");
Douglas Gregor5287b4e2009-04-25 21:04:17 +00003661
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003662 // All of the strings in the AST file are preceded by a 16-bit length.
3663 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00003664 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
3665 // unsigned integers. This is important to avoid integer overflow when
3666 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00003667 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00003668 unsigned StrLen = (((unsigned) StrLenPtr[0])
3669 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00003670 IdentifiersLoaded[ID]
Kovarththanan Rajaratnama3b09592010-03-12 10:32:27 +00003671 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003672 if (DeserializationListener)
3673 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003674 }
Mike Stump11289f42009-09-09 15:08:12 +00003675
Sebastian Redlc713b962010-07-21 00:46:22 +00003676 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003677}
3678
Sebastian Redl2c499f62010-08-18 23:56:43 +00003679void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00003680 ReadSLocEntryRecord(ID);
3681}
3682
Sebastian Redl2c499f62010-08-18 23:56:43 +00003683Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00003684 if (ID == 0)
3685 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00003686
Sebastian Redlada023c2010-08-04 20:40:17 +00003687 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003688 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00003689 return Selector();
3690 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003691
Sebastian Redlada023c2010-08-04 20:40:17 +00003692 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003693 // Load this selector from the selector table.
Sebastian Redlada023c2010-08-04 20:40:17 +00003694 unsigned Idx = ID - 1;
3695 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3696 PerFileData &F = *Chain[N - I - 1];
3697 if (Idx < F.LocalNumSelectors) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003698 ASTSelectorLookupTrait Trait(*this);
Sebastian Redlada023c2010-08-04 20:40:17 +00003699 SelectorsLoaded[ID - 1] =
3700 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
3701 if (DeserializationListener)
3702 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
3703 break;
3704 }
3705 Idx -= F.LocalNumSelectors;
3706 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003707 }
3708
Sebastian Redlada023c2010-08-04 20:40:17 +00003709 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00003710}
3711
Sebastian Redl2c499f62010-08-18 23:56:43 +00003712Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00003713 return DecodeSelector(ID);
3714}
3715
Sebastian Redl2c499f62010-08-18 23:56:43 +00003716uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00003717 // ID 0 (the null selector) is considered an external selector.
3718 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00003719}
3720
Mike Stump11289f42009-09-09 15:08:12 +00003721DeclarationName
Sebastian Redl2c499f62010-08-18 23:56:43 +00003722ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003723 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
3724 switch (Kind) {
3725 case DeclarationName::Identifier:
3726 return DeclarationName(GetIdentifierInfo(Record, Idx));
3727
3728 case DeclarationName::ObjCZeroArgSelector:
3729 case DeclarationName::ObjCOneArgSelector:
3730 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff3c301dc2009-04-23 15:15:40 +00003731 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003732
3733 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00003734 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00003735 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003736
3737 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00003738 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00003739 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003740
3741 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00003742 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor2211d342009-08-05 05:36:45 +00003743 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003744
3745 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00003746 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003747 (OverloadedOperatorKind)Record[Idx++]);
3748
Alexis Hunt3d221f22009-11-29 07:34:05 +00003749 case DeclarationName::CXXLiteralOperatorName:
3750 return Context->DeclarationNames.getCXXLiteralOperatorName(
3751 GetIdentifierInfo(Record, Idx));
3752
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003753 case DeclarationName::CXXUsingDirective:
3754 return DeclarationName::getUsingDirectiveName();
3755 }
3756
3757 // Required to silence GCC warning
3758 return DeclarationName();
3759}
Douglas Gregor55abb232009-04-10 20:39:37 +00003760
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003761TemplateName
Sebastian Redl2c499f62010-08-18 23:56:43 +00003762ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003763 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
3764 switch (Kind) {
3765 case TemplateName::Template:
3766 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
3767
3768 case TemplateName::OverloadedTemplate: {
3769 unsigned size = Record[Idx++];
3770 UnresolvedSet<8> Decls;
3771 while (size--)
3772 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
3773
3774 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
3775 }
3776
3777 case TemplateName::QualifiedTemplate: {
3778 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3779 bool hasTemplKeyword = Record[Idx++];
3780 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
3781 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
3782 }
3783
3784 case TemplateName::DependentTemplate: {
3785 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3786 if (Record[Idx++]) // isIdentifier
3787 return Context->getDependentTemplateName(NNS,
3788 GetIdentifierInfo(Record, Idx));
3789 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003790 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003791 }
3792 }
3793
3794 assert(0 && "Unhandled template name kind!");
3795 return TemplateName();
3796}
3797
3798TemplateArgument
Sebastian Redl2c499f62010-08-18 23:56:43 +00003799ASTReader::ReadTemplateArgument(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003800 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003801 switch ((TemplateArgument::ArgKind)Record[Idx++]) {
3802 case TemplateArgument::Null:
3803 return TemplateArgument();
3804 case TemplateArgument::Type:
3805 return TemplateArgument(GetType(Record[Idx++]));
3806 case TemplateArgument::Declaration:
3807 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00003808 case TemplateArgument::Integral: {
3809 llvm::APSInt Value = ReadAPSInt(Record, Idx);
3810 QualType T = GetType(Record[Idx++]);
3811 return TemplateArgument(Value, T);
3812 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003813 case TemplateArgument::Template:
3814 return TemplateArgument(ReadTemplateName(Record, Idx));
3815 case TemplateArgument::Expression:
Sebastian Redlc67764e2010-07-22 22:43:28 +00003816 return TemplateArgument(ReadExpr(DeclsCursor));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003817 case TemplateArgument::Pack: {
3818 unsigned NumArgs = Record[Idx++];
3819 llvm::SmallVector<TemplateArgument, 8> Args;
3820 Args.reserve(NumArgs);
3821 while (NumArgs--)
Sebastian Redlc67764e2010-07-22 22:43:28 +00003822 Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003823 TemplateArgument TemplArg;
3824 TemplArg.setArgumentPack(Args.data(), Args.size(), /*CopyArgs=*/true);
3825 return TemplArg;
3826 }
3827 }
3828
3829 assert(0 && "Unhandled template argument kind!");
3830 return TemplateArgument();
3831}
3832
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003833TemplateParameterList *
Sebastian Redl2c499f62010-08-18 23:56:43 +00003834ASTReader::ReadTemplateParameterList(const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003835 SourceLocation TemplateLoc = ReadSourceLocation(Record, Idx);
3836 SourceLocation LAngleLoc = ReadSourceLocation(Record, Idx);
3837 SourceLocation RAngleLoc = ReadSourceLocation(Record, Idx);
3838
3839 unsigned NumParams = Record[Idx++];
3840 llvm::SmallVector<NamedDecl *, 16> Params;
3841 Params.reserve(NumParams);
3842 while (NumParams--)
3843 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
3844
3845 TemplateParameterList* TemplateParams =
3846 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
3847 Params.data(), Params.size(), RAngleLoc);
3848 return TemplateParams;
3849}
3850
3851void
Sebastian Redl2c499f62010-08-18 23:56:43 +00003852ASTReader::
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003853ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003854 llvm::BitstreamCursor &DeclsCursor,
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003855 const RecordData &Record, unsigned &Idx) {
3856 unsigned NumTemplateArgs = Record[Idx++];
3857 TemplArgs.reserve(NumTemplateArgs);
3858 while (NumTemplateArgs--)
Sebastian Redlc67764e2010-07-22 22:43:28 +00003859 TemplArgs.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003860}
3861
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003862/// \brief Read a UnresolvedSet structure.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003863void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003864 const RecordData &Record, unsigned &Idx) {
3865 unsigned NumDecls = Record[Idx++];
3866 while (NumDecls--) {
3867 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
3868 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
3869 Set.addDecl(D, AS);
3870 }
3871}
3872
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003873CXXBaseSpecifier
Sebastian Redl2c499f62010-08-18 23:56:43 +00003874ASTReader::ReadCXXBaseSpecifier(llvm::BitstreamCursor &DeclsCursor,
Nick Lewycky19b9f952010-07-26 16:56:01 +00003875 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003876 bool isVirtual = static_cast<bool>(Record[Idx++]);
3877 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
3878 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Nick Lewycky19b9f952010-07-26 16:56:01 +00003879 TypeSourceInfo *TInfo = GetTypeSourceInfo(DeclsCursor, Record, Idx);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003880 SourceRange Range = ReadSourceRange(Record, Idx);
Nick Lewycky19b9f952010-07-26 16:56:01 +00003881 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003882}
3883
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003884std::pair<CXXBaseOrMemberInitializer **, unsigned>
Sebastian Redl2c499f62010-08-18 23:56:43 +00003885ASTReader::ReadCXXBaseOrMemberInitializers(llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003886 const RecordData &Record,
3887 unsigned &Idx) {
3888 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0;
3889 unsigned NumInitializers = Record[Idx++];
3890 if (NumInitializers) {
3891 ASTContext &C = *getContext();
3892
3893 BaseOrMemberInitializers
3894 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
3895 for (unsigned i=0; i != NumInitializers; ++i) {
3896 TypeSourceInfo *BaseClassInfo = 0;
3897 bool IsBaseVirtual = false;
3898 FieldDecl *Member = 0;
3899
3900 bool IsBaseInitializer = Record[Idx++];
3901 if (IsBaseInitializer) {
3902 BaseClassInfo = GetTypeSourceInfo(Cursor, Record, Idx);
3903 IsBaseVirtual = Record[Idx++];
3904 } else {
3905 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
3906 }
3907 SourceLocation MemberLoc = ReadSourceLocation(Record, Idx);
3908 Expr *Init = ReadExpr(Cursor);
3909 FieldDecl *AnonUnionMember
3910 = cast_or_null<FieldDecl>(GetDecl(Record[Idx++]));
3911 SourceLocation LParenLoc = ReadSourceLocation(Record, Idx);
3912 SourceLocation RParenLoc = ReadSourceLocation(Record, Idx);
3913 bool IsWritten = Record[Idx++];
3914 unsigned SourceOrderOrNumArrayIndices;
3915 llvm::SmallVector<VarDecl *, 8> Indices;
3916 if (IsWritten) {
3917 SourceOrderOrNumArrayIndices = Record[Idx++];
3918 } else {
3919 SourceOrderOrNumArrayIndices = Record[Idx++];
3920 Indices.reserve(SourceOrderOrNumArrayIndices);
3921 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
3922 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
3923 }
3924
3925 CXXBaseOrMemberInitializer *BOMInit;
3926 if (IsBaseInitializer) {
3927 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
3928 IsBaseVirtual, LParenLoc,
3929 Init, RParenLoc);
3930 } else if (IsWritten) {
3931 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc,
3932 LParenLoc, Init, RParenLoc);
3933 } else {
3934 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc,
3935 LParenLoc, Init, RParenLoc,
3936 Indices.data(),
3937 Indices.size());
3938 }
3939
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00003940 if (IsWritten)
3941 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003942 BOMInit->setAnonUnionMember(AnonUnionMember);
3943 BaseOrMemberInitializers[i] = BOMInit;
3944 }
3945 }
3946
3947 return std::make_pair(BaseOrMemberInitializers, NumInitializers);
3948}
3949
Chris Lattnerca025db2010-05-07 21:43:38 +00003950NestedNameSpecifier *
Sebastian Redl2c499f62010-08-18 23:56:43 +00003951ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003952 unsigned N = Record[Idx++];
3953 NestedNameSpecifier *NNS = 0, *Prev = 0;
3954 for (unsigned I = 0; I != N; ++I) {
3955 NestedNameSpecifier::SpecifierKind Kind
3956 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
3957 switch (Kind) {
3958 case NestedNameSpecifier::Identifier: {
3959 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
3960 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
3961 break;
3962 }
3963
3964 case NestedNameSpecifier::Namespace: {
3965 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
3966 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
3967 break;
3968 }
3969
3970 case NestedNameSpecifier::TypeSpec:
3971 case NestedNameSpecifier::TypeSpecWithTemplate: {
3972 Type *T = GetType(Record[Idx++]).getTypePtr();
3973 bool Template = Record[Idx++];
3974 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
3975 break;
3976 }
3977
3978 case NestedNameSpecifier::Global: {
3979 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
3980 // No associated value, and there can't be a prefix.
3981 break;
3982 }
Chris Lattnerca025db2010-05-07 21:43:38 +00003983 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00003984 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00003985 }
3986 return NNS;
3987}
3988
3989SourceRange
Sebastian Redl2c499f62010-08-18 23:56:43 +00003990ASTReader::ReadSourceRange(const RecordData &Record, unsigned &Idx) {
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00003991 SourceLocation beg = SourceLocation::getFromRawEncoding(Record[Idx++]);
3992 SourceLocation end = SourceLocation::getFromRawEncoding(Record[Idx++]);
3993 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00003994}
3995
Douglas Gregor1daeb692009-04-13 18:14:40 +00003996/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00003997llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003998 unsigned BitWidth = Record[Idx++];
3999 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4000 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4001 Idx += NumWords;
4002 return Result;
4003}
4004
4005/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004006llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004007 bool isUnsigned = Record[Idx++];
4008 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4009}
4010
Douglas Gregore0a3a512009-04-14 21:55:33 +00004011/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004012llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004013 return llvm::APFloat(ReadAPInt(Record, Idx));
4014}
4015
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004016// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00004017std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004018 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00004019 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004020 Idx += Len;
4021 return Result;
4022}
4023
Sebastian Redl2c499f62010-08-18 23:56:43 +00004024CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00004025 unsigned &Idx) {
4026 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4027 return CXXTemporary::Create(*Context, Decl);
4028}
4029
Sebastian Redl2c499f62010-08-18 23:56:43 +00004030DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00004031 return Diag(SourceLocation(), DiagID);
4032}
4033
Sebastian Redl2c499f62010-08-18 23:56:43 +00004034DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004035 return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00004036}
Douglas Gregora9af1d12009-04-17 00:04:06 +00004037
Douglas Gregora868bbd2009-04-21 22:25:48 +00004038/// \brief Retrieve the identifier table associated with the
4039/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004040IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004041 assert(PP && "Forgot to set Preprocessor ?");
4042 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00004043}
4044
Douglas Gregora9af1d12009-04-17 00:04:06 +00004045/// \brief Record that the given ID maps to the given switch-case
4046/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004047void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004048 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4049 SwitchCaseStmts[ID] = SC;
4050}
4051
4052/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004053SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004054 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4055 return SwitchCaseStmts[ID];
4056}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004057
4058/// \brief Record that the given label statement has been
4059/// deserialized and has the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004060void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump11289f42009-09-09 15:08:12 +00004061 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004062 "Deserialized label twice");
4063 LabelStmts[ID] = S;
4064
4065 // If we've already seen any goto statements that point to this
4066 // label, resolve them now.
4067 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
4068 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
4069 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
4070 Goto->second->setLabel(S);
4071 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor779d8652009-04-17 18:58:21 +00004072
4073 // If we've already seen any address-label statements that point to
4074 // this label, resolve them now.
4075 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump11289f42009-09-09 15:08:12 +00004076 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor779d8652009-04-17 18:58:21 +00004077 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump11289f42009-09-09 15:08:12 +00004078 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor779d8652009-04-17 18:58:21 +00004079 AddrLabel != AddrLabels.second; ++AddrLabel)
4080 AddrLabel->second->setLabel(S);
4081 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004082}
4083
4084/// \brief Set the label of the given statement to the label
4085/// identified by ID.
4086///
4087/// Depending on the order in which the label and other statements
4088/// referencing that label occur, this operation may complete
4089/// immediately (updating the statement) or it may queue the
4090/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004091void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004092 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4093 if (Label != LabelStmts.end()) {
4094 // We've already seen this label, so set the label of the goto and
4095 // we're done.
4096 S->setLabel(Label->second);
4097 } else {
4098 // We haven't seen this label yet, so add this goto to the set of
4099 // unresolved goto statements.
4100 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
4101 }
4102}
Douglas Gregor779d8652009-04-17 18:58:21 +00004103
4104/// \brief Set the label of the given expression to the label
4105/// identified by ID.
4106///
4107/// Depending on the order in which the label and other statements
4108/// referencing that label occur, this operation may complete
4109/// immediately (updating the statement) or it may queue the
4110/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004111void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor779d8652009-04-17 18:58:21 +00004112 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4113 if (Label != LabelStmts.end()) {
4114 // We've already seen this label, so set the label of the
4115 // label-address expression and we're done.
4116 S->setLabel(Label->second);
4117 } else {
4118 // We haven't seen this label yet, so add this label-address
4119 // expression to the set of unresolved label-address expressions.
4120 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
4121 }
4122}
Douglas Gregor1342e842009-07-06 18:54:52 +00004123
Sebastian Redl2c499f62010-08-18 23:56:43 +00004124void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004125 assert(NumCurrentElementsDeserializing &&
4126 "FinishedDeserializing not paired with StartedDeserializing");
4127 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004128 // If any identifiers with corresponding top-level declarations have
4129 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004130 while (!PendingIdentifierInfos.empty()) {
4131 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4132 PendingIdentifierInfos.front().DeclIDs, true);
4133 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00004134 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004135
4136 // We are not in recursive loading, so it's safe to pass the "interesting"
4137 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004138 if (Consumer)
4139 PassInterestingDeclsToConsumer();
Douglas Gregor1342e842009-07-06 18:54:52 +00004140 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004141 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00004142}
Douglas Gregorb473b072010-08-19 00:28:17 +00004143
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004144ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
4145 const char *isysroot, bool DisableValidation)
4146 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4147 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4148 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4149 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
4150 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00004151 TotalNumSLocEntries(0), NextSLocOffset(0), NumStatementsRead(0),
4152 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
4153 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004154 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4155 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4156 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
4157 RelocatablePCH = false;
4158}
4159
4160ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4161 Diagnostic &Diags, const char *isysroot,
4162 bool DisableValidation)
4163 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4164 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
4165 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
4166 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00004167 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4168 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4169 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4170 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4171 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4172 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004173 RelocatablePCH = false;
4174}
4175
4176ASTReader::~ASTReader() {
4177 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4178 delete Chain[e - i - 1];
4179 // Delete all visible decl lookup tables
4180 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4181 E = DeclContextOffsets.end();
4182 I != E; ++I) {
4183 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4184 J != F; ++J) {
4185 if (J->NameLookupTableData)
4186 delete static_cast<ASTDeclContextNameLookupTable*>(
4187 J->NameLookupTableData);
4188 }
4189 }
4190 for (DeclContextVisibleUpdatesPending::iterator
4191 I = PendingVisibleUpdates.begin(),
4192 E = PendingVisibleUpdates.end();
4193 I != E; ++I) {
4194 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4195 F = I->second.end();
4196 J != F; ++J)
4197 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4198 }
4199}
4200
Douglas Gregorb473b072010-08-19 00:28:17 +00004201ASTReader::PerFileData::PerFileData()
Sebastian Redlc1d035f2010-09-22 20:19:08 +00004202 : SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00004203 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
4204 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
4205 MacroDefinitionOffsets(0), LocalNumSelectors(0), SelectorOffsets(0),
4206 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
4207 DeclOffsets(0), LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redl3f6b7532010-10-01 19:59:12 +00004208 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregorb473b072010-08-19 00:28:17 +00004209{}
4210
4211ASTReader::PerFileData::~PerFileData() {
4212 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
4213 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4214}
4215