blob: 8413faa65d62575f2595dd357d9db62c884df2fa [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
Sebastian Redl2c373b92010-10-05 15:59:54 +0000570namespace clang {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000571class ASTIdentifierLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000572 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +0000573 ASTReader::PerFileData &F;
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 Redl2c373b92010-10-05 15:59:54 +0000587 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000588 IdentifierInfo *II = 0)
Sebastian Redl2c373b92010-10-05 15:59:54 +0000589 : Reader(Reader), F(F), 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 Redl2c373b92010-10-05 15:59:54 +0000681 Reader.ReadMacroRecord(F, 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.
Sebastian Redl2c373b92010-10-05 15:59:54 +0000955/// \returns true if there was an error.
956bool ASTReader::ParseLineTable(PerFileData &F,
957 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000958 unsigned Idx = 0;
959 LineTableInfo &LineTable = SourceMgr.getLineTable();
960
961 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +0000962 std::map<int, int> FileIDs;
963 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000964 // Extract the file name
965 unsigned FilenameLen = Record[Idx++];
966 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
967 Idx += FilenameLen;
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000968 MaybeAddSystemRootToFilename(Filename);
Mike Stump11289f42009-09-09 15:08:12 +0000969 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregora8854652009-04-13 17:12:42 +0000970 Filename.size());
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000971 }
972
973 // Parse the line entries
974 std::vector<LineEntry> Entries;
975 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000976 int FID = Record[Idx++];
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000977
978 // Extract the line entries
979 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000980 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000981 Entries.clear();
982 Entries.reserve(NumEntries);
983 for (unsigned I = 0; I != NumEntries; ++I) {
984 unsigned FileOffset = Record[Idx++];
985 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000986 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +0000987 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000988 = (SrcMgr::CharacteristicKind)Record[Idx++];
989 unsigned IncludeOffset = Record[Idx++];
990 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
991 FileKind, IncludeOffset));
992 }
993 LineTable.AddEntry(FID, Entries);
994 }
995
996 return false;
997}
998
Douglas Gregorc5046832009-04-27 18:38:38 +0000999namespace {
1000
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001001class ASTStatData {
Douglas Gregorc5046832009-04-27 18:38:38 +00001002public:
1003 const bool hasStat;
1004 const ino_t ino;
1005 const dev_t dev;
1006 const mode_t mode;
1007 const time_t mtime;
1008 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +00001009
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001010 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump11289f42009-09-09 15:08:12 +00001011 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
1012
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001013 ASTStatData()
Douglas Gregorc5046832009-04-27 18:38:38 +00001014 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
1015};
1016
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001017class ASTStatLookupTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001018 public:
1019 typedef const char *external_key_type;
1020 typedef const char *internal_key_type;
1021
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001022 typedef ASTStatData data_type;
Douglas Gregorc5046832009-04-27 18:38:38 +00001023
1024 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001025 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001026 }
1027
1028 static internal_key_type GetInternalKey(const char *path) { return path; }
1029
1030 static bool EqualKey(internal_key_type a, internal_key_type b) {
1031 return strcmp(a, b) == 0;
1032 }
1033
1034 static std::pair<unsigned, unsigned>
1035 ReadKeyDataLength(const unsigned char*& d) {
1036 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1037 unsigned DataLen = (unsigned) *d++;
1038 return std::make_pair(KeyLen + 1, DataLen);
1039 }
1040
1041 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1042 return (const char *)d;
1043 }
1044
1045 static data_type ReadData(const internal_key_type, const unsigned char *d,
1046 unsigned /*DataLen*/) {
1047 using namespace clang::io;
1048
1049 if (*d++ == 1)
1050 return data_type();
1051
1052 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1053 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1054 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +00001055 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +00001056 off_t size = (off_t) ReadUnalignedLE64(d);
1057 return data_type(ino, dev, mode, mtime, size);
1058 }
1059};
1060
1061/// \brief stat() cache for precompiled headers.
1062///
1063/// This cache is very similar to the stat cache used by pretokenized
1064/// headers.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001065class ASTStatCache : public StatSysCallCache {
1066 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregorc5046832009-04-27 18:38:38 +00001067 CacheTy *Cache;
1068
1069 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +00001070public:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001071 ASTStatCache(const unsigned char *Buckets,
Douglas Gregorc5046832009-04-27 18:38:38 +00001072 const unsigned char *Base,
1073 unsigned &NumStatHits,
Mike Stump11289f42009-09-09 15:08:12 +00001074 unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +00001075 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1076 Cache = CacheTy::Create(Buckets, Base);
1077 }
1078
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001079 ~ASTStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +00001080
Douglas Gregorc5046832009-04-27 18:38:38 +00001081 int stat(const char *path, struct stat *buf) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001082 // Do the lookup for the file's data in the AST file.
Douglas Gregorc5046832009-04-27 18:38:38 +00001083 CacheTy::iterator I = Cache->find(path);
1084
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001085 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregorc5046832009-04-27 18:38:38 +00001086 if (I == Cache->end()) {
1087 ++NumStatMisses;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001088 return StatSysCallCache::stat(path, buf);
Douglas Gregorc5046832009-04-27 18:38:38 +00001089 }
Mike Stump11289f42009-09-09 15:08:12 +00001090
Douglas Gregorc5046832009-04-27 18:38:38 +00001091 ++NumStatHits;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001092 ASTStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001093
Douglas Gregorc5046832009-04-27 18:38:38 +00001094 if (!Data.hasStat)
1095 return 1;
1096
1097 buf->st_ino = Data.ino;
1098 buf->st_dev = Data.dev;
1099 buf->st_mtime = Data.mtime;
1100 buf->st_mode = Data.mode;
1101 buf->st_size = Data.size;
1102 return 0;
1103 }
1104};
1105} // end anonymous namespace
1106
1107
Sebastian Redl393f8b72010-07-19 20:52:06 +00001108/// \brief Read a source manager block
Sebastian Redl2c499f62010-08-18 23:56:43 +00001109ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001110 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +00001111
Sebastian Redl393f8b72010-07-19 20:52:06 +00001112 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001113
Douglas Gregor258ae542009-04-27 06:38:32 +00001114 // Set the source-location entry cursor to the current position in
1115 // the stream. This cursor will be used to read the contents of the
1116 // source manager block initially, and then lazily read
1117 // source-location entries as needed.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001118 SLocEntryCursor = F.Stream;
Douglas Gregor258ae542009-04-27 06:38:32 +00001119
1120 // The stream itself is going to skip over the source manager block.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001121 if (F.Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001122 Error("malformed block record in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001123 return Failure;
1124 }
1125
1126 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001127 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001128 Error("malformed source manager block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001129 return Failure;
1130 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001131
Douglas Gregora7f71a92009-04-10 03:52:48 +00001132 RecordData Record;
1133 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001134 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001135 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001136 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001137 Error("error at end of Source Manager block in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001138 return Failure;
1139 }
Douglas Gregor92863e42009-04-10 23:10:45 +00001140 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001141 }
Mike Stump11289f42009-09-09 15:08:12 +00001142
Douglas Gregora7f71a92009-04-10 03:52:48 +00001143 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1144 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +00001145 SLocEntryCursor.ReadSubBlockID();
1146 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001147 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001148 return Failure;
1149 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001150 continue;
1151 }
Mike Stump11289f42009-09-09 15:08:12 +00001152
Douglas Gregora7f71a92009-04-10 03:52:48 +00001153 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001154 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001155 continue;
1156 }
Mike Stump11289f42009-09-09 15:08:12 +00001157
Douglas Gregora7f71a92009-04-10 03:52:48 +00001158 // Read a record.
1159 const char *BlobStart;
1160 unsigned BlobLen;
1161 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +00001162 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001163 default: // Default behavior: ignore.
1164 break;
1165
Sebastian Redl539c5062010-08-18 23:57:32 +00001166 case SM_LINE_TABLE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00001167 if (ParseLineTable(F, Record))
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001168 return Failure;
Chris Lattner184e65d2009-04-14 23:22:57 +00001169 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00001170
Sebastian Redl539c5062010-08-18 23:57:32 +00001171 case SM_SLOC_FILE_ENTRY:
1172 case SM_SLOC_BUFFER_ENTRY:
1173 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor258ae542009-04-27 06:38:32 +00001174 // Once we hit one of the source location entries, we're done.
1175 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001176 }
1177 }
1178}
1179
Sebastian Redl06750302010-07-20 21:50:20 +00001180/// \brief Get a cursor that's correctly positioned for reading the source
1181/// location entry with the given ID.
Sebastian Redl2c373b92010-10-05 15:59:54 +00001182ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl06750302010-07-20 21:50:20 +00001183 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1184 "SLocCursorForID should only be called for real IDs.");
1185
1186 ID -= 1;
1187 PerFileData *F = 0;
1188 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1189 F = Chain[N - I - 1];
1190 if (ID < F->LocalNumSLocEntries)
1191 break;
1192 ID -= F->LocalNumSLocEntries;
1193 }
1194 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1195
1196 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00001197 return F;
Sebastian Redl06750302010-07-20 21:50:20 +00001198}
1199
Douglas Gregor258ae542009-04-27 06:38:32 +00001200/// \brief Read in the source location entry with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001201ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001202 if (ID == 0)
1203 return Success;
1204
1205 if (ID > TotalNumSLocEntries) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001206 Error("source location entry ID out-of-range for AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001207 return Failure;
1208 }
1209
Sebastian Redl2c373b92010-10-05 15:59:54 +00001210 PerFileData *F = SLocCursorForID(ID);
1211 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001212
Douglas Gregor258ae542009-04-27 06:38:32 +00001213 ++NumSLocEntriesRead;
Douglas Gregor258ae542009-04-27 06:38:32 +00001214 unsigned Code = SLocEntryCursor.ReadCode();
1215 if (Code == llvm::bitc::END_BLOCK ||
1216 Code == llvm::bitc::ENTER_SUBBLOCK ||
1217 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001218 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001219 return Failure;
1220 }
1221
Douglas Gregor258ae542009-04-27 06:38:32 +00001222 RecordData Record;
1223 const char *BlobStart;
1224 unsigned BlobLen;
1225 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1226 default:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001227 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001228 return Failure;
1229
Sebastian Redl539c5062010-08-18 23:57:32 +00001230 case SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001231 std::string Filename(BlobStart, BlobStart + BlobLen);
1232 MaybeAddSystemRootToFilename(Filename);
1233 const FileEntry *File = FileMgr.getFile(Filename);
Chris Lattnerd20dc872009-06-15 04:35:16 +00001234 if (File == 0) {
1235 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001236 ErrorStr += Filename;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001237 ErrorStr += "' referenced by AST file";
Chris Lattnerd20dc872009-06-15 04:35:16 +00001238 Error(ErrorStr.c_str());
1239 return Failure;
1240 }
Mike Stump11289f42009-09-09 15:08:12 +00001241
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001242 if (Record.size() < 10) {
Ted Kremenekabb1ddd2010-03-18 21:23:05 +00001243 Error("source location entry is incorrect");
1244 return Failure;
1245 }
1246
Douglas Gregorce3a8292010-07-27 00:27:13 +00001247 if (!DisableValidation &&
1248 ((off_t)Record[4] != File->getSize()
Douglas Gregor08288f22010-04-09 15:54:22 +00001249#if !defined(LLVM_ON_WIN32)
1250 // In our regression testing, the Windows file system seems to
1251 // have inconsistent modification times that sometimes
1252 // erroneously trigger this error-handling path.
Douglas Gregorce3a8292010-07-27 00:27:13 +00001253 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor08288f22010-04-09 15:54:22 +00001254#endif
Douglas Gregorce3a8292010-07-27 00:27:13 +00001255 )) {
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001256 Diag(diag::err_fe_pch_file_modified)
1257 << Filename;
1258 return Failure;
1259 }
1260
Douglas Gregor258ae542009-04-27 06:38:32 +00001261 FileID FID = SourceMgr.createFileID(File,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001262 ReadSourceLocation(*F, Record[1]),
Douglas Gregor258ae542009-04-27 06:38:32 +00001263 (SrcMgr::CharacteristicKind)Record[2],
1264 ID, Record[0]);
1265 if (Record[3])
1266 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1267 .setHasLineDirectives();
1268
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001269 // Reconstruct header-search information for this file.
1270 HeaderFileInfo HFI;
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001271 HFI.isImport = Record[6];
1272 HFI.DirInfo = Record[7];
1273 HFI.NumIncludes = Record[8];
1274 HFI.ControllingMacroID = Record[9];
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001275 if (Listener)
1276 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor258ae542009-04-27 06:38:32 +00001277 break;
1278 }
1279
Sebastian Redl539c5062010-08-18 23:57:32 +00001280 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor258ae542009-04-27 06:38:32 +00001281 const char *Name = BlobStart;
1282 unsigned Offset = Record[0];
1283 unsigned Code = SLocEntryCursor.ReadCode();
1284 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001285 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +00001286 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001287
Sebastian Redl539c5062010-08-18 23:57:32 +00001288 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001289 Error("AST record has invalid code");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001290 return Failure;
1291 }
1292
Douglas Gregor258ae542009-04-27 06:38:32 +00001293 llvm::MemoryBuffer *Buffer
Chris Lattner58c79342010-04-05 22:42:27 +00001294 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1295 Name);
Douglas Gregor258ae542009-04-27 06:38:32 +00001296 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump11289f42009-09-09 15:08:12 +00001297
Douglas Gregore6648fb2009-04-28 20:33:11 +00001298 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001299 PCHPredefinesBlock Block = {
1300 BufferID,
1301 llvm::StringRef(BlobStart, BlobLen - 1)
1302 };
1303 PCHPredefinesBuffers.push_back(Block);
Douglas Gregore6648fb2009-04-28 20:33:11 +00001304 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001305
1306 break;
1307 }
1308
Sebastian Redl539c5062010-08-18 23:57:32 +00001309 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001310 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001311 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001312 ReadSourceLocation(*F, Record[2]),
1313 ReadSourceLocation(*F, Record[3]),
Douglas Gregor258ae542009-04-27 06:38:32 +00001314 Record[4],
1315 ID,
1316 Record[0]);
1317 break;
Mike Stump11289f42009-09-09 15:08:12 +00001318 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001319 }
1320
1321 return Success;
1322}
1323
Chris Lattnere78a6be2009-04-27 01:05:14 +00001324/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1325/// specified cursor. Read the abbreviations that are at the top of the block
1326/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001327bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001328 unsigned BlockID) {
1329 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001330 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001331 return Failure;
1332 }
Mike Stump11289f42009-09-09 15:08:12 +00001333
Chris Lattnere78a6be2009-04-27 01:05:14 +00001334 while (true) {
1335 unsigned Code = Cursor.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00001336
Chris Lattnere78a6be2009-04-27 01:05:14 +00001337 // We expect all abbrevs to be at the start of the block.
1338 if (Code != llvm::bitc::DEFINE_ABBREV)
1339 return false;
1340 Cursor.ReadAbbrevRecord();
1341 }
1342}
1343
Sebastian Redl2c373b92010-10-05 15:59:54 +00001344void ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001345 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redl2c373b92010-10-05 15:59:54 +00001346 llvm::BitstreamCursor &Stream = F.Stream;
Mike Stump11289f42009-09-09 15:08:12 +00001347
Douglas Gregorc3366a52009-04-21 23:56:24 +00001348 // Keep track of where we are in the stream, then jump back there
1349 // after reading this macro.
1350 SavedStreamPosition SavedPosition(Stream);
1351
1352 Stream.JumpToBit(Offset);
1353 RecordData Record;
1354 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1355 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001356
Douglas Gregorc3366a52009-04-21 23:56:24 +00001357 while (true) {
1358 unsigned Code = Stream.ReadCode();
1359 switch (Code) {
1360 case llvm::bitc::END_BLOCK:
1361 return;
1362
1363 case llvm::bitc::ENTER_SUBBLOCK:
1364 // No known subblocks, always skip them.
1365 Stream.ReadSubBlockID();
1366 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001367 Error("malformed block record in AST file");
Douglas Gregorc3366a52009-04-21 23:56:24 +00001368 return;
1369 }
1370 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001371
Douglas Gregorc3366a52009-04-21 23:56:24 +00001372 case llvm::bitc::DEFINE_ABBREV:
1373 Stream.ReadAbbrevRecord();
1374 continue;
1375 default: break;
1376 }
1377
1378 // Read a record.
1379 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001380 PreprocessorRecordTypes RecType =
1381 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001382 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001383 case PP_MACRO_OBJECT_LIKE:
1384 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001385 // If we already have a macro, that means that we've hit the end
1386 // of the definition of the macro we were looking for. We're
1387 // done.
1388 if (Macro)
1389 return;
1390
1391 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1392 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001393 Error("macro must have a name in AST file");
Douglas Gregorc3366a52009-04-21 23:56:24 +00001394 return;
1395 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00001396 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001397 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001398
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001399 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001400 MI->setIsUsed(isUsed);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001401 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001402
Douglas Gregoraae92242010-03-19 21:51:54 +00001403 unsigned NextIndex = 3;
Sebastian Redl539c5062010-08-18 23:57:32 +00001404 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001405 // Decode function-like macro info.
1406 bool isC99VarArgs = Record[3];
1407 bool isGNUVarArgs = Record[4];
1408 MacroArgs.clear();
1409 unsigned NumArgs = Record[5];
Douglas Gregoraae92242010-03-19 21:51:54 +00001410 NextIndex = 6 + NumArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001411 for (unsigned i = 0; i != NumArgs; ++i)
1412 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1413
1414 // Install function-like macro info.
1415 MI->setIsFunctionLike();
1416 if (isC99VarArgs) MI->setIsC99Varargs();
1417 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001418 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001419 PP->getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001420 }
1421
1422 // Finally, install the macro.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001423 PP->setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001424
1425 // Remember that we saw this macro last so that we add the tokens that
1426 // form its body to it.
1427 Macro = MI;
Douglas Gregoraae92242010-03-19 21:51:54 +00001428
1429 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1430 // We have a macro definition. Load it now.
1431 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1432 getMacroDefinition(Record[NextIndex]));
1433 }
1434
Douglas Gregorc3366a52009-04-21 23:56:24 +00001435 ++NumMacrosRead;
1436 break;
1437 }
Mike Stump11289f42009-09-09 15:08:12 +00001438
Sebastian Redl539c5062010-08-18 23:57:32 +00001439 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001440 // If we see a TOKEN before a PP_MACRO_*, then the file is
1441 // erroneous, just pretend we didn't see this.
1442 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001443
Douglas Gregorc3366a52009-04-21 23:56:24 +00001444 Token Tok;
1445 Tok.startToken();
Sebastian Redl2c373b92010-10-05 15:59:54 +00001446 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001447 Tok.setLength(Record[1]);
1448 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1449 Tok.setIdentifierInfo(II);
1450 Tok.setKind((tok::TokenKind)Record[3]);
1451 Tok.setFlag((Token::TokenFlags)Record[4]);
1452 Macro->AddTokenToBody(Tok);
1453 break;
1454 }
Douglas Gregoraae92242010-03-19 21:51:54 +00001455
Sebastian Redl539c5062010-08-18 23:57:32 +00001456 case PP_MACRO_INSTANTIATION: {
Douglas Gregoraae92242010-03-19 21:51:54 +00001457 // If we already have a macro, that means that we've hit the end
1458 // of the definition of the macro we were looking for. We're
1459 // done.
1460 if (Macro)
1461 return;
1462
1463 if (!PP->getPreprocessingRecord()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001464 Error("missing preprocessing record in AST file");
Douglas Gregoraae92242010-03-19 21:51:54 +00001465 return;
1466 }
1467
1468 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1469 if (PPRec.getPreprocessedEntity(Record[0]))
1470 return;
1471
1472 MacroInstantiation *MI
1473 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
Sebastian Redl2c373b92010-10-05 15:59:54 +00001474 SourceRange(ReadSourceLocation(F, Record[1]),
1475 ReadSourceLocation(F, Record[2])),
Douglas Gregoraae92242010-03-19 21:51:54 +00001476 getMacroDefinition(Record[4]));
1477 PPRec.SetPreallocatedEntity(Record[0], MI);
1478 return;
1479 }
1480
Sebastian Redl539c5062010-08-18 23:57:32 +00001481 case PP_MACRO_DEFINITION: {
Douglas Gregoraae92242010-03-19 21:51:54 +00001482 // If we already have a macro, that means that we've hit the end
1483 // of the definition of the macro we were looking for. We're
1484 // done.
1485 if (Macro)
1486 return;
1487
1488 if (!PP->getPreprocessingRecord()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001489 Error("missing preprocessing record in AST file");
Douglas Gregoraae92242010-03-19 21:51:54 +00001490 return;
1491 }
1492
1493 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1494 if (PPRec.getPreprocessedEntity(Record[0]))
1495 return;
1496
Douglas Gregor91096292010-10-02 19:29:26 +00001497 if (Record[1] > MacroDefinitionsLoaded.size()) {
Douglas Gregoraae92242010-03-19 21:51:54 +00001498 Error("out-of-bounds macro definition record");
1499 return;
1500 }
1501
Douglas Gregor91096292010-10-02 19:29:26 +00001502 // Decode the identifier info and then check again; if the macro is
1503 // still defined and associated with the identifier,
1504 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1505 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1506 MacroDefinition *MD
1507 = new (PPRec) MacroDefinition(II,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001508 ReadSourceLocation(F, Record[5]),
Douglas Gregor36ea4d42010-10-01 20:33:34 +00001509 SourceRange(
Sebastian Redl2c373b92010-10-05 15:59:54 +00001510 ReadSourceLocation(F, Record[2]),
1511 ReadSourceLocation(F, Record[3])));
Douglas Gregor91096292010-10-02 19:29:26 +00001512
1513 PPRec.SetPreallocatedEntity(Record[0], MD);
1514 MacroDefinitionsLoaded[Record[1] - 1] = MD;
1515
1516 if (DeserializationListener)
1517 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1518 }
1519
Douglas Gregoraae92242010-03-19 21:51:54 +00001520 return;
1521 }
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001522 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001523 }
1524}
1525
Sebastian Redl2c499f62010-08-18 23:56:43 +00001526void ASTReader::ReadDefinedMacros() {
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001527 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001528 PerFileData &F = *Chain[N - I - 1];
1529 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001530
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001531 // If there was no preprocessor block, skip this file.
1532 if (!MacroCursor.getBitStreamReader())
1533 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001534
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001535 llvm::BitstreamCursor Cursor = MacroCursor;
Sebastian Redl539c5062010-08-18 23:57:32 +00001536 if (Cursor.EnterSubBlock(PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001537 Error("malformed preprocessor block record in AST file");
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001538 return;
1539 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001540
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001541 RecordData Record;
1542 while (true) {
Sebastian Redl4102dd52010-09-28 02:55:49 +00001543 uint64_t Offset = Cursor.GetCurrentBitNo();
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001544 unsigned Code = Cursor.ReadCode();
1545 if (Code == llvm::bitc::END_BLOCK) {
1546 if (Cursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001547 Error("error at end of preprocessor block in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001548 return;
1549 }
1550 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001551 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001552
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001553 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1554 // No known subblocks, always skip them.
1555 Cursor.ReadSubBlockID();
1556 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001557 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001558 return;
1559 }
1560 continue;
1561 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001562
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001563 if (Code == llvm::bitc::DEFINE_ABBREV) {
1564 Cursor.ReadAbbrevRecord();
1565 continue;
1566 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001567
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001568 // Read a record.
1569 const char *BlobStart;
1570 unsigned BlobLen;
1571 Record.clear();
1572 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1573 default: // Default behavior: ignore.
1574 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001575
Sebastian Redl539c5062010-08-18 23:57:32 +00001576 case PP_MACRO_OBJECT_LIKE:
1577 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001578 DecodeIdentifierInfo(Record[0]);
1579 break;
1580
Sebastian Redl539c5062010-08-18 23:57:32 +00001581 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001582 // Ignore tokens.
1583 break;
Douglas Gregoraae92242010-03-19 21:51:54 +00001584
Sebastian Redl539c5062010-08-18 23:57:32 +00001585 case PP_MACRO_INSTANTIATION:
1586 case PP_MACRO_DEFINITION:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001587 // Read the macro record.
Sebastian Redl4102dd52010-09-28 02:55:49 +00001588 // FIXME: That's a stupid way to do this. We should reuse this cursor.
Sebastian Redl2c373b92010-10-05 15:59:54 +00001589 ReadMacroRecord(F, Offset);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001590 break;
1591 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001592 }
1593 }
1594}
1595
Sebastian Redl50e26582010-09-15 19:54:06 +00001596MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor91096292010-10-02 19:29:26 +00001597 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregoraae92242010-03-19 21:51:54 +00001598 return 0;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001599
Douglas Gregor91096292010-10-02 19:29:26 +00001600 if (!MacroDefinitionsLoaded[ID - 1]) {
1601 unsigned Index = ID - 1;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001602 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1603 PerFileData &F = *Chain[N - I - 1];
1604 if (Index < F.LocalNumMacroDefinitions) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001605 ReadMacroRecord(F, F.MacroDefinitionOffsets[Index]);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001606 break;
1607 }
1608 Index -= F.LocalNumMacroDefinitions;
1609 }
Douglas Gregor91096292010-10-02 19:29:26 +00001610 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001611 }
1612
Douglas Gregor91096292010-10-02 19:29:26 +00001613 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001614}
1615
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001616/// \brief If we are loading a relocatable PCH file, and the filename is
1617/// not an absolute path, add the system root to the beginning of the file
1618/// name.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001619void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001620 // If this is not a relocatable PCH file, there's nothing to do.
1621 if (!RelocatablePCH)
1622 return;
Mike Stump11289f42009-09-09 15:08:12 +00001623
Daniel Dunbarf2ce9a22009-11-18 19:50:41 +00001624 if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001625 return;
1626
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001627 if (isysroot == 0) {
1628 // If no system root was given, default to '/'
1629 Filename.insert(Filename.begin(), '/');
1630 return;
1631 }
Mike Stump11289f42009-09-09 15:08:12 +00001632
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001633 unsigned Length = strlen(isysroot);
1634 if (isysroot[Length - 1] != '/')
1635 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001636
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001637 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1638}
1639
Sebastian Redl2c499f62010-08-18 23:56:43 +00001640ASTReader::ASTReadResult
Sebastian Redl3e31c722010-08-18 23:56:56 +00001641ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001642 llvm::BitstreamCursor &Stream = F.Stream;
1643
Sebastian Redl539c5062010-08-18 23:57:32 +00001644 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001645 Error("malformed block record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001646 return Failure;
1647 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001648
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001649 // Read all of the records and blocks for the ASt file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001650 RecordData Record;
Sebastian Redl393f8b72010-07-19 20:52:06 +00001651 bool First = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001652 while (!Stream.AtEndOfStream()) {
1653 unsigned Code = Stream.ReadCode();
1654 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001655 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001656 Error("error at end of module block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001657 return Failure;
1658 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001659
Douglas Gregor55abb232009-04-10 20:39:37 +00001660 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001661 }
1662
1663 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1664 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001665 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001666 // We lazily load the decls block, but we want to set up the
1667 // DeclsCursor cursor to point into it. Clone our current bitcode
1668 // cursor to it, enter the block and read the abbrevs in that block.
1669 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001670 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001671 if (Stream.SkipBlock() || // Skip with the main cursor.
1672 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001673 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001674 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001675 return Failure;
1676 }
1677 break;
Mike Stump11289f42009-09-09 15:08:12 +00001678
Sebastian Redl539c5062010-08-18 23:57:32 +00001679 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001680 F.MacroCursor = Stream;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001681 if (PP)
1682 PP->setExternalSource(this);
1683
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001684 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001685 Error("malformed block record in AST file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001686 return Failure;
1687 }
1688 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001689
Sebastian Redl539c5062010-08-18 23:57:32 +00001690 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001691 switch (ReadSourceManagerBlock(F)) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001692 case Success:
1693 break;
1694
1695 case Failure:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001696 Error("malformed source manager block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001697 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001698
1699 case IgnorePCH:
1700 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001701 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001702 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001703 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00001704 First = false;
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001705 continue;
1706 }
1707
1708 if (Code == llvm::bitc::DEFINE_ABBREV) {
1709 Stream.ReadAbbrevRecord();
1710 continue;
1711 }
1712
1713 // Read and process a record.
1714 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001715 const char *BlobStart = 0;
1716 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001717 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001718 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001719 default: // Default behavior: ignore.
1720 break;
1721
Sebastian Redl539c5062010-08-18 23:57:32 +00001722 case METADATA: {
1723 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1724 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001725 : diag::warn_pch_version_too_new);
1726 return IgnorePCH;
1727 }
1728
1729 RelocatablePCH = Record[4];
1730 if (Listener) {
1731 std::string TargetTriple(BlobStart, BlobLen);
1732 if (Listener->ReadTargetTriple(TargetTriple))
1733 return IgnorePCH;
1734 }
1735 break;
1736 }
1737
Sebastian Redl539c5062010-08-18 23:57:32 +00001738 case CHAINED_METADATA: {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001739 if (!First) {
1740 Error("CHAINED_METADATA is not first record in block");
1741 return Failure;
1742 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001743 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1744 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001745 : diag::warn_pch_version_too_new);
1746 return IgnorePCH;
1747 }
1748
Sebastian Redl009e7f22010-10-05 16:15:19 +00001749 // Load the chained file, which is always a PCH file.
1750 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001751 case Failure: return Failure;
1752 // If we have to ignore the dependency, we'll have to ignore this too.
1753 case IgnorePCH: return IgnorePCH;
1754 case Success: break;
1755 }
1756 break;
1757 }
1758
Sebastian Redl539c5062010-08-18 23:57:32 +00001759 case TYPE_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001760 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001761 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001762 return Failure;
1763 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001764 F.TypeOffsets = (const uint32_t *)BlobStart;
1765 F.LocalNumTypes = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001766 break;
1767
Sebastian Redl539c5062010-08-18 23:57:32 +00001768 case DECL_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001769 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001770 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001771 return Failure;
1772 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001773 F.DeclOffsets = (const uint32_t *)BlobStart;
1774 F.LocalNumDecls = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001775 break;
Douglas Gregor55abb232009-04-10 20:39:37 +00001776
Sebastian Redl539c5062010-08-18 23:57:32 +00001777 case TU_UPDATE_LEXICAL: {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001778 DeclContextInfo Info = {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001779 /* No visible information */ 0,
Sebastian Redl539c5062010-08-18 23:57:32 +00001780 reinterpret_cast<const DeclID *>(BlobStart),
1781 BlobLen / sizeof(DeclID)
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001782 };
Douglas Gregoraa433012010-10-01 01:18:02 +00001783 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
1784 .push_back(Info);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001785 break;
1786 }
1787
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001788 case UPDATE_VISIBLE: {
1789 serialization::DeclID ID = Record[0];
1790 void *Table = ASTDeclContextNameLookupTable::Create(
1791 (const unsigned char *)BlobStart + Record[1],
1792 (const unsigned char *)BlobStart,
1793 ASTDeclContextNameLookupTrait(*this));
Douglas Gregoraa433012010-10-01 01:18:02 +00001794 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001795 DeclContextInfo Info = {
1796 Table, /* No lexical inforamtion */ 0, 0
1797 };
1798 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1799 } else
1800 PendingVisibleUpdates[ID].push_back(Table);
1801 break;
1802 }
1803
Sebastian Redl539c5062010-08-18 23:57:32 +00001804 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001805 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1806 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001807 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001808 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1809 Latest > FirstLatestDeclIDs[First]) &&
1810 "The new latest is supposed to come after the previous latest");
1811 FirstLatestDeclIDs[First] = Latest;
1812 }
1813 break;
1814 }
1815
Sebastian Redl539c5062010-08-18 23:57:32 +00001816 case LANGUAGE_OPTIONS:
Douglas Gregorce3a8292010-07-27 00:27:13 +00001817 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor55abb232009-04-10 20:39:37 +00001818 return IgnorePCH;
1819 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00001820
Sebastian Redl539c5062010-08-18 23:57:32 +00001821 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001822 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00001823 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001824 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001825 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00001826 (const unsigned char *)F.IdentifierTableData + Record[0],
1827 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001828 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001829 if (PP)
1830 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00001831 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001832 break;
1833
Sebastian Redl539c5062010-08-18 23:57:32 +00001834 case IDENTIFIER_OFFSET:
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001835 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001836 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001837 return Failure;
1838 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001839 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1840 F.LocalNumIdentifiers = Record[0];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001841 break;
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001842
Sebastian Redl539c5062010-08-18 23:57:32 +00001843 case EXTERNAL_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001844 // Optimization for the first block.
1845 if (ExternalDefinitions.empty())
1846 ExternalDefinitions.swap(Record);
1847 else
1848 ExternalDefinitions.insert(ExternalDefinitions.end(),
1849 Record.begin(), Record.end());
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001850 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00001851
Sebastian Redl539c5062010-08-18 23:57:32 +00001852 case SPECIAL_TYPES:
Sebastian Redlb293a452010-07-20 21:20:32 +00001853 // Optimization for the first block
1854 if (SpecialTypes.empty())
1855 SpecialTypes.swap(Record);
1856 else
1857 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregor652d82a2009-04-18 05:55:16 +00001858 break;
1859
Sebastian Redl539c5062010-08-18 23:57:32 +00001860 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001861 TotalNumStatements += Record[0];
1862 TotalNumMacros += Record[1];
1863 TotalLexicalDeclContexts += Record[2];
1864 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00001865 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00001866
Sebastian Redl539c5062010-08-18 23:57:32 +00001867 case TENTATIVE_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001868 // Optimization for the first block.
1869 if (TentativeDefinitions.empty())
1870 TentativeDefinitions.swap(Record);
1871 else
1872 TentativeDefinitions.insert(TentativeDefinitions.end(),
1873 Record.begin(), Record.end());
Douglas Gregord4df8652009-04-22 22:02:47 +00001874 break;
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001875
Sebastian Redl539c5062010-08-18 23:57:32 +00001876 case UNUSED_FILESCOPED_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001877 // Optimization for the first block.
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00001878 if (UnusedFileScopedDecls.empty())
1879 UnusedFileScopedDecls.swap(Record);
Sebastian Redlb293a452010-07-20 21:20:32 +00001880 else
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00001881 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1882 Record.begin(), Record.end());
Tanya Lattner90073802010-02-12 00:07:30 +00001883 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001884
Sebastian Redl539c5062010-08-18 23:57:32 +00001885 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001886 // Later blocks overwrite earlier ones.
1887 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00001888 break;
1889
Sebastian Redl539c5062010-08-18 23:57:32 +00001890 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001891 // Optimization for the first block.
1892 if (LocallyScopedExternalDecls.empty())
1893 LocallyScopedExternalDecls.swap(Record);
1894 else
1895 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1896 Record.begin(), Record.end());
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001897 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001898
Sebastian Redl539c5062010-08-18 23:57:32 +00001899 case SELECTOR_OFFSETS:
Sebastian Redla19a67f2010-08-03 21:58:15 +00001900 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00001901 F.LocalNumSelectors = Record[0];
Douglas Gregor95c13f52009-04-25 17:48:32 +00001902 break;
1903
Sebastian Redl539c5062010-08-18 23:57:32 +00001904 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00001905 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001906 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00001907 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001908 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00001909 F.SelectorLookupTableData + Record[0],
1910 F.SelectorLookupTableData,
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001911 ASTSelectorLookupTrait(*this));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00001912 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00001913 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00001914
Sebastian Redl96371b42010-09-22 00:42:30 +00001915 case REFERENCED_SELECTOR_POOL:
Sebastian Redl2c373b92010-10-05 15:59:54 +00001916 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001917 break;
1918
Sebastian Redl539c5062010-08-18 23:57:32 +00001919 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001920 if (!Record.empty() && Listener)
1921 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00001922 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00001923
Sebastian Redl539c5062010-08-18 23:57:32 +00001924 case SOURCE_LOCATION_OFFSETS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001925 F.SLocOffsets = (const uint32_t *)BlobStart;
1926 F.LocalNumSLocEntries = Record[0];
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001927 F.LocalSLocSize = Record[1];
Douglas Gregor258ae542009-04-27 06:38:32 +00001928 break;
1929
Sebastian Redl539c5062010-08-18 23:57:32 +00001930 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl96371b42010-09-22 00:42:30 +00001931 if (PreloadSLocEntries.empty())
1932 PreloadSLocEntries.swap(Record);
1933 else
1934 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
1935 Record.begin(), Record.end());
Douglas Gregor258ae542009-04-27 06:38:32 +00001936 break;
Douglas Gregorc5046832009-04-27 18:38:38 +00001937
Sebastian Redl539c5062010-08-18 23:57:32 +00001938 case STAT_CACHE: {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001939 ASTStatCache *MyStatCache =
1940 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001941 (const unsigned char *)BlobStart,
1942 NumStatHits, NumStatMisses);
1943 FileMgr.addStatCache(MyStatCache);
Sebastian Redl34522812010-07-16 17:50:48 +00001944 F.StatCache = MyStatCache;
Douglas Gregorc5046832009-04-27 18:38:38 +00001945 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001946 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001947
Sebastian Redl539c5062010-08-18 23:57:32 +00001948 case EXT_VECTOR_DECLS:
Sebastian Redl04f5c312010-07-28 21:38:49 +00001949 // Optimization for the first block.
1950 if (ExtVectorDecls.empty())
1951 ExtVectorDecls.swap(Record);
1952 else
1953 ExtVectorDecls.insert(ExtVectorDecls.end(),
1954 Record.begin(), Record.end());
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001955 break;
1956
Sebastian Redl539c5062010-08-18 23:57:32 +00001957 case VTABLE_USES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001958 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00001959 VTableUses.swap(Record);
1960 break;
1961
Sebastian Redl539c5062010-08-18 23:57:32 +00001962 case DYNAMIC_CLASSES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001963 // Optimization for the first block.
1964 if (DynamicClasses.empty())
1965 DynamicClasses.swap(Record);
1966 else
1967 DynamicClasses.insert(DynamicClasses.end(),
1968 Record.begin(), Record.end());
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00001969 break;
1970
Sebastian Redl539c5062010-08-18 23:57:32 +00001971 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redl2c373b92010-10-05 15:59:54 +00001972 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00001973 break;
1974
Sebastian Redl539c5062010-08-18 23:57:32 +00001975 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001976 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00001977 SemaDeclRefs.swap(Record);
1978 break;
1979
Sebastian Redl539c5062010-08-18 23:57:32 +00001980 case ORIGINAL_FILE_NAME:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001981 // The primary AST will be the last to get here, so it will be the one
Sebastian Redlb293a452010-07-20 21:20:32 +00001982 // that's used.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00001983 ActualOriginalFileName.assign(BlobStart, BlobLen);
1984 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001985 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001986 break;
Mike Stump11289f42009-09-09 15:08:12 +00001987
Sebastian Redl539c5062010-08-18 23:57:32 +00001988 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00001989 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001990 llvm::StringRef ASTBranch(BlobStart, BlobLen);
1991 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1992 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregord54f3a12009-10-05 21:07:28 +00001993 return IgnorePCH;
1994 }
1995 break;
1996 }
Sebastian Redlfa061442010-07-21 20:07:32 +00001997
Sebastian Redl539c5062010-08-18 23:57:32 +00001998 case MACRO_DEFINITION_OFFSETS:
Sebastian Redlfa061442010-07-21 20:07:32 +00001999 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2000 F.NumPreallocatedPreprocessingEntities = Record[0];
2001 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregoraae92242010-03-19 21:51:54 +00002002 break;
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002003
Sebastian Redl539c5062010-08-18 23:57:32 +00002004 case DECL_REPLACEMENTS: {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002005 if (Record.size() % 2 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002006 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002007 return Failure;
2008 }
2009 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl539c5062010-08-18 23:57:32 +00002010 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002011 std::make_pair(&F, Record[I+1]);
2012 break;
2013 }
Sebastian Redlaba202b2010-08-24 22:50:19 +00002014
2015 case ADDITIONAL_TEMPLATE_SPECIALIZATIONS: {
2016 AdditionalTemplateSpecializations &ATS =
2017 AdditionalTemplateSpecializationsPending[Record[0]];
2018 ATS.insert(ATS.end(), Record.begin()+1, Record.end());
2019 break;
2020 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002021 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00002022 First = false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002023 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002024 Error("premature end of bitstream in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002025 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002026}
2027
Sebastian Redl009e7f22010-10-05 16:15:19 +00002028ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2029 ASTFileType Type) {
2030 switch(ReadASTCore(FileName, Type)) {
Sebastian Redl2abc0382010-07-16 20:41:52 +00002031 case Failure: return Failure;
2032 case IgnorePCH: return IgnorePCH;
2033 case Success: break;
2034 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002035
2036 // Here comes stuff that we only do once the entire chain is loaded.
2037
Sebastian Redl96371b42010-09-22 00:42:30 +00002038 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redlfa061442010-07-21 20:07:32 +00002039 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redlada023c2010-08-04 20:40:17 +00002040 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2041 TotalNumSelectors = 0;
Sebastian Redl9e687992010-07-19 22:06:55 +00002042 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl96371b42010-09-22 00:42:30 +00002043 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002044 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002045 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl9e687992010-07-19 22:06:55 +00002046 TotalNumTypes += Chain[I]->LocalNumTypes;
2047 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redlfa061442010-07-21 20:07:32 +00002048 TotalNumPreallocatedPreprocessingEntities +=
2049 Chain[I]->NumPreallocatedPreprocessingEntities;
2050 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redlada023c2010-08-04 20:40:17 +00002051 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl9e687992010-07-19 22:06:55 +00002052 }
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002053 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002054 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl9e687992010-07-19 22:06:55 +00002055 TypesLoaded.resize(TotalNumTypes);
2056 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redlfa061442010-07-21 20:07:32 +00002057 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2058 if (PP) {
2059 if (TotalNumIdentifiers > 0)
2060 PP->getHeaderSearchInfo().SetExternalLookup(this);
2061 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2062 if (!PP->getPreprocessingRecord())
2063 PP->createPreprocessingRecord();
2064 PP->getPreprocessingRecord()->SetExternalSource(*this,
2065 TotalNumPreallocatedPreprocessingEntities);
2066 }
2067 }
Sebastian Redlada023c2010-08-04 20:40:17 +00002068 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl96371b42010-09-22 00:42:30 +00002069 // Preload SLocEntries.
2070 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2071 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2072 if (Result != Success)
2073 return Result;
2074 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002075
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002076 // Check the predefines buffers.
Douglas Gregorce3a8292010-07-27 00:27:13 +00002077 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002078 return IgnorePCH;
2079
2080 if (PP) {
2081 // Initialization of keywords and pragmas occurs before the
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002082 // AST file is read, so there may be some identifiers that were
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002083 // loaded into the IdentifierTable before we intercepted the
2084 // creation of identifiers. Iterate through the list of known
2085 // identifiers and determine whether we have to establish
2086 // preprocessor definitions or top-level identifier declaration
2087 // chains for those identifiers.
2088 //
2089 // We copy the IdentifierInfo pointers to a small vector first,
2090 // since de-serializing declarations or macro definitions can add
2091 // new entries into the identifier table, invalidating the
2092 // iterators.
2093 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2094 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2095 IdEnd = PP->getIdentifierTable().end();
2096 Id != IdEnd; ++Id)
2097 Identifiers.push_back(Id->second);
Sebastian Redlfa061442010-07-21 20:07:32 +00002098 // We need to search the tables in all files.
Sebastian Redlfa061442010-07-21 20:07:32 +00002099 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002100 ASTIdentifierLookupTable *IdTable
2101 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2102 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl5c415f32010-07-22 17:01:13 +00002103 // ones.
2104 if (!IdTable)
2105 continue;
Sebastian Redlfa061442010-07-21 20:07:32 +00002106 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2107 IdentifierInfo *II = Identifiers[I];
2108 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00002109 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redlfa061442010-07-21 20:07:32 +00002110 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002111 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redlb293a452010-07-20 21:20:32 +00002112 if (Pos == IdTable->end())
2113 continue;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002114
Sebastian Redlb293a452010-07-20 21:20:32 +00002115 // Dereferencing the iterator has the effect of populating the
2116 // IdentifierInfo node with the various declarations it needs.
2117 (void)*Pos;
2118 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002119 }
2120 }
2121
2122 if (Context)
2123 InitializeContext(*Context);
2124
2125 return Success;
2126}
2127
Sebastian Redl009e7f22010-10-05 16:15:19 +00002128ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2129 ASTFileType Type) {
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002130 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl009e7f22010-10-05 16:15:19 +00002131 Chain.push_back(new PerFileData(Type));
Sebastian Redl34522812010-07-16 17:50:48 +00002132 PerFileData &F = *Chain.back();
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002133 if (Prev)
2134 Prev->NextInSource = &F;
2135 else
2136 FirstInSource = &F;
2137 F.Loaders.push_back(Prev);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002138
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002139 // Set the AST file name.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002140 F.FileName = FileName;
2141
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002142 // Open the AST file.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002143 //
2144 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2145 std::string ErrStr;
2146 F.Buffer.reset(llvm::MemoryBuffer::getFileOrSTDIN(FileName, &ErrStr));
2147 if (!F.Buffer) {
2148 Error(ErrStr.c_str());
2149 return IgnorePCH;
2150 }
2151
2152 // Initialize the stream
2153 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2154 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl34522812010-07-16 17:50:48 +00002155 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002156 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002157 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002158
2159 // Sniff for the signature.
2160 if (Stream.Read(8) != 'C' ||
2161 Stream.Read(8) != 'P' ||
2162 Stream.Read(8) != 'C' ||
2163 Stream.Read(8) != 'H') {
2164 Diag(diag::err_not_a_pch_file) << FileName;
2165 return Failure;
2166 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002167
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002168 while (!Stream.AtEndOfStream()) {
2169 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002170
Douglas Gregor92863e42009-04-10 23:10:45 +00002171 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002172 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002173 return Failure;
2174 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002175
2176 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002177
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002178 // We only know the AST subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002179 switch (BlockID) {
2180 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002181 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002182 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002183 return Failure;
2184 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002185 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00002186 case AST_BLOCK_ID:
Sebastian Redl3e31c722010-08-18 23:56:56 +00002187 switch (ReadASTBlock(F)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002188 case Success:
2189 break;
2190
2191 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00002192 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00002193
2194 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00002195 // FIXME: We could consider reading through to the end of this
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002196 // AST block, skipping subblocks, to see if there are other
2197 // AST blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00002198
2199 // Clear out any preallocated source location entries, so that
2200 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002201 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00002202
2203 // Remove the stat cache.
Sebastian Redl34522812010-07-16 17:50:48 +00002204 if (F.StatCache)
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002205 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00002206
Douglas Gregor92863e42009-04-10 23:10:45 +00002207 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002208 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002209 break;
2210 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002211 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002212 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002213 return Failure;
2214 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002215 break;
2216 }
Mike Stump11289f42009-09-09 15:08:12 +00002217 }
2218
Sebastian Redl2abc0382010-07-16 20:41:52 +00002219 return Success;
2220}
2221
Sebastian Redl2c499f62010-08-18 23:56:43 +00002222void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002223 PP = &pp;
Sebastian Redlfa061442010-07-21 20:07:32 +00002224
2225 unsigned TotalNum = 0;
2226 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2227 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2228 if (TotalNum) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002229 if (!PP->getPreprocessingRecord())
2230 PP->createPreprocessingRecord();
Sebastian Redlfa061442010-07-21 20:07:32 +00002231 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregoraae92242010-03-19 21:51:54 +00002232 }
2233}
2234
Sebastian Redl2c499f62010-08-18 23:56:43 +00002235void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002236 Context = &Ctx;
2237 assert(Context && "Passed null context!");
2238
2239 assert(PP && "Forgot to set Preprocessor ?");
2240 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2241 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00002242 PP->setExternalSource(this);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002243
Douglas Gregoraa433012010-10-01 01:18:02 +00002244 // If we have an update block for the TU waiting, we have to add it before
2245 // deserializing the decl.
2246 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2247 if (DCU != DeclContextOffsets.end()) {
2248 // Insertion could invalidate map, so grab vector.
2249 DeclContextInfos T;
2250 T.swap(DCU->second);
2251 DeclContextOffsets.erase(DCU);
2252 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2253 }
2254
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002255 // Load the translation unit declaration
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00002256 GetTranslationUnitDecl();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002257
2258 // Load the special types.
2259 Context->setBuiltinVaListType(
Sebastian Redl539c5062010-08-18 23:57:32 +00002260 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2261 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002262 Context->setObjCIdType(GetType(Id));
Sebastian Redl539c5062010-08-18 23:57:32 +00002263 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002264 Context->setObjCSelType(GetType(Sel));
Sebastian Redl539c5062010-08-18 23:57:32 +00002265 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002266 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl539c5062010-08-18 23:57:32 +00002267 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002268 Context->setObjCClassType(GetType(Class));
Steve Naroff7cae42b2009-07-10 23:34:53 +00002269
Sebastian Redl539c5062010-08-18 23:57:32 +00002270 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002271 Context->setCFConstantStringType(GetType(String));
Mike Stump11289f42009-09-09 15:08:12 +00002272 if (unsigned FastEnum
Sebastian Redl539c5062010-08-18 23:57:32 +00002273 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002274 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl539c5062010-08-18 23:57:32 +00002275 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregor27821ce2009-07-07 16:35:42 +00002276 QualType FileType = GetType(File);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002277 if (FileType.isNull()) {
2278 Error("FILE type is NULL");
2279 return;
2280 }
John McCall9dd450b2009-09-21 23:43:11 +00002281 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregor27821ce2009-07-07 16:35:42 +00002282 Context->setFILEDecl(Typedef->getDecl());
2283 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002284 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002285 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002286 Error("Invalid FILE type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002287 return;
2288 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002289 Context->setFILEDecl(Tag->getDecl());
2290 }
2291 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002292 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002293 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002294 if (Jmp_bufType.isNull()) {
2295 Error("jmp_bug type is NULL");
2296 return;
2297 }
John McCall9dd450b2009-09-21 23:43:11 +00002298 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002299 Context->setjmp_bufDecl(Typedef->getDecl());
2300 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002301 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002302 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002303 Error("Invalid jmp_buf type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002304 return;
2305 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002306 Context->setjmp_bufDecl(Tag->getDecl());
2307 }
2308 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002309 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002310 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002311 if (Sigjmp_bufType.isNull()) {
2312 Error("sigjmp_buf type is NULL");
2313 return;
2314 }
John McCall9dd450b2009-09-21 23:43:11 +00002315 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002316 Context->setsigjmp_bufDecl(Typedef->getDecl());
2317 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002318 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002319 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stumpa4de80b2009-07-28 02:25:19 +00002320 Context->setsigjmp_bufDecl(Tag->getDecl());
2321 }
2322 }
Mike Stump11289f42009-09-09 15:08:12 +00002323 if (unsigned ObjCIdRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002324 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002325 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump11289f42009-09-09 15:08:12 +00002326 if (unsigned ObjCClassRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002327 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002328 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002329 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpd0153282009-10-20 02:12:22 +00002330 Context->setBlockDescriptorType(GetType(String));
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002331 if (unsigned String
Sebastian Redl539c5062010-08-18 23:57:32 +00002332 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002333 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002334 if (unsigned ObjCSelRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002335 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002336 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002337 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002338 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002339
Sebastian Redl539c5062010-08-18 23:57:32 +00002340 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002341 Context->setInt128Installed();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002342}
2343
Douglas Gregor45fe0362009-05-12 01:31:05 +00002344/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002345/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00002346/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002347std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Daniel Dunbar3b951482009-12-03 09:13:06 +00002348 Diagnostic &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002349 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002350 std::string ErrStr;
2351 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002352 Buffer.reset(llvm::MemoryBuffer::getFile(ASTFileName.c_str(), &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00002353 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00002354 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002355 return std::string();
2356 }
2357
2358 // Initialize the stream
2359 llvm::BitstreamReader StreamFile;
2360 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00002361 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00002362 (const unsigned char *)Buffer->getBufferEnd());
2363 Stream.init(StreamFile);
2364
2365 // Sniff for the signature.
2366 if (Stream.Read(8) != 'C' ||
2367 Stream.Read(8) != 'P' ||
2368 Stream.Read(8) != 'C' ||
2369 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002370 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002371 return std::string();
2372 }
2373
2374 RecordData Record;
2375 while (!Stream.AtEndOfStream()) {
2376 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002377
Douglas Gregor45fe0362009-05-12 01:31:05 +00002378 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2379 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00002380
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002381 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002382 switch (BlockID) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002383 case AST_BLOCK_ID:
2384 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002385 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002386 return std::string();
2387 }
2388 break;
Mike Stump11289f42009-09-09 15:08:12 +00002389
Douglas Gregor45fe0362009-05-12 01:31:05 +00002390 default:
2391 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002392 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002393 return std::string();
2394 }
2395 break;
2396 }
2397 continue;
2398 }
2399
2400 if (Code == llvm::bitc::END_BLOCK) {
2401 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002402 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002403 return std::string();
2404 }
2405 continue;
2406 }
2407
2408 if (Code == llvm::bitc::DEFINE_ABBREV) {
2409 Stream.ReadAbbrevRecord();
2410 continue;
2411 }
2412
2413 Record.clear();
2414 const char *BlobStart = 0;
2415 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002416 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl539c5062010-08-18 23:57:32 +00002417 == ORIGINAL_FILE_NAME)
Douglas Gregor45fe0362009-05-12 01:31:05 +00002418 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00002419 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00002420
2421 return std::string();
2422}
2423
Douglas Gregor55abb232009-04-10 20:39:37 +00002424/// \brief Parse the record that corresponds to a LangOptions data
2425/// structure.
2426///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002427/// This routine parses the language options from the AST file and then gives
2428/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00002429///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002430/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002431bool ASTReader::ParseLanguageOptions(
Douglas Gregor55abb232009-04-10 20:39:37 +00002432 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002433 if (Listener) {
2434 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00002435
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002436 #define PARSE_LANGOPT(Option) \
2437 LangOpts.Option = Record[Idx]; \
2438 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00002439
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002440 unsigned Idx = 0;
2441 PARSE_LANGOPT(Trigraphs);
2442 PARSE_LANGOPT(BCPLComment);
2443 PARSE_LANGOPT(DollarIdents);
2444 PARSE_LANGOPT(AsmPreprocessor);
2445 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00002446 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002447 PARSE_LANGOPT(ImplicitInt);
2448 PARSE_LANGOPT(Digraphs);
2449 PARSE_LANGOPT(HexFloats);
2450 PARSE_LANGOPT(C99);
2451 PARSE_LANGOPT(Microsoft);
2452 PARSE_LANGOPT(CPlusPlus);
2453 PARSE_LANGOPT(CPlusPlus0x);
2454 PARSE_LANGOPT(CXXOperatorNames);
2455 PARSE_LANGOPT(ObjC1);
2456 PARSE_LANGOPT(ObjC2);
2457 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00002458 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00002459 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002460 PARSE_LANGOPT(PascalStrings);
2461 PARSE_LANGOPT(WritableStrings);
2462 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00002463 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002464 PARSE_LANGOPT(Exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +00002465 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002466 PARSE_LANGOPT(NeXTRuntime);
2467 PARSE_LANGOPT(Freestanding);
2468 PARSE_LANGOPT(NoBuiltin);
2469 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00002470 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002471 PARSE_LANGOPT(Blocks);
2472 PARSE_LANGOPT(EmitAllDecls);
2473 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00002474 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2475 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002476 PARSE_LANGOPT(HeinousExtensions);
2477 PARSE_LANGOPT(Optimize);
2478 PARSE_LANGOPT(OptimizeSize);
2479 PARSE_LANGOPT(Static);
2480 PARSE_LANGOPT(PICLevel);
2481 PARSE_LANGOPT(GNUInline);
2482 PARSE_LANGOPT(NoInline);
2483 PARSE_LANGOPT(AccessControl);
2484 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00002485 PARSE_LANGOPT(ShortWChar);
Chris Lattner51924e512010-06-26 21:25:03 +00002486 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
2487 LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00002488 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00002489 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002490 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00002491 PARSE_LANGOPT(OpenCL);
Mike Stumpd9546382009-12-12 01:27:46 +00002492 PARSE_LANGOPT(CatchUndefined);
2493 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002494 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00002495
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002496 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00002497 }
Douglas Gregor55abb232009-04-10 20:39:37 +00002498
2499 return false;
2500}
2501
Sebastian Redl2c499f62010-08-18 23:56:43 +00002502void ASTReader::ReadPreprocessedEntities() {
Douglas Gregoraae92242010-03-19 21:51:54 +00002503 ReadDefinedMacros();
2504}
2505
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002506/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002507ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002508 PerFileData *F = 0;
2509 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2510 F = Chain[N - I - 1];
2511 if (Index < F->LocalNumTypes)
2512 break;
2513 Index -= F->LocalNumTypes;
2514 }
2515 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redl2c373b92010-10-05 15:59:54 +00002516 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002517}
2518
2519/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002520///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002521/// The index is the type ID, shifted and minus the number of predefs. This
2522/// routine actually reads the record corresponding to the type at the given
2523/// location. It is a helper routine for GetType, which deals with reading type
2524/// IDs.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002525QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002526 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00002527 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00002528
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002529 // Keep track of where we are in the stream, then jump back there
2530 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002531 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002532
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002533 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00002534
Douglas Gregor1342e842009-07-06 18:54:52 +00002535 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00002536 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00002537
Sebastian Redl2c373b92010-10-05 15:59:54 +00002538 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002539 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002540 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00002541 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2542 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002543 if (Record.size() != 2) {
2544 Error("Incorrect encoding of extended qualifier type");
2545 return QualType();
2546 }
Douglas Gregor455b8f42009-04-15 22:00:08 +00002547 QualType Base = GetType(Record[0]);
John McCall8ccfcb52009-09-24 19:53:00 +00002548 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2549 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00002550 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002551
Sebastian Redl539c5062010-08-18 23:57:32 +00002552 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002553 if (Record.size() != 1) {
2554 Error("Incorrect encoding of complex type");
2555 return QualType();
2556 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002557 QualType ElemType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002558 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002559 }
2560
Sebastian Redl539c5062010-08-18 23:57:32 +00002561 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002562 if (Record.size() != 1) {
2563 Error("Incorrect encoding of pointer type");
2564 return QualType();
2565 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002566 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002567 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002568 }
2569
Sebastian Redl539c5062010-08-18 23:57:32 +00002570 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002571 if (Record.size() != 1) {
2572 Error("Incorrect encoding of block pointer type");
2573 return QualType();
2574 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002575 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002576 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002577 }
2578
Sebastian Redl539c5062010-08-18 23:57:32 +00002579 case TYPE_LVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002580 if (Record.size() != 1) {
2581 Error("Incorrect encoding of lvalue reference type");
2582 return QualType();
2583 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002584 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002585 return Context->getLValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002586 }
2587
Sebastian Redl539c5062010-08-18 23:57:32 +00002588 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002589 if (Record.size() != 1) {
2590 Error("Incorrect encoding of rvalue reference type");
2591 return QualType();
2592 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002593 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002594 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002595 }
2596
Sebastian Redl539c5062010-08-18 23:57:32 +00002597 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00002598 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002599 Error("Incorrect encoding of member pointer type");
2600 return QualType();
2601 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002602 QualType PointeeType = GetType(Record[0]);
2603 QualType ClassType = GetType(Record[1]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002604 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002605 }
2606
Sebastian Redl539c5062010-08-18 23:57:32 +00002607 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002608 QualType ElementType = GetType(Record[0]);
2609 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2610 unsigned IndexTypeQuals = Record[2];
2611 unsigned Idx = 3;
2612 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00002613 return Context->getConstantArrayType(ElementType, Size,
2614 ASM, IndexTypeQuals);
2615 }
2616
Sebastian Redl539c5062010-08-18 23:57:32 +00002617 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002618 QualType ElementType = GetType(Record[0]);
2619 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2620 unsigned IndexTypeQuals = Record[2];
Chris Lattner8575daa2009-04-27 21:45:14 +00002621 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002622 }
2623
Sebastian Redl539c5062010-08-18 23:57:32 +00002624 case TYPE_VARIABLE_ARRAY: {
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002625 QualType ElementType = GetType(Record[0]);
2626 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2627 unsigned IndexTypeQuals = Record[2];
Sebastian Redl2c373b92010-10-05 15:59:54 +00002628 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
2629 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
2630 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor04318252009-07-06 15:59:29 +00002631 ASM, IndexTypeQuals,
2632 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002633 }
2634
Sebastian Redl539c5062010-08-18 23:57:32 +00002635 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002636 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002637 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002638 return QualType();
2639 }
2640
2641 QualType ElementType = GetType(Record[0]);
2642 unsigned NumElements = Record[1];
Chris Lattner37141f42010-06-23 06:00:24 +00002643 unsigned AltiVecSpec = Record[2];
2644 return Context->getVectorType(ElementType, NumElements,
2645 (VectorType::AltiVecSpecific)AltiVecSpec);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002646 }
2647
Sebastian Redl539c5062010-08-18 23:57:32 +00002648 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002649 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002650 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002651 return QualType();
2652 }
2653
2654 QualType ElementType = GetType(Record[0]);
2655 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00002656 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002657 }
2658
Sebastian Redl539c5062010-08-18 23:57:32 +00002659 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002660 if (Record.size() != 4) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002661 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002662 return QualType();
2663 }
2664 QualType ResultType = GetType(Record[0]);
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002665 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00002666 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002667 }
2668
Sebastian Redl539c5062010-08-18 23:57:32 +00002669 case TYPE_FUNCTION_PROTO: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002670 QualType ResultType = GetType(Record[0]);
Douglas Gregordc728752009-12-22 18:11:50 +00002671 bool NoReturn = Record[1];
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002672 unsigned RegParm = Record[2];
2673 CallingConv CallConv = (CallingConv)Record[3];
2674 unsigned Idx = 4;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002675 unsigned NumParams = Record[Idx++];
2676 llvm::SmallVector<QualType, 16> ParamTypes;
2677 for (unsigned I = 0; I != NumParams; ++I)
2678 ParamTypes.push_back(GetType(Record[Idx++]));
2679 bool isVariadic = Record[Idx++];
2680 unsigned Quals = Record[Idx++];
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002681 bool hasExceptionSpec = Record[Idx++];
2682 bool hasAnyExceptionSpec = Record[Idx++];
2683 unsigned NumExceptions = Record[Idx++];
2684 llvm::SmallVector<QualType, 2> Exceptions;
2685 for (unsigned I = 0; I != NumExceptions; ++I)
2686 Exceptions.push_back(GetType(Record[Idx++]));
Jay Foad7d0479f2009-05-21 09:52:38 +00002687 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002688 isVariadic, Quals, hasExceptionSpec,
2689 hasAnyExceptionSpec, NumExceptions,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00002690 Exceptions.data(),
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002691 FunctionType::ExtInfo(NoReturn, RegParm,
2692 CallConv));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002693 }
2694
Sebastian Redl539c5062010-08-18 23:57:32 +00002695 case TYPE_UNRESOLVED_USING:
John McCallb96ec562009-12-04 22:46:56 +00002696 return Context->getTypeDeclType(
2697 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2698
Sebastian Redl539c5062010-08-18 23:57:32 +00002699 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002700 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002701 Error("incorrect encoding of typedef type");
2702 return QualType();
2703 }
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002704 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2705 QualType Canonical = GetType(Record[1]);
2706 return Context->getTypedefType(Decl, Canonical);
2707 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002708
Sebastian Redl539c5062010-08-18 23:57:32 +00002709 case TYPE_TYPEOF_EXPR:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002710 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002711
Sebastian Redl539c5062010-08-18 23:57:32 +00002712 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002713 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002714 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002715 return QualType();
2716 }
2717 QualType UnderlyingType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002718 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002719 }
Mike Stump11289f42009-09-09 15:08:12 +00002720
Sebastian Redl539c5062010-08-18 23:57:32 +00002721 case TYPE_DECLTYPE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002722 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson81df7b82009-06-24 19:06:50 +00002723
Sebastian Redl539c5062010-08-18 23:57:32 +00002724 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002725 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002726 Error("incorrect encoding of record type");
2727 return QualType();
2728 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002729 bool IsDependent = Record[0];
2730 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
2731 T->Dependent = IsDependent;
2732 return T;
2733 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002734
Sebastian Redl539c5062010-08-18 23:57:32 +00002735 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002736 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002737 Error("incorrect encoding of enum type");
2738 return QualType();
2739 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002740 bool IsDependent = Record[0];
2741 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
2742 T->Dependent = IsDependent;
2743 return T;
2744 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00002745
Sebastian Redl539c5062010-08-18 23:57:32 +00002746 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002747 unsigned Idx = 0;
2748 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2749 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2750 QualType NamedType = GetType(Record[Idx++]);
2751 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00002752 }
2753
Sebastian Redl539c5062010-08-18 23:57:32 +00002754 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00002755 unsigned Idx = 0;
2756 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00002757 return Context->getObjCInterfaceType(ItfD);
2758 }
2759
Sebastian Redl539c5062010-08-18 23:57:32 +00002760 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00002761 unsigned Idx = 0;
2762 QualType Base = GetType(Record[Idx++]);
Chris Lattner587cbe12009-04-22 06:45:28 +00002763 unsigned NumProtos = Record[Idx++];
2764 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2765 for (unsigned I = 0; I != NumProtos; ++I)
2766 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
John McCall8b07ec22010-05-15 11:32:37 +00002767 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00002768 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002769
Sebastian Redl539c5062010-08-18 23:57:32 +00002770 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00002771 unsigned Idx = 0;
John McCall8b07ec22010-05-15 11:32:37 +00002772 QualType Pointee = GetType(Record[Idx++]);
2773 return Context->getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00002774 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00002775
Sebastian Redl539c5062010-08-18 23:57:32 +00002776 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00002777 unsigned Idx = 0;
2778 QualType Parm = GetType(Record[Idx++]);
2779 QualType Replacement = GetType(Record[Idx++]);
2780 return
2781 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2782 Replacement);
2783 }
John McCalle78aac42010-03-10 03:28:59 +00002784
Sebastian Redl539c5062010-08-18 23:57:32 +00002785 case TYPE_INJECTED_CLASS_NAME: {
John McCalle78aac42010-03-10 03:28:59 +00002786 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2787 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00002788 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002789 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00002790 return
2791 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00002792 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002793
Sebastian Redl539c5062010-08-18 23:57:32 +00002794 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002795 unsigned Idx = 0;
2796 unsigned Depth = Record[Idx++];
2797 unsigned Index = Record[Idx++];
2798 bool Pack = Record[Idx++];
2799 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
2800 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
2801 }
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002802
Sebastian Redl539c5062010-08-18 23:57:32 +00002803 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002804 unsigned Idx = 0;
2805 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2806 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2807 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00002808 QualType Canon = GetType(Record[Idx++]);
2809 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002810 }
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002811
Sebastian Redl539c5062010-08-18 23:57:32 +00002812 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002813 unsigned Idx = 0;
2814 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2815 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2816 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2817 unsigned NumArgs = Record[Idx++];
2818 llvm::SmallVector<TemplateArgument, 8> Args;
2819 Args.reserve(NumArgs);
2820 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00002821 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002822 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
2823 Args.size(), Args.data());
2824 }
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00002825
Sebastian Redl539c5062010-08-18 23:57:32 +00002826 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00002827 unsigned Idx = 0;
2828
2829 // ArrayType
2830 QualType ElementType = GetType(Record[Idx++]);
2831 ArrayType::ArraySizeModifier ASM
2832 = (ArrayType::ArraySizeModifier)Record[Idx++];
2833 unsigned IndexTypeQuals = Record[Idx++];
2834
2835 // DependentSizedArrayType
Sebastian Redl2c373b92010-10-05 15:59:54 +00002836 Expr *NumElts = ReadExpr(*Loc.F);
2837 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00002838
2839 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
2840 IndexTypeQuals, Brackets);
2841 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002842
Sebastian Redl539c5062010-08-18 23:57:32 +00002843 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002844 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002845 bool IsDependent = Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002846 TemplateName Name = ReadTemplateName(Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002847 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redl2c373b92010-10-05 15:59:54 +00002848 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002849 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002850 QualType T;
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002851 if (Canon.isNull())
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002852 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
2853 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002854 else
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002855 T = Context->getTemplateSpecializationType(Name, Args.data(),
2856 Args.size(), Canon);
2857 T->Dependent = IsDependent;
2858 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002859 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002860 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002861 // Suppress a GCC warning
2862 return QualType();
2863}
2864
Sebastian Redl2c373b92010-10-05 15:59:54 +00002865class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00002866 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +00002867 ASTReader::PerFileData &F;
Sebastian Redlc67764e2010-07-22 22:43:28 +00002868 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redl2c499f62010-08-18 23:56:43 +00002869 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00002870 unsigned &Idx;
2871
Sebastian Redl2c373b92010-10-05 15:59:54 +00002872 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
2873 unsigned &I) {
2874 return Reader.ReadSourceLocation(F, R, I);
2875 }
2876
John McCall8f115c62009-10-16 21:56:05 +00002877public:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002878 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl2c499f62010-08-18 23:56:43 +00002879 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl2c373b92010-10-05 15:59:54 +00002880 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
2881 { }
John McCall8f115c62009-10-16 21:56:05 +00002882
John McCall17001972009-10-18 01:05:36 +00002883 // We want compile-time assurance that we've enumerated all of
2884 // these, so unfortunately we have to declare them first, then
2885 // define them out-of-line.
2886#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00002887#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00002888 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00002889#include "clang/AST/TypeLocNodes.def"
2890
John McCall17001972009-10-18 01:05:36 +00002891 void VisitFunctionTypeLoc(FunctionTypeLoc);
2892 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00002893};
2894
John McCall17001972009-10-18 01:05:36 +00002895void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00002896 // nothing to do
2897}
John McCall17001972009-10-18 01:05:36 +00002898void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002899 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorc9b7a592010-01-18 18:04:31 +00002900 if (TL.needsExtraLocalData()) {
2901 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
2902 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
2903 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
2904 TL.setModeAttr(Record[Idx++]);
2905 }
John McCall8f115c62009-10-16 21:56:05 +00002906}
John McCall17001972009-10-18 01:05:36 +00002907void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002908 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00002909}
John McCall17001972009-10-18 01:05:36 +00002910void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002911 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00002912}
John McCall17001972009-10-18 01:05:36 +00002913void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002914 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00002915}
John McCall17001972009-10-18 01:05:36 +00002916void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002917 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00002918}
John McCall17001972009-10-18 01:05:36 +00002919void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002920 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00002921}
John McCall17001972009-10-18 01:05:36 +00002922void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002923 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00002924}
John McCall17001972009-10-18 01:05:36 +00002925void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002926 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
2927 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00002928 if (Record[Idx++])
Sebastian Redl2c373b92010-10-05 15:59:54 +00002929 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor12bfa382009-10-17 00:13:19 +00002930 else
John McCall17001972009-10-18 01:05:36 +00002931 TL.setSizeExpr(0);
2932}
2933void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
2934 VisitArrayTypeLoc(TL);
2935}
2936void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
2937 VisitArrayTypeLoc(TL);
2938}
2939void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
2940 VisitArrayTypeLoc(TL);
2941}
2942void TypeLocReader::VisitDependentSizedArrayTypeLoc(
2943 DependentSizedArrayTypeLoc TL) {
2944 VisitArrayTypeLoc(TL);
2945}
2946void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
2947 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002948 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002949}
2950void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002951 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002952}
2953void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002954 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002955}
2956void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002957 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
2958 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor7fb25412010-10-01 18:44:50 +00002959 TL.setTrailingReturn(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00002960 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCalle6347002009-10-23 01:28:53 +00002961 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall17001972009-10-18 01:05:36 +00002962 }
2963}
2964void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
2965 VisitFunctionTypeLoc(TL);
2966}
2967void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
2968 VisitFunctionTypeLoc(TL);
2969}
John McCallb96ec562009-12-04 22:46:56 +00002970void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002971 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallb96ec562009-12-04 22:46:56 +00002972}
John McCall17001972009-10-18 01:05:36 +00002973void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002974 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002975}
2976void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002977 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
2978 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
2979 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002980}
2981void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002982 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
2983 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
2984 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
2985 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002986}
2987void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002988 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002989}
2990void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002991 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002992}
2993void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002994 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002995}
John McCall17001972009-10-18 01:05:36 +00002996void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002997 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002998}
John McCallcebee162009-10-18 09:09:24 +00002999void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3000 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003001 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallcebee162009-10-18 09:09:24 +00003002}
John McCall17001972009-10-18 01:05:36 +00003003void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3004 TemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003005 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3006 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3007 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall0ad16662009-10-29 08:12:44 +00003008 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3009 TL.setArgLocInfo(i,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003010 Reader.GetTemplateArgumentLocInfo(F,
3011 TL.getTypePtr()->getArg(i).getKind(),
3012 Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003013}
Abramo Bagnara6150c882010-05-11 21:36:43 +00003014void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003015 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3016 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003017}
John McCalle78aac42010-03-10 03:28:59 +00003018void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003019 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalle78aac42010-03-10 03:28:59 +00003020}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00003021void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003022 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3023 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3024 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003025}
John McCallc392f372010-06-11 00:33:02 +00003026void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3027 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003028 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3029 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3030 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3031 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3032 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003033 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3034 TL.setArgLocInfo(I,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003035 Reader.GetTemplateArgumentLocInfo(F,
3036 TL.getTypePtr()->getArg(I).getKind(),
3037 Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003038}
John McCall17001972009-10-18 01:05:36 +00003039void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003040 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8b07ec22010-05-15 11:32:37 +00003041}
3042void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3043 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003044 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3045 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003046 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003047 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003048}
John McCallfc93cf92009-10-22 22:37:11 +00003049void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003050 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCallfc93cf92009-10-22 22:37:11 +00003051}
John McCall8f115c62009-10-16 21:56:05 +00003052
Sebastian Redl2c373b92010-10-05 15:59:54 +00003053TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003054 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00003055 unsigned &Idx) {
3056 QualType InfoTy = GetType(Record[Idx++]);
3057 if (InfoTy.isNull())
3058 return 0;
3059
John McCallbcd03502009-12-07 02:54:59 +00003060 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003061 TypeLocReader TLR(*this, F, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00003062 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00003063 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00003064 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00003065}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003066
Sebastian Redl539c5062010-08-18 23:57:32 +00003067QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00003068 unsigned FastQuals = ID & Qualifiers::FastMask;
3069 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003070
Sebastian Redl539c5062010-08-18 23:57:32 +00003071 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003072 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00003073 switch ((PredefinedTypeIDs)Index) {
3074 case PREDEF_TYPE_NULL_ID: return QualType();
3075 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3076 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003077
Sebastian Redl539c5062010-08-18 23:57:32 +00003078 case PREDEF_TYPE_CHAR_U_ID:
3079 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003080 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00003081 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003082 break;
3083
Sebastian Redl539c5062010-08-18 23:57:32 +00003084 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3085 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3086 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3087 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3088 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3089 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3090 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3091 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3092 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3093 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3094 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3095 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3096 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3097 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3098 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3099 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3100 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3101 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3102 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3103 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3104 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3105 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3106 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3107 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003108 }
3109
3110 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00003111 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003112 }
3113
Sebastian Redl539c5062010-08-18 23:57:32 +00003114 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003115 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00003116 if (TypesLoaded[Index].isNull()) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003117 TypesLoaded[Index] = ReadTypeRecord(Index);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003118 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003119 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003120 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003121 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003122 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00003123 }
Mike Stump11289f42009-09-09 15:08:12 +00003124
John McCall8ccfcb52009-09-24 19:53:00 +00003125 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003126}
3127
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003128TypeID ASTReader::GetTypeID(QualType T) const {
3129 return MakeTypeID(T,
3130 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3131}
3132
3133TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3134 if (T.isNull())
3135 return TypeIdx();
3136 assert(!T.getLocalFastQualifiers());
3137
3138 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3139 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3140 // comparing keys of ASTDeclContextNameLookupTable.
3141 // If the type didn't come from the AST file use a specially marked index
3142 // so that any hash/key comparison fail since no such index is stored
3143 // in a AST file.
3144 if (I == TypeIdxs.end())
3145 return TypeIdx(-1);
3146 return I->second;
3147}
3148
John McCall0ad16662009-10-29 08:12:44 +00003149TemplateArgumentLocInfo
Sebastian Redl2c373b92010-10-05 15:59:54 +00003150ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3151 TemplateArgument::ArgKind Kind,
John McCall0ad16662009-10-29 08:12:44 +00003152 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003153 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00003154 switch (Kind) {
3155 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003156 return ReadExpr(F);
John McCall0ad16662009-10-29 08:12:44 +00003157 case TemplateArgument::Type:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003158 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003159 case TemplateArgument::Template: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003160 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3161 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003162 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003163 }
John McCall0ad16662009-10-29 08:12:44 +00003164 case TemplateArgument::Null:
3165 case TemplateArgument::Integral:
3166 case TemplateArgument::Declaration:
3167 case TemplateArgument::Pack:
3168 return TemplateArgumentLocInfo();
3169 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00003170 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00003171 return TemplateArgumentLocInfo();
3172}
3173
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003174TemplateArgumentLoc
Sebastian Redl2c373b92010-10-05 15:59:54 +00003175ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003176 const RecordData &Record, unsigned &Index) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003177 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003178
3179 if (Arg.getKind() == TemplateArgument::Expression) {
3180 if (Record[Index++]) // bool InfoHasSameExpr.
3181 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3182 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00003183 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003184 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003185}
3186
Sebastian Redl2c499f62010-08-18 23:56:43 +00003187Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00003188 return GetDecl(ID);
3189}
3190
Sebastian Redl2c499f62010-08-18 23:56:43 +00003191TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003192 if (!DeclsLoaded[0]) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00003193 ReadDeclRecord(0, 1);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003194 if (DeserializationListener)
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003195 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003196 }
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00003197
3198 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3199}
3200
Sebastian Redl539c5062010-08-18 23:57:32 +00003201Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003202 if (ID == 0)
3203 return 0;
3204
Douglas Gregor745ed142009-04-25 18:35:21 +00003205 if (ID > DeclsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003206 Error("declaration ID out-of-range for AST file");
Douglas Gregor745ed142009-04-25 18:35:21 +00003207 return 0;
3208 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003209
Douglas Gregor745ed142009-04-25 18:35:21 +00003210 unsigned Index = ID - 1;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003211 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003212 ReadDeclRecord(Index, ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003213 if (DeserializationListener)
3214 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3215 }
Douglas Gregor745ed142009-04-25 18:35:21 +00003216
3217 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003218}
3219
Chris Lattner9c28af02009-04-27 05:46:25 +00003220/// \brief Resolve the offset of a statement into a statement.
3221///
3222/// This operation will read a new statement from the external
3223/// source each time it is called, and is meant to be used via a
3224/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00003225Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Sebastian Redl5c415f32010-07-22 17:01:13 +00003226 // Offset here is a global offset across the entire chain.
3227 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3228 PerFileData &F = *Chain[N - I - 1];
3229 if (Offset < F.SizeInBits) {
3230 // Since we know that this statement is part of a decl, make sure to use
3231 // the decl cursor to read it.
3232 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003233 return ReadStmtFromStream(F);
Sebastian Redl5c415f32010-07-22 17:01:13 +00003234 }
3235 Offset -= F.SizeInBits;
3236 }
3237 llvm_unreachable("Broken chain");
Douglas Gregor3c3aa612009-04-18 00:07:54 +00003238}
3239
Sebastian Redl2c499f62010-08-18 23:56:43 +00003240bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00003241 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump11289f42009-09-09 15:08:12 +00003242 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003243 "DeclContext has no lexical decls in storage");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003244
Sebastian Redl5c415f32010-07-22 17:01:13 +00003245 // There might be lexical decls in multiple parts of the chain, for the TU
3246 // at least.
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003247 // DeclContextOffsets might reallocate as we load additional decls below,
3248 // so make a copy of the vector.
3249 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl5c415f32010-07-22 17:01:13 +00003250 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3251 I != E; ++I) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003252 // IDs can be 0 if this context doesn't contain declarations.
3253 if (!I->LexicalDecls)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003254 continue;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003255
3256 // Load all of the declaration IDs
Sebastian Redl4102dd52010-09-28 02:55:49 +00003257 for (const DeclID *ID = I->LexicalDecls, *IDE = ID + I->NumLexicalDecls;
3258 ID != IDE; ++ID) {
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003259 Decl *D = GetDecl(*ID);
3260 assert(D && "Null decl in lexical decls");
3261 Decls.push_back(D);
3262 }
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003263 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003264
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003265 ++NumLexicalDeclContextsRead;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003266 return false;
3267}
3268
John McCall75b960e2010-06-01 09:23:16 +00003269DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00003270ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00003271 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003272 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003273 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003274 if (!Name)
3275 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3276 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003277
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003278 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl471ac2f2010-08-24 00:49:55 +00003279 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003280 // and namespaces. For any given name, the last available results replace
3281 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl5c415f32010-07-22 17:01:13 +00003282 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003283 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl5c415f32010-07-22 17:01:13 +00003284 I != E; ++I) {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003285 if (!I->NameLookupTableData)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003286 continue;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003287
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003288 ASTDeclContextNameLookupTable *LookupTable =
3289 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3290 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3291 if (Pos == LookupTable->end())
Sebastian Redl5c415f32010-07-22 17:01:13 +00003292 continue;
3293
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003294 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3295 for (; Data.first != Data.second; ++Data.first)
3296 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003297 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003298 }
3299
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003300 ++NumVisibleDeclContextsRead;
John McCall75b960e2010-06-01 09:23:16 +00003301
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003302 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00003303 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003304}
3305
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00003306void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3307 assert(DC->hasExternalVisibleStorage() &&
3308 "DeclContext has no visible decls in storage");
3309
3310 llvm::SmallVector<NamedDecl *, 64> Decls;
3311 // There might be visible decls in multiple parts of the chain, for the TU
3312 // and namespaces.
3313 DeclContextInfos &Infos = DeclContextOffsets[DC];
3314 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3315 I != E; ++I) {
3316 if (!I->NameLookupTableData)
3317 continue;
3318
3319 ASTDeclContextNameLookupTable *LookupTable =
3320 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3321 for (ASTDeclContextNameLookupTable::item_iterator
3322 ItemI = LookupTable->item_begin(),
3323 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3324 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3325 = *ItemI;
3326 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3327 Decls.clear();
3328 for (; Data.first != Data.second; ++Data.first)
3329 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3330 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3331 }
3332 }
3333}
3334
Sebastian Redl2c499f62010-08-18 23:56:43 +00003335void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003336 assert(Consumer);
3337 while (!InterestingDecls.empty()) {
3338 DeclGroupRef DG(InterestingDecls.front());
3339 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00003340 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003341 }
3342}
3343
Sebastian Redl2c499f62010-08-18 23:56:43 +00003344void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00003345 this->Consumer = Consumer;
3346
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003347 if (!Consumer)
3348 return;
3349
3350 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003351 // Force deserialization of this decl, which will cause it to be queued for
3352 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00003353 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003354 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00003355
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003356 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003357}
3358
Sebastian Redl2c499f62010-08-18 23:56:43 +00003359void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003360 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003361
Mike Stump11289f42009-09-09 15:08:12 +00003362 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003363 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00003364 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00003365 unsigned NumDeclsLoaded
3366 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3367 (Decl *)0);
3368 unsigned NumIdentifiersLoaded
3369 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3370 IdentifiersLoaded.end(),
3371 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00003372 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003373 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3374 SelectorsLoaded.end(),
3375 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00003376
Douglas Gregorc5046832009-04-27 18:38:38 +00003377 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3378 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor258ae542009-04-27 06:38:32 +00003379 if (TotalNumSLocEntries)
3380 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3381 NumSLocEntriesRead, TotalNumSLocEntries,
3382 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00003383 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003384 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003385 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3386 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3387 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003388 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003389 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3390 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00003391 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003392 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00003393 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3394 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00003395 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003396 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00003397 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3398 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003399 if (TotalNumStatements)
3400 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3401 NumStatementsRead, TotalNumStatements,
3402 ((float)NumStatementsRead/TotalNumStatements * 100));
3403 if (TotalNumMacros)
3404 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3405 NumMacrosRead, TotalNumMacros,
3406 ((float)NumMacrosRead/TotalNumMacros * 100));
3407 if (TotalLexicalDeclContexts)
3408 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3409 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3410 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3411 * 100));
3412 if (TotalVisibleDeclContexts)
3413 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3414 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3415 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3416 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003417 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003418 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003419 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3420 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00003421 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003422 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003423 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003424 std::fprintf(stderr, "\n");
3425}
3426
Sebastian Redl2c499f62010-08-18 23:56:43 +00003427void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00003428 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003429 S.ExternalSource = this;
3430
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003431 // Makes sure any declarations that were deserialized "too early"
3432 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003433 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3434 if (SemaObj->TUScope)
John McCall48871652010-08-21 09:40:31 +00003435 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003436
3437 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003438 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003439 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00003440
3441 // If there were any tentative definitions, deserialize them and add
Sebastian Redl35351a92010-01-31 22:27:38 +00003442 // them to Sema's list of tentative definitions.
Douglas Gregord4df8652009-04-22 22:02:47 +00003443 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3444 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redl35351a92010-01-31 22:27:38 +00003445 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregord4df8652009-04-22 22:02:47 +00003446 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00003447
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003448 // If there were any unused file scoped decls, deserialize them and add to
3449 // Sema's list of unused file scoped decls.
3450 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3451 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3452 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattner90073802010-02-12 00:07:30 +00003453 }
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003454
3455 // If there were any locally-scoped external declarations,
3456 // deserialize them and add them to Sema's table of locally-scoped
3457 // external declarations.
3458 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3459 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3460 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3461 }
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003462
3463 // If there were any ext_vector type declarations, deserialize them
3464 // and add them to Sema's vector of such declarations.
3465 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3466 SemaObj->ExtVectorDecls.push_back(
3467 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003468
3469 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3470 // Can we cut them down before writing them ?
3471
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003472 // If there were any dynamic classes declarations, deserialize them
3473 // and add them to Sema's vector of such declarations.
3474 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3475 SemaObj->DynamicClasses.push_back(
3476 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003477
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003478 // Load the offsets of the declarations that Sema references.
3479 // They will be lazily deserialized when needed.
3480 if (!SemaDeclRefs.empty()) {
3481 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3482 SemaObj->StdNamespace = SemaDeclRefs[0];
3483 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3484 }
3485
Sebastian Redl2c373b92010-10-05 15:59:54 +00003486 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
3487
3488 // If there are @selector references added them to its pool. This is for
3489 // implementation of -Wselector.
3490 if (!F->ReferencedSelectorsData.empty()) {
3491 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
3492 unsigned I = 0;
3493 while (I < DataSize) {
3494 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
3495 SourceLocation SelLoc = ReadSourceLocation(
3496 *F, F->ReferencedSelectorsData, I);
3497 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3498 }
3499 }
3500
3501 // If there were any pending implicit instantiations, deserialize them
3502 // and add them to Sema's queue of such instantiations.
3503 assert(F->PendingInstantiations.size() % 2 == 0 &&
3504 "Expected pairs of entries");
3505 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
3506 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
3507 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
3508 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
3509 }
3510 }
3511
3512 // The two special data sets below always come from the most recent PCH,
3513 // which is at the front of the chain.
3514 PerFileData &F = *Chain.front();
3515
3516 // If there were any weak undeclared identifiers, deserialize them and add to
3517 // Sema's list of weak undeclared identifiers.
3518 if (!WeakUndeclaredIdentifiers.empty()) {
3519 unsigned Idx = 0;
3520 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3521 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3522 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3523 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
3524 bool Used = WeakUndeclaredIdentifiers[Idx++];
3525 Sema::WeakInfo WI(AliasId, Loc);
3526 WI.setUsed(Used);
3527 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3528 }
3529 }
3530
3531 // If there were any VTable uses, deserialize the information and add it
3532 // to Sema's vector and map of VTable uses.
3533 if (!VTableUses.empty()) {
3534 unsigned Idx = 0;
3535 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3536 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3537 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
3538 bool DefinitionRequired = VTableUses[Idx++];
3539 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3540 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003541 }
3542 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00003543}
3544
Sebastian Redl2c499f62010-08-18 23:56:43 +00003545IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redl78f51772010-08-02 18:30:12 +00003546 // Try to find this name within our on-disk hash tables. We start with the
3547 // most recent one, since that one contains the most up-to-date info.
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003548 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003549 ASTIdentifierLookupTable *IdTable
3550 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003551 if (!IdTable)
3552 continue;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003553 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003554 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003555 if (Pos == IdTable->end())
3556 continue;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003557
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003558 // Dereferencing the iterator has the effect of building the
3559 // IdentifierInfo node and populating it with the various
3560 // declarations it needs.
Sebastian Redl78f51772010-08-02 18:30:12 +00003561 return *Pos;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003562 }
Sebastian Redl78f51772010-08-02 18:30:12 +00003563 return 0;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003564}
3565
Mike Stump11289f42009-09-09 15:08:12 +00003566std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redl2c499f62010-08-18 23:56:43 +00003567ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redlada023c2010-08-04 20:40:17 +00003568 // Find this selector in a hash table. We want to find the most recent entry.
3569 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3570 PerFileData &F = *Chain[I];
3571 if (!F.SelectorLookupTable)
3572 continue;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003573
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003574 ASTSelectorLookupTable *PoolTable
3575 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3576 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redlada023c2010-08-04 20:40:17 +00003577 if (Pos != PoolTable->end()) {
3578 ++NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003579 // FIXME: Not quite happy with the statistics here. We probably should
3580 // disable this tracking when called via LoadSelector.
3581 // Also, should entries without methods count as misses?
3582 ++NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003583 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redlada023c2010-08-04 20:40:17 +00003584 if (DeserializationListener)
3585 DeserializationListener->SelectorRead(Data.ID, Sel);
3586 return std::make_pair(Data.Instance, Data.Factory);
3587 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003588 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003589
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003590 ++NumMethodPoolMisses;
Sebastian Redlada023c2010-08-04 20:40:17 +00003591 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorc78d3462009-04-24 21:10:55 +00003592}
3593
Sebastian Redl2c499f62010-08-18 23:56:43 +00003594void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003595 // It would be complicated to avoid reading the methods anyway. So don't.
3596 ReadMethodPool(Sel);
3597}
3598
Sebastian Redl2c499f62010-08-18 23:56:43 +00003599void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00003600 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00003601 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00003602 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00003603 if (DeserializationListener)
3604 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003605}
3606
Douglas Gregor1342e842009-07-06 18:54:52 +00003607/// \brief Set the globally-visible declarations associated with the given
3608/// identifier.
3609///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003610/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00003611/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00003612/// them.
3613///
3614/// \param II an IdentifierInfo that refers to one or more globally-visible
3615/// declarations.
3616///
3617/// \param DeclIDs the set of declaration IDs with the name @p II that are
3618/// visible at global scope.
3619///
3620/// \param Nonrecursive should be true to indicate that the caller knows that
3621/// this call is non-recursive, and therefore the globally-visible declarations
3622/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00003623void
Sebastian Redl2c499f62010-08-18 23:56:43 +00003624ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregor1342e842009-07-06 18:54:52 +00003625 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
3626 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003627 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00003628 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
3629 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
3630 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003631 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00003632 return;
3633 }
Mike Stump11289f42009-09-09 15:08:12 +00003634
Douglas Gregor1342e842009-07-06 18:54:52 +00003635 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
3636 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
3637 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00003638 if (SemaObj->TUScope) {
3639 // Introduce this declaration into the translation-unit scope
3640 // and add it to the declaration chain for this identifier, so
3641 // that (unqualified) name lookup will find it.
John McCall48871652010-08-21 09:40:31 +00003642 SemaObj->TUScope->AddDecl(D);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00003643 }
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003644 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregor1342e842009-07-06 18:54:52 +00003645 } else {
3646 // Queue this declaration so that it will be added to the
3647 // translation unit scope and identifier's declaration chain
3648 // once a Sema object is known.
3649 PreloadedDecls.push_back(D);
3650 }
3651 }
3652}
3653
Sebastian Redl2c499f62010-08-18 23:56:43 +00003654IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003655 if (ID == 0)
3656 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00003657
Sebastian Redlc713b962010-07-21 00:46:22 +00003658 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003659 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003660 return 0;
3661 }
Mike Stump11289f42009-09-09 15:08:12 +00003662
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003663 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc713b962010-07-21 00:46:22 +00003664 ID -= 1;
3665 if (!IdentifiersLoaded[ID]) {
3666 unsigned Index = ID;
3667 const char *Str = 0;
3668 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3669 PerFileData *F = Chain[N - I - 1];
3670 if (Index < F->LocalNumIdentifiers) {
3671 uint32_t Offset = F->IdentifierOffsets[Index];
3672 Str = F->IdentifierTableData + Offset;
3673 break;
3674 }
3675 Index -= F->LocalNumIdentifiers;
3676 }
3677 assert(Str && "Broken Chain");
Douglas Gregor5287b4e2009-04-25 21:04:17 +00003678
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003679 // All of the strings in the AST file are preceded by a 16-bit length.
3680 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00003681 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
3682 // unsigned integers. This is important to avoid integer overflow when
3683 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00003684 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00003685 unsigned StrLen = (((unsigned) StrLenPtr[0])
3686 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00003687 IdentifiersLoaded[ID]
Kovarththanan Rajaratnama3b09592010-03-12 10:32:27 +00003688 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003689 if (DeserializationListener)
3690 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003691 }
Mike Stump11289f42009-09-09 15:08:12 +00003692
Sebastian Redlc713b962010-07-21 00:46:22 +00003693 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003694}
3695
Sebastian Redl2c499f62010-08-18 23:56:43 +00003696void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00003697 ReadSLocEntryRecord(ID);
3698}
3699
Sebastian Redl2c499f62010-08-18 23:56:43 +00003700Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00003701 if (ID == 0)
3702 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00003703
Sebastian Redlada023c2010-08-04 20:40:17 +00003704 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003705 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00003706 return Selector();
3707 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003708
Sebastian Redlada023c2010-08-04 20:40:17 +00003709 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003710 // Load this selector from the selector table.
Sebastian Redlada023c2010-08-04 20:40:17 +00003711 unsigned Idx = ID - 1;
3712 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3713 PerFileData &F = *Chain[N - I - 1];
3714 if (Idx < F.LocalNumSelectors) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003715 ASTSelectorLookupTrait Trait(*this);
Sebastian Redlada023c2010-08-04 20:40:17 +00003716 SelectorsLoaded[ID - 1] =
3717 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
3718 if (DeserializationListener)
3719 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
3720 break;
3721 }
3722 Idx -= F.LocalNumSelectors;
3723 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003724 }
3725
Sebastian Redlada023c2010-08-04 20:40:17 +00003726 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00003727}
3728
Sebastian Redl2c499f62010-08-18 23:56:43 +00003729Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00003730 return DecodeSelector(ID);
3731}
3732
Sebastian Redl2c499f62010-08-18 23:56:43 +00003733uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00003734 // ID 0 (the null selector) is considered an external selector.
3735 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00003736}
3737
Mike Stump11289f42009-09-09 15:08:12 +00003738DeclarationName
Sebastian Redl2c499f62010-08-18 23:56:43 +00003739ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003740 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
3741 switch (Kind) {
3742 case DeclarationName::Identifier:
3743 return DeclarationName(GetIdentifierInfo(Record, Idx));
3744
3745 case DeclarationName::ObjCZeroArgSelector:
3746 case DeclarationName::ObjCOneArgSelector:
3747 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff3c301dc2009-04-23 15:15:40 +00003748 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003749
3750 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00003751 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00003752 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003753
3754 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00003755 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00003756 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003757
3758 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00003759 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor2211d342009-08-05 05:36:45 +00003760 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003761
3762 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00003763 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003764 (OverloadedOperatorKind)Record[Idx++]);
3765
Alexis Hunt3d221f22009-11-29 07:34:05 +00003766 case DeclarationName::CXXLiteralOperatorName:
3767 return Context->DeclarationNames.getCXXLiteralOperatorName(
3768 GetIdentifierInfo(Record, Idx));
3769
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003770 case DeclarationName::CXXUsingDirective:
3771 return DeclarationName::getUsingDirectiveName();
3772 }
3773
3774 // Required to silence GCC warning
3775 return DeclarationName();
3776}
Douglas Gregor55abb232009-04-10 20:39:37 +00003777
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003778TemplateName
Sebastian Redl2c499f62010-08-18 23:56:43 +00003779ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003780 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
3781 switch (Kind) {
3782 case TemplateName::Template:
3783 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
3784
3785 case TemplateName::OverloadedTemplate: {
3786 unsigned size = Record[Idx++];
3787 UnresolvedSet<8> Decls;
3788 while (size--)
3789 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
3790
3791 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
3792 }
3793
3794 case TemplateName::QualifiedTemplate: {
3795 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3796 bool hasTemplKeyword = Record[Idx++];
3797 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
3798 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
3799 }
3800
3801 case TemplateName::DependentTemplate: {
3802 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3803 if (Record[Idx++]) // isIdentifier
3804 return Context->getDependentTemplateName(NNS,
3805 GetIdentifierInfo(Record, Idx));
3806 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003807 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003808 }
3809 }
3810
3811 assert(0 && "Unhandled template name kind!");
3812 return TemplateName();
3813}
3814
3815TemplateArgument
Sebastian Redl2c373b92010-10-05 15:59:54 +00003816ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003817 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003818 switch ((TemplateArgument::ArgKind)Record[Idx++]) {
3819 case TemplateArgument::Null:
3820 return TemplateArgument();
3821 case TemplateArgument::Type:
3822 return TemplateArgument(GetType(Record[Idx++]));
3823 case TemplateArgument::Declaration:
3824 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00003825 case TemplateArgument::Integral: {
3826 llvm::APSInt Value = ReadAPSInt(Record, Idx);
3827 QualType T = GetType(Record[Idx++]);
3828 return TemplateArgument(Value, T);
3829 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003830 case TemplateArgument::Template:
3831 return TemplateArgument(ReadTemplateName(Record, Idx));
3832 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003833 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003834 case TemplateArgument::Pack: {
3835 unsigned NumArgs = Record[Idx++];
3836 llvm::SmallVector<TemplateArgument, 8> Args;
3837 Args.reserve(NumArgs);
3838 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003839 Args.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003840 TemplateArgument TemplArg;
3841 TemplArg.setArgumentPack(Args.data(), Args.size(), /*CopyArgs=*/true);
3842 return TemplArg;
3843 }
3844 }
3845
3846 assert(0 && "Unhandled template argument kind!");
3847 return TemplateArgument();
3848}
3849
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003850TemplateParameterList *
Sebastian Redl2c373b92010-10-05 15:59:54 +00003851ASTReader::ReadTemplateParameterList(PerFileData &F,
3852 const RecordData &Record, unsigned &Idx) {
3853 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
3854 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
3855 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003856
3857 unsigned NumParams = Record[Idx++];
3858 llvm::SmallVector<NamedDecl *, 16> Params;
3859 Params.reserve(NumParams);
3860 while (NumParams--)
3861 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
3862
3863 TemplateParameterList* TemplateParams =
3864 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
3865 Params.data(), Params.size(), RAngleLoc);
3866 return TemplateParams;
3867}
3868
3869void
Sebastian Redl2c499f62010-08-18 23:56:43 +00003870ASTReader::
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003871ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003872 PerFileData &F, const RecordData &Record,
3873 unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003874 unsigned NumTemplateArgs = Record[Idx++];
3875 TemplArgs.reserve(NumTemplateArgs);
3876 while (NumTemplateArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003877 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003878}
3879
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003880/// \brief Read a UnresolvedSet structure.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003881void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003882 const RecordData &Record, unsigned &Idx) {
3883 unsigned NumDecls = Record[Idx++];
3884 while (NumDecls--) {
3885 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
3886 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
3887 Set.addDecl(D, AS);
3888 }
3889}
3890
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003891CXXBaseSpecifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00003892ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky19b9f952010-07-26 16:56:01 +00003893 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003894 bool isVirtual = static_cast<bool>(Record[Idx++]);
3895 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
3896 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003897 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
3898 SourceRange Range = ReadSourceRange(F, Record, Idx);
Nick Lewycky19b9f952010-07-26 16:56:01 +00003899 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003900}
3901
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003902std::pair<CXXBaseOrMemberInitializer **, unsigned>
Sebastian Redl2c373b92010-10-05 15:59:54 +00003903ASTReader::ReadCXXBaseOrMemberInitializers(PerFileData &F,
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003904 const RecordData &Record,
3905 unsigned &Idx) {
3906 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0;
3907 unsigned NumInitializers = Record[Idx++];
3908 if (NumInitializers) {
3909 ASTContext &C = *getContext();
3910
3911 BaseOrMemberInitializers
3912 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
3913 for (unsigned i=0; i != NumInitializers; ++i) {
3914 TypeSourceInfo *BaseClassInfo = 0;
3915 bool IsBaseVirtual = false;
3916 FieldDecl *Member = 0;
3917
3918 bool IsBaseInitializer = Record[Idx++];
3919 if (IsBaseInitializer) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003920 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003921 IsBaseVirtual = Record[Idx++];
3922 } else {
3923 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
3924 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00003925 SourceLocation MemberLoc = ReadSourceLocation(F, Record, Idx);
3926 Expr *Init = ReadExpr(F);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003927 FieldDecl *AnonUnionMember
3928 = cast_or_null<FieldDecl>(GetDecl(Record[Idx++]));
Sebastian Redl2c373b92010-10-05 15:59:54 +00003929 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
3930 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003931 bool IsWritten = Record[Idx++];
3932 unsigned SourceOrderOrNumArrayIndices;
3933 llvm::SmallVector<VarDecl *, 8> Indices;
3934 if (IsWritten) {
3935 SourceOrderOrNumArrayIndices = Record[Idx++];
3936 } else {
3937 SourceOrderOrNumArrayIndices = Record[Idx++];
3938 Indices.reserve(SourceOrderOrNumArrayIndices);
3939 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
3940 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
3941 }
3942
3943 CXXBaseOrMemberInitializer *BOMInit;
3944 if (IsBaseInitializer) {
3945 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
3946 IsBaseVirtual, LParenLoc,
3947 Init, RParenLoc);
3948 } else if (IsWritten) {
3949 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc,
3950 LParenLoc, Init, RParenLoc);
3951 } else {
3952 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc,
3953 LParenLoc, Init, RParenLoc,
3954 Indices.data(),
3955 Indices.size());
3956 }
3957
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00003958 if (IsWritten)
3959 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003960 BOMInit->setAnonUnionMember(AnonUnionMember);
3961 BaseOrMemberInitializers[i] = BOMInit;
3962 }
3963 }
3964
3965 return std::make_pair(BaseOrMemberInitializers, NumInitializers);
3966}
3967
Chris Lattnerca025db2010-05-07 21:43:38 +00003968NestedNameSpecifier *
Sebastian Redl2c499f62010-08-18 23:56:43 +00003969ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003970 unsigned N = Record[Idx++];
3971 NestedNameSpecifier *NNS = 0, *Prev = 0;
3972 for (unsigned I = 0; I != N; ++I) {
3973 NestedNameSpecifier::SpecifierKind Kind
3974 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
3975 switch (Kind) {
3976 case NestedNameSpecifier::Identifier: {
3977 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
3978 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
3979 break;
3980 }
3981
3982 case NestedNameSpecifier::Namespace: {
3983 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
3984 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
3985 break;
3986 }
3987
3988 case NestedNameSpecifier::TypeSpec:
3989 case NestedNameSpecifier::TypeSpecWithTemplate: {
3990 Type *T = GetType(Record[Idx++]).getTypePtr();
3991 bool Template = Record[Idx++];
3992 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
3993 break;
3994 }
3995
3996 case NestedNameSpecifier::Global: {
3997 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
3998 // No associated value, and there can't be a prefix.
3999 break;
4000 }
Chris Lattnerca025db2010-05-07 21:43:38 +00004001 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00004002 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00004003 }
4004 return NNS;
4005}
4006
4007SourceRange
Sebastian Redl2c373b92010-10-05 15:59:54 +00004008ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4009 unsigned &Idx) {
4010 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4011 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00004012 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00004013}
4014
Douglas Gregor1daeb692009-04-13 18:14:40 +00004015/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004016llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004017 unsigned BitWidth = Record[Idx++];
4018 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4019 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4020 Idx += NumWords;
4021 return Result;
4022}
4023
4024/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004025llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004026 bool isUnsigned = Record[Idx++];
4027 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4028}
4029
Douglas Gregore0a3a512009-04-14 21:55:33 +00004030/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004031llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004032 return llvm::APFloat(ReadAPInt(Record, Idx));
4033}
4034
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004035// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00004036std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004037 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00004038 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004039 Idx += Len;
4040 return Result;
4041}
4042
Sebastian Redl2c499f62010-08-18 23:56:43 +00004043CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00004044 unsigned &Idx) {
4045 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4046 return CXXTemporary::Create(*Context, Decl);
4047}
4048
Sebastian Redl2c499f62010-08-18 23:56:43 +00004049DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00004050 return Diag(SourceLocation(), DiagID);
4051}
4052
Sebastian Redl2c499f62010-08-18 23:56:43 +00004053DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004054 return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00004055}
Douglas Gregora9af1d12009-04-17 00:04:06 +00004056
Douglas Gregora868bbd2009-04-21 22:25:48 +00004057/// \brief Retrieve the identifier table associated with the
4058/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004059IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004060 assert(PP && "Forgot to set Preprocessor ?");
4061 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00004062}
4063
Douglas Gregora9af1d12009-04-17 00:04:06 +00004064/// \brief Record that the given ID maps to the given switch-case
4065/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004066void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004067 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4068 SwitchCaseStmts[ID] = SC;
4069}
4070
4071/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004072SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004073 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4074 return SwitchCaseStmts[ID];
4075}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004076
4077/// \brief Record that the given label statement has been
4078/// deserialized and has the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004079void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump11289f42009-09-09 15:08:12 +00004080 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004081 "Deserialized label twice");
4082 LabelStmts[ID] = S;
4083
4084 // If we've already seen any goto statements that point to this
4085 // label, resolve them now.
4086 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
4087 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
4088 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
4089 Goto->second->setLabel(S);
4090 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor779d8652009-04-17 18:58:21 +00004091
4092 // If we've already seen any address-label statements that point to
4093 // this label, resolve them now.
4094 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump11289f42009-09-09 15:08:12 +00004095 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor779d8652009-04-17 18:58:21 +00004096 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump11289f42009-09-09 15:08:12 +00004097 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor779d8652009-04-17 18:58:21 +00004098 AddrLabel != AddrLabels.second; ++AddrLabel)
4099 AddrLabel->second->setLabel(S);
4100 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004101}
4102
4103/// \brief Set the label of the given statement to the label
4104/// identified by ID.
4105///
4106/// Depending on the order in which the label and other statements
4107/// referencing that label occur, this operation may complete
4108/// immediately (updating the statement) or it may queue the
4109/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004110void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004111 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4112 if (Label != LabelStmts.end()) {
4113 // We've already seen this label, so set the label of the goto and
4114 // we're done.
4115 S->setLabel(Label->second);
4116 } else {
4117 // We haven't seen this label yet, so add this goto to the set of
4118 // unresolved goto statements.
4119 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
4120 }
4121}
Douglas Gregor779d8652009-04-17 18:58:21 +00004122
4123/// \brief Set the label of the given expression to the label
4124/// identified by ID.
4125///
4126/// Depending on the order in which the label and other statements
4127/// referencing that label occur, this operation may complete
4128/// immediately (updating the statement) or it may queue the
4129/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004130void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor779d8652009-04-17 18:58:21 +00004131 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4132 if (Label != LabelStmts.end()) {
4133 // We've already seen this label, so set the label of the
4134 // label-address expression and we're done.
4135 S->setLabel(Label->second);
4136 } else {
4137 // We haven't seen this label yet, so add this label-address
4138 // expression to the set of unresolved label-address expressions.
4139 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
4140 }
4141}
Douglas Gregor1342e842009-07-06 18:54:52 +00004142
Sebastian Redl2c499f62010-08-18 23:56:43 +00004143void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004144 assert(NumCurrentElementsDeserializing &&
4145 "FinishedDeserializing not paired with StartedDeserializing");
4146 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004147 // If any identifiers with corresponding top-level declarations have
4148 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004149 while (!PendingIdentifierInfos.empty()) {
4150 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4151 PendingIdentifierInfos.front().DeclIDs, true);
4152 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00004153 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004154
4155 // We are not in recursive loading, so it's safe to pass the "interesting"
4156 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004157 if (Consumer)
4158 PassInterestingDeclsToConsumer();
Douglas Gregor1342e842009-07-06 18:54:52 +00004159 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004160 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00004161}
Douglas Gregorb473b072010-08-19 00:28:17 +00004162
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004163ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
4164 const char *isysroot, bool DisableValidation)
4165 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4166 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4167 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4168 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
4169 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00004170 TotalNumSLocEntries(0), NextSLocOffset(0), NumStatementsRead(0),
4171 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
4172 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004173 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4174 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4175 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
4176 RelocatablePCH = false;
4177}
4178
4179ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4180 Diagnostic &Diags, const char *isysroot,
4181 bool DisableValidation)
4182 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4183 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
4184 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
4185 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00004186 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4187 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4188 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4189 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4190 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4191 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004192 RelocatablePCH = false;
4193}
4194
4195ASTReader::~ASTReader() {
4196 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4197 delete Chain[e - i - 1];
4198 // Delete all visible decl lookup tables
4199 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4200 E = DeclContextOffsets.end();
4201 I != E; ++I) {
4202 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4203 J != F; ++J) {
4204 if (J->NameLookupTableData)
4205 delete static_cast<ASTDeclContextNameLookupTable*>(
4206 J->NameLookupTableData);
4207 }
4208 }
4209 for (DeclContextVisibleUpdatesPending::iterator
4210 I = PendingVisibleUpdates.begin(),
4211 E = PendingVisibleUpdates.end();
4212 I != E; ++I) {
4213 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4214 F = I->second.end();
4215 J != F; ++J)
4216 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4217 }
4218}
4219
Sebastian Redl009e7f22010-10-05 16:15:19 +00004220ASTReader::PerFileData::PerFileData(ASTFileType Ty)
4221 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00004222 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
4223 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
4224 MacroDefinitionOffsets(0), LocalNumSelectors(0), SelectorOffsets(0),
4225 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
4226 DeclOffsets(0), LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redl3f6b7532010-10-01 19:59:12 +00004227 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregorb473b072010-08-19 00:28:17 +00004228{}
4229
4230ASTReader::PerFileData::~PerFileData() {
4231 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
4232 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4233}
4234