blob: 0443edb26afc03306575ff851e22978d2ce2561c [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);
266 Right.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000267
Daniel Dunbar499baed2009-11-11 05:26:28 +0000268 // Sort both sets of predefined buffer lines, since we allow some extra
269 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000270 std::sort(CmdLineLines.begin(), CmdLineLines.end());
271 std::sort(PCHLines.begin(), PCHLines.end());
272
Daniel Dunbar499baed2009-11-11 05:26:28 +0000273 // Determine which predefines that were used to build the PCH file are missing
274 // from the command line.
275 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000276 std::set_difference(PCHLines.begin(), PCHLines.end(),
277 CmdLineLines.begin(), CmdLineLines.end(),
278 std::back_inserter(MissingPredefines));
279
280 bool MissingDefines = false;
281 bool ConflictingDefines = false;
282 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000283 llvm::StringRef Missing = MissingPredefines[I];
284 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000285 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
286 return true;
287 }
Mike Stump11289f42009-09-09 15:08:12 +0000288
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000289 // This is a macro definition. Determine the name of the macro we're
290 // defining.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000291 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000292 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000293 = Missing.find_first_of("( \n\r", StartOfMacroName);
294 assert(EndOfMacroName != std::string::npos &&
295 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000296 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000297
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000298 // Determine whether this macro was given a different definition on the
299 // command line.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000300 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000301 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000302 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000303 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
304 MacroDefStart);
305 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000306 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000307 // Different macro; we're done.
308 ConflictPos = CmdLineLines.end();
Mike Stump11289f42009-09-09 15:08:12 +0000309 break;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000310 }
Mike Stump11289f42009-09-09 15:08:12 +0000311
312 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000313 "Invalid #define in predefines buffer?");
Mike Stump11289f42009-09-09 15:08:12 +0000314 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000315 (*ConflictPos)[MacroDefLen] != '(')
316 continue; // Longer macro name; keep trying.
Mike Stump11289f42009-09-09 15:08:12 +0000317
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000318 // We found a conflicting macro definition.
319 break;
320 }
Mike Stump11289f42009-09-09 15:08:12 +0000321
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000322 if (ConflictPos != CmdLineLines.end()) {
323 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
324 << MacroName;
325
326 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000327 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
328 FindMacro(Buffers, Missing);
329 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
330 SourceLocation PCHMissingLoc =
331 SourceMgr.getLocForStartOfFile(MacroLoc.first)
332 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar499baed2009-11-11 05:26:28 +0000333 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000334
335 ConflictingDefines = true;
336 continue;
337 }
Mike Stump11289f42009-09-09 15:08:12 +0000338
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000339 // If the macro doesn't conflict, then we'll just pick up the macro
340 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000341 if (ConflictingDefines)
342 continue; // Don't complain if there are already conflicting defs
Mike Stump11289f42009-09-09 15:08:12 +0000343
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000344 if (!MissingDefines) {
345 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
346 MissingDefines = true;
347 }
348
349 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000350 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
351 FindMacro(Buffers, Missing);
352 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
353 SourceLocation PCHMissingLoc =
354 SourceMgr.getLocForStartOfFile(MacroLoc.first)
355 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000356 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
357 }
Mike Stump11289f42009-09-09 15:08:12 +0000358
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000359 if (ConflictingDefines)
360 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000361
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000362 // Determine what predefines were introduced based on command-line
363 // parameters that were not present when building the PCH
364 // file. Extra #defines are okay, so long as the identifiers being
365 // defined were not used within the precompiled header.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000366 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000367 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
368 PCHLines.begin(), PCHLines.end(),
Mike Stump11289f42009-09-09 15:08:12 +0000369 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000370 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000371 llvm::StringRef &Extra = ExtraPredefines[I];
372 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000373 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
374 return true;
375 }
376
377 // This is an extra macro definition. Determine the name of the
378 // macro we're defining.
379 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000380 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000381 = Extra.find_first_of("( \n\r", StartOfMacroName);
382 assert(EndOfMacroName != std::string::npos &&
383 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000384 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000385
386 // Check whether this name was used somewhere in the PCH file. If
387 // so, defining it as a macro could change behavior, so we reject
388 // the PCH file.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000389 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar045c92f2009-11-11 00:52:00 +0000390 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000391 return true;
392 }
393
394 // Add this definition to the suggested predefines buffer.
395 SuggestedPredefines += Extra;
396 SuggestedPredefines += '\n';
397 }
398
399 // If we get here, it's because the predefines buffer had compatible
400 // contents. Accept the PCH file.
401 return false;
402}
403
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000404void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
405 unsigned ID) {
406 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
407 ++NumHeaderInfos;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000408}
409
410void PCHValidator::ReadCounter(unsigned Value) {
411 PP.setCounterValue(Value);
412}
413
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000414//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +0000415// AST reader implementation
Douglas Gregora868bbd2009-04-21 22:25:48 +0000416//===----------------------------------------------------------------------===//
417
Sebastian Redl07a89a82010-07-30 00:29:29 +0000418void
Sebastian Redl3e31c722010-08-18 23:56:56 +0000419ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redl07a89a82010-07-30 00:29:29 +0000420 DeserializationListener = Listener;
421 if (DeserializationListener)
422 DeserializationListener->SetReader(this);
423}
424
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000425
Douglas Gregora868bbd2009-04-21 22:25:48 +0000426namespace {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000427class ASTSelectorLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000428 ASTReader &Reader;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000429
430public:
Sebastian Redl834bb972010-08-04 17:20:04 +0000431 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +0000432 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +0000433 ObjCMethodList Instance, Factory;
434 };
Douglas Gregorc78d3462009-04-24 21:10:55 +0000435
436 typedef Selector external_key_type;
437 typedef external_key_type internal_key_type;
438
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000439 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump11289f42009-09-09 15:08:12 +0000440
Douglas Gregorc78d3462009-04-24 21:10:55 +0000441 static bool EqualKey(const internal_key_type& a,
442 const internal_key_type& b) {
443 return a == b;
444 }
Mike Stump11289f42009-09-09 15:08:12 +0000445
Douglas Gregorc78d3462009-04-24 21:10:55 +0000446 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +0000447 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000448 }
Mike Stump11289f42009-09-09 15:08:12 +0000449
Douglas Gregorc78d3462009-04-24 21:10:55 +0000450 // This hopefully will just get inlined and removed by the optimizer.
451 static const internal_key_type&
452 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000453
Douglas Gregorc78d3462009-04-24 21:10:55 +0000454 static std::pair<unsigned, unsigned>
455 ReadKeyDataLength(const unsigned char*& d) {
456 using namespace clang::io;
457 unsigned KeyLen = ReadUnalignedLE16(d);
458 unsigned DataLen = ReadUnalignedLE16(d);
459 return std::make_pair(KeyLen, DataLen);
460 }
Mike Stump11289f42009-09-09 15:08:12 +0000461
Douglas Gregor95c13f52009-04-25 17:48:32 +0000462 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000463 using namespace clang::io;
Chris Lattner8575daa2009-04-27 21:45:14 +0000464 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000465 unsigned N = ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000466 IdentifierInfo *FirstII
Douglas Gregorc78d3462009-04-24 21:10:55 +0000467 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
468 if (N == 0)
469 return SelTable.getNullarySelector(FirstII);
470 else if (N == 1)
471 return SelTable.getUnarySelector(FirstII);
472
473 llvm::SmallVector<IdentifierInfo *, 16> Args;
474 Args.push_back(FirstII);
475 for (unsigned I = 1; I != N; ++I)
476 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
477
Douglas Gregor038c3382009-05-22 22:45:36 +0000478 return SelTable.getSelector(N, Args.data());
Douglas Gregorc78d3462009-04-24 21:10:55 +0000479 }
Mike Stump11289f42009-09-09 15:08:12 +0000480
Douglas Gregorc78d3462009-04-24 21:10:55 +0000481 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
482 using namespace clang::io;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000483
484 data_type Result;
485
Sebastian Redl834bb972010-08-04 17:20:04 +0000486 Result.ID = ReadUnalignedLE32(d);
487 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
488 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
489
Douglas Gregorc78d3462009-04-24 21:10:55 +0000490 // Load instance methods
491 ObjCMethodList *Prev = 0;
492 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000493 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000494 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000495 if (!Result.Instance.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000496 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000497 Result.Instance.Method = Method;
498 Prev = &Result.Instance;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000499 continue;
500 }
501
Ted Kremenekda4abf12010-02-11 00:53:01 +0000502 ObjCMethodList *Mem =
503 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
504 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000505 Prev = Prev->Next;
506 }
507
508 // Load factory methods
509 Prev = 0;
510 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000511 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000512 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000513 if (!Result.Factory.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000514 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000515 Result.Factory.Method = Method;
516 Prev = &Result.Factory;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000517 continue;
518 }
519
Ted Kremenekda4abf12010-02-11 00:53:01 +0000520 ObjCMethodList *Mem =
521 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
522 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000523 Prev = Prev->Next;
524 }
525
526 return Result;
527 }
528};
Mike Stump11289f42009-09-09 15:08:12 +0000529
530} // end anonymous namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +0000531
532/// \brief The on-disk hash table used for the global method pool.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000533typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
534 ASTSelectorLookupTable;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000535
536namespace {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000537class ASTIdentifierLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000538 ASTReader &Reader;
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000539 llvm::BitstreamCursor &Stream;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000540
541 // If we know the IdentifierInfo in advance, it is here and we will
542 // not build a new one. Used when deserializing information about an
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000543 // identifier that was constructed before the AST file was read.
Douglas Gregora868bbd2009-04-21 22:25:48 +0000544 IdentifierInfo *KnownII;
545
546public:
547 typedef IdentifierInfo * data_type;
548
549 typedef const std::pair<const char*, unsigned> external_key_type;
550
551 typedef external_key_type internal_key_type;
552
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000553 ASTIdentifierLookupTrait(ASTReader &Reader, llvm::BitstreamCursor &Stream,
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000554 IdentifierInfo *II = 0)
555 : Reader(Reader), Stream(Stream), KnownII(II) { }
Mike Stump11289f42009-09-09 15:08:12 +0000556
Douglas Gregora868bbd2009-04-21 22:25:48 +0000557 static bool EqualKey(const internal_key_type& a,
558 const internal_key_type& b) {
559 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
560 : false;
561 }
Mike Stump11289f42009-09-09 15:08:12 +0000562
Douglas Gregora868bbd2009-04-21 22:25:48 +0000563 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000564 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregora868bbd2009-04-21 22:25:48 +0000565 }
Mike Stump11289f42009-09-09 15:08:12 +0000566
Douglas Gregora868bbd2009-04-21 22:25:48 +0000567 // This hopefully will just get inlined and removed by the optimizer.
568 static const internal_key_type&
569 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000570
Douglas Gregora868bbd2009-04-21 22:25:48 +0000571 static std::pair<unsigned, unsigned>
572 ReadKeyDataLength(const unsigned char*& d) {
573 using namespace clang::io;
Douglas Gregor6b7bf5a2009-04-25 20:26:24 +0000574 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor5287b4e2009-04-25 21:04:17 +0000575 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000576 return std::make_pair(KeyLen, DataLen);
577 }
Mike Stump11289f42009-09-09 15:08:12 +0000578
Douglas Gregora868bbd2009-04-21 22:25:48 +0000579 static std::pair<const char*, unsigned>
580 ReadKey(const unsigned char* d, unsigned n) {
581 assert(n >= 2 && d[n-1] == '\0');
582 return std::make_pair((const char*) d, n-1);
583 }
Mike Stump11289f42009-09-09 15:08:12 +0000584
585 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregora868bbd2009-04-21 22:25:48 +0000586 const unsigned char* d,
587 unsigned DataLen) {
588 using namespace clang::io;
Sebastian Redl539c5062010-08-18 23:57:32 +0000589 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000590 bool IsInteresting = ID & 0x01;
591
592 // Wipe out the "is interesting" bit.
593 ID = ID >> 1;
594
595 if (!IsInteresting) {
Sebastian Redl98912122010-07-27 23:01:28 +0000596 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregor1d583f22009-04-28 21:18:29 +0000597 // and associate it with the persistent ID.
598 IdentifierInfo *II = KnownII;
599 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000600 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000601 Reader.SetIdentifierInfo(ID, II);
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000602 II->setIsFromAST();
Douglas Gregor1d583f22009-04-28 21:18:29 +0000603 return II;
604 }
605
Douglas Gregorb9256522009-04-28 21:32:13 +0000606 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000607 bool CPlusPlusOperatorKeyword = Bits & 0x01;
608 Bits >>= 1;
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000609 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
610 Bits >>= 1;
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000611 bool Poisoned = Bits & 0x01;
612 Bits >>= 1;
613 bool ExtensionToken = Bits & 0x01;
614 Bits >>= 1;
615 bool hasMacroDefinition = Bits & 0x01;
616 Bits >>= 1;
617 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
618 Bits >>= 10;
Mike Stump11289f42009-09-09 15:08:12 +0000619
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000620 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorb9256522009-04-28 21:32:13 +0000621 DataLen -= 6;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000622
623 // Build the IdentifierInfo itself and link the identifier ID with
624 // the new IdentifierInfo.
625 IdentifierInfo *II = KnownII;
626 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000627 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000628 Reader.SetIdentifierInfo(ID, II);
629
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000630 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000631 // Token IDs are read-only.
632 if (HasRevertedTokenIDToIdentifier)
633 II->RevertTokenIDToIdentifier();
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000634 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump11289f42009-09-09 15:08:12 +0000635 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000636 "Incorrect extension token flag");
637 (void)ExtensionToken;
638 II->setIsPoisoned(Poisoned);
639 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
640 "Incorrect C++ operator keyword flag");
641 (void)CPlusPlusOperatorKeyword;
642
Douglas Gregorc3366a52009-04-21 23:56:24 +0000643 // If this identifier is a macro, deserialize the macro
644 // definition.
645 if (hasMacroDefinition) {
Douglas Gregorb9256522009-04-28 21:32:13 +0000646 uint32_t Offset = ReadUnalignedLE32(d);
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000647 Reader.ReadMacroRecord(Stream, Offset);
Douglas Gregorb9256522009-04-28 21:32:13 +0000648 DataLen -= 4;
Douglas Gregorc3366a52009-04-21 23:56:24 +0000649 }
Douglas Gregora868bbd2009-04-21 22:25:48 +0000650
651 // Read all of the declarations visible at global scope with this
652 // name.
Chris Lattner1d728882009-04-27 22:17:41 +0000653 if (Reader.getContext() == 0) return II;
Douglas Gregor1342e842009-07-06 18:54:52 +0000654 if (DataLen > 0) {
655 llvm::SmallVector<uint32_t, 4> DeclIDs;
656 for (; DataLen > 0; DataLen -= 4)
657 DeclIDs.push_back(ReadUnalignedLE32(d));
658 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000659 }
Mike Stump11289f42009-09-09 15:08:12 +0000660
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000661 II->setIsFromAST();
Douglas Gregora868bbd2009-04-21 22:25:48 +0000662 return II;
663 }
664};
Mike Stump11289f42009-09-09 15:08:12 +0000665
666} // end anonymous namespace
Douglas Gregora868bbd2009-04-21 22:25:48 +0000667
668/// \brief The on-disk hash table used to contain information about
669/// all of the identifiers in the program.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000670typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
671 ASTIdentifierLookupTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000672
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000673namespace {
674class ASTDeclContextNameLookupTrait {
675 ASTReader &Reader;
676
677public:
678 /// \brief Pair of begin/end iterators for DeclIDs.
679 typedef std::pair<DeclID *, DeclID *> data_type;
680
681 /// \brief Special internal key for declaration names.
682 /// The hash table creates keys for comparison; we do not create
683 /// a DeclarationName for the internal key to avoid deserializing types.
684 struct DeclNameKey {
685 DeclarationName::NameKind Kind;
686 uint64_t Data;
687 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
688 };
689
690 typedef DeclarationName external_key_type;
691 typedef DeclNameKey internal_key_type;
692
693 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
694
695 static bool EqualKey(const internal_key_type& a,
696 const internal_key_type& b) {
697 return a.Kind == b.Kind && a.Data == b.Data;
698 }
699
700 unsigned ComputeHash(const DeclNameKey &Key) const {
701 llvm::FoldingSetNodeID ID;
702 ID.AddInteger(Key.Kind);
703
704 switch (Key.Kind) {
705 case DeclarationName::Identifier:
706 case DeclarationName::CXXLiteralOperatorName:
707 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
708 break;
709 case DeclarationName::ObjCZeroArgSelector:
710 case DeclarationName::ObjCOneArgSelector:
711 case DeclarationName::ObjCMultiArgSelector:
712 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
713 break;
714 case DeclarationName::CXXConstructorName:
715 case DeclarationName::CXXDestructorName:
716 case DeclarationName::CXXConversionFunctionName:
717 ID.AddInteger((TypeID)Key.Data);
718 break;
719 case DeclarationName::CXXOperatorName:
720 ID.AddInteger((OverloadedOperatorKind)Key.Data);
721 break;
722 case DeclarationName::CXXUsingDirective:
723 break;
724 }
725
726 return ID.ComputeHash();
727 }
728
729 internal_key_type GetInternalKey(const external_key_type& Name) const {
730 DeclNameKey Key;
731 Key.Kind = Name.getNameKind();
732 switch (Name.getNameKind()) {
733 case DeclarationName::Identifier:
734 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
735 break;
736 case DeclarationName::ObjCZeroArgSelector:
737 case DeclarationName::ObjCOneArgSelector:
738 case DeclarationName::ObjCMultiArgSelector:
739 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
740 break;
741 case DeclarationName::CXXConstructorName:
742 case DeclarationName::CXXDestructorName:
743 case DeclarationName::CXXConversionFunctionName:
744 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
745 break;
746 case DeclarationName::CXXOperatorName:
747 Key.Data = Name.getCXXOverloadedOperator();
748 break;
749 case DeclarationName::CXXLiteralOperatorName:
750 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
751 break;
752 case DeclarationName::CXXUsingDirective:
753 break;
754 }
755
756 return Key;
757 }
758
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000759 external_key_type GetExternalKey(const internal_key_type& Key) const {
760 ASTContext *Context = Reader.getContext();
761 switch (Key.Kind) {
762 case DeclarationName::Identifier:
763 return DeclarationName((IdentifierInfo*)Key.Data);
764
765 case DeclarationName::ObjCZeroArgSelector:
766 case DeclarationName::ObjCOneArgSelector:
767 case DeclarationName::ObjCMultiArgSelector:
768 return DeclarationName(Selector(Key.Data));
769
770 case DeclarationName::CXXConstructorName:
771 return Context->DeclarationNames.getCXXConstructorName(
772 Context->getCanonicalType(Reader.GetType(Key.Data)));
773
774 case DeclarationName::CXXDestructorName:
775 return Context->DeclarationNames.getCXXDestructorName(
776 Context->getCanonicalType(Reader.GetType(Key.Data)));
777
778 case DeclarationName::CXXConversionFunctionName:
779 return Context->DeclarationNames.getCXXConversionFunctionName(
780 Context->getCanonicalType(Reader.GetType(Key.Data)));
781
782 case DeclarationName::CXXOperatorName:
783 return Context->DeclarationNames.getCXXOperatorName(
784 (OverloadedOperatorKind)Key.Data);
785
786 case DeclarationName::CXXLiteralOperatorName:
787 return Context->DeclarationNames.getCXXLiteralOperatorName(
788 (IdentifierInfo*)Key.Data);
789
790 case DeclarationName::CXXUsingDirective:
791 return DeclarationName::getUsingDirectiveName();
792 }
793
794 llvm_unreachable("Invalid Name Kind ?");
795 }
796
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000797 static std::pair<unsigned, unsigned>
798 ReadKeyDataLength(const unsigned char*& d) {
799 using namespace clang::io;
800 unsigned KeyLen = ReadUnalignedLE16(d);
801 unsigned DataLen = ReadUnalignedLE16(d);
802 return std::make_pair(KeyLen, DataLen);
803 }
804
805 internal_key_type ReadKey(const unsigned char* d, unsigned) {
806 using namespace clang::io;
807
808 DeclNameKey Key;
809 Key.Kind = (DeclarationName::NameKind)*d++;
810 switch (Key.Kind) {
811 case DeclarationName::Identifier:
812 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
813 break;
814 case DeclarationName::ObjCZeroArgSelector:
815 case DeclarationName::ObjCOneArgSelector:
816 case DeclarationName::ObjCMultiArgSelector:
817 Key.Data =
818 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
819 break;
820 case DeclarationName::CXXConstructorName:
821 case DeclarationName::CXXDestructorName:
822 case DeclarationName::CXXConversionFunctionName:
823 Key.Data = ReadUnalignedLE32(d); // TypeID
824 break;
825 case DeclarationName::CXXOperatorName:
826 Key.Data = *d++; // OverloadedOperatorKind
827 break;
828 case DeclarationName::CXXLiteralOperatorName:
829 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
830 break;
831 case DeclarationName::CXXUsingDirective:
832 break;
833 }
834
835 return Key;
836 }
837
838 data_type ReadData(internal_key_type, const unsigned char* d,
839 unsigned DataLen) {
840 using namespace clang::io;
841 unsigned NumDecls = ReadUnalignedLE16(d);
842 DeclID *Start = (DeclID *)d;
843 return std::make_pair(Start, Start + NumDecls);
844 }
845};
846
847} // end anonymous namespace
848
849/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
850typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
851 ASTDeclContextNameLookupTable;
852
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000853bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
854 const std::pair<uint64_t, uint64_t> &Offsets,
855 DeclContextInfo &Info) {
856 SavedStreamPosition SavedPosition(Cursor);
857 // First the lexical decls.
858 if (Offsets.first != 0) {
859 Cursor.JumpToBit(Offsets.first);
860
861 RecordData Record;
862 const char *Blob;
863 unsigned BlobLen;
864 unsigned Code = Cursor.ReadCode();
865 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
866 if (RecCode != DECL_CONTEXT_LEXICAL) {
867 Error("Expected lexical block");
868 return true;
869 }
870
871 Info.LexicalDecls = reinterpret_cast<const DeclID*>(Blob);
872 Info.NumLexicalDecls = BlobLen / sizeof(DeclID);
873 } else {
874 Info.LexicalDecls = 0;
875 Info.NumLexicalDecls = 0;
876 }
877
878 // Now the lookup table.
879 if (Offsets.second != 0) {
880 Cursor.JumpToBit(Offsets.second);
881
882 RecordData Record;
883 const char *Blob;
884 unsigned BlobLen;
885 unsigned Code = Cursor.ReadCode();
886 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
887 if (RecCode != DECL_CONTEXT_VISIBLE) {
888 Error("Expected visible lookup table block");
889 return true;
890 }
891 Info.NameLookupTableData
892 = ASTDeclContextNameLookupTable::Create(
893 (const unsigned char *)Blob + Record[0],
894 (const unsigned char *)Blob,
895 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl9d8f58b2010-08-24 00:50:00 +0000896 } else {
897 Info.NameLookupTableData = 0;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000898 }
899
900 return false;
901}
902
Sebastian Redl2c499f62010-08-18 23:56:43 +0000903void ASTReader::Error(const char *Msg) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000904 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000905}
906
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000907/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000908bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000909 if (Listener)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000910 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000911 ActualOriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000912 SuggestedPredefines);
Douglas Gregorc379c072009-04-28 18:58:38 +0000913 return false;
Douglas Gregor92863e42009-04-10 23:10:45 +0000914}
915
Douglas Gregorc5046832009-04-27 18:38:38 +0000916//===----------------------------------------------------------------------===//
917// Source Manager Deserialization
918//===----------------------------------------------------------------------===//
919
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000920/// \brief Read the line table in the source manager block.
921/// \returns true if ther was an error.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000922bool ASTReader::ParseLineTable(llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000923 unsigned Idx = 0;
924 LineTableInfo &LineTable = SourceMgr.getLineTable();
925
926 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +0000927 std::map<int, int> FileIDs;
928 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000929 // Extract the file name
930 unsigned FilenameLen = Record[Idx++];
931 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
932 Idx += FilenameLen;
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000933 MaybeAddSystemRootToFilename(Filename);
Mike Stump11289f42009-09-09 15:08:12 +0000934 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregora8854652009-04-13 17:12:42 +0000935 Filename.size());
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000936 }
937
938 // Parse the line entries
939 std::vector<LineEntry> Entries;
940 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000941 int FID = Record[Idx++];
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000942
943 // Extract the line entries
944 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000945 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000946 Entries.clear();
947 Entries.reserve(NumEntries);
948 for (unsigned I = 0; I != NumEntries; ++I) {
949 unsigned FileOffset = Record[Idx++];
950 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000951 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +0000952 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000953 = (SrcMgr::CharacteristicKind)Record[Idx++];
954 unsigned IncludeOffset = Record[Idx++];
955 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
956 FileKind, IncludeOffset));
957 }
958 LineTable.AddEntry(FID, Entries);
959 }
960
961 return false;
962}
963
Douglas Gregorc5046832009-04-27 18:38:38 +0000964namespace {
965
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000966class ASTStatData {
Douglas Gregorc5046832009-04-27 18:38:38 +0000967public:
968 const bool hasStat;
969 const ino_t ino;
970 const dev_t dev;
971 const mode_t mode;
972 const time_t mtime;
973 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +0000974
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000975 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump11289f42009-09-09 15:08:12 +0000976 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
977
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000978 ASTStatData()
Douglas Gregorc5046832009-04-27 18:38:38 +0000979 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
980};
981
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000982class ASTStatLookupTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000983 public:
984 typedef const char *external_key_type;
985 typedef const char *internal_key_type;
986
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000987 typedef ASTStatData data_type;
Douglas Gregorc5046832009-04-27 18:38:38 +0000988
989 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000990 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000991 }
992
993 static internal_key_type GetInternalKey(const char *path) { return path; }
994
995 static bool EqualKey(internal_key_type a, internal_key_type b) {
996 return strcmp(a, b) == 0;
997 }
998
999 static std::pair<unsigned, unsigned>
1000 ReadKeyDataLength(const unsigned char*& d) {
1001 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1002 unsigned DataLen = (unsigned) *d++;
1003 return std::make_pair(KeyLen + 1, DataLen);
1004 }
1005
1006 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1007 return (const char *)d;
1008 }
1009
1010 static data_type ReadData(const internal_key_type, const unsigned char *d,
1011 unsigned /*DataLen*/) {
1012 using namespace clang::io;
1013
1014 if (*d++ == 1)
1015 return data_type();
1016
1017 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1018 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1019 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +00001020 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +00001021 off_t size = (off_t) ReadUnalignedLE64(d);
1022 return data_type(ino, dev, mode, mtime, size);
1023 }
1024};
1025
1026/// \brief stat() cache for precompiled headers.
1027///
1028/// This cache is very similar to the stat cache used by pretokenized
1029/// headers.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001030class ASTStatCache : public StatSysCallCache {
1031 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregorc5046832009-04-27 18:38:38 +00001032 CacheTy *Cache;
1033
1034 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +00001035public:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001036 ASTStatCache(const unsigned char *Buckets,
Douglas Gregorc5046832009-04-27 18:38:38 +00001037 const unsigned char *Base,
1038 unsigned &NumStatHits,
Mike Stump11289f42009-09-09 15:08:12 +00001039 unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +00001040 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1041 Cache = CacheTy::Create(Buckets, Base);
1042 }
1043
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001044 ~ASTStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +00001045
Douglas Gregorc5046832009-04-27 18:38:38 +00001046 int stat(const char *path, struct stat *buf) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001047 // Do the lookup for the file's data in the AST file.
Douglas Gregorc5046832009-04-27 18:38:38 +00001048 CacheTy::iterator I = Cache->find(path);
1049
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001050 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregorc5046832009-04-27 18:38:38 +00001051 if (I == Cache->end()) {
1052 ++NumStatMisses;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001053 return StatSysCallCache::stat(path, buf);
Douglas Gregorc5046832009-04-27 18:38:38 +00001054 }
Mike Stump11289f42009-09-09 15:08:12 +00001055
Douglas Gregorc5046832009-04-27 18:38:38 +00001056 ++NumStatHits;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001057 ASTStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001058
Douglas Gregorc5046832009-04-27 18:38:38 +00001059 if (!Data.hasStat)
1060 return 1;
1061
1062 buf->st_ino = Data.ino;
1063 buf->st_dev = Data.dev;
1064 buf->st_mtime = Data.mtime;
1065 buf->st_mode = Data.mode;
1066 buf->st_size = Data.size;
1067 return 0;
1068 }
1069};
1070} // end anonymous namespace
1071
1072
Sebastian Redl393f8b72010-07-19 20:52:06 +00001073/// \brief Read a source manager block
Sebastian Redl2c499f62010-08-18 23:56:43 +00001074ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001075 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +00001076
Sebastian Redl393f8b72010-07-19 20:52:06 +00001077 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001078
Douglas Gregor258ae542009-04-27 06:38:32 +00001079 // Set the source-location entry cursor to the current position in
1080 // the stream. This cursor will be used to read the contents of the
1081 // source manager block initially, and then lazily read
1082 // source-location entries as needed.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001083 SLocEntryCursor = F.Stream;
Douglas Gregor258ae542009-04-27 06:38:32 +00001084
1085 // The stream itself is going to skip over the source manager block.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001086 if (F.Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001087 Error("malformed block record in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001088 return Failure;
1089 }
1090
1091 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001092 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001093 Error("malformed source manager block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001094 return Failure;
1095 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001096
Douglas Gregora7f71a92009-04-10 03:52:48 +00001097 RecordData Record;
1098 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001099 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001100 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001101 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001102 Error("error at end of Source Manager block in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001103 return Failure;
1104 }
Douglas Gregor92863e42009-04-10 23:10:45 +00001105 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001106 }
Mike Stump11289f42009-09-09 15:08:12 +00001107
Douglas Gregora7f71a92009-04-10 03:52:48 +00001108 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1109 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +00001110 SLocEntryCursor.ReadSubBlockID();
1111 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001112 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001113 return Failure;
1114 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001115 continue;
1116 }
Mike Stump11289f42009-09-09 15:08:12 +00001117
Douglas Gregora7f71a92009-04-10 03:52:48 +00001118 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001119 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001120 continue;
1121 }
Mike Stump11289f42009-09-09 15:08:12 +00001122
Douglas Gregora7f71a92009-04-10 03:52:48 +00001123 // Read a record.
1124 const char *BlobStart;
1125 unsigned BlobLen;
1126 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +00001127 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001128 default: // Default behavior: ignore.
1129 break;
1130
Sebastian Redl539c5062010-08-18 23:57:32 +00001131 case SM_LINE_TABLE:
Sebastian Redlb293a452010-07-20 21:20:32 +00001132 if (ParseLineTable(Record))
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001133 return Failure;
Chris Lattner184e65d2009-04-14 23:22:57 +00001134 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00001135
Sebastian Redl539c5062010-08-18 23:57:32 +00001136 case SM_SLOC_FILE_ENTRY:
1137 case SM_SLOC_BUFFER_ENTRY:
1138 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor258ae542009-04-27 06:38:32 +00001139 // Once we hit one of the source location entries, we're done.
1140 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001141 }
1142 }
1143}
1144
Sebastian Redl06750302010-07-20 21:50:20 +00001145/// \brief Get a cursor that's correctly positioned for reading the source
1146/// location entry with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001147llvm::BitstreamCursor &ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl06750302010-07-20 21:50:20 +00001148 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1149 "SLocCursorForID should only be called for real IDs.");
1150
1151 ID -= 1;
1152 PerFileData *F = 0;
1153 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1154 F = Chain[N - I - 1];
1155 if (ID < F->LocalNumSLocEntries)
1156 break;
1157 ID -= F->LocalNumSLocEntries;
1158 }
1159 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1160
1161 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
1162 return F->SLocEntryCursor;
1163}
1164
Douglas Gregor258ae542009-04-27 06:38:32 +00001165/// \brief Read in the source location entry with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001166ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001167 if (ID == 0)
1168 return Success;
1169
1170 if (ID > TotalNumSLocEntries) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001171 Error("source location entry ID out-of-range for AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001172 return Failure;
1173 }
1174
Sebastian Redl06750302010-07-20 21:50:20 +00001175 llvm::BitstreamCursor &SLocEntryCursor = SLocCursorForID(ID);
Sebastian Redl34522812010-07-16 17:50:48 +00001176
Douglas Gregor258ae542009-04-27 06:38:32 +00001177 ++NumSLocEntriesRead;
Douglas Gregor258ae542009-04-27 06:38:32 +00001178 unsigned Code = SLocEntryCursor.ReadCode();
1179 if (Code == llvm::bitc::END_BLOCK ||
1180 Code == llvm::bitc::ENTER_SUBBLOCK ||
1181 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001182 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001183 return Failure;
1184 }
1185
Douglas Gregor258ae542009-04-27 06:38:32 +00001186 RecordData Record;
1187 const char *BlobStart;
1188 unsigned BlobLen;
1189 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1190 default:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001191 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001192 return Failure;
1193
Sebastian Redl539c5062010-08-18 23:57:32 +00001194 case SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001195 std::string Filename(BlobStart, BlobStart + BlobLen);
1196 MaybeAddSystemRootToFilename(Filename);
1197 const FileEntry *File = FileMgr.getFile(Filename);
Chris Lattnerd20dc872009-06-15 04:35:16 +00001198 if (File == 0) {
1199 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001200 ErrorStr += Filename;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001201 ErrorStr += "' referenced by AST file";
Chris Lattnerd20dc872009-06-15 04:35:16 +00001202 Error(ErrorStr.c_str());
1203 return Failure;
1204 }
Mike Stump11289f42009-09-09 15:08:12 +00001205
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001206 if (Record.size() < 10) {
Ted Kremenekabb1ddd2010-03-18 21:23:05 +00001207 Error("source location entry is incorrect");
1208 return Failure;
1209 }
1210
Douglas Gregorce3a8292010-07-27 00:27:13 +00001211 if (!DisableValidation &&
1212 ((off_t)Record[4] != File->getSize()
Douglas Gregor08288f22010-04-09 15:54:22 +00001213#if !defined(LLVM_ON_WIN32)
1214 // In our regression testing, the Windows file system seems to
1215 // have inconsistent modification times that sometimes
1216 // erroneously trigger this error-handling path.
Douglas Gregorce3a8292010-07-27 00:27:13 +00001217 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor08288f22010-04-09 15:54:22 +00001218#endif
Douglas Gregorce3a8292010-07-27 00:27:13 +00001219 )) {
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001220 Diag(diag::err_fe_pch_file_modified)
1221 << Filename;
1222 return Failure;
1223 }
1224
Douglas Gregor258ae542009-04-27 06:38:32 +00001225 FileID FID = SourceMgr.createFileID(File,
1226 SourceLocation::getFromRawEncoding(Record[1]),
1227 (SrcMgr::CharacteristicKind)Record[2],
1228 ID, Record[0]);
1229 if (Record[3])
1230 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1231 .setHasLineDirectives();
1232
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001233 // Reconstruct header-search information for this file.
1234 HeaderFileInfo HFI;
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001235 HFI.isImport = Record[6];
1236 HFI.DirInfo = Record[7];
1237 HFI.NumIncludes = Record[8];
1238 HFI.ControllingMacroID = Record[9];
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001239 if (Listener)
1240 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor258ae542009-04-27 06:38:32 +00001241 break;
1242 }
1243
Sebastian Redl539c5062010-08-18 23:57:32 +00001244 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor258ae542009-04-27 06:38:32 +00001245 const char *Name = BlobStart;
1246 unsigned Offset = Record[0];
1247 unsigned Code = SLocEntryCursor.ReadCode();
1248 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001249 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +00001250 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001251
Sebastian Redl539c5062010-08-18 23:57:32 +00001252 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001253 Error("AST record has invalid code");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001254 return Failure;
1255 }
1256
Douglas Gregor258ae542009-04-27 06:38:32 +00001257 llvm::MemoryBuffer *Buffer
Chris Lattner58c79342010-04-05 22:42:27 +00001258 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1259 Name);
Douglas Gregor258ae542009-04-27 06:38:32 +00001260 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump11289f42009-09-09 15:08:12 +00001261
Douglas Gregore6648fb2009-04-28 20:33:11 +00001262 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001263 PCHPredefinesBlock Block = {
1264 BufferID,
1265 llvm::StringRef(BlobStart, BlobLen - 1)
1266 };
1267 PCHPredefinesBuffers.push_back(Block);
Douglas Gregore6648fb2009-04-28 20:33:11 +00001268 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001269
1270 break;
1271 }
1272
Sebastian Redl539c5062010-08-18 23:57:32 +00001273 case SM_SLOC_INSTANTIATION_ENTRY: {
Mike Stump11289f42009-09-09 15:08:12 +00001274 SourceLocation SpellingLoc
Douglas Gregor258ae542009-04-27 06:38:32 +00001275 = SourceLocation::getFromRawEncoding(Record[1]);
1276 SourceMgr.createInstantiationLoc(SpellingLoc,
1277 SourceLocation::getFromRawEncoding(Record[2]),
1278 SourceLocation::getFromRawEncoding(Record[3]),
1279 Record[4],
1280 ID,
1281 Record[0]);
1282 break;
Mike Stump11289f42009-09-09 15:08:12 +00001283 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001284 }
1285
1286 return Success;
1287}
1288
Chris Lattnere78a6be2009-04-27 01:05:14 +00001289/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1290/// specified cursor. Read the abbreviations that are at the top of the block
1291/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001292bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001293 unsigned BlockID) {
1294 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001295 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001296 return Failure;
1297 }
Mike Stump11289f42009-09-09 15:08:12 +00001298
Chris Lattnere78a6be2009-04-27 01:05:14 +00001299 while (true) {
1300 unsigned Code = Cursor.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00001301
Chris Lattnere78a6be2009-04-27 01:05:14 +00001302 // We expect all abbrevs to be at the start of the block.
1303 if (Code != llvm::bitc::DEFINE_ABBREV)
1304 return false;
1305 Cursor.ReadAbbrevRecord();
1306 }
1307}
1308
Sebastian Redl2c499f62010-08-18 23:56:43 +00001309void ASTReader::ReadMacroRecord(llvm::BitstreamCursor &Stream, uint64_t Offset){
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001310 assert(PP && "Forgot to set Preprocessor ?");
Mike Stump11289f42009-09-09 15:08:12 +00001311
Douglas Gregorc3366a52009-04-21 23:56:24 +00001312 // Keep track of where we are in the stream, then jump back there
1313 // after reading this macro.
1314 SavedStreamPosition SavedPosition(Stream);
1315
1316 Stream.JumpToBit(Offset);
1317 RecordData Record;
1318 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1319 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001320
Douglas Gregorc3366a52009-04-21 23:56:24 +00001321 while (true) {
1322 unsigned Code = Stream.ReadCode();
1323 switch (Code) {
1324 case llvm::bitc::END_BLOCK:
1325 return;
1326
1327 case llvm::bitc::ENTER_SUBBLOCK:
1328 // No known subblocks, always skip them.
1329 Stream.ReadSubBlockID();
1330 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001331 Error("malformed block record in AST file");
Douglas Gregorc3366a52009-04-21 23:56:24 +00001332 return;
1333 }
1334 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001335
Douglas Gregorc3366a52009-04-21 23:56:24 +00001336 case llvm::bitc::DEFINE_ABBREV:
1337 Stream.ReadAbbrevRecord();
1338 continue;
1339 default: break;
1340 }
1341
1342 // Read a record.
1343 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001344 PreprocessorRecordTypes RecType =
1345 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001346 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001347 case PP_MACRO_OBJECT_LIKE:
1348 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001349 // If we already have a macro, that means that we've hit the end
1350 // of the definition of the macro we were looking for. We're
1351 // done.
1352 if (Macro)
1353 return;
1354
1355 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1356 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001357 Error("macro must have a name in AST file");
Douglas Gregorc3366a52009-04-21 23:56:24 +00001358 return;
1359 }
1360 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1361 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001362
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001363 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001364 MI->setIsUsed(isUsed);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001365 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001366
Douglas Gregoraae92242010-03-19 21:51:54 +00001367 unsigned NextIndex = 3;
Sebastian Redl539c5062010-08-18 23:57:32 +00001368 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001369 // Decode function-like macro info.
1370 bool isC99VarArgs = Record[3];
1371 bool isGNUVarArgs = Record[4];
1372 MacroArgs.clear();
1373 unsigned NumArgs = Record[5];
Douglas Gregoraae92242010-03-19 21:51:54 +00001374 NextIndex = 6 + NumArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001375 for (unsigned i = 0; i != NumArgs; ++i)
1376 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1377
1378 // Install function-like macro info.
1379 MI->setIsFunctionLike();
1380 if (isC99VarArgs) MI->setIsC99Varargs();
1381 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001382 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001383 PP->getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001384 }
1385
1386 // Finally, install the macro.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001387 PP->setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001388
1389 // Remember that we saw this macro last so that we add the tokens that
1390 // form its body to it.
1391 Macro = MI;
Douglas Gregoraae92242010-03-19 21:51:54 +00001392
1393 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1394 // We have a macro definition. Load it now.
1395 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1396 getMacroDefinition(Record[NextIndex]));
1397 }
1398
Douglas Gregorc3366a52009-04-21 23:56:24 +00001399 ++NumMacrosRead;
1400 break;
1401 }
Mike Stump11289f42009-09-09 15:08:12 +00001402
Sebastian Redl539c5062010-08-18 23:57:32 +00001403 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001404 // If we see a TOKEN before a PP_MACRO_*, then the file is
1405 // erroneous, just pretend we didn't see this.
1406 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001407
Douglas Gregorc3366a52009-04-21 23:56:24 +00001408 Token Tok;
1409 Tok.startToken();
1410 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1411 Tok.setLength(Record[1]);
1412 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1413 Tok.setIdentifierInfo(II);
1414 Tok.setKind((tok::TokenKind)Record[3]);
1415 Tok.setFlag((Token::TokenFlags)Record[4]);
1416 Macro->AddTokenToBody(Tok);
1417 break;
1418 }
Douglas Gregoraae92242010-03-19 21:51:54 +00001419
Sebastian Redl539c5062010-08-18 23:57:32 +00001420 case PP_MACRO_INSTANTIATION: {
Douglas Gregoraae92242010-03-19 21:51:54 +00001421 // If we already have a macro, that means that we've hit the end
1422 // of the definition of the macro we were looking for. We're
1423 // done.
1424 if (Macro)
1425 return;
1426
1427 if (!PP->getPreprocessingRecord()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001428 Error("missing preprocessing record in AST file");
Douglas Gregoraae92242010-03-19 21:51:54 +00001429 return;
1430 }
1431
1432 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1433 if (PPRec.getPreprocessedEntity(Record[0]))
1434 return;
1435
1436 MacroInstantiation *MI
1437 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1438 SourceRange(
1439 SourceLocation::getFromRawEncoding(Record[1]),
1440 SourceLocation::getFromRawEncoding(Record[2])),
1441 getMacroDefinition(Record[4]));
1442 PPRec.SetPreallocatedEntity(Record[0], MI);
1443 return;
1444 }
1445
Sebastian Redl539c5062010-08-18 23:57:32 +00001446 case PP_MACRO_DEFINITION: {
Douglas Gregoraae92242010-03-19 21:51:54 +00001447 // If we already have a macro, that means that we've hit the end
1448 // of the definition of the macro we were looking for. We're
1449 // done.
1450 if (Macro)
1451 return;
1452
1453 if (!PP->getPreprocessingRecord()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001454 Error("missing preprocessing record in AST file");
Douglas Gregoraae92242010-03-19 21:51:54 +00001455 return;
1456 }
1457
1458 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1459 if (PPRec.getPreprocessedEntity(Record[0]))
1460 return;
1461
1462 if (Record[1] >= MacroDefinitionsLoaded.size()) {
1463 Error("out-of-bounds macro definition record");
1464 return;
1465 }
1466
1467 MacroDefinition *MD
1468 = new (PPRec) MacroDefinition(DecodeIdentifierInfo(Record[4]),
1469 SourceLocation::getFromRawEncoding(Record[5]),
1470 SourceRange(
1471 SourceLocation::getFromRawEncoding(Record[2]),
1472 SourceLocation::getFromRawEncoding(Record[3])));
1473 PPRec.SetPreallocatedEntity(Record[0], MD);
1474 MacroDefinitionsLoaded[Record[1]] = MD;
1475 return;
1476 }
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001477 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001478 }
1479}
1480
Sebastian Redl2c499f62010-08-18 23:56:43 +00001481void ASTReader::ReadDefinedMacros() {
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001482 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1483 llvm::BitstreamCursor &MacroCursor = Chain[N - I - 1]->MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001484
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001485 // If there was no preprocessor block, skip this file.
1486 if (!MacroCursor.getBitStreamReader())
1487 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001488
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001489 llvm::BitstreamCursor Cursor = MacroCursor;
Sebastian Redl539c5062010-08-18 23:57:32 +00001490 if (Cursor.EnterSubBlock(PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001491 Error("malformed preprocessor block record in AST file");
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001492 return;
1493 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001494
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001495 RecordData Record;
1496 while (true) {
Sebastian Redl4102dd52010-09-28 02:55:49 +00001497 uint64_t Offset = Cursor.GetCurrentBitNo();
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001498 unsigned Code = Cursor.ReadCode();
1499 if (Code == llvm::bitc::END_BLOCK) {
1500 if (Cursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001501 Error("error at end of preprocessor block in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001502 return;
1503 }
1504 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001505 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001506
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001507 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1508 // No known subblocks, always skip them.
1509 Cursor.ReadSubBlockID();
1510 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001511 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001512 return;
1513 }
1514 continue;
1515 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001516
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001517 if (Code == llvm::bitc::DEFINE_ABBREV) {
1518 Cursor.ReadAbbrevRecord();
1519 continue;
1520 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001521
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001522 // Read a record.
1523 const char *BlobStart;
1524 unsigned BlobLen;
1525 Record.clear();
1526 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1527 default: // Default behavior: ignore.
1528 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001529
Sebastian Redl539c5062010-08-18 23:57:32 +00001530 case PP_MACRO_OBJECT_LIKE:
1531 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001532 DecodeIdentifierInfo(Record[0]);
1533 break;
1534
Sebastian Redl539c5062010-08-18 23:57:32 +00001535 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001536 // Ignore tokens.
1537 break;
Douglas Gregoraae92242010-03-19 21:51:54 +00001538
Sebastian Redl539c5062010-08-18 23:57:32 +00001539 case PP_MACRO_INSTANTIATION:
1540 case PP_MACRO_DEFINITION:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001541 // Read the macro record.
Sebastian Redl4102dd52010-09-28 02:55:49 +00001542 // FIXME: That's a stupid way to do this. We should reuse this cursor.
1543 ReadMacroRecord(Chain[N - I - 1]->Stream, Offset);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001544 break;
1545 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001546 }
1547 }
1548}
1549
Sebastian Redl50e26582010-09-15 19:54:06 +00001550MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregoraae92242010-03-19 21:51:54 +00001551 if (ID == 0 || ID >= MacroDefinitionsLoaded.size())
1552 return 0;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001553
1554 if (!MacroDefinitionsLoaded[ID]) {
1555 unsigned Index = ID;
1556 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1557 PerFileData &F = *Chain[N - I - 1];
1558 if (Index < F.LocalNumMacroDefinitions) {
1559 ReadMacroRecord(F.Stream, F.MacroDefinitionOffsets[Index]);
1560 break;
1561 }
1562 Index -= F.LocalNumMacroDefinitions;
1563 }
1564 assert(MacroDefinitionsLoaded[ID] && "Broken chain");
1565 }
1566
Douglas Gregoraae92242010-03-19 21:51:54 +00001567 return MacroDefinitionsLoaded[ID];
1568}
1569
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001570/// \brief If we are loading a relocatable PCH file, and the filename is
1571/// not an absolute path, add the system root to the beginning of the file
1572/// name.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001573void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001574 // If this is not a relocatable PCH file, there's nothing to do.
1575 if (!RelocatablePCH)
1576 return;
Mike Stump11289f42009-09-09 15:08:12 +00001577
Daniel Dunbarf2ce9a22009-11-18 19:50:41 +00001578 if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001579 return;
1580
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001581 if (isysroot == 0) {
1582 // If no system root was given, default to '/'
1583 Filename.insert(Filename.begin(), '/');
1584 return;
1585 }
Mike Stump11289f42009-09-09 15:08:12 +00001586
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001587 unsigned Length = strlen(isysroot);
1588 if (isysroot[Length - 1] != '/')
1589 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001590
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001591 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1592}
1593
Sebastian Redl2c499f62010-08-18 23:56:43 +00001594ASTReader::ASTReadResult
Sebastian Redl3e31c722010-08-18 23:56:56 +00001595ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001596 llvm::BitstreamCursor &Stream = F.Stream;
1597
Sebastian Redl539c5062010-08-18 23:57:32 +00001598 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001599 Error("malformed block record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001600 return Failure;
1601 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001602
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001603 // Read all of the records and blocks for the ASt file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001604 RecordData Record;
Sebastian Redl393f8b72010-07-19 20:52:06 +00001605 bool First = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001606 while (!Stream.AtEndOfStream()) {
1607 unsigned Code = Stream.ReadCode();
1608 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001609 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001610 Error("error at end of module block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001611 return Failure;
1612 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001613
Douglas Gregor55abb232009-04-10 20:39:37 +00001614 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001615 }
1616
1617 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1618 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001619 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001620 // We lazily load the decls block, but we want to set up the
1621 // DeclsCursor cursor to point into it. Clone our current bitcode
1622 // cursor to it, enter the block and read the abbrevs in that block.
1623 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001624 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001625 if (Stream.SkipBlock() || // Skip with the main cursor.
1626 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001627 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001628 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001629 return Failure;
1630 }
1631 break;
Mike Stump11289f42009-09-09 15:08:12 +00001632
Sebastian Redl539c5062010-08-18 23:57:32 +00001633 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001634 F.MacroCursor = Stream;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001635 if (PP)
1636 PP->setExternalSource(this);
1637
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001638 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001639 Error("malformed block record in AST file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001640 return Failure;
1641 }
1642 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001643
Sebastian Redl539c5062010-08-18 23:57:32 +00001644 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001645 switch (ReadSourceManagerBlock(F)) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001646 case Success:
1647 break;
1648
1649 case Failure:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001650 Error("malformed source manager block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001651 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001652
1653 case IgnorePCH:
1654 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001655 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001656 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001657 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00001658 First = false;
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001659 continue;
1660 }
1661
1662 if (Code == llvm::bitc::DEFINE_ABBREV) {
1663 Stream.ReadAbbrevRecord();
1664 continue;
1665 }
1666
1667 // Read and process a record.
1668 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001669 const char *BlobStart = 0;
1670 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001671 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Douglas Gregorbfbde532009-04-10 21:16:55 +00001672 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001673 default: // Default behavior: ignore.
1674 break;
1675
Sebastian Redl539c5062010-08-18 23:57:32 +00001676 case METADATA: {
1677 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1678 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001679 : diag::warn_pch_version_too_new);
1680 return IgnorePCH;
1681 }
1682
1683 RelocatablePCH = Record[4];
1684 if (Listener) {
1685 std::string TargetTriple(BlobStart, BlobLen);
1686 if (Listener->ReadTargetTriple(TargetTriple))
1687 return IgnorePCH;
1688 }
1689 break;
1690 }
1691
Sebastian Redl539c5062010-08-18 23:57:32 +00001692 case CHAINED_METADATA: {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001693 if (!First) {
1694 Error("CHAINED_METADATA is not first record in block");
1695 return Failure;
1696 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001697 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1698 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001699 : diag::warn_pch_version_too_new);
1700 return IgnorePCH;
1701 }
1702
1703 // Load the chained file.
Sebastian Redl3e31c722010-08-18 23:56:56 +00001704 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen))) {
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001705 case Failure: return Failure;
1706 // If we have to ignore the dependency, we'll have to ignore this too.
1707 case IgnorePCH: return IgnorePCH;
1708 case Success: break;
1709 }
1710 break;
1711 }
1712
Sebastian Redl539c5062010-08-18 23:57:32 +00001713 case TYPE_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001714 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001715 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001716 return Failure;
1717 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001718 F.TypeOffsets = (const uint32_t *)BlobStart;
1719 F.LocalNumTypes = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001720 break;
1721
Sebastian Redl539c5062010-08-18 23:57:32 +00001722 case DECL_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001723 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001724 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001725 return Failure;
1726 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001727 F.DeclOffsets = (const uint32_t *)BlobStart;
1728 F.LocalNumDecls = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001729 break;
Douglas Gregor55abb232009-04-10 20:39:37 +00001730
Sebastian Redl539c5062010-08-18 23:57:32 +00001731 case TU_UPDATE_LEXICAL: {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001732 DeclContextInfo Info = {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001733 /* No visible information */ 0,
Sebastian Redl539c5062010-08-18 23:57:32 +00001734 reinterpret_cast<const DeclID *>(BlobStart),
1735 BlobLen / sizeof(DeclID)
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001736 };
1737 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1738 break;
1739 }
1740
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001741 case UPDATE_VISIBLE: {
1742 serialization::DeclID ID = Record[0];
1743 void *Table = ASTDeclContextNameLookupTable::Create(
1744 (const unsigned char *)BlobStart + Record[1],
1745 (const unsigned char *)BlobStart,
1746 ASTDeclContextNameLookupTrait(*this));
1747 if (ID == 1) { // Is it the TU?
1748 DeclContextInfo Info = {
1749 Table, /* No lexical inforamtion */ 0, 0
1750 };
1751 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1752 } else
1753 PendingVisibleUpdates[ID].push_back(Table);
1754 break;
1755 }
1756
Sebastian Redl539c5062010-08-18 23:57:32 +00001757 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001758 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1759 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001760 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001761 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1762 Latest > FirstLatestDeclIDs[First]) &&
1763 "The new latest is supposed to come after the previous latest");
1764 FirstLatestDeclIDs[First] = Latest;
1765 }
1766 break;
1767 }
1768
Sebastian Redl539c5062010-08-18 23:57:32 +00001769 case LANGUAGE_OPTIONS:
Douglas Gregorce3a8292010-07-27 00:27:13 +00001770 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor55abb232009-04-10 20:39:37 +00001771 return IgnorePCH;
1772 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00001773
Sebastian Redl539c5062010-08-18 23:57:32 +00001774 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001775 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00001776 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001777 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001778 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00001779 (const unsigned char *)F.IdentifierTableData + Record[0],
1780 (const unsigned char *)F.IdentifierTableData,
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001781 ASTIdentifierLookupTrait(*this, F.Stream));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001782 if (PP)
1783 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00001784 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001785 break;
1786
Sebastian Redl539c5062010-08-18 23:57:32 +00001787 case IDENTIFIER_OFFSET:
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001788 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001789 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001790 return Failure;
1791 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001792 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1793 F.LocalNumIdentifiers = Record[0];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001794 break;
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001795
Sebastian Redl539c5062010-08-18 23:57:32 +00001796 case EXTERNAL_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001797 // Optimization for the first block.
1798 if (ExternalDefinitions.empty())
1799 ExternalDefinitions.swap(Record);
1800 else
1801 ExternalDefinitions.insert(ExternalDefinitions.end(),
1802 Record.begin(), Record.end());
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001803 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00001804
Sebastian Redl539c5062010-08-18 23:57:32 +00001805 case SPECIAL_TYPES:
Sebastian Redlb293a452010-07-20 21:20:32 +00001806 // Optimization for the first block
1807 if (SpecialTypes.empty())
1808 SpecialTypes.swap(Record);
1809 else
1810 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregor652d82a2009-04-18 05:55:16 +00001811 break;
1812
Sebastian Redl539c5062010-08-18 23:57:32 +00001813 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001814 TotalNumStatements += Record[0];
1815 TotalNumMacros += Record[1];
1816 TotalLexicalDeclContexts += Record[2];
1817 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00001818 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00001819
Sebastian Redl539c5062010-08-18 23:57:32 +00001820 case TENTATIVE_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001821 // Optimization for the first block.
1822 if (TentativeDefinitions.empty())
1823 TentativeDefinitions.swap(Record);
1824 else
1825 TentativeDefinitions.insert(TentativeDefinitions.end(),
1826 Record.begin(), Record.end());
Douglas Gregord4df8652009-04-22 22:02:47 +00001827 break;
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001828
Sebastian Redl539c5062010-08-18 23:57:32 +00001829 case UNUSED_FILESCOPED_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001830 // Optimization for the first block.
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00001831 if (UnusedFileScopedDecls.empty())
1832 UnusedFileScopedDecls.swap(Record);
Sebastian Redlb293a452010-07-20 21:20:32 +00001833 else
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00001834 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1835 Record.begin(), Record.end());
Tanya Lattner90073802010-02-12 00:07:30 +00001836 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001837
Sebastian Redl539c5062010-08-18 23:57:32 +00001838 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001839 // Later blocks overwrite earlier ones.
1840 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00001841 break;
1842
Sebastian Redl539c5062010-08-18 23:57:32 +00001843 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001844 // Optimization for the first block.
1845 if (LocallyScopedExternalDecls.empty())
1846 LocallyScopedExternalDecls.swap(Record);
1847 else
1848 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1849 Record.begin(), Record.end());
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001850 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001851
Sebastian Redl539c5062010-08-18 23:57:32 +00001852 case SELECTOR_OFFSETS:
Sebastian Redla19a67f2010-08-03 21:58:15 +00001853 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00001854 F.LocalNumSelectors = Record[0];
Douglas Gregor95c13f52009-04-25 17:48:32 +00001855 break;
1856
Sebastian Redl539c5062010-08-18 23:57:32 +00001857 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00001858 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001859 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00001860 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001861 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00001862 F.SelectorLookupTableData + Record[0],
1863 F.SelectorLookupTableData,
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001864 ASTSelectorLookupTrait(*this));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00001865 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00001866 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00001867
Sebastian Redl96371b42010-09-22 00:42:30 +00001868 case REFERENCED_SELECTOR_POOL:
1869 if (ReferencedSelectorsData.empty())
1870 ReferencedSelectorsData.swap(Record);
1871 else
1872 ReferencedSelectorsData.insert(ReferencedSelectorsData.end(),
1873 Record.begin(), Record.end());
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001874 break;
1875
Sebastian Redl539c5062010-08-18 23:57:32 +00001876 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001877 if (!Record.empty() && Listener)
1878 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00001879 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00001880
Sebastian Redl539c5062010-08-18 23:57:32 +00001881 case SOURCE_LOCATION_OFFSETS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001882 F.SLocOffsets = (const uint32_t *)BlobStart;
1883 F.LocalNumSLocEntries = Record[0];
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001884 F.LocalSLocSize = Record[1];
Douglas Gregor258ae542009-04-27 06:38:32 +00001885 break;
1886
Sebastian Redl539c5062010-08-18 23:57:32 +00001887 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl96371b42010-09-22 00:42:30 +00001888 if (PreloadSLocEntries.empty())
1889 PreloadSLocEntries.swap(Record);
1890 else
1891 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
1892 Record.begin(), Record.end());
Douglas Gregor258ae542009-04-27 06:38:32 +00001893 break;
Douglas Gregorc5046832009-04-27 18:38:38 +00001894
Sebastian Redl539c5062010-08-18 23:57:32 +00001895 case STAT_CACHE: {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001896 ASTStatCache *MyStatCache =
1897 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001898 (const unsigned char *)BlobStart,
1899 NumStatHits, NumStatMisses);
1900 FileMgr.addStatCache(MyStatCache);
Sebastian Redl34522812010-07-16 17:50:48 +00001901 F.StatCache = MyStatCache;
Douglas Gregorc5046832009-04-27 18:38:38 +00001902 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001903 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001904
Sebastian Redl539c5062010-08-18 23:57:32 +00001905 case EXT_VECTOR_DECLS:
Sebastian Redl04f5c312010-07-28 21:38:49 +00001906 // Optimization for the first block.
1907 if (ExtVectorDecls.empty())
1908 ExtVectorDecls.swap(Record);
1909 else
1910 ExtVectorDecls.insert(ExtVectorDecls.end(),
1911 Record.begin(), Record.end());
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001912 break;
1913
Sebastian Redl539c5062010-08-18 23:57:32 +00001914 case VTABLE_USES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001915 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00001916 VTableUses.swap(Record);
1917 break;
1918
Sebastian Redl539c5062010-08-18 23:57:32 +00001919 case DYNAMIC_CLASSES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001920 // Optimization for the first block.
1921 if (DynamicClasses.empty())
1922 DynamicClasses.swap(Record);
1923 else
1924 DynamicClasses.insert(DynamicClasses.end(),
1925 Record.begin(), Record.end());
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00001926 break;
1927
Sebastian Redl539c5062010-08-18 23:57:32 +00001928 case PENDING_IMPLICIT_INSTANTIATIONS:
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00001929 // Optimization for the first block.
Chandler Carruth54080172010-08-25 08:44:16 +00001930 if (PendingInstantiations.empty())
1931 PendingInstantiations.swap(Record);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00001932 else
Chandler Carruth54080172010-08-25 08:44:16 +00001933 PendingInstantiations.insert(PendingInstantiations.end(),
1934 Record.begin(), Record.end());
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00001935 break;
1936
Sebastian Redl539c5062010-08-18 23:57:32 +00001937 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001938 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00001939 SemaDeclRefs.swap(Record);
1940 break;
1941
Sebastian Redl539c5062010-08-18 23:57:32 +00001942 case ORIGINAL_FILE_NAME:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001943 // The primary AST will be the last to get here, so it will be the one
Sebastian Redlb293a452010-07-20 21:20:32 +00001944 // that's used.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00001945 ActualOriginalFileName.assign(BlobStart, BlobLen);
1946 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001947 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001948 break;
Mike Stump11289f42009-09-09 15:08:12 +00001949
Sebastian Redl539c5062010-08-18 23:57:32 +00001950 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00001951 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001952 llvm::StringRef ASTBranch(BlobStart, BlobLen);
1953 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1954 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregord54f3a12009-10-05 21:07:28 +00001955 return IgnorePCH;
1956 }
1957 break;
1958 }
Sebastian Redlfa061442010-07-21 20:07:32 +00001959
Sebastian Redl539c5062010-08-18 23:57:32 +00001960 case MACRO_DEFINITION_OFFSETS:
Sebastian Redlfa061442010-07-21 20:07:32 +00001961 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
1962 F.NumPreallocatedPreprocessingEntities = Record[0];
1963 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001964 break;
Sebastian Redle7c1fe62010-08-13 00:28:03 +00001965
Sebastian Redl539c5062010-08-18 23:57:32 +00001966 case DECL_REPLACEMENTS: {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00001967 if (Record.size() % 2 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001968 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redle7c1fe62010-08-13 00:28:03 +00001969 return Failure;
1970 }
1971 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl539c5062010-08-18 23:57:32 +00001972 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redle7c1fe62010-08-13 00:28:03 +00001973 std::make_pair(&F, Record[I+1]);
1974 break;
1975 }
Sebastian Redlaba202b2010-08-24 22:50:19 +00001976
1977 case ADDITIONAL_TEMPLATE_SPECIALIZATIONS: {
1978 AdditionalTemplateSpecializations &ATS =
1979 AdditionalTemplateSpecializationsPending[Record[0]];
1980 ATS.insert(ATS.end(), Record.begin()+1, Record.end());
1981 break;
1982 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001983 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00001984 First = false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001985 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001986 Error("premature end of bitstream in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001987 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001988}
1989
Sebastian Redl3e31c722010-08-18 23:56:56 +00001990ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName) {
1991 switch(ReadASTCore(FileName)) {
Sebastian Redl2abc0382010-07-16 20:41:52 +00001992 case Failure: return Failure;
1993 case IgnorePCH: return IgnorePCH;
1994 case Success: break;
1995 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001996
1997 // Here comes stuff that we only do once the entire chain is loaded.
1998
Sebastian Redl96371b42010-09-22 00:42:30 +00001999 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redlfa061442010-07-21 20:07:32 +00002000 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redlada023c2010-08-04 20:40:17 +00002001 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2002 TotalNumSelectors = 0;
Sebastian Redl9e687992010-07-19 22:06:55 +00002003 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl96371b42010-09-22 00:42:30 +00002004 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002005 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002006 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl9e687992010-07-19 22:06:55 +00002007 TotalNumTypes += Chain[I]->LocalNumTypes;
2008 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redlfa061442010-07-21 20:07:32 +00002009 TotalNumPreallocatedPreprocessingEntities +=
2010 Chain[I]->NumPreallocatedPreprocessingEntities;
2011 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redlada023c2010-08-04 20:40:17 +00002012 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl9e687992010-07-19 22:06:55 +00002013 }
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002014 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002015 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl9e687992010-07-19 22:06:55 +00002016 TypesLoaded.resize(TotalNumTypes);
2017 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redlfa061442010-07-21 20:07:32 +00002018 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2019 if (PP) {
2020 if (TotalNumIdentifiers > 0)
2021 PP->getHeaderSearchInfo().SetExternalLookup(this);
2022 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2023 if (!PP->getPreprocessingRecord())
2024 PP->createPreprocessingRecord();
2025 PP->getPreprocessingRecord()->SetExternalSource(*this,
2026 TotalNumPreallocatedPreprocessingEntities);
2027 }
2028 }
Sebastian Redlada023c2010-08-04 20:40:17 +00002029 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl96371b42010-09-22 00:42:30 +00002030 // Preload SLocEntries.
2031 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2032 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2033 if (Result != Success)
2034 return Result;
2035 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002036
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002037 // Check the predefines buffers.
Douglas Gregorce3a8292010-07-27 00:27:13 +00002038 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002039 return IgnorePCH;
2040
2041 if (PP) {
2042 // Initialization of keywords and pragmas occurs before the
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002043 // AST file is read, so there may be some identifiers that were
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002044 // loaded into the IdentifierTable before we intercepted the
2045 // creation of identifiers. Iterate through the list of known
2046 // identifiers and determine whether we have to establish
2047 // preprocessor definitions or top-level identifier declaration
2048 // chains for those identifiers.
2049 //
2050 // We copy the IdentifierInfo pointers to a small vector first,
2051 // since de-serializing declarations or macro definitions can add
2052 // new entries into the identifier table, invalidating the
2053 // iterators.
2054 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2055 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2056 IdEnd = PP->getIdentifierTable().end();
2057 Id != IdEnd; ++Id)
2058 Identifiers.push_back(Id->second);
Sebastian Redlfa061442010-07-21 20:07:32 +00002059 // We need to search the tables in all files.
Sebastian Redlfa061442010-07-21 20:07:32 +00002060 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002061 ASTIdentifierLookupTable *IdTable
2062 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2063 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl5c415f32010-07-22 17:01:13 +00002064 // ones.
2065 if (!IdTable)
2066 continue;
Sebastian Redlfa061442010-07-21 20:07:32 +00002067 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2068 IdentifierInfo *II = Identifiers[I];
2069 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002070 ASTIdentifierLookupTrait Info(*this, Chain[J]->Stream, II);
Sebastian Redlfa061442010-07-21 20:07:32 +00002071 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002072 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redlb293a452010-07-20 21:20:32 +00002073 if (Pos == IdTable->end())
2074 continue;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002075
Sebastian Redlb293a452010-07-20 21:20:32 +00002076 // Dereferencing the iterator has the effect of populating the
2077 // IdentifierInfo node with the various declarations it needs.
2078 (void)*Pos;
2079 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002080 }
2081 }
2082
2083 if (Context)
2084 InitializeContext(*Context);
2085
2086 return Success;
2087}
2088
Sebastian Redl3e31c722010-08-18 23:56:56 +00002089ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName) {
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002090 Chain.push_back(new PerFileData());
Sebastian Redl34522812010-07-16 17:50:48 +00002091 PerFileData &F = *Chain.back();
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002092
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002093 // Set the AST file name.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002094 F.FileName = FileName;
2095
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002096 // Open the AST file.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002097 //
2098 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2099 std::string ErrStr;
2100 F.Buffer.reset(llvm::MemoryBuffer::getFileOrSTDIN(FileName, &ErrStr));
2101 if (!F.Buffer) {
2102 Error(ErrStr.c_str());
2103 return IgnorePCH;
2104 }
2105
2106 // Initialize the stream
2107 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2108 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl34522812010-07-16 17:50:48 +00002109 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002110 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002111 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002112
2113 // Sniff for the signature.
2114 if (Stream.Read(8) != 'C' ||
2115 Stream.Read(8) != 'P' ||
2116 Stream.Read(8) != 'C' ||
2117 Stream.Read(8) != 'H') {
2118 Diag(diag::err_not_a_pch_file) << FileName;
2119 return Failure;
2120 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002121
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002122 while (!Stream.AtEndOfStream()) {
2123 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002124
Douglas Gregor92863e42009-04-10 23:10:45 +00002125 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002126 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002127 return Failure;
2128 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002129
2130 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002131
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002132 // We only know the AST subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002133 switch (BlockID) {
2134 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002135 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002136 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002137 return Failure;
2138 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002139 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00002140 case AST_BLOCK_ID:
Sebastian Redl3e31c722010-08-18 23:56:56 +00002141 switch (ReadASTBlock(F)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002142 case Success:
2143 break;
2144
2145 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00002146 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00002147
2148 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00002149 // FIXME: We could consider reading through to the end of this
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002150 // AST block, skipping subblocks, to see if there are other
2151 // AST blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00002152
2153 // Clear out any preallocated source location entries, so that
2154 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002155 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00002156
2157 // Remove the stat cache.
Sebastian Redl34522812010-07-16 17:50:48 +00002158 if (F.StatCache)
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002159 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00002160
Douglas Gregor92863e42009-04-10 23:10:45 +00002161 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002162 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002163 break;
2164 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002165 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002166 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002167 return Failure;
2168 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002169 break;
2170 }
Mike Stump11289f42009-09-09 15:08:12 +00002171 }
2172
Sebastian Redl2abc0382010-07-16 20:41:52 +00002173 return Success;
2174}
2175
Sebastian Redl2c499f62010-08-18 23:56:43 +00002176void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002177 PP = &pp;
Sebastian Redlfa061442010-07-21 20:07:32 +00002178
2179 unsigned TotalNum = 0;
2180 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2181 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2182 if (TotalNum) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002183 if (!PP->getPreprocessingRecord())
2184 PP->createPreprocessingRecord();
Sebastian Redlfa061442010-07-21 20:07:32 +00002185 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregoraae92242010-03-19 21:51:54 +00002186 }
2187}
2188
Sebastian Redl2c499f62010-08-18 23:56:43 +00002189void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002190 Context = &Ctx;
2191 assert(Context && "Passed null context!");
2192
2193 assert(PP && "Forgot to set Preprocessor ?");
2194 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2195 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00002196 PP->setExternalSource(this);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002197
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002198 // Load the translation unit declaration
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00002199 GetTranslationUnitDecl();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002200
2201 // Load the special types.
2202 Context->setBuiltinVaListType(
Sebastian Redl539c5062010-08-18 23:57:32 +00002203 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2204 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002205 Context->setObjCIdType(GetType(Id));
Sebastian Redl539c5062010-08-18 23:57:32 +00002206 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002207 Context->setObjCSelType(GetType(Sel));
Sebastian Redl539c5062010-08-18 23:57:32 +00002208 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002209 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl539c5062010-08-18 23:57:32 +00002210 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002211 Context->setObjCClassType(GetType(Class));
Steve Naroff7cae42b2009-07-10 23:34:53 +00002212
Sebastian Redl539c5062010-08-18 23:57:32 +00002213 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002214 Context->setCFConstantStringType(GetType(String));
Mike Stump11289f42009-09-09 15:08:12 +00002215 if (unsigned FastEnum
Sebastian Redl539c5062010-08-18 23:57:32 +00002216 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002217 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl539c5062010-08-18 23:57:32 +00002218 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregor27821ce2009-07-07 16:35:42 +00002219 QualType FileType = GetType(File);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002220 if (FileType.isNull()) {
2221 Error("FILE type is NULL");
2222 return;
2223 }
John McCall9dd450b2009-09-21 23:43:11 +00002224 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregor27821ce2009-07-07 16:35:42 +00002225 Context->setFILEDecl(Typedef->getDecl());
2226 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002227 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002228 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002229 Error("Invalid FILE type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002230 return;
2231 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002232 Context->setFILEDecl(Tag->getDecl());
2233 }
2234 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002235 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002236 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002237 if (Jmp_bufType.isNull()) {
2238 Error("jmp_bug type is NULL");
2239 return;
2240 }
John McCall9dd450b2009-09-21 23:43:11 +00002241 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002242 Context->setjmp_bufDecl(Typedef->getDecl());
2243 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002244 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002245 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002246 Error("Invalid jmp_buf type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002247 return;
2248 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002249 Context->setjmp_bufDecl(Tag->getDecl());
2250 }
2251 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002252 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002253 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002254 if (Sigjmp_bufType.isNull()) {
2255 Error("sigjmp_buf type is NULL");
2256 return;
2257 }
John McCall9dd450b2009-09-21 23:43:11 +00002258 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002259 Context->setsigjmp_bufDecl(Typedef->getDecl());
2260 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002261 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002262 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stumpa4de80b2009-07-28 02:25:19 +00002263 Context->setsigjmp_bufDecl(Tag->getDecl());
2264 }
2265 }
Mike Stump11289f42009-09-09 15:08:12 +00002266 if (unsigned ObjCIdRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002267 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002268 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump11289f42009-09-09 15:08:12 +00002269 if (unsigned ObjCClassRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002270 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002271 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002272 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpd0153282009-10-20 02:12:22 +00002273 Context->setBlockDescriptorType(GetType(String));
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002274 if (unsigned String
Sebastian Redl539c5062010-08-18 23:57:32 +00002275 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002276 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002277 if (unsigned ObjCSelRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002278 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002279 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002280 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002281 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002282
Sebastian Redl539c5062010-08-18 23:57:32 +00002283 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002284 Context->setInt128Installed();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002285}
2286
Douglas Gregor45fe0362009-05-12 01:31:05 +00002287/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002288/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00002289/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002290std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Daniel Dunbar3b951482009-12-03 09:13:06 +00002291 Diagnostic &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002292 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002293 std::string ErrStr;
2294 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002295 Buffer.reset(llvm::MemoryBuffer::getFile(ASTFileName.c_str(), &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00002296 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00002297 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002298 return std::string();
2299 }
2300
2301 // Initialize the stream
2302 llvm::BitstreamReader StreamFile;
2303 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00002304 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00002305 (const unsigned char *)Buffer->getBufferEnd());
2306 Stream.init(StreamFile);
2307
2308 // Sniff for the signature.
2309 if (Stream.Read(8) != 'C' ||
2310 Stream.Read(8) != 'P' ||
2311 Stream.Read(8) != 'C' ||
2312 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002313 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002314 return std::string();
2315 }
2316
2317 RecordData Record;
2318 while (!Stream.AtEndOfStream()) {
2319 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002320
Douglas Gregor45fe0362009-05-12 01:31:05 +00002321 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2322 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00002323
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002324 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002325 switch (BlockID) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002326 case AST_BLOCK_ID:
2327 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002328 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002329 return std::string();
2330 }
2331 break;
Mike Stump11289f42009-09-09 15:08:12 +00002332
Douglas Gregor45fe0362009-05-12 01:31:05 +00002333 default:
2334 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002335 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002336 return std::string();
2337 }
2338 break;
2339 }
2340 continue;
2341 }
2342
2343 if (Code == llvm::bitc::END_BLOCK) {
2344 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002345 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002346 return std::string();
2347 }
2348 continue;
2349 }
2350
2351 if (Code == llvm::bitc::DEFINE_ABBREV) {
2352 Stream.ReadAbbrevRecord();
2353 continue;
2354 }
2355
2356 Record.clear();
2357 const char *BlobStart = 0;
2358 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002359 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl539c5062010-08-18 23:57:32 +00002360 == ORIGINAL_FILE_NAME)
Douglas Gregor45fe0362009-05-12 01:31:05 +00002361 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00002362 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00002363
2364 return std::string();
2365}
2366
Douglas Gregor55abb232009-04-10 20:39:37 +00002367/// \brief Parse the record that corresponds to a LangOptions data
2368/// structure.
2369///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002370/// This routine parses the language options from the AST file and then gives
2371/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00002372///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002373/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002374bool ASTReader::ParseLanguageOptions(
Douglas Gregor55abb232009-04-10 20:39:37 +00002375 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002376 if (Listener) {
2377 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00002378
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002379 #define PARSE_LANGOPT(Option) \
2380 LangOpts.Option = Record[Idx]; \
2381 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00002382
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002383 unsigned Idx = 0;
2384 PARSE_LANGOPT(Trigraphs);
2385 PARSE_LANGOPT(BCPLComment);
2386 PARSE_LANGOPT(DollarIdents);
2387 PARSE_LANGOPT(AsmPreprocessor);
2388 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00002389 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002390 PARSE_LANGOPT(ImplicitInt);
2391 PARSE_LANGOPT(Digraphs);
2392 PARSE_LANGOPT(HexFloats);
2393 PARSE_LANGOPT(C99);
2394 PARSE_LANGOPT(Microsoft);
2395 PARSE_LANGOPT(CPlusPlus);
2396 PARSE_LANGOPT(CPlusPlus0x);
2397 PARSE_LANGOPT(CXXOperatorNames);
2398 PARSE_LANGOPT(ObjC1);
2399 PARSE_LANGOPT(ObjC2);
2400 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00002401 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00002402 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002403 PARSE_LANGOPT(PascalStrings);
2404 PARSE_LANGOPT(WritableStrings);
2405 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00002406 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002407 PARSE_LANGOPT(Exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +00002408 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002409 PARSE_LANGOPT(NeXTRuntime);
2410 PARSE_LANGOPT(Freestanding);
2411 PARSE_LANGOPT(NoBuiltin);
2412 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00002413 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002414 PARSE_LANGOPT(Blocks);
2415 PARSE_LANGOPT(EmitAllDecls);
2416 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00002417 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2418 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002419 PARSE_LANGOPT(HeinousExtensions);
2420 PARSE_LANGOPT(Optimize);
2421 PARSE_LANGOPT(OptimizeSize);
2422 PARSE_LANGOPT(Static);
2423 PARSE_LANGOPT(PICLevel);
2424 PARSE_LANGOPT(GNUInline);
2425 PARSE_LANGOPT(NoInline);
2426 PARSE_LANGOPT(AccessControl);
2427 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00002428 PARSE_LANGOPT(ShortWChar);
Chris Lattner51924e512010-06-26 21:25:03 +00002429 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
2430 LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00002431 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00002432 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002433 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00002434 PARSE_LANGOPT(OpenCL);
Mike Stumpd9546382009-12-12 01:27:46 +00002435 PARSE_LANGOPT(CatchUndefined);
2436 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002437 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00002438
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002439 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00002440 }
Douglas Gregor55abb232009-04-10 20:39:37 +00002441
2442 return false;
2443}
2444
Sebastian Redl2c499f62010-08-18 23:56:43 +00002445void ASTReader::ReadPreprocessedEntities() {
Douglas Gregoraae92242010-03-19 21:51:54 +00002446 ReadDefinedMacros();
2447}
2448
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002449/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002450ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002451 PerFileData *F = 0;
2452 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2453 F = Chain[N - I - 1];
2454 if (Index < F->LocalNumTypes)
2455 break;
2456 Index -= F->LocalNumTypes;
2457 }
2458 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redlb2831db2010-07-20 22:55:31 +00002459 return RecordLocation(&F->DeclsCursor, F->TypeOffsets[Index]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002460}
2461
2462/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002463///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002464/// The index is the type ID, shifted and minus the number of predefs. This
2465/// routine actually reads the record corresponding to the type at the given
2466/// location. It is a helper routine for GetType, which deals with reading type
2467/// IDs.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002468QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002469 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlb2831db2010-07-20 22:55:31 +00002470 llvm::BitstreamCursor &DeclsCursor = *Loc.first;
Sebastian Redl34522812010-07-16 17:50:48 +00002471
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002472 // Keep track of where we are in the stream, then jump back there
2473 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002474 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002475
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002476 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00002477
Douglas Gregor1342e842009-07-06 18:54:52 +00002478 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00002479 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00002480
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002481 DeclsCursor.JumpToBit(Loc.second);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002482 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002483 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00002484 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2485 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002486 if (Record.size() != 2) {
2487 Error("Incorrect encoding of extended qualifier type");
2488 return QualType();
2489 }
Douglas Gregor455b8f42009-04-15 22:00:08 +00002490 QualType Base = GetType(Record[0]);
John McCall8ccfcb52009-09-24 19:53:00 +00002491 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2492 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00002493 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002494
Sebastian Redl539c5062010-08-18 23:57:32 +00002495 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002496 if (Record.size() != 1) {
2497 Error("Incorrect encoding of complex type");
2498 return QualType();
2499 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002500 QualType ElemType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002501 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002502 }
2503
Sebastian Redl539c5062010-08-18 23:57:32 +00002504 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002505 if (Record.size() != 1) {
2506 Error("Incorrect encoding of pointer type");
2507 return QualType();
2508 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002509 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002510 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002511 }
2512
Sebastian Redl539c5062010-08-18 23:57:32 +00002513 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002514 if (Record.size() != 1) {
2515 Error("Incorrect encoding of block pointer type");
2516 return QualType();
2517 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002518 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002519 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002520 }
2521
Sebastian Redl539c5062010-08-18 23:57:32 +00002522 case TYPE_LVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002523 if (Record.size() != 1) {
2524 Error("Incorrect encoding of lvalue reference type");
2525 return QualType();
2526 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002527 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002528 return Context->getLValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002529 }
2530
Sebastian Redl539c5062010-08-18 23:57:32 +00002531 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002532 if (Record.size() != 1) {
2533 Error("Incorrect encoding of rvalue reference type");
2534 return QualType();
2535 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002536 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002537 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002538 }
2539
Sebastian Redl539c5062010-08-18 23:57:32 +00002540 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00002541 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002542 Error("Incorrect encoding of member pointer type");
2543 return QualType();
2544 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002545 QualType PointeeType = GetType(Record[0]);
2546 QualType ClassType = GetType(Record[1]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002547 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002548 }
2549
Sebastian Redl539c5062010-08-18 23:57:32 +00002550 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002551 QualType ElementType = GetType(Record[0]);
2552 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2553 unsigned IndexTypeQuals = Record[2];
2554 unsigned Idx = 3;
2555 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00002556 return Context->getConstantArrayType(ElementType, Size,
2557 ASM, IndexTypeQuals);
2558 }
2559
Sebastian Redl539c5062010-08-18 23:57:32 +00002560 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002561 QualType ElementType = GetType(Record[0]);
2562 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2563 unsigned IndexTypeQuals = Record[2];
Chris Lattner8575daa2009-04-27 21:45:14 +00002564 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002565 }
2566
Sebastian Redl539c5062010-08-18 23:57:32 +00002567 case TYPE_VARIABLE_ARRAY: {
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002568 QualType ElementType = GetType(Record[0]);
2569 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2570 unsigned IndexTypeQuals = Record[2];
Douglas Gregor04318252009-07-06 15:59:29 +00002571 SourceLocation LBLoc = SourceLocation::getFromRawEncoding(Record[3]);
2572 SourceLocation RBLoc = SourceLocation::getFromRawEncoding(Record[4]);
Sebastian Redlc67764e2010-07-22 22:43:28 +00002573 return Context->getVariableArrayType(ElementType, ReadExpr(DeclsCursor),
Douglas Gregor04318252009-07-06 15:59:29 +00002574 ASM, IndexTypeQuals,
2575 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002576 }
2577
Sebastian Redl539c5062010-08-18 23:57:32 +00002578 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002579 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002580 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002581 return QualType();
2582 }
2583
2584 QualType ElementType = GetType(Record[0]);
2585 unsigned NumElements = Record[1];
Chris Lattner37141f42010-06-23 06:00:24 +00002586 unsigned AltiVecSpec = Record[2];
2587 return Context->getVectorType(ElementType, NumElements,
2588 (VectorType::AltiVecSpecific)AltiVecSpec);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002589 }
2590
Sebastian Redl539c5062010-08-18 23:57:32 +00002591 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002592 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002593 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002594 return QualType();
2595 }
2596
2597 QualType ElementType = GetType(Record[0]);
2598 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00002599 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002600 }
2601
Sebastian Redl539c5062010-08-18 23:57:32 +00002602 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002603 if (Record.size() != 4) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002604 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002605 return QualType();
2606 }
2607 QualType ResultType = GetType(Record[0]);
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002608 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00002609 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002610 }
2611
Sebastian Redl539c5062010-08-18 23:57:32 +00002612 case TYPE_FUNCTION_PROTO: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002613 QualType ResultType = GetType(Record[0]);
Douglas Gregordc728752009-12-22 18:11:50 +00002614 bool NoReturn = Record[1];
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002615 unsigned RegParm = Record[2];
2616 CallingConv CallConv = (CallingConv)Record[3];
2617 unsigned Idx = 4;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002618 unsigned NumParams = Record[Idx++];
2619 llvm::SmallVector<QualType, 16> ParamTypes;
2620 for (unsigned I = 0; I != NumParams; ++I)
2621 ParamTypes.push_back(GetType(Record[Idx++]));
2622 bool isVariadic = Record[Idx++];
2623 unsigned Quals = Record[Idx++];
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002624 bool hasExceptionSpec = Record[Idx++];
2625 bool hasAnyExceptionSpec = Record[Idx++];
2626 unsigned NumExceptions = Record[Idx++];
2627 llvm::SmallVector<QualType, 2> Exceptions;
2628 for (unsigned I = 0; I != NumExceptions; ++I)
2629 Exceptions.push_back(GetType(Record[Idx++]));
Jay Foad7d0479f2009-05-21 09:52:38 +00002630 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002631 isVariadic, Quals, hasExceptionSpec,
2632 hasAnyExceptionSpec, NumExceptions,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00002633 Exceptions.data(),
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002634 FunctionType::ExtInfo(NoReturn, RegParm,
2635 CallConv));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002636 }
2637
Sebastian Redl539c5062010-08-18 23:57:32 +00002638 case TYPE_UNRESOLVED_USING:
John McCallb96ec562009-12-04 22:46:56 +00002639 return Context->getTypeDeclType(
2640 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2641
Sebastian Redl539c5062010-08-18 23:57:32 +00002642 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002643 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002644 Error("incorrect encoding of typedef type");
2645 return QualType();
2646 }
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002647 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2648 QualType Canonical = GetType(Record[1]);
2649 return Context->getTypedefType(Decl, Canonical);
2650 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002651
Sebastian Redl539c5062010-08-18 23:57:32 +00002652 case TYPE_TYPEOF_EXPR:
Sebastian Redlc67764e2010-07-22 22:43:28 +00002653 return Context->getTypeOfExprType(ReadExpr(DeclsCursor));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002654
Sebastian Redl539c5062010-08-18 23:57:32 +00002655 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002656 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002657 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002658 return QualType();
2659 }
2660 QualType UnderlyingType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002661 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002662 }
Mike Stump11289f42009-09-09 15:08:12 +00002663
Sebastian Redl539c5062010-08-18 23:57:32 +00002664 case TYPE_DECLTYPE:
Sebastian Redlc67764e2010-07-22 22:43:28 +00002665 return Context->getDecltypeType(ReadExpr(DeclsCursor));
Anders Carlsson81df7b82009-06-24 19:06:50 +00002666
Sebastian Redl539c5062010-08-18 23:57:32 +00002667 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002668 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002669 Error("incorrect encoding of record type");
2670 return QualType();
2671 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002672 bool IsDependent = Record[0];
2673 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
2674 T->Dependent = IsDependent;
2675 return T;
2676 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002677
Sebastian Redl539c5062010-08-18 23:57:32 +00002678 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002679 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002680 Error("incorrect encoding of enum type");
2681 return QualType();
2682 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002683 bool IsDependent = Record[0];
2684 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
2685 T->Dependent = IsDependent;
2686 return T;
2687 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00002688
Sebastian Redl539c5062010-08-18 23:57:32 +00002689 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002690 unsigned Idx = 0;
2691 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2692 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2693 QualType NamedType = GetType(Record[Idx++]);
2694 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00002695 }
2696
Sebastian Redl539c5062010-08-18 23:57:32 +00002697 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00002698 unsigned Idx = 0;
2699 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00002700 return Context->getObjCInterfaceType(ItfD);
2701 }
2702
Sebastian Redl539c5062010-08-18 23:57:32 +00002703 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00002704 unsigned Idx = 0;
2705 QualType Base = GetType(Record[Idx++]);
Chris Lattner587cbe12009-04-22 06:45:28 +00002706 unsigned NumProtos = Record[Idx++];
2707 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2708 for (unsigned I = 0; I != NumProtos; ++I)
2709 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
John McCall8b07ec22010-05-15 11:32:37 +00002710 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00002711 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002712
Sebastian Redl539c5062010-08-18 23:57:32 +00002713 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00002714 unsigned Idx = 0;
John McCall8b07ec22010-05-15 11:32:37 +00002715 QualType Pointee = GetType(Record[Idx++]);
2716 return Context->getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00002717 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00002718
Sebastian Redl539c5062010-08-18 23:57:32 +00002719 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00002720 unsigned Idx = 0;
2721 QualType Parm = GetType(Record[Idx++]);
2722 QualType Replacement = GetType(Record[Idx++]);
2723 return
2724 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2725 Replacement);
2726 }
John McCalle78aac42010-03-10 03:28:59 +00002727
Sebastian Redl539c5062010-08-18 23:57:32 +00002728 case TYPE_INJECTED_CLASS_NAME: {
John McCalle78aac42010-03-10 03:28:59 +00002729 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2730 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00002731 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002732 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00002733 return
2734 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00002735 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002736
Sebastian Redl539c5062010-08-18 23:57:32 +00002737 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002738 unsigned Idx = 0;
2739 unsigned Depth = Record[Idx++];
2740 unsigned Index = Record[Idx++];
2741 bool Pack = Record[Idx++];
2742 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
2743 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
2744 }
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002745
Sebastian Redl539c5062010-08-18 23:57:32 +00002746 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002747 unsigned Idx = 0;
2748 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2749 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2750 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00002751 QualType Canon = GetType(Record[Idx++]);
2752 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002753 }
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002754
Sebastian Redl539c5062010-08-18 23:57:32 +00002755 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002756 unsigned Idx = 0;
2757 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2758 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2759 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2760 unsigned NumArgs = Record[Idx++];
2761 llvm::SmallVector<TemplateArgument, 8> Args;
2762 Args.reserve(NumArgs);
2763 while (NumArgs--)
Sebastian Redlc67764e2010-07-22 22:43:28 +00002764 Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002765 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
2766 Args.size(), Args.data());
2767 }
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00002768
Sebastian Redl539c5062010-08-18 23:57:32 +00002769 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00002770 unsigned Idx = 0;
2771
2772 // ArrayType
2773 QualType ElementType = GetType(Record[Idx++]);
2774 ArrayType::ArraySizeModifier ASM
2775 = (ArrayType::ArraySizeModifier)Record[Idx++];
2776 unsigned IndexTypeQuals = Record[Idx++];
2777
2778 // DependentSizedArrayType
Sebastian Redlc67764e2010-07-22 22:43:28 +00002779 Expr *NumElts = ReadExpr(DeclsCursor);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00002780 SourceRange Brackets = ReadSourceRange(Record, Idx);
2781
2782 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
2783 IndexTypeQuals, Brackets);
2784 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002785
Sebastian Redl539c5062010-08-18 23:57:32 +00002786 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002787 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002788 bool IsDependent = Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002789 TemplateName Name = ReadTemplateName(Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002790 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc67764e2010-07-22 22:43:28 +00002791 ReadTemplateArgumentList(Args, DeclsCursor, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002792 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002793 QualType T;
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002794 if (Canon.isNull())
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002795 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
2796 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002797 else
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002798 T = Context->getTemplateSpecializationType(Name, Args.data(),
2799 Args.size(), Canon);
2800 T->Dependent = IsDependent;
2801 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002802 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002803 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002804 // Suppress a GCC warning
2805 return QualType();
2806}
2807
John McCall8f115c62009-10-16 21:56:05 +00002808namespace {
2809
2810class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00002811 ASTReader &Reader;
Sebastian Redlc67764e2010-07-22 22:43:28 +00002812 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redl2c499f62010-08-18 23:56:43 +00002813 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00002814 unsigned &Idx;
2815
2816public:
Sebastian Redl2c499f62010-08-18 23:56:43 +00002817 TypeLocReader(ASTReader &Reader, llvm::BitstreamCursor &Cursor,
2818 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redlc67764e2010-07-22 22:43:28 +00002819 : Reader(Reader), DeclsCursor(Cursor), Record(Record), Idx(Idx) { }
John McCall8f115c62009-10-16 21:56:05 +00002820
John McCall17001972009-10-18 01:05:36 +00002821 // We want compile-time assurance that we've enumerated all of
2822 // these, so unfortunately we have to declare them first, then
2823 // define them out-of-line.
2824#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00002825#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00002826 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00002827#include "clang/AST/TypeLocNodes.def"
2828
John McCall17001972009-10-18 01:05:36 +00002829 void VisitFunctionTypeLoc(FunctionTypeLoc);
2830 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00002831};
2832
2833}
2834
John McCall17001972009-10-18 01:05:36 +00002835void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00002836 // nothing to do
2837}
John McCall17001972009-10-18 01:05:36 +00002838void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +00002839 TL.setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2840 if (TL.needsExtraLocalData()) {
2841 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
2842 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
2843 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
2844 TL.setModeAttr(Record[Idx++]);
2845 }
John McCall8f115c62009-10-16 21:56:05 +00002846}
John McCall17001972009-10-18 01:05:36 +00002847void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
2848 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002849}
John McCall17001972009-10-18 01:05:36 +00002850void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
2851 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002852}
John McCall17001972009-10-18 01:05:36 +00002853void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
2854 TL.setCaretLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002855}
John McCall17001972009-10-18 01:05:36 +00002856void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
2857 TL.setAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002858}
John McCall17001972009-10-18 01:05:36 +00002859void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
2860 TL.setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002861}
John McCall17001972009-10-18 01:05:36 +00002862void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
2863 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002864}
John McCall17001972009-10-18 01:05:36 +00002865void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
2866 TL.setLBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2867 TL.setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002868 if (Record[Idx++])
Sebastian Redlc67764e2010-07-22 22:43:28 +00002869 TL.setSizeExpr(Reader.ReadExpr(DeclsCursor));
Douglas Gregor12bfa382009-10-17 00:13:19 +00002870 else
John McCall17001972009-10-18 01:05:36 +00002871 TL.setSizeExpr(0);
2872}
2873void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
2874 VisitArrayTypeLoc(TL);
2875}
2876void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
2877 VisitArrayTypeLoc(TL);
2878}
2879void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
2880 VisitArrayTypeLoc(TL);
2881}
2882void TypeLocReader::VisitDependentSizedArrayTypeLoc(
2883 DependentSizedArrayTypeLoc TL) {
2884 VisitArrayTypeLoc(TL);
2885}
2886void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
2887 DependentSizedExtVectorTypeLoc TL) {
2888 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2889}
2890void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
2891 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2892}
2893void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
2894 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2895}
2896void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
2897 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2898 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2899 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCalle6347002009-10-23 01:28:53 +00002900 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall17001972009-10-18 01:05:36 +00002901 }
2902}
2903void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
2904 VisitFunctionTypeLoc(TL);
2905}
2906void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
2907 VisitFunctionTypeLoc(TL);
2908}
John McCallb96ec562009-12-04 22:46:56 +00002909void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
2910 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2911}
John McCall17001972009-10-18 01:05:36 +00002912void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
2913 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2914}
2915void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +00002916 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2917 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2918 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall17001972009-10-18 01:05:36 +00002919}
2920void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +00002921 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2922 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2923 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Sebastian Redlc67764e2010-07-22 22:43:28 +00002924 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(DeclsCursor, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002925}
2926void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
2927 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2928}
2929void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
2930 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2931}
2932void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
2933 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2934}
John McCall17001972009-10-18 01:05:36 +00002935void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
2936 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2937}
John McCallcebee162009-10-18 09:09:24 +00002938void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
2939 SubstTemplateTypeParmTypeLoc TL) {
2940 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2941}
John McCall17001972009-10-18 01:05:36 +00002942void TypeLocReader::VisitTemplateSpecializationTypeLoc(
2943 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +00002944 TL.setTemplateNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2945 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2946 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2947 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
2948 TL.setArgLocInfo(i,
2949 Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(i).getKind(),
Sebastian Redlc67764e2010-07-22 22:43:28 +00002950 DeclsCursor, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002951}
Abramo Bagnara6150c882010-05-11 21:36:43 +00002952void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +00002953 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2954 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002955}
John McCalle78aac42010-03-10 03:28:59 +00002956void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
2957 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2958}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002959void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +00002960 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2961 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002962 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2963}
John McCallc392f372010-06-11 00:33:02 +00002964void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
2965 DependentTemplateSpecializationTypeLoc TL) {
2966 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2967 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
2968 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2969 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2970 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2971 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
2972 TL.setArgLocInfo(I,
2973 Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(I).getKind(),
Sebastian Redlc67764e2010-07-22 22:43:28 +00002974 DeclsCursor, Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00002975}
John McCall17001972009-10-18 01:05:36 +00002976void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
2977 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00002978}
2979void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
2980 TL.setHasBaseTypeAsWritten(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00002981 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2982 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2983 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
2984 TL.setProtocolLoc(i, SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002985}
John McCallfc93cf92009-10-22 22:37:11 +00002986void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
2987 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCallfc93cf92009-10-22 22:37:11 +00002988}
John McCall8f115c62009-10-16 21:56:05 +00002989
Sebastian Redl2c499f62010-08-18 23:56:43 +00002990TypeSourceInfo *ASTReader::GetTypeSourceInfo(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redlc67764e2010-07-22 22:43:28 +00002991 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00002992 unsigned &Idx) {
2993 QualType InfoTy = GetType(Record[Idx++]);
2994 if (InfoTy.isNull())
2995 return 0;
2996
John McCallbcd03502009-12-07 02:54:59 +00002997 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redlc67764e2010-07-22 22:43:28 +00002998 TypeLocReader TLR(*this, DeclsCursor, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00002999 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00003000 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00003001 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00003002}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003003
Sebastian Redl539c5062010-08-18 23:57:32 +00003004QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00003005 unsigned FastQuals = ID & Qualifiers::FastMask;
3006 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003007
Sebastian Redl539c5062010-08-18 23:57:32 +00003008 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003009 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00003010 switch ((PredefinedTypeIDs)Index) {
3011 case PREDEF_TYPE_NULL_ID: return QualType();
3012 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3013 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003014
Sebastian Redl539c5062010-08-18 23:57:32 +00003015 case PREDEF_TYPE_CHAR_U_ID:
3016 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003017 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00003018 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003019 break;
3020
Sebastian Redl539c5062010-08-18 23:57:32 +00003021 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3022 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3023 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3024 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3025 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3026 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3027 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3028 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3029 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3030 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3031 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3032 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3033 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3034 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3035 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3036 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3037 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3038 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3039 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3040 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3041 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3042 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3043 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3044 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003045 }
3046
3047 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00003048 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003049 }
3050
Sebastian Redl539c5062010-08-18 23:57:32 +00003051 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003052 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00003053 if (TypesLoaded[Index].isNull()) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003054 TypesLoaded[Index] = ReadTypeRecord(Index);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003055 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003056 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003057 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003058 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003059 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00003060 }
Mike Stump11289f42009-09-09 15:08:12 +00003061
John McCall8ccfcb52009-09-24 19:53:00 +00003062 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003063}
3064
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003065TypeID ASTReader::GetTypeID(QualType T) const {
3066 return MakeTypeID(T,
3067 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3068}
3069
3070TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3071 if (T.isNull())
3072 return TypeIdx();
3073 assert(!T.getLocalFastQualifiers());
3074
3075 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3076 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3077 // comparing keys of ASTDeclContextNameLookupTable.
3078 // If the type didn't come from the AST file use a specially marked index
3079 // so that any hash/key comparison fail since no such index is stored
3080 // in a AST file.
3081 if (I == TypeIdxs.end())
3082 return TypeIdx(-1);
3083 return I->second;
3084}
3085
John McCall0ad16662009-10-29 08:12:44 +00003086TemplateArgumentLocInfo
Sebastian Redl2c499f62010-08-18 23:56:43 +00003087ASTReader::GetTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003088 llvm::BitstreamCursor &DeclsCursor,
John McCall0ad16662009-10-29 08:12:44 +00003089 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003090 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00003091 switch (Kind) {
3092 case TemplateArgument::Expression:
Sebastian Redlc67764e2010-07-22 22:43:28 +00003093 return ReadExpr(DeclsCursor);
John McCall0ad16662009-10-29 08:12:44 +00003094 case TemplateArgument::Type:
Sebastian Redlc67764e2010-07-22 22:43:28 +00003095 return GetTypeSourceInfo(DeclsCursor, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003096 case TemplateArgument::Template: {
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003097 SourceRange QualifierRange = ReadSourceRange(Record, Index);
3098 SourceLocation TemplateNameLoc = ReadSourceLocation(Record, Index);
3099 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003100 }
John McCall0ad16662009-10-29 08:12:44 +00003101 case TemplateArgument::Null:
3102 case TemplateArgument::Integral:
3103 case TemplateArgument::Declaration:
3104 case TemplateArgument::Pack:
3105 return TemplateArgumentLocInfo();
3106 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00003107 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00003108 return TemplateArgumentLocInfo();
3109}
3110
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003111TemplateArgumentLoc
Sebastian Redl2c499f62010-08-18 23:56:43 +00003112ASTReader::ReadTemplateArgumentLoc(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003113 const RecordData &Record, unsigned &Index) {
3114 TemplateArgument Arg = ReadTemplateArgument(DeclsCursor, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003115
3116 if (Arg.getKind() == TemplateArgument::Expression) {
3117 if (Record[Index++]) // bool InfoHasSameExpr.
3118 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3119 }
3120 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(Arg.getKind(),
Sebastian Redlc67764e2010-07-22 22:43:28 +00003121 DeclsCursor,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003122 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003123}
3124
Sebastian Redl2c499f62010-08-18 23:56:43 +00003125Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00003126 return GetDecl(ID);
3127}
3128
Sebastian Redl2c499f62010-08-18 23:56:43 +00003129TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003130 if (!DeclsLoaded[0]) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00003131 ReadDeclRecord(0, 1);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003132 if (DeserializationListener)
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003133 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003134 }
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00003135
3136 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3137}
3138
Sebastian Redl539c5062010-08-18 23:57:32 +00003139Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003140 if (ID == 0)
3141 return 0;
3142
Douglas Gregor745ed142009-04-25 18:35:21 +00003143 if (ID > DeclsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003144 Error("declaration ID out-of-range for AST file");
Douglas Gregor745ed142009-04-25 18:35:21 +00003145 return 0;
3146 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003147
Douglas Gregor745ed142009-04-25 18:35:21 +00003148 unsigned Index = ID - 1;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003149 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003150 ReadDeclRecord(Index, ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003151 if (DeserializationListener)
3152 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3153 }
Douglas Gregor745ed142009-04-25 18:35:21 +00003154
3155 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003156}
3157
Chris Lattner9c28af02009-04-27 05:46:25 +00003158/// \brief Resolve the offset of a statement into a statement.
3159///
3160/// This operation will read a new statement from the external
3161/// source each time it is called, and is meant to be used via a
3162/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00003163Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Sebastian Redl5c415f32010-07-22 17:01:13 +00003164 // Offset here is a global offset across the entire chain.
3165 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3166 PerFileData &F = *Chain[N - I - 1];
3167 if (Offset < F.SizeInBits) {
3168 // Since we know that this statement is part of a decl, make sure to use
3169 // the decl cursor to read it.
3170 F.DeclsCursor.JumpToBit(Offset);
3171 return ReadStmtFromStream(F.DeclsCursor);
3172 }
3173 Offset -= F.SizeInBits;
3174 }
3175 llvm_unreachable("Broken chain");
Douglas Gregor3c3aa612009-04-18 00:07:54 +00003176}
3177
Sebastian Redl2c499f62010-08-18 23:56:43 +00003178bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00003179 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump11289f42009-09-09 15:08:12 +00003180 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003181 "DeclContext has no lexical decls in storage");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003182
Sebastian Redl5c415f32010-07-22 17:01:13 +00003183 // There might be lexical decls in multiple parts of the chain, for the TU
3184 // at least.
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003185 // DeclContextOffsets might reallocate as we load additional decls below,
3186 // so make a copy of the vector.
3187 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl5c415f32010-07-22 17:01:13 +00003188 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3189 I != E; ++I) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003190 // IDs can be 0 if this context doesn't contain declarations.
3191 if (!I->LexicalDecls)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003192 continue;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003193
3194 // Load all of the declaration IDs
Sebastian Redl4102dd52010-09-28 02:55:49 +00003195 for (const DeclID *ID = I->LexicalDecls, *IDE = ID + I->NumLexicalDecls;
3196 ID != IDE; ++ID) {
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003197 Decl *D = GetDecl(*ID);
3198 assert(D && "Null decl in lexical decls");
3199 Decls.push_back(D);
3200 }
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003201 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003202
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003203 ++NumLexicalDeclContextsRead;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003204 return false;
3205}
3206
John McCall75b960e2010-06-01 09:23:16 +00003207DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00003208ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00003209 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003210 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003211 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003212 if (!Name)
3213 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3214 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003215
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003216 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl471ac2f2010-08-24 00:49:55 +00003217 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003218 // and namespaces. For any given name, the last available results replace
3219 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl5c415f32010-07-22 17:01:13 +00003220 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003221 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl5c415f32010-07-22 17:01:13 +00003222 I != E; ++I) {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003223 if (!I->NameLookupTableData)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003224 continue;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003225
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003226 ASTDeclContextNameLookupTable *LookupTable =
3227 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3228 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3229 if (Pos == LookupTable->end())
Sebastian Redl5c415f32010-07-22 17:01:13 +00003230 continue;
3231
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003232 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3233 for (; Data.first != Data.second; ++Data.first)
3234 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003235 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003236 }
3237
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003238 ++NumVisibleDeclContextsRead;
John McCall75b960e2010-06-01 09:23:16 +00003239
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003240 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00003241 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003242}
3243
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00003244void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3245 assert(DC->hasExternalVisibleStorage() &&
3246 "DeclContext has no visible decls in storage");
3247
3248 llvm::SmallVector<NamedDecl *, 64> Decls;
3249 // There might be visible decls in multiple parts of the chain, for the TU
3250 // and namespaces.
3251 DeclContextInfos &Infos = DeclContextOffsets[DC];
3252 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3253 I != E; ++I) {
3254 if (!I->NameLookupTableData)
3255 continue;
3256
3257 ASTDeclContextNameLookupTable *LookupTable =
3258 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3259 for (ASTDeclContextNameLookupTable::item_iterator
3260 ItemI = LookupTable->item_begin(),
3261 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3262 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3263 = *ItemI;
3264 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3265 Decls.clear();
3266 for (; Data.first != Data.second; ++Data.first)
3267 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3268 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3269 }
3270 }
3271}
3272
Sebastian Redl2c499f62010-08-18 23:56:43 +00003273void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003274 assert(Consumer);
3275 while (!InterestingDecls.empty()) {
3276 DeclGroupRef DG(InterestingDecls.front());
3277 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00003278 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003279 }
3280}
3281
Sebastian Redl2c499f62010-08-18 23:56:43 +00003282void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00003283 this->Consumer = Consumer;
3284
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003285 if (!Consumer)
3286 return;
3287
3288 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003289 // Force deserialization of this decl, which will cause it to be queued for
3290 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00003291 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003292 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00003293
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003294 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003295}
3296
Sebastian Redl2c499f62010-08-18 23:56:43 +00003297void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003298 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003299
Mike Stump11289f42009-09-09 15:08:12 +00003300 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003301 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00003302 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00003303 unsigned NumDeclsLoaded
3304 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3305 (Decl *)0);
3306 unsigned NumIdentifiersLoaded
3307 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3308 IdentifiersLoaded.end(),
3309 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00003310 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003311 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3312 SelectorsLoaded.end(),
3313 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00003314
Douglas Gregorc5046832009-04-27 18:38:38 +00003315 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3316 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor258ae542009-04-27 06:38:32 +00003317 if (TotalNumSLocEntries)
3318 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3319 NumSLocEntriesRead, TotalNumSLocEntries,
3320 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00003321 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003322 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003323 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3324 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3325 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003326 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003327 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3328 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00003329 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003330 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00003331 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3332 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00003333 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003334 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00003335 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3336 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003337 if (TotalNumStatements)
3338 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3339 NumStatementsRead, TotalNumStatements,
3340 ((float)NumStatementsRead/TotalNumStatements * 100));
3341 if (TotalNumMacros)
3342 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3343 NumMacrosRead, TotalNumMacros,
3344 ((float)NumMacrosRead/TotalNumMacros * 100));
3345 if (TotalLexicalDeclContexts)
3346 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3347 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3348 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3349 * 100));
3350 if (TotalVisibleDeclContexts)
3351 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3352 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3353 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3354 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003355 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003356 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003357 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3358 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00003359 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003360 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003361 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003362 std::fprintf(stderr, "\n");
3363}
3364
Sebastian Redl2c499f62010-08-18 23:56:43 +00003365void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00003366 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003367 S.ExternalSource = this;
3368
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003369 // Makes sure any declarations that were deserialized "too early"
3370 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003371 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3372 if (SemaObj->TUScope)
John McCall48871652010-08-21 09:40:31 +00003373 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003374
3375 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003376 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003377 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00003378
3379 // If there were any tentative definitions, deserialize them and add
Sebastian Redl35351a92010-01-31 22:27:38 +00003380 // them to Sema's list of tentative definitions.
Douglas Gregord4df8652009-04-22 22:02:47 +00003381 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3382 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redl35351a92010-01-31 22:27:38 +00003383 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregord4df8652009-04-22 22:02:47 +00003384 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00003385
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003386 // If there were any unused file scoped decls, deserialize them and add to
3387 // Sema's list of unused file scoped decls.
3388 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3389 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3390 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattner90073802010-02-12 00:07:30 +00003391 }
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003392
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003393 // If there were any weak undeclared identifiers, deserialize them and add to
3394 // Sema's list of weak undeclared identifiers.
3395 if (!WeakUndeclaredIdentifiers.empty()) {
3396 unsigned Idx = 0;
3397 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3398 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3399 IdentifierInfo *AliasId=GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3400 SourceLocation Loc = ReadSourceLocation(WeakUndeclaredIdentifiers, Idx);
3401 bool Used = WeakUndeclaredIdentifiers[Idx++];
3402 Sema::WeakInfo WI(AliasId, Loc);
3403 WI.setUsed(Used);
3404 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3405 }
3406 }
3407
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003408 // If there were any locally-scoped external declarations,
3409 // deserialize them and add them to Sema's table of locally-scoped
3410 // external declarations.
3411 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3412 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3413 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3414 }
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003415
3416 // If there were any ext_vector type declarations, deserialize them
3417 // and add them to Sema's vector of such declarations.
3418 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3419 SemaObj->ExtVectorDecls.push_back(
3420 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003421
3422 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3423 // Can we cut them down before writing them ?
3424
3425 // If there were any VTable uses, deserialize the information and add it
3426 // to Sema's vector and map of VTable uses.
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003427 if (!VTableUses.empty()) {
3428 unsigned Idx = 0;
3429 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3430 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3431 SourceLocation Loc = ReadSourceLocation(VTableUses, Idx);
3432 bool DefinitionRequired = VTableUses[Idx++];
3433 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3434 SemaObj->VTablesUsed[Class] = DefinitionRequired;
3435 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003436 }
3437
3438 // If there were any dynamic classes declarations, deserialize them
3439 // and add them to Sema's vector of such declarations.
3440 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3441 SemaObj->DynamicClasses.push_back(
3442 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003443
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003444 // If there were any pending implicit instantiations, deserialize them
3445 // and add them to Sema's queue of such instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003446 assert(PendingInstantiations.size() % 2 == 0 && "Expected pairs of entries");
3447 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
3448 ValueDecl *D=cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
3449 SourceLocation Loc = ReadSourceLocation(PendingInstantiations, Idx);
3450 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003451 }
3452
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003453 // Load the offsets of the declarations that Sema references.
3454 // They will be lazily deserialized when needed.
3455 if (!SemaDeclRefs.empty()) {
3456 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3457 SemaObj->StdNamespace = SemaDeclRefs[0];
3458 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3459 }
3460
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003461 // If there are @selector references added them to its pool. This is for
3462 // implementation of -Wselector.
Sebastian Redlada023c2010-08-04 20:40:17 +00003463 if (!ReferencedSelectorsData.empty()) {
3464 unsigned int DataSize = ReferencedSelectorsData.size()-1;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003465 unsigned I = 0;
3466 while (I < DataSize) {
Sebastian Redlada023c2010-08-04 20:40:17 +00003467 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003468 SourceLocation SelLoc =
Sebastian Redlada023c2010-08-04 20:40:17 +00003469 SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003470 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3471 }
3472 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00003473}
3474
Sebastian Redl2c499f62010-08-18 23:56:43 +00003475IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redl78f51772010-08-02 18:30:12 +00003476 // Try to find this name within our on-disk hash tables. We start with the
3477 // most recent one, since that one contains the most up-to-date info.
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003478 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003479 ASTIdentifierLookupTable *IdTable
3480 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003481 if (!IdTable)
3482 continue;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003483 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003484 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003485 if (Pos == IdTable->end())
3486 continue;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003487
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003488 // Dereferencing the iterator has the effect of building the
3489 // IdentifierInfo node and populating it with the various
3490 // declarations it needs.
Sebastian Redl78f51772010-08-02 18:30:12 +00003491 return *Pos;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003492 }
Sebastian Redl78f51772010-08-02 18:30:12 +00003493 return 0;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003494}
3495
Mike Stump11289f42009-09-09 15:08:12 +00003496std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redl2c499f62010-08-18 23:56:43 +00003497ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redlada023c2010-08-04 20:40:17 +00003498 // Find this selector in a hash table. We want to find the most recent entry.
3499 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3500 PerFileData &F = *Chain[I];
3501 if (!F.SelectorLookupTable)
3502 continue;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003503
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003504 ASTSelectorLookupTable *PoolTable
3505 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3506 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redlada023c2010-08-04 20:40:17 +00003507 if (Pos != PoolTable->end()) {
3508 ++NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003509 // FIXME: Not quite happy with the statistics here. We probably should
3510 // disable this tracking when called via LoadSelector.
3511 // Also, should entries without methods count as misses?
3512 ++NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003513 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redlada023c2010-08-04 20:40:17 +00003514 if (DeserializationListener)
3515 DeserializationListener->SelectorRead(Data.ID, Sel);
3516 return std::make_pair(Data.Instance, Data.Factory);
3517 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003518 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003519
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003520 ++NumMethodPoolMisses;
Sebastian Redlada023c2010-08-04 20:40:17 +00003521 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorc78d3462009-04-24 21:10:55 +00003522}
3523
Sebastian Redl2c499f62010-08-18 23:56:43 +00003524void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003525 // It would be complicated to avoid reading the methods anyway. So don't.
3526 ReadMethodPool(Sel);
3527}
3528
Sebastian Redl2c499f62010-08-18 23:56:43 +00003529void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00003530 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00003531 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00003532 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00003533 if (DeserializationListener)
3534 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003535}
3536
Douglas Gregor1342e842009-07-06 18:54:52 +00003537/// \brief Set the globally-visible declarations associated with the given
3538/// identifier.
3539///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003540/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00003541/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00003542/// them.
3543///
3544/// \param II an IdentifierInfo that refers to one or more globally-visible
3545/// declarations.
3546///
3547/// \param DeclIDs the set of declaration IDs with the name @p II that are
3548/// visible at global scope.
3549///
3550/// \param Nonrecursive should be true to indicate that the caller knows that
3551/// this call is non-recursive, and therefore the globally-visible declarations
3552/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00003553void
Sebastian Redl2c499f62010-08-18 23:56:43 +00003554ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregor1342e842009-07-06 18:54:52 +00003555 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
3556 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003557 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00003558 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
3559 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
3560 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003561 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00003562 return;
3563 }
Mike Stump11289f42009-09-09 15:08:12 +00003564
Douglas Gregor1342e842009-07-06 18:54:52 +00003565 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
3566 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
3567 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00003568 if (SemaObj->TUScope) {
3569 // Introduce this declaration into the translation-unit scope
3570 // and add it to the declaration chain for this identifier, so
3571 // that (unqualified) name lookup will find it.
John McCall48871652010-08-21 09:40:31 +00003572 SemaObj->TUScope->AddDecl(D);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00003573 }
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003574 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregor1342e842009-07-06 18:54:52 +00003575 } else {
3576 // Queue this declaration so that it will be added to the
3577 // translation unit scope and identifier's declaration chain
3578 // once a Sema object is known.
3579 PreloadedDecls.push_back(D);
3580 }
3581 }
3582}
3583
Sebastian Redl2c499f62010-08-18 23:56:43 +00003584IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003585 if (ID == 0)
3586 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00003587
Sebastian Redlc713b962010-07-21 00:46:22 +00003588 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003589 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003590 return 0;
3591 }
Mike Stump11289f42009-09-09 15:08:12 +00003592
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003593 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc713b962010-07-21 00:46:22 +00003594 ID -= 1;
3595 if (!IdentifiersLoaded[ID]) {
3596 unsigned Index = ID;
3597 const char *Str = 0;
3598 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3599 PerFileData *F = Chain[N - I - 1];
3600 if (Index < F->LocalNumIdentifiers) {
3601 uint32_t Offset = F->IdentifierOffsets[Index];
3602 Str = F->IdentifierTableData + Offset;
3603 break;
3604 }
3605 Index -= F->LocalNumIdentifiers;
3606 }
3607 assert(Str && "Broken Chain");
Douglas Gregor5287b4e2009-04-25 21:04:17 +00003608
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003609 // All of the strings in the AST file are preceded by a 16-bit length.
3610 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00003611 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
3612 // unsigned integers. This is important to avoid integer overflow when
3613 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00003614 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00003615 unsigned StrLen = (((unsigned) StrLenPtr[0])
3616 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00003617 IdentifiersLoaded[ID]
Kovarththanan Rajaratnama3b09592010-03-12 10:32:27 +00003618 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003619 if (DeserializationListener)
3620 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003621 }
Mike Stump11289f42009-09-09 15:08:12 +00003622
Sebastian Redlc713b962010-07-21 00:46:22 +00003623 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003624}
3625
Sebastian Redl2c499f62010-08-18 23:56:43 +00003626void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00003627 ReadSLocEntryRecord(ID);
3628}
3629
Sebastian Redl2c499f62010-08-18 23:56:43 +00003630Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00003631 if (ID == 0)
3632 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00003633
Sebastian Redlada023c2010-08-04 20:40:17 +00003634 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003635 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00003636 return Selector();
3637 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003638
Sebastian Redlada023c2010-08-04 20:40:17 +00003639 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003640 // Load this selector from the selector table.
Sebastian Redlada023c2010-08-04 20:40:17 +00003641 unsigned Idx = ID - 1;
3642 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3643 PerFileData &F = *Chain[N - I - 1];
3644 if (Idx < F.LocalNumSelectors) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003645 ASTSelectorLookupTrait Trait(*this);
Sebastian Redlada023c2010-08-04 20:40:17 +00003646 SelectorsLoaded[ID - 1] =
3647 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
3648 if (DeserializationListener)
3649 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
3650 break;
3651 }
3652 Idx -= F.LocalNumSelectors;
3653 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003654 }
3655
Sebastian Redlada023c2010-08-04 20:40:17 +00003656 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00003657}
3658
Sebastian Redl2c499f62010-08-18 23:56:43 +00003659Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00003660 return DecodeSelector(ID);
3661}
3662
Sebastian Redl2c499f62010-08-18 23:56:43 +00003663uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00003664 // ID 0 (the null selector) is considered an external selector.
3665 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00003666}
3667
Mike Stump11289f42009-09-09 15:08:12 +00003668DeclarationName
Sebastian Redl2c499f62010-08-18 23:56:43 +00003669ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003670 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
3671 switch (Kind) {
3672 case DeclarationName::Identifier:
3673 return DeclarationName(GetIdentifierInfo(Record, Idx));
3674
3675 case DeclarationName::ObjCZeroArgSelector:
3676 case DeclarationName::ObjCOneArgSelector:
3677 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff3c301dc2009-04-23 15:15:40 +00003678 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003679
3680 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00003681 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00003682 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003683
3684 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00003685 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00003686 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003687
3688 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00003689 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor2211d342009-08-05 05:36:45 +00003690 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003691
3692 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00003693 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003694 (OverloadedOperatorKind)Record[Idx++]);
3695
Alexis Hunt3d221f22009-11-29 07:34:05 +00003696 case DeclarationName::CXXLiteralOperatorName:
3697 return Context->DeclarationNames.getCXXLiteralOperatorName(
3698 GetIdentifierInfo(Record, Idx));
3699
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003700 case DeclarationName::CXXUsingDirective:
3701 return DeclarationName::getUsingDirectiveName();
3702 }
3703
3704 // Required to silence GCC warning
3705 return DeclarationName();
3706}
Douglas Gregor55abb232009-04-10 20:39:37 +00003707
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003708TemplateName
Sebastian Redl2c499f62010-08-18 23:56:43 +00003709ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003710 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
3711 switch (Kind) {
3712 case TemplateName::Template:
3713 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
3714
3715 case TemplateName::OverloadedTemplate: {
3716 unsigned size = Record[Idx++];
3717 UnresolvedSet<8> Decls;
3718 while (size--)
3719 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
3720
3721 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
3722 }
3723
3724 case TemplateName::QualifiedTemplate: {
3725 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3726 bool hasTemplKeyword = Record[Idx++];
3727 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
3728 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
3729 }
3730
3731 case TemplateName::DependentTemplate: {
3732 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3733 if (Record[Idx++]) // isIdentifier
3734 return Context->getDependentTemplateName(NNS,
3735 GetIdentifierInfo(Record, Idx));
3736 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003737 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003738 }
3739 }
3740
3741 assert(0 && "Unhandled template name kind!");
3742 return TemplateName();
3743}
3744
3745TemplateArgument
Sebastian Redl2c499f62010-08-18 23:56:43 +00003746ASTReader::ReadTemplateArgument(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003747 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003748 switch ((TemplateArgument::ArgKind)Record[Idx++]) {
3749 case TemplateArgument::Null:
3750 return TemplateArgument();
3751 case TemplateArgument::Type:
3752 return TemplateArgument(GetType(Record[Idx++]));
3753 case TemplateArgument::Declaration:
3754 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00003755 case TemplateArgument::Integral: {
3756 llvm::APSInt Value = ReadAPSInt(Record, Idx);
3757 QualType T = GetType(Record[Idx++]);
3758 return TemplateArgument(Value, T);
3759 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003760 case TemplateArgument::Template:
3761 return TemplateArgument(ReadTemplateName(Record, Idx));
3762 case TemplateArgument::Expression:
Sebastian Redlc67764e2010-07-22 22:43:28 +00003763 return TemplateArgument(ReadExpr(DeclsCursor));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003764 case TemplateArgument::Pack: {
3765 unsigned NumArgs = Record[Idx++];
3766 llvm::SmallVector<TemplateArgument, 8> Args;
3767 Args.reserve(NumArgs);
3768 while (NumArgs--)
Sebastian Redlc67764e2010-07-22 22:43:28 +00003769 Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003770 TemplateArgument TemplArg;
3771 TemplArg.setArgumentPack(Args.data(), Args.size(), /*CopyArgs=*/true);
3772 return TemplArg;
3773 }
3774 }
3775
3776 assert(0 && "Unhandled template argument kind!");
3777 return TemplateArgument();
3778}
3779
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003780TemplateParameterList *
Sebastian Redl2c499f62010-08-18 23:56:43 +00003781ASTReader::ReadTemplateParameterList(const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003782 SourceLocation TemplateLoc = ReadSourceLocation(Record, Idx);
3783 SourceLocation LAngleLoc = ReadSourceLocation(Record, Idx);
3784 SourceLocation RAngleLoc = ReadSourceLocation(Record, Idx);
3785
3786 unsigned NumParams = Record[Idx++];
3787 llvm::SmallVector<NamedDecl *, 16> Params;
3788 Params.reserve(NumParams);
3789 while (NumParams--)
3790 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
3791
3792 TemplateParameterList* TemplateParams =
3793 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
3794 Params.data(), Params.size(), RAngleLoc);
3795 return TemplateParams;
3796}
3797
3798void
Sebastian Redl2c499f62010-08-18 23:56:43 +00003799ASTReader::
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003800ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003801 llvm::BitstreamCursor &DeclsCursor,
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003802 const RecordData &Record, unsigned &Idx) {
3803 unsigned NumTemplateArgs = Record[Idx++];
3804 TemplArgs.reserve(NumTemplateArgs);
3805 while (NumTemplateArgs--)
Sebastian Redlc67764e2010-07-22 22:43:28 +00003806 TemplArgs.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003807}
3808
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003809/// \brief Read a UnresolvedSet structure.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003810void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003811 const RecordData &Record, unsigned &Idx) {
3812 unsigned NumDecls = Record[Idx++];
3813 while (NumDecls--) {
3814 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
3815 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
3816 Set.addDecl(D, AS);
3817 }
3818}
3819
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003820CXXBaseSpecifier
Sebastian Redl2c499f62010-08-18 23:56:43 +00003821ASTReader::ReadCXXBaseSpecifier(llvm::BitstreamCursor &DeclsCursor,
Nick Lewycky19b9f952010-07-26 16:56:01 +00003822 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003823 bool isVirtual = static_cast<bool>(Record[Idx++]);
3824 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
3825 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Nick Lewycky19b9f952010-07-26 16:56:01 +00003826 TypeSourceInfo *TInfo = GetTypeSourceInfo(DeclsCursor, Record, Idx);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003827 SourceRange Range = ReadSourceRange(Record, Idx);
Nick Lewycky19b9f952010-07-26 16:56:01 +00003828 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003829}
3830
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003831std::pair<CXXBaseOrMemberInitializer **, unsigned>
Sebastian Redl2c499f62010-08-18 23:56:43 +00003832ASTReader::ReadCXXBaseOrMemberInitializers(llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003833 const RecordData &Record,
3834 unsigned &Idx) {
3835 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0;
3836 unsigned NumInitializers = Record[Idx++];
3837 if (NumInitializers) {
3838 ASTContext &C = *getContext();
3839
3840 BaseOrMemberInitializers
3841 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
3842 for (unsigned i=0; i != NumInitializers; ++i) {
3843 TypeSourceInfo *BaseClassInfo = 0;
3844 bool IsBaseVirtual = false;
3845 FieldDecl *Member = 0;
3846
3847 bool IsBaseInitializer = Record[Idx++];
3848 if (IsBaseInitializer) {
3849 BaseClassInfo = GetTypeSourceInfo(Cursor, Record, Idx);
3850 IsBaseVirtual = Record[Idx++];
3851 } else {
3852 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
3853 }
3854 SourceLocation MemberLoc = ReadSourceLocation(Record, Idx);
3855 Expr *Init = ReadExpr(Cursor);
3856 FieldDecl *AnonUnionMember
3857 = cast_or_null<FieldDecl>(GetDecl(Record[Idx++]));
3858 SourceLocation LParenLoc = ReadSourceLocation(Record, Idx);
3859 SourceLocation RParenLoc = ReadSourceLocation(Record, Idx);
3860 bool IsWritten = Record[Idx++];
3861 unsigned SourceOrderOrNumArrayIndices;
3862 llvm::SmallVector<VarDecl *, 8> Indices;
3863 if (IsWritten) {
3864 SourceOrderOrNumArrayIndices = Record[Idx++];
3865 } else {
3866 SourceOrderOrNumArrayIndices = Record[Idx++];
3867 Indices.reserve(SourceOrderOrNumArrayIndices);
3868 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
3869 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
3870 }
3871
3872 CXXBaseOrMemberInitializer *BOMInit;
3873 if (IsBaseInitializer) {
3874 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
3875 IsBaseVirtual, LParenLoc,
3876 Init, RParenLoc);
3877 } else if (IsWritten) {
3878 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc,
3879 LParenLoc, Init, RParenLoc);
3880 } else {
3881 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc,
3882 LParenLoc, Init, RParenLoc,
3883 Indices.data(),
3884 Indices.size());
3885 }
3886
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00003887 if (IsWritten)
3888 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003889 BOMInit->setAnonUnionMember(AnonUnionMember);
3890 BaseOrMemberInitializers[i] = BOMInit;
3891 }
3892 }
3893
3894 return std::make_pair(BaseOrMemberInitializers, NumInitializers);
3895}
3896
Chris Lattnerca025db2010-05-07 21:43:38 +00003897NestedNameSpecifier *
Sebastian Redl2c499f62010-08-18 23:56:43 +00003898ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003899 unsigned N = Record[Idx++];
3900 NestedNameSpecifier *NNS = 0, *Prev = 0;
3901 for (unsigned I = 0; I != N; ++I) {
3902 NestedNameSpecifier::SpecifierKind Kind
3903 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
3904 switch (Kind) {
3905 case NestedNameSpecifier::Identifier: {
3906 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
3907 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
3908 break;
3909 }
3910
3911 case NestedNameSpecifier::Namespace: {
3912 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
3913 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
3914 break;
3915 }
3916
3917 case NestedNameSpecifier::TypeSpec:
3918 case NestedNameSpecifier::TypeSpecWithTemplate: {
3919 Type *T = GetType(Record[Idx++]).getTypePtr();
3920 bool Template = Record[Idx++];
3921 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
3922 break;
3923 }
3924
3925 case NestedNameSpecifier::Global: {
3926 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
3927 // No associated value, and there can't be a prefix.
3928 break;
3929 }
Chris Lattnerca025db2010-05-07 21:43:38 +00003930 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00003931 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00003932 }
3933 return NNS;
3934}
3935
3936SourceRange
Sebastian Redl2c499f62010-08-18 23:56:43 +00003937ASTReader::ReadSourceRange(const RecordData &Record, unsigned &Idx) {
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00003938 SourceLocation beg = SourceLocation::getFromRawEncoding(Record[Idx++]);
3939 SourceLocation end = SourceLocation::getFromRawEncoding(Record[Idx++]);
3940 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00003941}
3942
Douglas Gregor1daeb692009-04-13 18:14:40 +00003943/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00003944llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003945 unsigned BitWidth = Record[Idx++];
3946 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
3947 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
3948 Idx += NumWords;
3949 return Result;
3950}
3951
3952/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00003953llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003954 bool isUnsigned = Record[Idx++];
3955 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
3956}
3957
Douglas Gregore0a3a512009-04-14 21:55:33 +00003958/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00003959llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003960 return llvm::APFloat(ReadAPInt(Record, Idx));
3961}
3962
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003963// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00003964std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003965 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00003966 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003967 Idx += Len;
3968 return Result;
3969}
3970
Sebastian Redl2c499f62010-08-18 23:56:43 +00003971CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00003972 unsigned &Idx) {
3973 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
3974 return CXXTemporary::Create(*Context, Decl);
3975}
3976
Sebastian Redl2c499f62010-08-18 23:56:43 +00003977DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00003978 return Diag(SourceLocation(), DiagID);
3979}
3980
Sebastian Redl2c499f62010-08-18 23:56:43 +00003981DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003982 return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00003983}
Douglas Gregora9af1d12009-04-17 00:04:06 +00003984
Douglas Gregora868bbd2009-04-21 22:25:48 +00003985/// \brief Retrieve the identifier table associated with the
3986/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003987IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003988 assert(PP && "Forgot to set Preprocessor ?");
3989 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00003990}
3991
Douglas Gregora9af1d12009-04-17 00:04:06 +00003992/// \brief Record that the given ID maps to the given switch-case
3993/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003994void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00003995 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3996 SwitchCaseStmts[ID] = SC;
3997}
3998
3999/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004000SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004001 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4002 return SwitchCaseStmts[ID];
4003}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004004
4005/// \brief Record that the given label statement has been
4006/// deserialized and has the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004007void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump11289f42009-09-09 15:08:12 +00004008 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004009 "Deserialized label twice");
4010 LabelStmts[ID] = S;
4011
4012 // If we've already seen any goto statements that point to this
4013 // label, resolve them now.
4014 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
4015 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
4016 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
4017 Goto->second->setLabel(S);
4018 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor779d8652009-04-17 18:58:21 +00004019
4020 // If we've already seen any address-label statements that point to
4021 // this label, resolve them now.
4022 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump11289f42009-09-09 15:08:12 +00004023 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor779d8652009-04-17 18:58:21 +00004024 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump11289f42009-09-09 15:08:12 +00004025 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor779d8652009-04-17 18:58:21 +00004026 AddrLabel != AddrLabels.second; ++AddrLabel)
4027 AddrLabel->second->setLabel(S);
4028 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004029}
4030
4031/// \brief Set the label of the given statement to the label
4032/// identified by ID.
4033///
4034/// Depending on the order in which the label and other statements
4035/// referencing that label occur, this operation may complete
4036/// immediately (updating the statement) or it may queue the
4037/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004038void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004039 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4040 if (Label != LabelStmts.end()) {
4041 // We've already seen this label, so set the label of the goto and
4042 // we're done.
4043 S->setLabel(Label->second);
4044 } else {
4045 // We haven't seen this label yet, so add this goto to the set of
4046 // unresolved goto statements.
4047 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
4048 }
4049}
Douglas Gregor779d8652009-04-17 18:58:21 +00004050
4051/// \brief Set the label of the given expression to the label
4052/// identified by ID.
4053///
4054/// Depending on the order in which the label and other statements
4055/// referencing that label occur, this operation may complete
4056/// immediately (updating the statement) or it may queue the
4057/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004058void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor779d8652009-04-17 18:58:21 +00004059 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4060 if (Label != LabelStmts.end()) {
4061 // We've already seen this label, so set the label of the
4062 // label-address expression and we're done.
4063 S->setLabel(Label->second);
4064 } else {
4065 // We haven't seen this label yet, so add this label-address
4066 // expression to the set of unresolved label-address expressions.
4067 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
4068 }
4069}
Douglas Gregor1342e842009-07-06 18:54:52 +00004070
Sebastian Redl2c499f62010-08-18 23:56:43 +00004071void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004072 assert(NumCurrentElementsDeserializing &&
4073 "FinishedDeserializing not paired with StartedDeserializing");
4074 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004075 // If any identifiers with corresponding top-level declarations have
4076 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004077 while (!PendingIdentifierInfos.empty()) {
4078 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4079 PendingIdentifierInfos.front().DeclIDs, true);
4080 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00004081 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004082
4083 // We are not in recursive loading, so it's safe to pass the "interesting"
4084 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004085 if (Consumer)
4086 PassInterestingDeclsToConsumer();
Douglas Gregor1342e842009-07-06 18:54:52 +00004087 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004088 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00004089}
Douglas Gregorb473b072010-08-19 00:28:17 +00004090
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004091ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
4092 const char *isysroot, bool DisableValidation)
4093 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4094 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4095 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4096 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
4097 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00004098 TotalNumSLocEntries(0), NextSLocOffset(0), NumStatementsRead(0),
4099 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
4100 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004101 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4102 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4103 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
4104 RelocatablePCH = false;
4105}
4106
4107ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4108 Diagnostic &Diags, const char *isysroot,
4109 bool DisableValidation)
4110 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4111 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
4112 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
4113 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00004114 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4115 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4116 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4117 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4118 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4119 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004120 RelocatablePCH = false;
4121}
4122
4123ASTReader::~ASTReader() {
4124 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4125 delete Chain[e - i - 1];
4126 // Delete all visible decl lookup tables
4127 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4128 E = DeclContextOffsets.end();
4129 I != E; ++I) {
4130 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4131 J != F; ++J) {
4132 if (J->NameLookupTableData)
4133 delete static_cast<ASTDeclContextNameLookupTable*>(
4134 J->NameLookupTableData);
4135 }
4136 }
4137 for (DeclContextVisibleUpdatesPending::iterator
4138 I = PendingVisibleUpdates.begin(),
4139 E = PendingVisibleUpdates.end();
4140 I != E; ++I) {
4141 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4142 F = I->second.end();
4143 J != F; ++J)
4144 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4145 }
4146}
4147
Douglas Gregorb473b072010-08-19 00:28:17 +00004148ASTReader::PerFileData::PerFileData()
Sebastian Redlc1d035f2010-09-22 20:19:08 +00004149 : SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00004150 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
4151 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
4152 MacroDefinitionOffsets(0), LocalNumSelectors(0), SelectorOffsets(0),
4153 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
4154 DeclOffsets(0), LocalNumTypes(0), TypeOffsets(0), StatCache(0),
4155 NumPreallocatedPreprocessingEntities(0)
Douglas Gregorb473b072010-08-19 00:28:17 +00004156{}
4157
4158ASTReader::PerFileData::~PerFileData() {
4159 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
4160 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4161}
4162