blob: ada41b446caed0b744cd5ee5de4fad6a3f1e6da5 [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);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +000084 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian62c56022010-04-22 21:01:59 +000085 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 Kyrtzidis74825bc2010-10-08 00:25:19 +0000121 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000122 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump11289f42009-09-09 15:08:12 +0000123 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000124 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
125 return true;
126 }
127 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000128 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
129 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000130 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman9d905792009-06-25 22:57:40 +0000131 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Mike Stumpd9546382009-12-12 01:27:46 +0000132 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbar143021e2009-09-21 04:16:19 +0000133 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000134 PARSE_LANGOPT_BENIGN(SpellChecking);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +0000135#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000136#undef PARSE_LANGOPT_BENIGN
137
138 return false;
139}
140
Daniel Dunbar20a682d2009-11-11 00:52:11 +0000141bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
142 if (Triple == PP.getTargetInfo().getTriple().str())
143 return false;
144
145 Reader.Diag(diag::warn_pch_target_triple)
146 << Triple << PP.getTargetInfo().getTriple().str();
147 return true;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000148}
149
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000150struct EmptyStringRef {
Benjamin Kramer8d5609b2010-07-14 23:19:41 +0000151 bool operator ()(llvm::StringRef r) const { return r.empty(); }
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000152};
153struct EmptyBlock {
154 bool operator ()(const PCHPredefinesBlock &r) const { return r.Data.empty(); }
155};
156
157static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
158 PCHPredefinesBlocks R) {
159 // First, sum up the lengths.
160 unsigned LL = 0, RL = 0;
161 for (unsigned I = 0, N = L.size(); I != N; ++I) {
162 LL += L[I].size();
163 }
164 for (unsigned I = 0, N = R.size(); I != N; ++I) {
165 RL += R[I].Data.size();
166 }
167 if (LL != RL)
168 return false;
169 if (LL == 0 && RL == 0)
170 return true;
171
172 // Kick out empty parts, they confuse the algorithm below.
173 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
174 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
175
176 // Do it the hard way. At this point, both vectors must be non-empty.
177 llvm::StringRef LR = L[0], RR = R[0].Data;
178 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbar01ad0a72010-07-16 00:00:11 +0000179 (void) RN;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000180 for (;;) {
181 // Compare the current pieces.
182 if (LR.size() == RR.size()) {
183 // If they're the same length, it's pretty easy.
184 if (LR != RR)
185 return false;
186 // Both pieces are done, advance.
187 ++LI;
188 ++RI;
189 // If either string is done, they're both done, since they're the same
190 // length.
191 if (LI == LN) {
192 assert(RI == RN && "Strings not the same length after all?");
193 return true;
194 }
195 LR = L[LI];
196 RR = R[RI].Data;
197 } else if (LR.size() < RR.size()) {
198 // Right piece is longer.
199 if (!RR.startswith(LR))
200 return false;
201 ++LI;
202 assert(LI != LN && "Strings not the same length after all?");
203 RR = RR.substr(LR.size());
204 LR = L[LI];
205 } else {
206 // Left piece is longer.
207 if (!LR.startswith(RR))
208 return false;
209 ++RI;
210 assert(RI != RN && "Strings not the same length after all?");
211 LR = LR.substr(RR.size());
212 RR = R[RI].Data;
213 }
214 }
215}
216
217static std::pair<FileID, llvm::StringRef::size_type>
218FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
219 std::pair<FileID, llvm::StringRef::size_type> Res;
220 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
221 Res.second = Buffers[I].Data.find(MacroDef);
222 if (Res.second != llvm::StringRef::npos) {
223 Res.first = Buffers[I].BufferID;
224 break;
225 }
226 }
227 return Res;
228}
229
230bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000231 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000232 std::string &SuggestedPredefines) {
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000233 // We are in the context of an implicit include, so the predefines buffer will
234 // have a #include entry for the PCH file itself (as normalized by the
235 // preprocessor initialization). Find it and skip over it in the checking
236 // below.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000237 llvm::SmallString<256> PCHInclude;
238 PCHInclude += "#include \"";
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000239 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000240 PCHInclude += "\"\n";
241 std::pair<llvm::StringRef,llvm::StringRef> Split =
242 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
243 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000244 if (Left == PP.getPredefines()) {
245 Error("Missing PCH include entry!");
246 return true;
247 }
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000248
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000249 // If the concatenation of all the PCH buffers is equal to the adjusted
250 // command line, we're done.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000251 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
252 CommandLine.push_back(Left);
253 CommandLine.push_back(Right);
254 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000255 return false;
256
257 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000258
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000259 // The predefines buffers are different. Determine what the differences are,
260 // and whether they require us to reject the PCH file.
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000261 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000262 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
263 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000264
265 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
266 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000267
268 // Pick out implicit #includes after the PCH and don't consider them for
269 // validation; we will insert them into SuggestedPredefines so that the
270 // preprocessor includes them.
271 std::string IncludesAfterPCH;
272 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
273 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
274 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
275 if (AfterPCHLines[i].startswith("#include ")) {
276 IncludesAfterPCH += AfterPCHLines[i];
277 IncludesAfterPCH += '\n';
278 } else {
279 CmdLineLines.push_back(AfterPCHLines[i]);
280 }
281 }
282
283 // Make sure we add the includes last into SuggestedPredefines before we
284 // exit this function.
285 struct AddIncludesRAII {
286 std::string &SuggestedPredefines;
287 std::string &IncludesAfterPCH;
288
289 AddIncludesRAII(std::string &SuggestedPredefines,
290 std::string &IncludesAfterPCH)
291 : SuggestedPredefines(SuggestedPredefines),
292 IncludesAfterPCH(IncludesAfterPCH) { }
293 ~AddIncludesRAII() {
294 SuggestedPredefines += IncludesAfterPCH;
295 }
296 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000297
Daniel Dunbar499baed2009-11-11 05:26:28 +0000298 // Sort both sets of predefined buffer lines, since we allow some extra
299 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000300 std::sort(CmdLineLines.begin(), CmdLineLines.end());
301 std::sort(PCHLines.begin(), PCHLines.end());
302
Daniel Dunbar499baed2009-11-11 05:26:28 +0000303 // Determine which predefines that were used to build the PCH file are missing
304 // from the command line.
305 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000306 std::set_difference(PCHLines.begin(), PCHLines.end(),
307 CmdLineLines.begin(), CmdLineLines.end(),
308 std::back_inserter(MissingPredefines));
309
310 bool MissingDefines = false;
311 bool ConflictingDefines = false;
312 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000313 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000314 if (Missing.startswith("#include ")) {
315 // An -include was specified when generating the PCH; it is included in
316 // the PCH, just ignore it.
317 continue;
318 }
Daniel Dunbar499baed2009-11-11 05:26:28 +0000319 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000320 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
321 return true;
322 }
Mike Stump11289f42009-09-09 15:08:12 +0000323
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000324 // This is a macro definition. Determine the name of the macro we're
325 // defining.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000326 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000327 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000328 = Missing.find_first_of("( \n\r", StartOfMacroName);
329 assert(EndOfMacroName != std::string::npos &&
330 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000331 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000332
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000333 // Determine whether this macro was given a different definition on the
334 // command line.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000335 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000336 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000337 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000338 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
339 MacroDefStart);
340 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000341 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000342 // Different macro; we're done.
343 ConflictPos = CmdLineLines.end();
Mike Stump11289f42009-09-09 15:08:12 +0000344 break;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000345 }
Mike Stump11289f42009-09-09 15:08:12 +0000346
347 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000348 "Invalid #define in predefines buffer?");
Mike Stump11289f42009-09-09 15:08:12 +0000349 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000350 (*ConflictPos)[MacroDefLen] != '(')
351 continue; // Longer macro name; keep trying.
Mike Stump11289f42009-09-09 15:08:12 +0000352
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000353 // We found a conflicting macro definition.
354 break;
355 }
Mike Stump11289f42009-09-09 15:08:12 +0000356
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000357 if (ConflictPos != CmdLineLines.end()) {
358 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
359 << MacroName;
360
361 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000362 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
363 FindMacro(Buffers, Missing);
364 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
365 SourceLocation PCHMissingLoc =
366 SourceMgr.getLocForStartOfFile(MacroLoc.first)
367 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar499baed2009-11-11 05:26:28 +0000368 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000369
370 ConflictingDefines = true;
371 continue;
372 }
Mike Stump11289f42009-09-09 15:08:12 +0000373
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000374 // If the macro doesn't conflict, then we'll just pick up the macro
375 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000376 if (ConflictingDefines)
377 continue; // Don't complain if there are already conflicting defs
Mike Stump11289f42009-09-09 15:08:12 +0000378
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000379 if (!MissingDefines) {
380 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
381 MissingDefines = true;
382 }
383
384 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000385 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
386 FindMacro(Buffers, Missing);
387 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
388 SourceLocation PCHMissingLoc =
389 SourceMgr.getLocForStartOfFile(MacroLoc.first)
390 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000391 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
392 }
Mike Stump11289f42009-09-09 15:08:12 +0000393
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000394 if (ConflictingDefines)
395 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000396
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000397 // Determine what predefines were introduced based on command-line
398 // parameters that were not present when building the PCH
399 // file. Extra #defines are okay, so long as the identifiers being
400 // defined were not used within the precompiled header.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000401 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000402 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
403 PCHLines.begin(), PCHLines.end(),
Mike Stump11289f42009-09-09 15:08:12 +0000404 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000405 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000406 llvm::StringRef &Extra = ExtraPredefines[I];
407 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000408 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
409 return true;
410 }
411
412 // This is an extra macro definition. Determine the name of the
413 // macro we're defining.
414 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000415 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000416 = Extra.find_first_of("( \n\r", StartOfMacroName);
417 assert(EndOfMacroName != std::string::npos &&
418 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000419 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000420
421 // Check whether this name was used somewhere in the PCH file. If
422 // so, defining it as a macro could change behavior, so we reject
423 // the PCH file.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000424 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar045c92f2009-11-11 00:52:00 +0000425 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000426 return true;
427 }
428
429 // Add this definition to the suggested predefines buffer.
430 SuggestedPredefines += Extra;
431 SuggestedPredefines += '\n';
432 }
433
434 // If we get here, it's because the predefines buffer had compatible
435 // contents. Accept the PCH file.
436 return false;
437}
438
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000439void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
440 unsigned ID) {
441 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
442 ++NumHeaderInfos;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000443}
444
445void PCHValidator::ReadCounter(unsigned Value) {
446 PP.setCounterValue(Value);
447}
448
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000449//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +0000450// AST reader implementation
Douglas Gregora868bbd2009-04-21 22:25:48 +0000451//===----------------------------------------------------------------------===//
452
Sebastian Redl07a89a82010-07-30 00:29:29 +0000453void
Sebastian Redl3e31c722010-08-18 23:56:56 +0000454ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redl07a89a82010-07-30 00:29:29 +0000455 DeserializationListener = Listener;
456 if (DeserializationListener)
457 DeserializationListener->SetReader(this);
458}
459
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000460
Douglas Gregora868bbd2009-04-21 22:25:48 +0000461namespace {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000462class ASTSelectorLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000463 ASTReader &Reader;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000464
465public:
Sebastian Redl834bb972010-08-04 17:20:04 +0000466 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +0000467 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +0000468 ObjCMethodList Instance, Factory;
469 };
Douglas Gregorc78d3462009-04-24 21:10:55 +0000470
471 typedef Selector external_key_type;
472 typedef external_key_type internal_key_type;
473
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000474 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump11289f42009-09-09 15:08:12 +0000475
Douglas Gregorc78d3462009-04-24 21:10:55 +0000476 static bool EqualKey(const internal_key_type& a,
477 const internal_key_type& b) {
478 return a == b;
479 }
Mike Stump11289f42009-09-09 15:08:12 +0000480
Douglas Gregorc78d3462009-04-24 21:10:55 +0000481 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +0000482 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000483 }
Mike Stump11289f42009-09-09 15:08:12 +0000484
Douglas Gregorc78d3462009-04-24 21:10:55 +0000485 // This hopefully will just get inlined and removed by the optimizer.
486 static const internal_key_type&
487 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000488
Douglas Gregorc78d3462009-04-24 21:10:55 +0000489 static std::pair<unsigned, unsigned>
490 ReadKeyDataLength(const unsigned char*& d) {
491 using namespace clang::io;
492 unsigned KeyLen = ReadUnalignedLE16(d);
493 unsigned DataLen = ReadUnalignedLE16(d);
494 return std::make_pair(KeyLen, DataLen);
495 }
Mike Stump11289f42009-09-09 15:08:12 +0000496
Douglas Gregor95c13f52009-04-25 17:48:32 +0000497 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000498 using namespace clang::io;
Chris Lattner8575daa2009-04-27 21:45:14 +0000499 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000500 unsigned N = ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000501 IdentifierInfo *FirstII
Douglas Gregorc78d3462009-04-24 21:10:55 +0000502 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
503 if (N == 0)
504 return SelTable.getNullarySelector(FirstII);
505 else if (N == 1)
506 return SelTable.getUnarySelector(FirstII);
507
508 llvm::SmallVector<IdentifierInfo *, 16> Args;
509 Args.push_back(FirstII);
510 for (unsigned I = 1; I != N; ++I)
511 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
512
Douglas Gregor038c3382009-05-22 22:45:36 +0000513 return SelTable.getSelector(N, Args.data());
Douglas Gregorc78d3462009-04-24 21:10:55 +0000514 }
Mike Stump11289f42009-09-09 15:08:12 +0000515
Douglas Gregorc78d3462009-04-24 21:10:55 +0000516 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
517 using namespace clang::io;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000518
519 data_type Result;
520
Sebastian Redl834bb972010-08-04 17:20:04 +0000521 Result.ID = ReadUnalignedLE32(d);
522 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
523 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
524
Douglas Gregorc78d3462009-04-24 21:10:55 +0000525 // Load instance methods
526 ObjCMethodList *Prev = 0;
527 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000528 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000529 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000530 if (!Result.Instance.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000531 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000532 Result.Instance.Method = Method;
533 Prev = &Result.Instance;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000534 continue;
535 }
536
Ted Kremenekda4abf12010-02-11 00:53:01 +0000537 ObjCMethodList *Mem =
538 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
539 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000540 Prev = Prev->Next;
541 }
542
543 // Load factory methods
544 Prev = 0;
545 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000546 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000547 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000548 if (!Result.Factory.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000549 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000550 Result.Factory.Method = Method;
551 Prev = &Result.Factory;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000552 continue;
553 }
554
Ted Kremenekda4abf12010-02-11 00:53:01 +0000555 ObjCMethodList *Mem =
556 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
557 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000558 Prev = Prev->Next;
559 }
560
561 return Result;
562 }
563};
Mike Stump11289f42009-09-09 15:08:12 +0000564
565} // end anonymous namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +0000566
567/// \brief The on-disk hash table used for the global method pool.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000568typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
569 ASTSelectorLookupTable;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000570
Sebastian Redl2c373b92010-10-05 15:59:54 +0000571namespace clang {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000572class ASTIdentifierLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000573 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +0000574 ASTReader::PerFileData &F;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000575
576 // If we know the IdentifierInfo in advance, it is here and we will
577 // not build a new one. Used when deserializing information about an
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000578 // identifier that was constructed before the AST file was read.
Douglas Gregora868bbd2009-04-21 22:25:48 +0000579 IdentifierInfo *KnownII;
580
581public:
582 typedef IdentifierInfo * data_type;
583
584 typedef const std::pair<const char*, unsigned> external_key_type;
585
586 typedef external_key_type internal_key_type;
587
Sebastian Redl2c373b92010-10-05 15:59:54 +0000588 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000589 IdentifierInfo *II = 0)
Sebastian Redl2c373b92010-10-05 15:59:54 +0000590 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump11289f42009-09-09 15:08:12 +0000591
Douglas Gregora868bbd2009-04-21 22:25:48 +0000592 static bool EqualKey(const internal_key_type& a,
593 const internal_key_type& b) {
594 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
595 : false;
596 }
Mike Stump11289f42009-09-09 15:08:12 +0000597
Douglas Gregora868bbd2009-04-21 22:25:48 +0000598 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000599 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregora868bbd2009-04-21 22:25:48 +0000600 }
Mike Stump11289f42009-09-09 15:08:12 +0000601
Douglas Gregora868bbd2009-04-21 22:25:48 +0000602 // This hopefully will just get inlined and removed by the optimizer.
603 static const internal_key_type&
604 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000605
Douglas Gregor57756ea2010-10-14 22:11:03 +0000606 // This hopefully will just get inlined and removed by the optimizer.
607 static const external_key_type&
608 GetExternalKey(const internal_key_type& x) { return x; }
609
Douglas Gregora868bbd2009-04-21 22:25:48 +0000610 static std::pair<unsigned, unsigned>
611 ReadKeyDataLength(const unsigned char*& d) {
612 using namespace clang::io;
Douglas Gregor6b7bf5a2009-04-25 20:26:24 +0000613 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor5287b4e2009-04-25 21:04:17 +0000614 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000615 return std::make_pair(KeyLen, DataLen);
616 }
Mike Stump11289f42009-09-09 15:08:12 +0000617
Douglas Gregora868bbd2009-04-21 22:25:48 +0000618 static std::pair<const char*, unsigned>
619 ReadKey(const unsigned char* d, unsigned n) {
620 assert(n >= 2 && d[n-1] == '\0');
621 return std::make_pair((const char*) d, n-1);
622 }
Mike Stump11289f42009-09-09 15:08:12 +0000623
624 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregora868bbd2009-04-21 22:25:48 +0000625 const unsigned char* d,
626 unsigned DataLen) {
627 using namespace clang::io;
Sebastian Redl539c5062010-08-18 23:57:32 +0000628 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000629 bool IsInteresting = ID & 0x01;
630
631 // Wipe out the "is interesting" bit.
632 ID = ID >> 1;
633
634 if (!IsInteresting) {
Sebastian Redl98912122010-07-27 23:01:28 +0000635 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregor1d583f22009-04-28 21:18:29 +0000636 // and associate it with the persistent ID.
637 IdentifierInfo *II = KnownII;
638 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000639 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000640 Reader.SetIdentifierInfo(ID, II);
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000641 II->setIsFromAST();
Douglas Gregor1d583f22009-04-28 21:18:29 +0000642 return II;
643 }
644
Douglas Gregorb9256522009-04-28 21:32:13 +0000645 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000646 bool CPlusPlusOperatorKeyword = Bits & 0x01;
647 Bits >>= 1;
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000648 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
649 Bits >>= 1;
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000650 bool Poisoned = Bits & 0x01;
651 Bits >>= 1;
652 bool ExtensionToken = Bits & 0x01;
653 Bits >>= 1;
654 bool hasMacroDefinition = Bits & 0x01;
655 Bits >>= 1;
656 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
657 Bits >>= 10;
Mike Stump11289f42009-09-09 15:08:12 +0000658
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000659 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorb9256522009-04-28 21:32:13 +0000660 DataLen -= 6;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000661
662 // Build the IdentifierInfo itself and link the identifier ID with
663 // the new IdentifierInfo.
664 IdentifierInfo *II = KnownII;
665 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000666 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000667 Reader.SetIdentifierInfo(ID, II);
668
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000669 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000670 // Token IDs are read-only.
671 if (HasRevertedTokenIDToIdentifier)
672 II->RevertTokenIDToIdentifier();
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000673 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump11289f42009-09-09 15:08:12 +0000674 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000675 "Incorrect extension token flag");
676 (void)ExtensionToken;
677 II->setIsPoisoned(Poisoned);
678 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
679 "Incorrect C++ operator keyword flag");
680 (void)CPlusPlusOperatorKeyword;
681
Douglas Gregorc3366a52009-04-21 23:56:24 +0000682 // If this identifier is a macro, deserialize the macro
683 // definition.
684 if (hasMacroDefinition) {
Douglas Gregorb9256522009-04-28 21:32:13 +0000685 uint32_t Offset = ReadUnalignedLE32(d);
Sebastian Redl2c373b92010-10-05 15:59:54 +0000686 Reader.ReadMacroRecord(F, Offset);
Douglas Gregorb9256522009-04-28 21:32:13 +0000687 DataLen -= 4;
Douglas Gregorc3366a52009-04-21 23:56:24 +0000688 }
Douglas Gregora868bbd2009-04-21 22:25:48 +0000689
690 // Read all of the declarations visible at global scope with this
691 // name.
Chris Lattner1d728882009-04-27 22:17:41 +0000692 if (Reader.getContext() == 0) return II;
Douglas Gregor1342e842009-07-06 18:54:52 +0000693 if (DataLen > 0) {
694 llvm::SmallVector<uint32_t, 4> DeclIDs;
695 for (; DataLen > 0; DataLen -= 4)
696 DeclIDs.push_back(ReadUnalignedLE32(d));
697 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000698 }
Mike Stump11289f42009-09-09 15:08:12 +0000699
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000700 II->setIsFromAST();
Douglas Gregora868bbd2009-04-21 22:25:48 +0000701 return II;
702 }
703};
Mike Stump11289f42009-09-09 15:08:12 +0000704
705} // end anonymous namespace
Douglas Gregora868bbd2009-04-21 22:25:48 +0000706
707/// \brief The on-disk hash table used to contain information about
708/// all of the identifiers in the program.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000709typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
710 ASTIdentifierLookupTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000711
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000712namespace {
713class ASTDeclContextNameLookupTrait {
714 ASTReader &Reader;
715
716public:
717 /// \brief Pair of begin/end iterators for DeclIDs.
718 typedef std::pair<DeclID *, DeclID *> data_type;
719
720 /// \brief Special internal key for declaration names.
721 /// The hash table creates keys for comparison; we do not create
722 /// a DeclarationName for the internal key to avoid deserializing types.
723 struct DeclNameKey {
724 DeclarationName::NameKind Kind;
725 uint64_t Data;
726 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
727 };
728
729 typedef DeclarationName external_key_type;
730 typedef DeclNameKey internal_key_type;
731
732 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
733
734 static bool EqualKey(const internal_key_type& a,
735 const internal_key_type& b) {
736 return a.Kind == b.Kind && a.Data == b.Data;
737 }
738
739 unsigned ComputeHash(const DeclNameKey &Key) const {
740 llvm::FoldingSetNodeID ID;
741 ID.AddInteger(Key.Kind);
742
743 switch (Key.Kind) {
744 case DeclarationName::Identifier:
745 case DeclarationName::CXXLiteralOperatorName:
746 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
747 break;
748 case DeclarationName::ObjCZeroArgSelector:
749 case DeclarationName::ObjCOneArgSelector:
750 case DeclarationName::ObjCMultiArgSelector:
751 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
752 break;
753 case DeclarationName::CXXConstructorName:
754 case DeclarationName::CXXDestructorName:
755 case DeclarationName::CXXConversionFunctionName:
756 ID.AddInteger((TypeID)Key.Data);
757 break;
758 case DeclarationName::CXXOperatorName:
759 ID.AddInteger((OverloadedOperatorKind)Key.Data);
760 break;
761 case DeclarationName::CXXUsingDirective:
762 break;
763 }
764
765 return ID.ComputeHash();
766 }
767
768 internal_key_type GetInternalKey(const external_key_type& Name) const {
769 DeclNameKey Key;
770 Key.Kind = Name.getNameKind();
771 switch (Name.getNameKind()) {
772 case DeclarationName::Identifier:
773 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
774 break;
775 case DeclarationName::ObjCZeroArgSelector:
776 case DeclarationName::ObjCOneArgSelector:
777 case DeclarationName::ObjCMultiArgSelector:
778 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
779 break;
780 case DeclarationName::CXXConstructorName:
781 case DeclarationName::CXXDestructorName:
782 case DeclarationName::CXXConversionFunctionName:
783 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
784 break;
785 case DeclarationName::CXXOperatorName:
786 Key.Data = Name.getCXXOverloadedOperator();
787 break;
788 case DeclarationName::CXXLiteralOperatorName:
789 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
790 break;
791 case DeclarationName::CXXUsingDirective:
792 break;
793 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000794
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000795 return Key;
796 }
797
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000798 external_key_type GetExternalKey(const internal_key_type& Key) const {
799 ASTContext *Context = Reader.getContext();
800 switch (Key.Kind) {
801 case DeclarationName::Identifier:
802 return DeclarationName((IdentifierInfo*)Key.Data);
803
804 case DeclarationName::ObjCZeroArgSelector:
805 case DeclarationName::ObjCOneArgSelector:
806 case DeclarationName::ObjCMultiArgSelector:
807 return DeclarationName(Selector(Key.Data));
808
809 case DeclarationName::CXXConstructorName:
810 return Context->DeclarationNames.getCXXConstructorName(
811 Context->getCanonicalType(Reader.GetType(Key.Data)));
812
813 case DeclarationName::CXXDestructorName:
814 return Context->DeclarationNames.getCXXDestructorName(
815 Context->getCanonicalType(Reader.GetType(Key.Data)));
816
817 case DeclarationName::CXXConversionFunctionName:
818 return Context->DeclarationNames.getCXXConversionFunctionName(
819 Context->getCanonicalType(Reader.GetType(Key.Data)));
820
821 case DeclarationName::CXXOperatorName:
822 return Context->DeclarationNames.getCXXOperatorName(
823 (OverloadedOperatorKind)Key.Data);
824
825 case DeclarationName::CXXLiteralOperatorName:
826 return Context->DeclarationNames.getCXXLiteralOperatorName(
827 (IdentifierInfo*)Key.Data);
828
829 case DeclarationName::CXXUsingDirective:
830 return DeclarationName::getUsingDirectiveName();
831 }
832
833 llvm_unreachable("Invalid Name Kind ?");
834 }
835
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000836 static std::pair<unsigned, unsigned>
837 ReadKeyDataLength(const unsigned char*& d) {
838 using namespace clang::io;
839 unsigned KeyLen = ReadUnalignedLE16(d);
840 unsigned DataLen = ReadUnalignedLE16(d);
841 return std::make_pair(KeyLen, DataLen);
842 }
843
844 internal_key_type ReadKey(const unsigned char* d, unsigned) {
845 using namespace clang::io;
846
847 DeclNameKey Key;
848 Key.Kind = (DeclarationName::NameKind)*d++;
849 switch (Key.Kind) {
850 case DeclarationName::Identifier:
851 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
852 break;
853 case DeclarationName::ObjCZeroArgSelector:
854 case DeclarationName::ObjCOneArgSelector:
855 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000856 Key.Data =
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000857 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
858 break;
859 case DeclarationName::CXXConstructorName:
860 case DeclarationName::CXXDestructorName:
861 case DeclarationName::CXXConversionFunctionName:
862 Key.Data = ReadUnalignedLE32(d); // TypeID
863 break;
864 case DeclarationName::CXXOperatorName:
865 Key.Data = *d++; // OverloadedOperatorKind
866 break;
867 case DeclarationName::CXXLiteralOperatorName:
868 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
869 break;
870 case DeclarationName::CXXUsingDirective:
871 break;
872 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000873
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000874 return Key;
875 }
876
877 data_type ReadData(internal_key_type, const unsigned char* d,
878 unsigned DataLen) {
879 using namespace clang::io;
880 unsigned NumDecls = ReadUnalignedLE16(d);
881 DeclID *Start = (DeclID *)d;
882 return std::make_pair(Start, Start + NumDecls);
883 }
884};
885
886} // end anonymous namespace
887
888/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
889typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
890 ASTDeclContextNameLookupTable;
891
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000892bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
893 const std::pair<uint64_t, uint64_t> &Offsets,
894 DeclContextInfo &Info) {
895 SavedStreamPosition SavedPosition(Cursor);
896 // First the lexical decls.
897 if (Offsets.first != 0) {
898 Cursor.JumpToBit(Offsets.first);
899
900 RecordData Record;
901 const char *Blob;
902 unsigned BlobLen;
903 unsigned Code = Cursor.ReadCode();
904 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
905 if (RecCode != DECL_CONTEXT_LEXICAL) {
906 Error("Expected lexical block");
907 return true;
908 }
909
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000910 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
911 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000912 } else {
913 Info.LexicalDecls = 0;
914 Info.NumLexicalDecls = 0;
915 }
916
917 // Now the lookup table.
918 if (Offsets.second != 0) {
919 Cursor.JumpToBit(Offsets.second);
920
921 RecordData Record;
922 const char *Blob;
923 unsigned BlobLen;
924 unsigned Code = Cursor.ReadCode();
925 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
926 if (RecCode != DECL_CONTEXT_VISIBLE) {
927 Error("Expected visible lookup table block");
928 return true;
929 }
930 Info.NameLookupTableData
931 = ASTDeclContextNameLookupTable::Create(
932 (const unsigned char *)Blob + Record[0],
933 (const unsigned char *)Blob,
934 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl9d8f58b2010-08-24 00:50:00 +0000935 } else {
936 Info.NameLookupTableData = 0;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000937 }
938
939 return false;
940}
941
Sebastian Redl2c499f62010-08-18 23:56:43 +0000942void ASTReader::Error(const char *Msg) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000943 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000944}
945
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000946/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000947bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000948 if (Listener)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000949 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000950 ActualOriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000951 SuggestedPredefines);
Douglas Gregorc379c072009-04-28 18:58:38 +0000952 return false;
Douglas Gregor92863e42009-04-10 23:10:45 +0000953}
954
Douglas Gregorc5046832009-04-27 18:38:38 +0000955//===----------------------------------------------------------------------===//
956// Source Manager Deserialization
957//===----------------------------------------------------------------------===//
958
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000959/// \brief Read the line table in the source manager block.
Sebastian Redl2c373b92010-10-05 15:59:54 +0000960/// \returns true if there was an error.
961bool ASTReader::ParseLineTable(PerFileData &F,
962 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000963 unsigned Idx = 0;
964 LineTableInfo &LineTable = SourceMgr.getLineTable();
965
966 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +0000967 std::map<int, int> FileIDs;
968 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000969 // Extract the file name
970 unsigned FilenameLen = Record[Idx++];
971 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
972 Idx += FilenameLen;
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000973 MaybeAddSystemRootToFilename(Filename);
Mike Stump11289f42009-09-09 15:08:12 +0000974 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregora8854652009-04-13 17:12:42 +0000975 Filename.size());
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000976 }
977
978 // Parse the line entries
979 std::vector<LineEntry> Entries;
980 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000981 int FID = Record[Idx++];
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000982
983 // Extract the line entries
984 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000985 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000986 Entries.clear();
987 Entries.reserve(NumEntries);
988 for (unsigned I = 0; I != NumEntries; ++I) {
989 unsigned FileOffset = Record[Idx++];
990 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000991 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +0000992 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000993 = (SrcMgr::CharacteristicKind)Record[Idx++];
994 unsigned IncludeOffset = Record[Idx++];
995 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
996 FileKind, IncludeOffset));
997 }
998 LineTable.AddEntry(FID, Entries);
999 }
1000
1001 return false;
1002}
1003
Douglas Gregorc5046832009-04-27 18:38:38 +00001004namespace {
1005
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001006class ASTStatData {
Douglas Gregorc5046832009-04-27 18:38:38 +00001007public:
1008 const bool hasStat;
1009 const ino_t ino;
1010 const dev_t dev;
1011 const mode_t mode;
1012 const time_t mtime;
1013 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +00001014
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001015 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump11289f42009-09-09 15:08:12 +00001016 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
1017
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001018 ASTStatData()
Douglas Gregorc5046832009-04-27 18:38:38 +00001019 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
1020};
1021
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001022class ASTStatLookupTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001023 public:
1024 typedef const char *external_key_type;
1025 typedef const char *internal_key_type;
1026
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001027 typedef ASTStatData data_type;
Douglas Gregorc5046832009-04-27 18:38:38 +00001028
1029 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001030 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001031 }
1032
1033 static internal_key_type GetInternalKey(const char *path) { return path; }
1034
1035 static bool EqualKey(internal_key_type a, internal_key_type b) {
1036 return strcmp(a, b) == 0;
1037 }
1038
1039 static std::pair<unsigned, unsigned>
1040 ReadKeyDataLength(const unsigned char*& d) {
1041 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1042 unsigned DataLen = (unsigned) *d++;
1043 return std::make_pair(KeyLen + 1, DataLen);
1044 }
1045
1046 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1047 return (const char *)d;
1048 }
1049
1050 static data_type ReadData(const internal_key_type, const unsigned char *d,
1051 unsigned /*DataLen*/) {
1052 using namespace clang::io;
1053
1054 if (*d++ == 1)
1055 return data_type();
1056
1057 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1058 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1059 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +00001060 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +00001061 off_t size = (off_t) ReadUnalignedLE64(d);
1062 return data_type(ino, dev, mode, mtime, size);
1063 }
1064};
1065
1066/// \brief stat() cache for precompiled headers.
1067///
1068/// This cache is very similar to the stat cache used by pretokenized
1069/// headers.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001070class ASTStatCache : public StatSysCallCache {
1071 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregorc5046832009-04-27 18:38:38 +00001072 CacheTy *Cache;
1073
1074 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +00001075public:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001076 ASTStatCache(const unsigned char *Buckets,
Douglas Gregorc5046832009-04-27 18:38:38 +00001077 const unsigned char *Base,
1078 unsigned &NumStatHits,
Mike Stump11289f42009-09-09 15:08:12 +00001079 unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +00001080 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1081 Cache = CacheTy::Create(Buckets, Base);
1082 }
1083
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001084 ~ASTStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +00001085
Douglas Gregorc5046832009-04-27 18:38:38 +00001086 int stat(const char *path, struct stat *buf) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001087 // Do the lookup for the file's data in the AST file.
Douglas Gregorc5046832009-04-27 18:38:38 +00001088 CacheTy::iterator I = Cache->find(path);
1089
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001090 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregorc5046832009-04-27 18:38:38 +00001091 if (I == Cache->end()) {
1092 ++NumStatMisses;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001093 return StatSysCallCache::stat(path, buf);
Douglas Gregorc5046832009-04-27 18:38:38 +00001094 }
Mike Stump11289f42009-09-09 15:08:12 +00001095
Douglas Gregorc5046832009-04-27 18:38:38 +00001096 ++NumStatHits;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001097 ASTStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001098
Douglas Gregorc5046832009-04-27 18:38:38 +00001099 if (!Data.hasStat)
1100 return 1;
1101
1102 buf->st_ino = Data.ino;
1103 buf->st_dev = Data.dev;
1104 buf->st_mtime = Data.mtime;
1105 buf->st_mode = Data.mode;
1106 buf->st_size = Data.size;
1107 return 0;
1108 }
1109};
1110} // end anonymous namespace
1111
1112
Sebastian Redl393f8b72010-07-19 20:52:06 +00001113/// \brief Read a source manager block
Sebastian Redl2c499f62010-08-18 23:56:43 +00001114ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001115 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +00001116
Sebastian Redl393f8b72010-07-19 20:52:06 +00001117 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001118
Douglas Gregor258ae542009-04-27 06:38:32 +00001119 // Set the source-location entry cursor to the current position in
1120 // the stream. This cursor will be used to read the contents of the
1121 // source manager block initially, and then lazily read
1122 // source-location entries as needed.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001123 SLocEntryCursor = F.Stream;
Douglas Gregor258ae542009-04-27 06:38:32 +00001124
1125 // The stream itself is going to skip over the source manager block.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001126 if (F.Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001127 Error("malformed block record in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001128 return Failure;
1129 }
1130
1131 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001132 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001133 Error("malformed source manager block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001134 return Failure;
1135 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001136
Douglas Gregora7f71a92009-04-10 03:52:48 +00001137 RecordData Record;
1138 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001139 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001140 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001141 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001142 Error("error at end of Source Manager block in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001143 return Failure;
1144 }
Douglas Gregor92863e42009-04-10 23:10:45 +00001145 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001146 }
Mike Stump11289f42009-09-09 15:08:12 +00001147
Douglas Gregora7f71a92009-04-10 03:52:48 +00001148 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1149 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +00001150 SLocEntryCursor.ReadSubBlockID();
1151 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001152 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001153 return Failure;
1154 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001155 continue;
1156 }
Mike Stump11289f42009-09-09 15:08:12 +00001157
Douglas Gregora7f71a92009-04-10 03:52:48 +00001158 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001159 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001160 continue;
1161 }
Mike Stump11289f42009-09-09 15:08:12 +00001162
Douglas Gregora7f71a92009-04-10 03:52:48 +00001163 // Read a record.
1164 const char *BlobStart;
1165 unsigned BlobLen;
1166 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +00001167 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001168 default: // Default behavior: ignore.
1169 break;
1170
Sebastian Redl539c5062010-08-18 23:57:32 +00001171 case SM_LINE_TABLE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00001172 if (ParseLineTable(F, Record))
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001173 return Failure;
Chris Lattner184e65d2009-04-14 23:22:57 +00001174 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00001175
Sebastian Redl539c5062010-08-18 23:57:32 +00001176 case SM_SLOC_FILE_ENTRY:
1177 case SM_SLOC_BUFFER_ENTRY:
1178 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor258ae542009-04-27 06:38:32 +00001179 // Once we hit one of the source location entries, we're done.
1180 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001181 }
1182 }
1183}
1184
Sebastian Redl06750302010-07-20 21:50:20 +00001185/// \brief Get a cursor that's correctly positioned for reading the source
1186/// location entry with the given ID.
Sebastian Redl2c373b92010-10-05 15:59:54 +00001187ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl06750302010-07-20 21:50:20 +00001188 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1189 "SLocCursorForID should only be called for real IDs.");
1190
1191 ID -= 1;
1192 PerFileData *F = 0;
1193 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1194 F = Chain[N - I - 1];
1195 if (ID < F->LocalNumSLocEntries)
1196 break;
1197 ID -= F->LocalNumSLocEntries;
1198 }
1199 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1200
1201 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00001202 return F;
Sebastian Redl06750302010-07-20 21:50:20 +00001203}
1204
Douglas Gregor258ae542009-04-27 06:38:32 +00001205/// \brief Read in the source location entry with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001206ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001207 if (ID == 0)
1208 return Success;
1209
1210 if (ID > TotalNumSLocEntries) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001211 Error("source location entry ID out-of-range for AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001212 return Failure;
1213 }
1214
Sebastian Redl2c373b92010-10-05 15:59:54 +00001215 PerFileData *F = SLocCursorForID(ID);
1216 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001217
Douglas Gregor258ae542009-04-27 06:38:32 +00001218 ++NumSLocEntriesRead;
Douglas Gregor258ae542009-04-27 06:38:32 +00001219 unsigned Code = SLocEntryCursor.ReadCode();
1220 if (Code == llvm::bitc::END_BLOCK ||
1221 Code == llvm::bitc::ENTER_SUBBLOCK ||
1222 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001223 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001224 return Failure;
1225 }
1226
Douglas Gregor258ae542009-04-27 06:38:32 +00001227 RecordData Record;
1228 const char *BlobStart;
1229 unsigned BlobLen;
1230 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1231 default:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001232 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001233 return Failure;
1234
Sebastian Redl539c5062010-08-18 23:57:32 +00001235 case SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001236 std::string Filename(BlobStart, BlobStart + BlobLen);
1237 MaybeAddSystemRootToFilename(Filename);
1238 const FileEntry *File = FileMgr.getFile(Filename);
Chris Lattnerd20dc872009-06-15 04:35:16 +00001239 if (File == 0) {
1240 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001241 ErrorStr += Filename;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001242 ErrorStr += "' referenced by AST file";
Chris Lattnerd20dc872009-06-15 04:35:16 +00001243 Error(ErrorStr.c_str());
1244 return Failure;
1245 }
Mike Stump11289f42009-09-09 15:08:12 +00001246
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001247 if (Record.size() < 10) {
Ted Kremenekabb1ddd2010-03-18 21:23:05 +00001248 Error("source location entry is incorrect");
1249 return Failure;
1250 }
1251
Douglas Gregorce3a8292010-07-27 00:27:13 +00001252 if (!DisableValidation &&
1253 ((off_t)Record[4] != File->getSize()
Douglas Gregor08288f22010-04-09 15:54:22 +00001254#if !defined(LLVM_ON_WIN32)
1255 // In our regression testing, the Windows file system seems to
1256 // have inconsistent modification times that sometimes
1257 // erroneously trigger this error-handling path.
Douglas Gregorce3a8292010-07-27 00:27:13 +00001258 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor08288f22010-04-09 15:54:22 +00001259#endif
Douglas Gregorce3a8292010-07-27 00:27:13 +00001260 )) {
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001261 Diag(diag::err_fe_pch_file_modified)
1262 << Filename;
1263 return Failure;
1264 }
1265
Douglas Gregor258ae542009-04-27 06:38:32 +00001266 FileID FID = SourceMgr.createFileID(File,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001267 ReadSourceLocation(*F, Record[1]),
Douglas Gregor258ae542009-04-27 06:38:32 +00001268 (SrcMgr::CharacteristicKind)Record[2],
1269 ID, Record[0]);
1270 if (Record[3])
1271 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1272 .setHasLineDirectives();
1273
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001274 // Reconstruct header-search information for this file.
1275 HeaderFileInfo HFI;
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001276 HFI.isImport = Record[6];
1277 HFI.DirInfo = Record[7];
1278 HFI.NumIncludes = Record[8];
1279 HFI.ControllingMacroID = Record[9];
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001280 if (Listener)
1281 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor258ae542009-04-27 06:38:32 +00001282 break;
1283 }
1284
Sebastian Redl539c5062010-08-18 23:57:32 +00001285 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor258ae542009-04-27 06:38:32 +00001286 const char *Name = BlobStart;
1287 unsigned Offset = Record[0];
1288 unsigned Code = SLocEntryCursor.ReadCode();
1289 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001290 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +00001291 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001292
Sebastian Redl539c5062010-08-18 23:57:32 +00001293 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001294 Error("AST record has invalid code");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001295 return Failure;
1296 }
1297
Douglas Gregor258ae542009-04-27 06:38:32 +00001298 llvm::MemoryBuffer *Buffer
Chris Lattner58c79342010-04-05 22:42:27 +00001299 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1300 Name);
Douglas Gregor258ae542009-04-27 06:38:32 +00001301 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump11289f42009-09-09 15:08:12 +00001302
Douglas Gregore6648fb2009-04-28 20:33:11 +00001303 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001304 PCHPredefinesBlock Block = {
1305 BufferID,
1306 llvm::StringRef(BlobStart, BlobLen - 1)
1307 };
1308 PCHPredefinesBuffers.push_back(Block);
Douglas Gregore6648fb2009-04-28 20:33:11 +00001309 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001310
1311 break;
1312 }
1313
Sebastian Redl539c5062010-08-18 23:57:32 +00001314 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001315 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001316 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001317 ReadSourceLocation(*F, Record[2]),
1318 ReadSourceLocation(*F, Record[3]),
Douglas Gregor258ae542009-04-27 06:38:32 +00001319 Record[4],
1320 ID,
1321 Record[0]);
1322 break;
Mike Stump11289f42009-09-09 15:08:12 +00001323 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001324 }
1325
1326 return Success;
1327}
1328
Chris Lattnere78a6be2009-04-27 01:05:14 +00001329/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1330/// specified cursor. Read the abbreviations that are at the top of the block
1331/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001332bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001333 unsigned BlockID) {
1334 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001335 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001336 return Failure;
1337 }
Mike Stump11289f42009-09-09 15:08:12 +00001338
Chris Lattnere78a6be2009-04-27 01:05:14 +00001339 while (true) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001340 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattnere78a6be2009-04-27 01:05:14 +00001341 unsigned Code = Cursor.ReadCode();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001342
Chris Lattnere78a6be2009-04-27 01:05:14 +00001343 // We expect all abbrevs to be at the start of the block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001344 if (Code != llvm::bitc::DEFINE_ABBREV) {
1345 Cursor.JumpToBit(Offset);
Chris Lattnere78a6be2009-04-27 01:05:14 +00001346 return false;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001347 }
Chris Lattnere78a6be2009-04-27 01:05:14 +00001348 Cursor.ReadAbbrevRecord();
1349 }
1350}
1351
Sebastian Redl2c373b92010-10-05 15:59:54 +00001352void ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001353 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregor796d76a2010-10-20 22:00:55 +00001354 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump11289f42009-09-09 15:08:12 +00001355
Douglas Gregorc3366a52009-04-21 23:56:24 +00001356 // Keep track of where we are in the stream, then jump back there
1357 // after reading this macro.
1358 SavedStreamPosition SavedPosition(Stream);
1359
1360 Stream.JumpToBit(Offset);
1361 RecordData Record;
1362 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1363 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001364
Douglas Gregorc3366a52009-04-21 23:56:24 +00001365 while (true) {
1366 unsigned Code = Stream.ReadCode();
1367 switch (Code) {
1368 case llvm::bitc::END_BLOCK:
1369 return;
1370
1371 case llvm::bitc::ENTER_SUBBLOCK:
1372 // No known subblocks, always skip them.
1373 Stream.ReadSubBlockID();
1374 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001375 Error("malformed block record in AST file");
Douglas Gregorc3366a52009-04-21 23:56:24 +00001376 return;
1377 }
1378 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001379
Douglas Gregorc3366a52009-04-21 23:56:24 +00001380 case llvm::bitc::DEFINE_ABBREV:
1381 Stream.ReadAbbrevRecord();
1382 continue;
1383 default: break;
1384 }
1385
1386 // Read a record.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001387 const char *BlobStart = 0;
1388 unsigned BlobLen = 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001389 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001390 PreprocessorRecordTypes RecType =
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001391 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001392 BlobLen);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001393 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001394 case PP_MACRO_OBJECT_LIKE:
1395 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001396 // If we already have a macro, that means that we've hit the end
1397 // of the definition of the macro we were looking for. We're
1398 // done.
1399 if (Macro)
1400 return;
1401
1402 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1403 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001404 Error("macro must have a name in AST file");
Douglas Gregorc3366a52009-04-21 23:56:24 +00001405 return;
1406 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00001407 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001408 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001409
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001410 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001411 MI->setIsUsed(isUsed);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001412 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001413
Douglas Gregoraae92242010-03-19 21:51:54 +00001414 unsigned NextIndex = 3;
Sebastian Redl539c5062010-08-18 23:57:32 +00001415 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001416 // Decode function-like macro info.
1417 bool isC99VarArgs = Record[3];
1418 bool isGNUVarArgs = Record[4];
1419 MacroArgs.clear();
1420 unsigned NumArgs = Record[5];
Douglas Gregoraae92242010-03-19 21:51:54 +00001421 NextIndex = 6 + NumArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001422 for (unsigned i = 0; i != NumArgs; ++i)
1423 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1424
1425 // Install function-like macro info.
1426 MI->setIsFunctionLike();
1427 if (isC99VarArgs) MI->setIsC99Varargs();
1428 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001429 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001430 PP->getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001431 }
1432
1433 // Finally, install the macro.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001434 PP->setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001435
1436 // Remember that we saw this macro last so that we add the tokens that
1437 // form its body to it.
1438 Macro = MI;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001439
Douglas Gregoraae92242010-03-19 21:51:54 +00001440 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1441 // We have a macro definition. Load it now.
1442 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1443 getMacroDefinition(Record[NextIndex]));
1444 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001445
Douglas Gregorc3366a52009-04-21 23:56:24 +00001446 ++NumMacrosRead;
1447 break;
1448 }
Mike Stump11289f42009-09-09 15:08:12 +00001449
Sebastian Redl539c5062010-08-18 23:57:32 +00001450 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001451 // If we see a TOKEN before a PP_MACRO_*, then the file is
1452 // erroneous, just pretend we didn't see this.
1453 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001454
Douglas Gregorc3366a52009-04-21 23:56:24 +00001455 Token Tok;
1456 Tok.startToken();
Sebastian Redl2c373b92010-10-05 15:59:54 +00001457 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001458 Tok.setLength(Record[1]);
1459 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1460 Tok.setIdentifierInfo(II);
1461 Tok.setKind((tok::TokenKind)Record[3]);
1462 Tok.setFlag((Token::TokenFlags)Record[4]);
1463 Macro->AddTokenToBody(Tok);
1464 break;
1465 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001466
Sebastian Redl539c5062010-08-18 23:57:32 +00001467 case PP_MACRO_INSTANTIATION: {
Douglas Gregoraae92242010-03-19 21:51:54 +00001468 // If we already have a macro, that means that we've hit the end
1469 // of the definition of the macro we were looking for. We're
1470 // done.
1471 if (Macro)
1472 return;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001473
Douglas Gregoraae92242010-03-19 21:51:54 +00001474 if (!PP->getPreprocessingRecord()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001475 Error("missing preprocessing record in AST file");
Douglas Gregoraae92242010-03-19 21:51:54 +00001476 return;
1477 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001478
Douglas Gregoraae92242010-03-19 21:51:54 +00001479 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1480 if (PPRec.getPreprocessedEntity(Record[0]))
1481 return;
1482
1483 MacroInstantiation *MI
1484 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
Sebastian Redl2c373b92010-10-05 15:59:54 +00001485 SourceRange(ReadSourceLocation(F, Record[1]),
1486 ReadSourceLocation(F, Record[2])),
Douglas Gregoraae92242010-03-19 21:51:54 +00001487 getMacroDefinition(Record[4]));
1488 PPRec.SetPreallocatedEntity(Record[0], MI);
1489 return;
1490 }
1491
Sebastian Redl539c5062010-08-18 23:57:32 +00001492 case PP_MACRO_DEFINITION: {
Douglas Gregoraae92242010-03-19 21:51:54 +00001493 // If we already have a macro, that means that we've hit the end
1494 // of the definition of the macro we were looking for. We're
1495 // done.
1496 if (Macro)
1497 return;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001498
Douglas Gregoraae92242010-03-19 21:51:54 +00001499 if (!PP->getPreprocessingRecord()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001500 Error("missing preprocessing record in AST file");
Douglas Gregoraae92242010-03-19 21:51:54 +00001501 return;
1502 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001503
Douglas Gregoraae92242010-03-19 21:51:54 +00001504 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1505 if (PPRec.getPreprocessedEntity(Record[0]))
1506 return;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001507
Douglas Gregor91096292010-10-02 19:29:26 +00001508 if (Record[1] > MacroDefinitionsLoaded.size()) {
Douglas Gregoraae92242010-03-19 21:51:54 +00001509 Error("out-of-bounds macro definition record");
1510 return;
1511 }
1512
Douglas Gregor91096292010-10-02 19:29:26 +00001513 // Decode the identifier info and then check again; if the macro is
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001514 // still defined and associated with the identifier,
Douglas Gregor91096292010-10-02 19:29:26 +00001515 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1516 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1517 MacroDefinition *MD
1518 = new (PPRec) MacroDefinition(II,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001519 ReadSourceLocation(F, Record[5]),
Douglas Gregor36ea4d42010-10-01 20:33:34 +00001520 SourceRange(
Sebastian Redl2c373b92010-10-05 15:59:54 +00001521 ReadSourceLocation(F, Record[2]),
1522 ReadSourceLocation(F, Record[3])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001523
Douglas Gregor91096292010-10-02 19:29:26 +00001524 PPRec.SetPreallocatedEntity(Record[0], MD);
1525 MacroDefinitionsLoaded[Record[1] - 1] = MD;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001526
Douglas Gregor91096292010-10-02 19:29:26 +00001527 if (DeserializationListener)
1528 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1529 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001530
Douglas Gregoraae92242010-03-19 21:51:54 +00001531 return;
1532 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001533
Douglas Gregor796d76a2010-10-20 22:00:55 +00001534 case PP_INCLUSION_DIRECTIVE: {
1535 // If we already have a macro, that means that we've hit the end
1536 // of the definition of the macro we were looking for. We're
1537 // done.
1538 if (Macro)
1539 return;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001540
Douglas Gregor796d76a2010-10-20 22:00:55 +00001541 if (!PP->getPreprocessingRecord()) {
1542 Error("missing preprocessing record in AST file");
1543 return;
1544 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001545
Douglas Gregor796d76a2010-10-20 22:00:55 +00001546 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1547 if (PPRec.getPreprocessedEntity(Record[0]))
1548 return;
1549
1550 const char *FullFileNameStart = BlobStart + Record[3];
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001551 const FileEntry *File
Douglas Gregor796d76a2010-10-20 22:00:55 +00001552 = PP->getFileManager().getFile(FullFileNameStart,
1553 FullFileNameStart + (BlobLen - Record[3]));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001554
Douglas Gregor796d76a2010-10-20 22:00:55 +00001555 // FIXME: Stable encoding
1556 InclusionDirective::InclusionKind Kind
1557 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1558 InclusionDirective *ID
1559 = new (PPRec) InclusionDirective(Kind,
1560 llvm::StringRef(BlobStart, Record[3]),
1561 Record[4],
1562 File,
1563 SourceRange(ReadSourceLocation(F, Record[1]),
1564 ReadSourceLocation(F, Record[2])));
1565 PPRec.SetPreallocatedEntity(Record[0], ID);
1566 return;
1567 }
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001568 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001569 }
1570}
1571
Sebastian Redl2c499f62010-08-18 23:56:43 +00001572void ASTReader::ReadDefinedMacros() {
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001573 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001574 PerFileData &F = *Chain[N - I - 1];
1575 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001576
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001577 // If there was no preprocessor block, skip this file.
1578 if (!MacroCursor.getBitStreamReader())
1579 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001580
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001581 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001582 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001583
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001584 RecordData Record;
1585 while (true) {
Sebastian Redl4102dd52010-09-28 02:55:49 +00001586 uint64_t Offset = Cursor.GetCurrentBitNo();
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001587 unsigned Code = Cursor.ReadCode();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001588 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001589 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001590
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001591 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1592 // No known subblocks, always skip them.
1593 Cursor.ReadSubBlockID();
1594 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001595 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001596 return;
1597 }
1598 continue;
1599 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001600
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001601 if (Code == llvm::bitc::DEFINE_ABBREV) {
1602 Cursor.ReadAbbrevRecord();
1603 continue;
1604 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001605
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001606 // Read a record.
1607 const char *BlobStart;
1608 unsigned BlobLen;
1609 Record.clear();
1610 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1611 default: // Default behavior: ignore.
1612 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001613
Sebastian Redl539c5062010-08-18 23:57:32 +00001614 case PP_MACRO_OBJECT_LIKE:
1615 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001616 DecodeIdentifierInfo(Record[0]);
1617 break;
1618
Sebastian Redl539c5062010-08-18 23:57:32 +00001619 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001620 // Ignore tokens.
1621 break;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001622
Sebastian Redl539c5062010-08-18 23:57:32 +00001623 case PP_MACRO_INSTANTIATION:
1624 case PP_MACRO_DEFINITION:
Douglas Gregor796d76a2010-10-20 22:00:55 +00001625 case PP_INCLUSION_DIRECTIVE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001626 // Read the macro record.
Sebastian Redl4102dd52010-09-28 02:55:49 +00001627 // FIXME: That's a stupid way to do this. We should reuse this cursor.
Sebastian Redl2c373b92010-10-05 15:59:54 +00001628 ReadMacroRecord(F, Offset);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001629 break;
1630 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001631 }
1632 }
1633}
1634
Sebastian Redl50e26582010-09-15 19:54:06 +00001635MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor91096292010-10-02 19:29:26 +00001636 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregoraae92242010-03-19 21:51:54 +00001637 return 0;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001638
Douglas Gregor91096292010-10-02 19:29:26 +00001639 if (!MacroDefinitionsLoaded[ID - 1]) {
1640 unsigned Index = ID - 1;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001641 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1642 PerFileData &F = *Chain[N - I - 1];
1643 if (Index < F.LocalNumMacroDefinitions) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001644 ReadMacroRecord(F, F.MacroDefinitionOffsets[Index]);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001645 break;
1646 }
1647 Index -= F.LocalNumMacroDefinitions;
1648 }
Douglas Gregor91096292010-10-02 19:29:26 +00001649 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001650 }
1651
Douglas Gregor91096292010-10-02 19:29:26 +00001652 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001653}
1654
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001655/// \brief If we are loading a relocatable PCH file, and the filename is
1656/// not an absolute path, add the system root to the beginning of the file
1657/// name.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001658void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001659 // If this is not a relocatable PCH file, there's nothing to do.
1660 if (!RelocatablePCH)
1661 return;
Mike Stump11289f42009-09-09 15:08:12 +00001662
Daniel Dunbarf2ce9a22009-11-18 19:50:41 +00001663 if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001664 return;
1665
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001666 if (isysroot == 0) {
1667 // If no system root was given, default to '/'
1668 Filename.insert(Filename.begin(), '/');
1669 return;
1670 }
Mike Stump11289f42009-09-09 15:08:12 +00001671
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001672 unsigned Length = strlen(isysroot);
1673 if (isysroot[Length - 1] != '/')
1674 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001675
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001676 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1677}
1678
Sebastian Redl2c499f62010-08-18 23:56:43 +00001679ASTReader::ASTReadResult
Sebastian Redl3e31c722010-08-18 23:56:56 +00001680ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001681 llvm::BitstreamCursor &Stream = F.Stream;
1682
Sebastian Redl539c5062010-08-18 23:57:32 +00001683 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001684 Error("malformed block record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001685 return Failure;
1686 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001687
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001688 // Read all of the records and blocks for the ASt file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001689 RecordData Record;
Sebastian Redl393f8b72010-07-19 20:52:06 +00001690 bool First = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001691 while (!Stream.AtEndOfStream()) {
1692 unsigned Code = Stream.ReadCode();
1693 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001694 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001695 Error("error at end of module block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001696 return Failure;
1697 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001698
Douglas Gregor55abb232009-04-10 20:39:37 +00001699 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001700 }
1701
1702 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1703 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001704 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001705 // We lazily load the decls block, but we want to set up the
1706 // DeclsCursor cursor to point into it. Clone our current bitcode
1707 // cursor to it, enter the block and read the abbrevs in that block.
1708 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001709 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001710 if (Stream.SkipBlock() || // Skip with the main cursor.
1711 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001712 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001713 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001714 return Failure;
1715 }
1716 break;
Mike Stump11289f42009-09-09 15:08:12 +00001717
Sebastian Redl539c5062010-08-18 23:57:32 +00001718 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001719 F.MacroCursor = Stream;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001720 if (PP)
1721 PP->setExternalSource(this);
1722
Douglas Gregor796d76a2010-10-20 22:00:55 +00001723 if (Stream.SkipBlock() ||
1724 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001725 Error("malformed block record in AST file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001726 return Failure;
1727 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001728 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001729 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001730
Sebastian Redl539c5062010-08-18 23:57:32 +00001731 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001732 switch (ReadSourceManagerBlock(F)) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001733 case Success:
1734 break;
1735
1736 case Failure:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001737 Error("malformed source manager block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001738 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001739
1740 case IgnorePCH:
1741 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001742 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001743 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001744 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00001745 First = false;
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001746 continue;
1747 }
1748
1749 if (Code == llvm::bitc::DEFINE_ABBREV) {
1750 Stream.ReadAbbrevRecord();
1751 continue;
1752 }
1753
1754 // Read and process a record.
1755 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001756 const char *BlobStart = 0;
1757 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001758 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001759 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001760 default: // Default behavior: ignore.
1761 break;
1762
Sebastian Redl539c5062010-08-18 23:57:32 +00001763 case METADATA: {
1764 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1765 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001766 : diag::warn_pch_version_too_new);
1767 return IgnorePCH;
1768 }
1769
1770 RelocatablePCH = Record[4];
1771 if (Listener) {
1772 std::string TargetTriple(BlobStart, BlobLen);
1773 if (Listener->ReadTargetTriple(TargetTriple))
1774 return IgnorePCH;
1775 }
1776 break;
1777 }
1778
Sebastian Redl539c5062010-08-18 23:57:32 +00001779 case CHAINED_METADATA: {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001780 if (!First) {
1781 Error("CHAINED_METADATA is not first record in block");
1782 return Failure;
1783 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001784 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1785 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001786 : diag::warn_pch_version_too_new);
1787 return IgnorePCH;
1788 }
1789
Sebastian Redl009e7f22010-10-05 16:15:19 +00001790 // Load the chained file, which is always a PCH file.
1791 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001792 case Failure: return Failure;
1793 // If we have to ignore the dependency, we'll have to ignore this too.
1794 case IgnorePCH: return IgnorePCH;
1795 case Success: break;
1796 }
1797 break;
1798 }
1799
Sebastian Redl539c5062010-08-18 23:57:32 +00001800 case TYPE_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001801 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001802 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001803 return Failure;
1804 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001805 F.TypeOffsets = (const uint32_t *)BlobStart;
1806 F.LocalNumTypes = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001807 break;
1808
Sebastian Redl539c5062010-08-18 23:57:32 +00001809 case DECL_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001810 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001811 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001812 return Failure;
1813 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001814 F.DeclOffsets = (const uint32_t *)BlobStart;
1815 F.LocalNumDecls = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001816 break;
Douglas Gregor55abb232009-04-10 20:39:37 +00001817
Sebastian Redl539c5062010-08-18 23:57:32 +00001818 case TU_UPDATE_LEXICAL: {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001819 DeclContextInfo Info = {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001820 /* No visible information */ 0,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001821 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
1822 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001823 };
Douglas Gregoraa433012010-10-01 01:18:02 +00001824 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
1825 .push_back(Info);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001826 break;
1827 }
1828
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001829 case UPDATE_VISIBLE: {
1830 serialization::DeclID ID = Record[0];
1831 void *Table = ASTDeclContextNameLookupTable::Create(
1832 (const unsigned char *)BlobStart + Record[1],
1833 (const unsigned char *)BlobStart,
1834 ASTDeclContextNameLookupTrait(*this));
Douglas Gregoraa433012010-10-01 01:18:02 +00001835 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001836 DeclContextInfo Info = {
1837 Table, /* No lexical inforamtion */ 0, 0
1838 };
1839 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1840 } else
1841 PendingVisibleUpdates[ID].push_back(Table);
1842 break;
1843 }
1844
Sebastian Redl539c5062010-08-18 23:57:32 +00001845 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001846 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1847 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001848 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001849 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1850 Latest > FirstLatestDeclIDs[First]) &&
1851 "The new latest is supposed to come after the previous latest");
1852 FirstLatestDeclIDs[First] = Latest;
1853 }
1854 break;
1855 }
1856
Sebastian Redl539c5062010-08-18 23:57:32 +00001857 case LANGUAGE_OPTIONS:
Douglas Gregorce3a8292010-07-27 00:27:13 +00001858 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor55abb232009-04-10 20:39:37 +00001859 return IgnorePCH;
1860 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00001861
Sebastian Redl539c5062010-08-18 23:57:32 +00001862 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001863 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00001864 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001865 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001866 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00001867 (const unsigned char *)F.IdentifierTableData + Record[0],
1868 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001869 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001870 if (PP)
1871 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00001872 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001873 break;
1874
Sebastian Redl539c5062010-08-18 23:57:32 +00001875 case IDENTIFIER_OFFSET:
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001876 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001877 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001878 return Failure;
1879 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001880 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1881 F.LocalNumIdentifiers = Record[0];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001882 break;
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001883
Sebastian Redl539c5062010-08-18 23:57:32 +00001884 case EXTERNAL_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001885 // Optimization for the first block.
1886 if (ExternalDefinitions.empty())
1887 ExternalDefinitions.swap(Record);
1888 else
1889 ExternalDefinitions.insert(ExternalDefinitions.end(),
1890 Record.begin(), Record.end());
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001891 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00001892
Sebastian Redl539c5062010-08-18 23:57:32 +00001893 case SPECIAL_TYPES:
Sebastian Redlb293a452010-07-20 21:20:32 +00001894 // Optimization for the first block
1895 if (SpecialTypes.empty())
1896 SpecialTypes.swap(Record);
1897 else
1898 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregor652d82a2009-04-18 05:55:16 +00001899 break;
1900
Sebastian Redl539c5062010-08-18 23:57:32 +00001901 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001902 TotalNumStatements += Record[0];
1903 TotalNumMacros += Record[1];
1904 TotalLexicalDeclContexts += Record[2];
1905 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00001906 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00001907
Sebastian Redl539c5062010-08-18 23:57:32 +00001908 case TENTATIVE_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001909 // Optimization for the first block.
1910 if (TentativeDefinitions.empty())
1911 TentativeDefinitions.swap(Record);
1912 else
1913 TentativeDefinitions.insert(TentativeDefinitions.end(),
1914 Record.begin(), Record.end());
Douglas Gregord4df8652009-04-22 22:02:47 +00001915 break;
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001916
Sebastian Redl539c5062010-08-18 23:57:32 +00001917 case UNUSED_FILESCOPED_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001918 // Optimization for the first block.
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00001919 if (UnusedFileScopedDecls.empty())
1920 UnusedFileScopedDecls.swap(Record);
Sebastian Redlb293a452010-07-20 21:20:32 +00001921 else
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00001922 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1923 Record.begin(), Record.end());
Tanya Lattner90073802010-02-12 00:07:30 +00001924 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001925
Sebastian Redl539c5062010-08-18 23:57:32 +00001926 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001927 // Later blocks overwrite earlier ones.
1928 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00001929 break;
1930
Sebastian Redl539c5062010-08-18 23:57:32 +00001931 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001932 // Optimization for the first block.
1933 if (LocallyScopedExternalDecls.empty())
1934 LocallyScopedExternalDecls.swap(Record);
1935 else
1936 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1937 Record.begin(), Record.end());
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001938 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001939
Sebastian Redl539c5062010-08-18 23:57:32 +00001940 case SELECTOR_OFFSETS:
Sebastian Redla19a67f2010-08-03 21:58:15 +00001941 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00001942 F.LocalNumSelectors = Record[0];
Douglas Gregor95c13f52009-04-25 17:48:32 +00001943 break;
1944
Sebastian Redl539c5062010-08-18 23:57:32 +00001945 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00001946 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001947 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00001948 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001949 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00001950 F.SelectorLookupTableData + Record[0],
1951 F.SelectorLookupTableData,
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001952 ASTSelectorLookupTrait(*this));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00001953 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00001954 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00001955
Sebastian Redl96371b42010-09-22 00:42:30 +00001956 case REFERENCED_SELECTOR_POOL:
Sebastian Redl2c373b92010-10-05 15:59:54 +00001957 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001958 break;
1959
Sebastian Redl539c5062010-08-18 23:57:32 +00001960 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001961 if (!Record.empty() && Listener)
1962 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00001963 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00001964
Sebastian Redl539c5062010-08-18 23:57:32 +00001965 case SOURCE_LOCATION_OFFSETS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001966 F.SLocOffsets = (const uint32_t *)BlobStart;
1967 F.LocalNumSLocEntries = Record[0];
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001968 F.LocalSLocSize = Record[1];
Douglas Gregor258ae542009-04-27 06:38:32 +00001969 break;
1970
Sebastian Redl539c5062010-08-18 23:57:32 +00001971 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl96371b42010-09-22 00:42:30 +00001972 if (PreloadSLocEntries.empty())
1973 PreloadSLocEntries.swap(Record);
1974 else
1975 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
1976 Record.begin(), Record.end());
Douglas Gregor258ae542009-04-27 06:38:32 +00001977 break;
Douglas Gregorc5046832009-04-27 18:38:38 +00001978
Sebastian Redl539c5062010-08-18 23:57:32 +00001979 case STAT_CACHE: {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001980 ASTStatCache *MyStatCache =
1981 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001982 (const unsigned char *)BlobStart,
1983 NumStatHits, NumStatMisses);
1984 FileMgr.addStatCache(MyStatCache);
Sebastian Redl34522812010-07-16 17:50:48 +00001985 F.StatCache = MyStatCache;
Douglas Gregorc5046832009-04-27 18:38:38 +00001986 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001987 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001988
Sebastian Redl539c5062010-08-18 23:57:32 +00001989 case EXT_VECTOR_DECLS:
Sebastian Redl04f5c312010-07-28 21:38:49 +00001990 // Optimization for the first block.
1991 if (ExtVectorDecls.empty())
1992 ExtVectorDecls.swap(Record);
1993 else
1994 ExtVectorDecls.insert(ExtVectorDecls.end(),
1995 Record.begin(), Record.end());
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001996 break;
1997
Sebastian Redl539c5062010-08-18 23:57:32 +00001998 case VTABLE_USES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001999 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002000 VTableUses.swap(Record);
2001 break;
2002
Sebastian Redl539c5062010-08-18 23:57:32 +00002003 case DYNAMIC_CLASSES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002004 // Optimization for the first block.
2005 if (DynamicClasses.empty())
2006 DynamicClasses.swap(Record);
2007 else
2008 DynamicClasses.insert(DynamicClasses.end(),
2009 Record.begin(), Record.end());
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002010 break;
2011
Sebastian Redl539c5062010-08-18 23:57:32 +00002012 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002013 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002014 break;
2015
Sebastian Redl539c5062010-08-18 23:57:32 +00002016 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002017 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002018 SemaDeclRefs.swap(Record);
2019 break;
2020
Sebastian Redl539c5062010-08-18 23:57:32 +00002021 case ORIGINAL_FILE_NAME:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002022 // The primary AST will be the last to get here, so it will be the one
Sebastian Redlb293a452010-07-20 21:20:32 +00002023 // that's used.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00002024 ActualOriginalFileName.assign(BlobStart, BlobLen);
2025 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002026 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00002027 break;
Mike Stump11289f42009-09-09 15:08:12 +00002028
Sebastian Redl539c5062010-08-18 23:57:32 +00002029 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00002030 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002031 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2032 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2033 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregord54f3a12009-10-05 21:07:28 +00002034 return IgnorePCH;
2035 }
2036 break;
2037 }
Sebastian Redlfa061442010-07-21 20:07:32 +00002038
Sebastian Redl539c5062010-08-18 23:57:32 +00002039 case MACRO_DEFINITION_OFFSETS:
Sebastian Redlfa061442010-07-21 20:07:32 +00002040 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2041 F.NumPreallocatedPreprocessingEntities = Record[0];
2042 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregoraae92242010-03-19 21:51:54 +00002043 break;
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002044
Sebastian Redl539c5062010-08-18 23:57:32 +00002045 case DECL_REPLACEMENTS: {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002046 if (Record.size() % 2 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002047 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002048 return Failure;
2049 }
2050 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl539c5062010-08-18 23:57:32 +00002051 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002052 std::make_pair(&F, Record[I+1]);
2053 break;
2054 }
Sebastian Redlaba202b2010-08-24 22:50:19 +00002055
2056 case ADDITIONAL_TEMPLATE_SPECIALIZATIONS: {
2057 AdditionalTemplateSpecializations &ATS =
2058 AdditionalTemplateSpecializationsPending[Record[0]];
2059 ATS.insert(ATS.end(), Record.begin()+1, Record.end());
2060 break;
2061 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002062 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00002063 First = false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002064 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002065 Error("premature end of bitstream in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002066 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002067}
2068
Sebastian Redl009e7f22010-10-05 16:15:19 +00002069ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2070 ASTFileType Type) {
2071 switch(ReadASTCore(FileName, Type)) {
Sebastian Redl2abc0382010-07-16 20:41:52 +00002072 case Failure: return Failure;
2073 case IgnorePCH: return IgnorePCH;
2074 case Success: break;
2075 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002076
2077 // Here comes stuff that we only do once the entire chain is loaded.
2078
Sebastian Redl96371b42010-09-22 00:42:30 +00002079 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redlfa061442010-07-21 20:07:32 +00002080 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redlada023c2010-08-04 20:40:17 +00002081 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2082 TotalNumSelectors = 0;
Sebastian Redl9e687992010-07-19 22:06:55 +00002083 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl96371b42010-09-22 00:42:30 +00002084 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002085 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002086 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl9e687992010-07-19 22:06:55 +00002087 TotalNumTypes += Chain[I]->LocalNumTypes;
2088 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redlfa061442010-07-21 20:07:32 +00002089 TotalNumPreallocatedPreprocessingEntities +=
2090 Chain[I]->NumPreallocatedPreprocessingEntities;
2091 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redlada023c2010-08-04 20:40:17 +00002092 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl9e687992010-07-19 22:06:55 +00002093 }
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002094 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002095 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl9e687992010-07-19 22:06:55 +00002096 TypesLoaded.resize(TotalNumTypes);
2097 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redlfa061442010-07-21 20:07:32 +00002098 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2099 if (PP) {
2100 if (TotalNumIdentifiers > 0)
2101 PP->getHeaderSearchInfo().SetExternalLookup(this);
2102 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2103 if (!PP->getPreprocessingRecord())
2104 PP->createPreprocessingRecord();
2105 PP->getPreprocessingRecord()->SetExternalSource(*this,
2106 TotalNumPreallocatedPreprocessingEntities);
2107 }
2108 }
Sebastian Redlada023c2010-08-04 20:40:17 +00002109 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl96371b42010-09-22 00:42:30 +00002110 // Preload SLocEntries.
2111 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2112 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2113 if (Result != Success)
2114 return Result;
2115 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002116
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002117 // Check the predefines buffers.
Douglas Gregorce3a8292010-07-27 00:27:13 +00002118 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002119 return IgnorePCH;
2120
2121 if (PP) {
2122 // Initialization of keywords and pragmas occurs before the
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002123 // AST file is read, so there may be some identifiers that were
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002124 // loaded into the IdentifierTable before we intercepted the
2125 // creation of identifiers. Iterate through the list of known
2126 // identifiers and determine whether we have to establish
2127 // preprocessor definitions or top-level identifier declaration
2128 // chains for those identifiers.
2129 //
2130 // We copy the IdentifierInfo pointers to a small vector first,
2131 // since de-serializing declarations or macro definitions can add
2132 // new entries into the identifier table, invalidating the
2133 // iterators.
2134 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2135 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2136 IdEnd = PP->getIdentifierTable().end();
2137 Id != IdEnd; ++Id)
2138 Identifiers.push_back(Id->second);
Sebastian Redlfa061442010-07-21 20:07:32 +00002139 // We need to search the tables in all files.
Sebastian Redlfa061442010-07-21 20:07:32 +00002140 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002141 ASTIdentifierLookupTable *IdTable
2142 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2143 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl5c415f32010-07-22 17:01:13 +00002144 // ones.
2145 if (!IdTable)
2146 continue;
Sebastian Redlfa061442010-07-21 20:07:32 +00002147 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2148 IdentifierInfo *II = Identifiers[I];
2149 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00002150 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redlfa061442010-07-21 20:07:32 +00002151 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002152 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redlb293a452010-07-20 21:20:32 +00002153 if (Pos == IdTable->end())
2154 continue;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002155
Sebastian Redlb293a452010-07-20 21:20:32 +00002156 // Dereferencing the iterator has the effect of populating the
2157 // IdentifierInfo node with the various declarations it needs.
2158 (void)*Pos;
2159 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002160 }
2161 }
2162
2163 if (Context)
2164 InitializeContext(*Context);
2165
2166 return Success;
2167}
2168
Sebastian Redl009e7f22010-10-05 16:15:19 +00002169ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2170 ASTFileType Type) {
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002171 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl009e7f22010-10-05 16:15:19 +00002172 Chain.push_back(new PerFileData(Type));
Sebastian Redl34522812010-07-16 17:50:48 +00002173 PerFileData &F = *Chain.back();
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002174 if (Prev)
2175 Prev->NextInSource = &F;
2176 else
2177 FirstInSource = &F;
2178 F.Loaders.push_back(Prev);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002179
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002180 // Set the AST file name.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002181 F.FileName = FileName;
2182
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002183 // Open the AST file.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002184 //
2185 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2186 std::string ErrStr;
2187 F.Buffer.reset(llvm::MemoryBuffer::getFileOrSTDIN(FileName, &ErrStr));
2188 if (!F.Buffer) {
2189 Error(ErrStr.c_str());
2190 return IgnorePCH;
2191 }
2192
2193 // Initialize the stream
2194 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2195 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl34522812010-07-16 17:50:48 +00002196 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002197 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002198 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002199
2200 // Sniff for the signature.
2201 if (Stream.Read(8) != 'C' ||
2202 Stream.Read(8) != 'P' ||
2203 Stream.Read(8) != 'C' ||
2204 Stream.Read(8) != 'H') {
2205 Diag(diag::err_not_a_pch_file) << FileName;
2206 return Failure;
2207 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002208
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002209 while (!Stream.AtEndOfStream()) {
2210 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002211
Douglas Gregor92863e42009-04-10 23:10:45 +00002212 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002213 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002214 return Failure;
2215 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002216
2217 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002218
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002219 // We only know the AST subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002220 switch (BlockID) {
2221 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002222 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002223 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002224 return Failure;
2225 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002226 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00002227 case AST_BLOCK_ID:
Sebastian Redl3e31c722010-08-18 23:56:56 +00002228 switch (ReadASTBlock(F)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002229 case Success:
2230 break;
2231
2232 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00002233 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00002234
2235 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00002236 // FIXME: We could consider reading through to the end of this
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002237 // AST block, skipping subblocks, to see if there are other
2238 // AST blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00002239
2240 // Clear out any preallocated source location entries, so that
2241 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002242 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00002243
2244 // Remove the stat cache.
Sebastian Redl34522812010-07-16 17:50:48 +00002245 if (F.StatCache)
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002246 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00002247
Douglas Gregor92863e42009-04-10 23:10:45 +00002248 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002249 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002250 break;
2251 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002252 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002253 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002254 return Failure;
2255 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002256 break;
2257 }
Mike Stump11289f42009-09-09 15:08:12 +00002258 }
2259
Sebastian Redl2abc0382010-07-16 20:41:52 +00002260 return Success;
2261}
2262
Sebastian Redl2c499f62010-08-18 23:56:43 +00002263void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002264 PP = &pp;
Sebastian Redlfa061442010-07-21 20:07:32 +00002265
2266 unsigned TotalNum = 0;
2267 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2268 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2269 if (TotalNum) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002270 if (!PP->getPreprocessingRecord())
2271 PP->createPreprocessingRecord();
Sebastian Redlfa061442010-07-21 20:07:32 +00002272 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregoraae92242010-03-19 21:51:54 +00002273 }
2274}
2275
Sebastian Redl2c499f62010-08-18 23:56:43 +00002276void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002277 Context = &Ctx;
2278 assert(Context && "Passed null context!");
2279
2280 assert(PP && "Forgot to set Preprocessor ?");
2281 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2282 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00002283 PP->setExternalSource(this);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002284
Douglas Gregoraa433012010-10-01 01:18:02 +00002285 // If we have an update block for the TU waiting, we have to add it before
2286 // deserializing the decl.
2287 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2288 if (DCU != DeclContextOffsets.end()) {
2289 // Insertion could invalidate map, so grab vector.
2290 DeclContextInfos T;
2291 T.swap(DCU->second);
2292 DeclContextOffsets.erase(DCU);
2293 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2294 }
2295
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002296 // Load the translation unit declaration
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00002297 GetTranslationUnitDecl();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002298
2299 // Load the special types.
2300 Context->setBuiltinVaListType(
Sebastian Redl539c5062010-08-18 23:57:32 +00002301 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2302 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002303 Context->setObjCIdType(GetType(Id));
Sebastian Redl539c5062010-08-18 23:57:32 +00002304 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002305 Context->setObjCSelType(GetType(Sel));
Sebastian Redl539c5062010-08-18 23:57:32 +00002306 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002307 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl539c5062010-08-18 23:57:32 +00002308 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002309 Context->setObjCClassType(GetType(Class));
Steve Naroff7cae42b2009-07-10 23:34:53 +00002310
Sebastian Redl539c5062010-08-18 23:57:32 +00002311 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002312 Context->setCFConstantStringType(GetType(String));
Mike Stump11289f42009-09-09 15:08:12 +00002313 if (unsigned FastEnum
Sebastian Redl539c5062010-08-18 23:57:32 +00002314 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002315 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl539c5062010-08-18 23:57:32 +00002316 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregor27821ce2009-07-07 16:35:42 +00002317 QualType FileType = GetType(File);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002318 if (FileType.isNull()) {
2319 Error("FILE type is NULL");
2320 return;
2321 }
John McCall9dd450b2009-09-21 23:43:11 +00002322 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregor27821ce2009-07-07 16:35:42 +00002323 Context->setFILEDecl(Typedef->getDecl());
2324 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002325 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002326 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002327 Error("Invalid FILE type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002328 return;
2329 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002330 Context->setFILEDecl(Tag->getDecl());
2331 }
2332 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002333 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002334 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002335 if (Jmp_bufType.isNull()) {
2336 Error("jmp_bug type is NULL");
2337 return;
2338 }
John McCall9dd450b2009-09-21 23:43:11 +00002339 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002340 Context->setjmp_bufDecl(Typedef->getDecl());
2341 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002342 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002343 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002344 Error("Invalid jmp_buf type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002345 return;
2346 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002347 Context->setjmp_bufDecl(Tag->getDecl());
2348 }
2349 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002350 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002351 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002352 if (Sigjmp_bufType.isNull()) {
2353 Error("sigjmp_buf type is NULL");
2354 return;
2355 }
John McCall9dd450b2009-09-21 23:43:11 +00002356 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002357 Context->setsigjmp_bufDecl(Typedef->getDecl());
2358 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002359 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002360 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stumpa4de80b2009-07-28 02:25:19 +00002361 Context->setsigjmp_bufDecl(Tag->getDecl());
2362 }
2363 }
Mike Stump11289f42009-09-09 15:08:12 +00002364 if (unsigned ObjCIdRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002365 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002366 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump11289f42009-09-09 15:08:12 +00002367 if (unsigned ObjCClassRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002368 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002369 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002370 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpd0153282009-10-20 02:12:22 +00002371 Context->setBlockDescriptorType(GetType(String));
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002372 if (unsigned String
Sebastian Redl539c5062010-08-18 23:57:32 +00002373 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002374 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002375 if (unsigned ObjCSelRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002376 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002377 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002378 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002379 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002380
Sebastian Redl539c5062010-08-18 23:57:32 +00002381 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002382 Context->setInt128Installed();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002383}
2384
Douglas Gregor45fe0362009-05-12 01:31:05 +00002385/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002386/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00002387/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002388std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Daniel Dunbar3b951482009-12-03 09:13:06 +00002389 Diagnostic &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002390 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002391 std::string ErrStr;
2392 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002393 Buffer.reset(llvm::MemoryBuffer::getFile(ASTFileName.c_str(), &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00002394 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00002395 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002396 return std::string();
2397 }
2398
2399 // Initialize the stream
2400 llvm::BitstreamReader StreamFile;
2401 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00002402 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00002403 (const unsigned char *)Buffer->getBufferEnd());
2404 Stream.init(StreamFile);
2405
2406 // Sniff for the signature.
2407 if (Stream.Read(8) != 'C' ||
2408 Stream.Read(8) != 'P' ||
2409 Stream.Read(8) != 'C' ||
2410 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002411 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002412 return std::string();
2413 }
2414
2415 RecordData Record;
2416 while (!Stream.AtEndOfStream()) {
2417 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002418
Douglas Gregor45fe0362009-05-12 01:31:05 +00002419 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2420 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00002421
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002422 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002423 switch (BlockID) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002424 case AST_BLOCK_ID:
2425 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002426 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002427 return std::string();
2428 }
2429 break;
Mike Stump11289f42009-09-09 15:08:12 +00002430
Douglas Gregor45fe0362009-05-12 01:31:05 +00002431 default:
2432 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002433 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002434 return std::string();
2435 }
2436 break;
2437 }
2438 continue;
2439 }
2440
2441 if (Code == llvm::bitc::END_BLOCK) {
2442 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002443 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002444 return std::string();
2445 }
2446 continue;
2447 }
2448
2449 if (Code == llvm::bitc::DEFINE_ABBREV) {
2450 Stream.ReadAbbrevRecord();
2451 continue;
2452 }
2453
2454 Record.clear();
2455 const char *BlobStart = 0;
2456 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002457 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl539c5062010-08-18 23:57:32 +00002458 == ORIGINAL_FILE_NAME)
Douglas Gregor45fe0362009-05-12 01:31:05 +00002459 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00002460 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00002461
2462 return std::string();
2463}
2464
Douglas Gregor55abb232009-04-10 20:39:37 +00002465/// \brief Parse the record that corresponds to a LangOptions data
2466/// structure.
2467///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002468/// This routine parses the language options from the AST file and then gives
2469/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00002470///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002471/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002472bool ASTReader::ParseLanguageOptions(
Douglas Gregor55abb232009-04-10 20:39:37 +00002473 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002474 if (Listener) {
2475 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00002476
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002477 #define PARSE_LANGOPT(Option) \
2478 LangOpts.Option = Record[Idx]; \
2479 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00002480
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002481 unsigned Idx = 0;
2482 PARSE_LANGOPT(Trigraphs);
2483 PARSE_LANGOPT(BCPLComment);
2484 PARSE_LANGOPT(DollarIdents);
2485 PARSE_LANGOPT(AsmPreprocessor);
2486 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00002487 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002488 PARSE_LANGOPT(ImplicitInt);
2489 PARSE_LANGOPT(Digraphs);
2490 PARSE_LANGOPT(HexFloats);
2491 PARSE_LANGOPT(C99);
2492 PARSE_LANGOPT(Microsoft);
2493 PARSE_LANGOPT(CPlusPlus);
2494 PARSE_LANGOPT(CPlusPlus0x);
2495 PARSE_LANGOPT(CXXOperatorNames);
2496 PARSE_LANGOPT(ObjC1);
2497 PARSE_LANGOPT(ObjC2);
2498 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00002499 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00002500 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002501 PARSE_LANGOPT(PascalStrings);
2502 PARSE_LANGOPT(WritableStrings);
2503 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00002504 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002505 PARSE_LANGOPT(Exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +00002506 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002507 PARSE_LANGOPT(NeXTRuntime);
2508 PARSE_LANGOPT(Freestanding);
2509 PARSE_LANGOPT(NoBuiltin);
2510 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00002511 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002512 PARSE_LANGOPT(Blocks);
2513 PARSE_LANGOPT(EmitAllDecls);
2514 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00002515 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2516 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002517 PARSE_LANGOPT(HeinousExtensions);
2518 PARSE_LANGOPT(Optimize);
2519 PARSE_LANGOPT(OptimizeSize);
2520 PARSE_LANGOPT(Static);
2521 PARSE_LANGOPT(PICLevel);
2522 PARSE_LANGOPT(GNUInline);
2523 PARSE_LANGOPT(NoInline);
2524 PARSE_LANGOPT(AccessControl);
2525 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00002526 PARSE_LANGOPT(ShortWChar);
Chris Lattner51924e512010-06-26 21:25:03 +00002527 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
2528 LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00002529 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00002530 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002531 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00002532 PARSE_LANGOPT(OpenCL);
Mike Stumpd9546382009-12-12 01:27:46 +00002533 PARSE_LANGOPT(CatchUndefined);
2534 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002535 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00002536
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002537 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00002538 }
Douglas Gregor55abb232009-04-10 20:39:37 +00002539
2540 return false;
2541}
2542
Sebastian Redl2c499f62010-08-18 23:56:43 +00002543void ASTReader::ReadPreprocessedEntities() {
Douglas Gregoraae92242010-03-19 21:51:54 +00002544 ReadDefinedMacros();
2545}
2546
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002547/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002548ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002549 PerFileData *F = 0;
2550 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2551 F = Chain[N - I - 1];
2552 if (Index < F->LocalNumTypes)
2553 break;
2554 Index -= F->LocalNumTypes;
2555 }
2556 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redl2c373b92010-10-05 15:59:54 +00002557 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002558}
2559
2560/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002561///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002562/// The index is the type ID, shifted and minus the number of predefs. This
2563/// routine actually reads the record corresponding to the type at the given
2564/// location. It is a helper routine for GetType, which deals with reading type
2565/// IDs.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002566QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002567 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00002568 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00002569
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002570 // Keep track of where we are in the stream, then jump back there
2571 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002572 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002573
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002574 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00002575
Douglas Gregor1342e842009-07-06 18:54:52 +00002576 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00002577 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00002578
Sebastian Redl2c373b92010-10-05 15:59:54 +00002579 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002580 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002581 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00002582 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2583 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002584 if (Record.size() != 2) {
2585 Error("Incorrect encoding of extended qualifier type");
2586 return QualType();
2587 }
Douglas Gregor455b8f42009-04-15 22:00:08 +00002588 QualType Base = GetType(Record[0]);
John McCall8ccfcb52009-09-24 19:53:00 +00002589 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2590 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00002591 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002592
Sebastian Redl539c5062010-08-18 23:57:32 +00002593 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002594 if (Record.size() != 1) {
2595 Error("Incorrect encoding of complex type");
2596 return QualType();
2597 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002598 QualType ElemType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002599 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002600 }
2601
Sebastian Redl539c5062010-08-18 23:57:32 +00002602 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002603 if (Record.size() != 1) {
2604 Error("Incorrect encoding of pointer type");
2605 return QualType();
2606 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002607 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002608 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002609 }
2610
Sebastian Redl539c5062010-08-18 23:57:32 +00002611 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002612 if (Record.size() != 1) {
2613 Error("Incorrect encoding of block pointer type");
2614 return QualType();
2615 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002616 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002617 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002618 }
2619
Sebastian Redl539c5062010-08-18 23:57:32 +00002620 case TYPE_LVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002621 if (Record.size() != 1) {
2622 Error("Incorrect encoding of lvalue reference type");
2623 return QualType();
2624 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002625 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002626 return Context->getLValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002627 }
2628
Sebastian Redl539c5062010-08-18 23:57:32 +00002629 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002630 if (Record.size() != 1) {
2631 Error("Incorrect encoding of rvalue reference type");
2632 return QualType();
2633 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002634 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002635 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002636 }
2637
Sebastian Redl539c5062010-08-18 23:57:32 +00002638 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00002639 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002640 Error("Incorrect encoding of member pointer type");
2641 return QualType();
2642 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002643 QualType PointeeType = GetType(Record[0]);
2644 QualType ClassType = GetType(Record[1]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002645 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002646 }
2647
Sebastian Redl539c5062010-08-18 23:57:32 +00002648 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002649 QualType ElementType = GetType(Record[0]);
2650 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2651 unsigned IndexTypeQuals = Record[2];
2652 unsigned Idx = 3;
2653 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00002654 return Context->getConstantArrayType(ElementType, Size,
2655 ASM, IndexTypeQuals);
2656 }
2657
Sebastian Redl539c5062010-08-18 23:57:32 +00002658 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002659 QualType ElementType = GetType(Record[0]);
2660 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2661 unsigned IndexTypeQuals = Record[2];
Chris Lattner8575daa2009-04-27 21:45:14 +00002662 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002663 }
2664
Sebastian Redl539c5062010-08-18 23:57:32 +00002665 case TYPE_VARIABLE_ARRAY: {
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002666 QualType ElementType = GetType(Record[0]);
2667 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2668 unsigned IndexTypeQuals = Record[2];
Sebastian Redl2c373b92010-10-05 15:59:54 +00002669 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
2670 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
2671 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor04318252009-07-06 15:59:29 +00002672 ASM, IndexTypeQuals,
2673 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002674 }
2675
Sebastian Redl539c5062010-08-18 23:57:32 +00002676 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002677 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002678 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002679 return QualType();
2680 }
2681
2682 QualType ElementType = GetType(Record[0]);
2683 unsigned NumElements = Record[1];
Chris Lattner37141f42010-06-23 06:00:24 +00002684 unsigned AltiVecSpec = Record[2];
2685 return Context->getVectorType(ElementType, NumElements,
2686 (VectorType::AltiVecSpecific)AltiVecSpec);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002687 }
2688
Sebastian Redl539c5062010-08-18 23:57:32 +00002689 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002690 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002691 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002692 return QualType();
2693 }
2694
2695 QualType ElementType = GetType(Record[0]);
2696 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00002697 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002698 }
2699
Sebastian Redl539c5062010-08-18 23:57:32 +00002700 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002701 if (Record.size() != 4) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002702 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002703 return QualType();
2704 }
2705 QualType ResultType = GetType(Record[0]);
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002706 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00002707 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002708 }
2709
Sebastian Redl539c5062010-08-18 23:57:32 +00002710 case TYPE_FUNCTION_PROTO: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002711 QualType ResultType = GetType(Record[0]);
Douglas Gregordc728752009-12-22 18:11:50 +00002712 bool NoReturn = Record[1];
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002713 unsigned RegParm = Record[2];
2714 CallingConv CallConv = (CallingConv)Record[3];
2715 unsigned Idx = 4;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002716 unsigned NumParams = Record[Idx++];
2717 llvm::SmallVector<QualType, 16> ParamTypes;
2718 for (unsigned I = 0; I != NumParams; ++I)
2719 ParamTypes.push_back(GetType(Record[Idx++]));
2720 bool isVariadic = Record[Idx++];
2721 unsigned Quals = Record[Idx++];
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002722 bool hasExceptionSpec = Record[Idx++];
2723 bool hasAnyExceptionSpec = Record[Idx++];
2724 unsigned NumExceptions = Record[Idx++];
2725 llvm::SmallVector<QualType, 2> Exceptions;
2726 for (unsigned I = 0; I != NumExceptions; ++I)
2727 Exceptions.push_back(GetType(Record[Idx++]));
Jay Foad7d0479f2009-05-21 09:52:38 +00002728 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002729 isVariadic, Quals, hasExceptionSpec,
2730 hasAnyExceptionSpec, NumExceptions,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00002731 Exceptions.data(),
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002732 FunctionType::ExtInfo(NoReturn, RegParm,
2733 CallConv));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002734 }
2735
Sebastian Redl539c5062010-08-18 23:57:32 +00002736 case TYPE_UNRESOLVED_USING:
John McCallb96ec562009-12-04 22:46:56 +00002737 return Context->getTypeDeclType(
2738 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2739
Sebastian Redl539c5062010-08-18 23:57:32 +00002740 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002741 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002742 Error("incorrect encoding of typedef type");
2743 return QualType();
2744 }
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002745 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2746 QualType Canonical = GetType(Record[1]);
2747 return Context->getTypedefType(Decl, Canonical);
2748 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002749
Sebastian Redl539c5062010-08-18 23:57:32 +00002750 case TYPE_TYPEOF_EXPR:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002751 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002752
Sebastian Redl539c5062010-08-18 23:57:32 +00002753 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002754 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002755 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002756 return QualType();
2757 }
2758 QualType UnderlyingType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002759 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002760 }
Mike Stump11289f42009-09-09 15:08:12 +00002761
Sebastian Redl539c5062010-08-18 23:57:32 +00002762 case TYPE_DECLTYPE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002763 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson81df7b82009-06-24 19:06:50 +00002764
Sebastian Redl539c5062010-08-18 23:57:32 +00002765 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002766 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002767 Error("incorrect encoding of record type");
2768 return QualType();
2769 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002770 bool IsDependent = Record[0];
2771 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCall25c9d112010-10-14 21:48:26 +00002772 T->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002773 return T;
2774 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002775
Sebastian Redl539c5062010-08-18 23:57:32 +00002776 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002777 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002778 Error("incorrect encoding of enum type");
2779 return QualType();
2780 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002781 bool IsDependent = Record[0];
2782 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCall25c9d112010-10-14 21:48:26 +00002783 T->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002784 return T;
2785 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00002786
Sebastian Redl539c5062010-08-18 23:57:32 +00002787 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002788 unsigned Idx = 0;
2789 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2790 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2791 QualType NamedType = GetType(Record[Idx++]);
2792 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00002793 }
2794
Sebastian Redl539c5062010-08-18 23:57:32 +00002795 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00002796 unsigned Idx = 0;
2797 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00002798 return Context->getObjCInterfaceType(ItfD);
2799 }
2800
Sebastian Redl539c5062010-08-18 23:57:32 +00002801 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00002802 unsigned Idx = 0;
2803 QualType Base = GetType(Record[Idx++]);
Chris Lattner587cbe12009-04-22 06:45:28 +00002804 unsigned NumProtos = Record[Idx++];
2805 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2806 for (unsigned I = 0; I != NumProtos; ++I)
2807 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002808 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00002809 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002810
Sebastian Redl539c5062010-08-18 23:57:32 +00002811 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00002812 unsigned Idx = 0;
John McCall8b07ec22010-05-15 11:32:37 +00002813 QualType Pointee = GetType(Record[Idx++]);
2814 return Context->getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00002815 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00002816
Sebastian Redl539c5062010-08-18 23:57:32 +00002817 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00002818 unsigned Idx = 0;
2819 QualType Parm = GetType(Record[Idx++]);
2820 QualType Replacement = GetType(Record[Idx++]);
2821 return
2822 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2823 Replacement);
2824 }
John McCalle78aac42010-03-10 03:28:59 +00002825
Sebastian Redl539c5062010-08-18 23:57:32 +00002826 case TYPE_INJECTED_CLASS_NAME: {
John McCalle78aac42010-03-10 03:28:59 +00002827 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2828 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00002829 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002830 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00002831 return
2832 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00002833 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002834
Sebastian Redl539c5062010-08-18 23:57:32 +00002835 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002836 unsigned Idx = 0;
2837 unsigned Depth = Record[Idx++];
2838 unsigned Index = Record[Idx++];
2839 bool Pack = Record[Idx++];
2840 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
2841 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
2842 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002843
Sebastian Redl539c5062010-08-18 23:57:32 +00002844 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002845 unsigned Idx = 0;
2846 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2847 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2848 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00002849 QualType Canon = GetType(Record[Idx++]);
2850 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002851 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002852
Sebastian Redl539c5062010-08-18 23:57:32 +00002853 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002854 unsigned Idx = 0;
2855 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2856 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2857 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2858 unsigned NumArgs = Record[Idx++];
2859 llvm::SmallVector<TemplateArgument, 8> Args;
2860 Args.reserve(NumArgs);
2861 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00002862 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002863 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
2864 Args.size(), Args.data());
2865 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002866
Sebastian Redl539c5062010-08-18 23:57:32 +00002867 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00002868 unsigned Idx = 0;
2869
2870 // ArrayType
2871 QualType ElementType = GetType(Record[Idx++]);
2872 ArrayType::ArraySizeModifier ASM
2873 = (ArrayType::ArraySizeModifier)Record[Idx++];
2874 unsigned IndexTypeQuals = Record[Idx++];
2875
2876 // DependentSizedArrayType
Sebastian Redl2c373b92010-10-05 15:59:54 +00002877 Expr *NumElts = ReadExpr(*Loc.F);
2878 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00002879
2880 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
2881 IndexTypeQuals, Brackets);
2882 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002883
Sebastian Redl539c5062010-08-18 23:57:32 +00002884 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002885 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002886 bool IsDependent = Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002887 TemplateName Name = ReadTemplateName(Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002888 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redl2c373b92010-10-05 15:59:54 +00002889 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002890 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002891 QualType T;
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002892 if (Canon.isNull())
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002893 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
2894 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002895 else
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002896 T = Context->getTemplateSpecializationType(Name, Args.data(),
2897 Args.size(), Canon);
John McCall25c9d112010-10-14 21:48:26 +00002898 T->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002899 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002900 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002901 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002902 // Suppress a GCC warning
2903 return QualType();
2904}
2905
Sebastian Redl2c373b92010-10-05 15:59:54 +00002906class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00002907 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +00002908 ASTReader::PerFileData &F;
Sebastian Redlc67764e2010-07-22 22:43:28 +00002909 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redl2c499f62010-08-18 23:56:43 +00002910 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00002911 unsigned &Idx;
2912
Sebastian Redl2c373b92010-10-05 15:59:54 +00002913 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
2914 unsigned &I) {
2915 return Reader.ReadSourceLocation(F, R, I);
2916 }
2917
John McCall8f115c62009-10-16 21:56:05 +00002918public:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002919 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl2c499f62010-08-18 23:56:43 +00002920 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl2c373b92010-10-05 15:59:54 +00002921 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
2922 { }
John McCall8f115c62009-10-16 21:56:05 +00002923
John McCall17001972009-10-18 01:05:36 +00002924 // We want compile-time assurance that we've enumerated all of
2925 // these, so unfortunately we have to declare them first, then
2926 // define them out-of-line.
2927#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00002928#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00002929 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00002930#include "clang/AST/TypeLocNodes.def"
2931
John McCall17001972009-10-18 01:05:36 +00002932 void VisitFunctionTypeLoc(FunctionTypeLoc);
2933 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00002934};
2935
John McCall17001972009-10-18 01:05:36 +00002936void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00002937 // nothing to do
2938}
John McCall17001972009-10-18 01:05:36 +00002939void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002940 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorc9b7a592010-01-18 18:04:31 +00002941 if (TL.needsExtraLocalData()) {
2942 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
2943 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
2944 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
2945 TL.setModeAttr(Record[Idx++]);
2946 }
John McCall8f115c62009-10-16 21:56:05 +00002947}
John McCall17001972009-10-18 01:05:36 +00002948void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002949 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00002950}
John McCall17001972009-10-18 01:05:36 +00002951void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002952 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00002953}
John McCall17001972009-10-18 01:05:36 +00002954void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002955 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00002956}
John McCall17001972009-10-18 01:05:36 +00002957void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002958 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00002959}
John McCall17001972009-10-18 01:05:36 +00002960void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002961 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00002962}
John McCall17001972009-10-18 01:05:36 +00002963void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002964 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00002965}
John McCall17001972009-10-18 01:05:36 +00002966void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002967 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
2968 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00002969 if (Record[Idx++])
Sebastian Redl2c373b92010-10-05 15:59:54 +00002970 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor12bfa382009-10-17 00:13:19 +00002971 else
John McCall17001972009-10-18 01:05:36 +00002972 TL.setSizeExpr(0);
2973}
2974void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
2975 VisitArrayTypeLoc(TL);
2976}
2977void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
2978 VisitArrayTypeLoc(TL);
2979}
2980void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
2981 VisitArrayTypeLoc(TL);
2982}
2983void TypeLocReader::VisitDependentSizedArrayTypeLoc(
2984 DependentSizedArrayTypeLoc TL) {
2985 VisitArrayTypeLoc(TL);
2986}
2987void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
2988 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002989 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002990}
2991void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002992 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002993}
2994void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002995 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002996}
2997void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00002998 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
2999 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor7fb25412010-10-01 18:44:50 +00003000 TL.setTrailingReturn(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00003001 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCalle6347002009-10-23 01:28:53 +00003002 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall17001972009-10-18 01:05:36 +00003003 }
3004}
3005void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3006 VisitFunctionTypeLoc(TL);
3007}
3008void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3009 VisitFunctionTypeLoc(TL);
3010}
John McCallb96ec562009-12-04 22:46:56 +00003011void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003012 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallb96ec562009-12-04 22:46:56 +00003013}
John McCall17001972009-10-18 01:05:36 +00003014void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003015 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003016}
3017void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003018 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3019 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3020 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003021}
3022void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003023 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3024 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3025 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3026 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003027}
3028void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003029 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003030}
3031void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003032 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003033}
3034void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003035 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003036}
John McCall17001972009-10-18 01:05:36 +00003037void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003038 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003039}
John McCallcebee162009-10-18 09:09:24 +00003040void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3041 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003042 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallcebee162009-10-18 09:09:24 +00003043}
John McCall17001972009-10-18 01:05:36 +00003044void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3045 TemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003046 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3047 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3048 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall0ad16662009-10-29 08:12:44 +00003049 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3050 TL.setArgLocInfo(i,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003051 Reader.GetTemplateArgumentLocInfo(F,
3052 TL.getTypePtr()->getArg(i).getKind(),
3053 Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003054}
Abramo Bagnara6150c882010-05-11 21:36:43 +00003055void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003056 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3057 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003058}
John McCalle78aac42010-03-10 03:28:59 +00003059void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003060 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalle78aac42010-03-10 03:28:59 +00003061}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00003062void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003063 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3064 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3065 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003066}
John McCallc392f372010-06-11 00:33:02 +00003067void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3068 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003069 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3070 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3071 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3072 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3073 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003074 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3075 TL.setArgLocInfo(I,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003076 Reader.GetTemplateArgumentLocInfo(F,
3077 TL.getTypePtr()->getArg(I).getKind(),
3078 Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003079}
John McCall17001972009-10-18 01:05:36 +00003080void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003081 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8b07ec22010-05-15 11:32:37 +00003082}
3083void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3084 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003085 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3086 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003087 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003088 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003089}
John McCallfc93cf92009-10-22 22:37:11 +00003090void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003091 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCallfc93cf92009-10-22 22:37:11 +00003092}
John McCall8f115c62009-10-16 21:56:05 +00003093
Sebastian Redl2c373b92010-10-05 15:59:54 +00003094TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003095 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00003096 unsigned &Idx) {
3097 QualType InfoTy = GetType(Record[Idx++]);
3098 if (InfoTy.isNull())
3099 return 0;
3100
John McCallbcd03502009-12-07 02:54:59 +00003101 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003102 TypeLocReader TLR(*this, F, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00003103 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00003104 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00003105 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00003106}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003107
Sebastian Redl539c5062010-08-18 23:57:32 +00003108QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00003109 unsigned FastQuals = ID & Qualifiers::FastMask;
3110 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003111
Sebastian Redl539c5062010-08-18 23:57:32 +00003112 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003113 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00003114 switch ((PredefinedTypeIDs)Index) {
3115 case PREDEF_TYPE_NULL_ID: return QualType();
3116 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3117 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003118
Sebastian Redl539c5062010-08-18 23:57:32 +00003119 case PREDEF_TYPE_CHAR_U_ID:
3120 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003121 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00003122 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003123 break;
3124
Sebastian Redl539c5062010-08-18 23:57:32 +00003125 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3126 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3127 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3128 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3129 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3130 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3131 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3132 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3133 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3134 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3135 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3136 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3137 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3138 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3139 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3140 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3141 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3142 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3143 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3144 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3145 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3146 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3147 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3148 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003149 }
3150
3151 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00003152 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003153 }
3154
Sebastian Redl539c5062010-08-18 23:57:32 +00003155 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003156 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00003157 if (TypesLoaded[Index].isNull()) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003158 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003159 if (TypesLoaded[Index].isNull())
3160 return QualType();
3161
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003162 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003163 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003164 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003165 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003166 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00003167 }
Mike Stump11289f42009-09-09 15:08:12 +00003168
John McCall8ccfcb52009-09-24 19:53:00 +00003169 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003170}
3171
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003172TypeID ASTReader::GetTypeID(QualType T) const {
3173 return MakeTypeID(T,
3174 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3175}
3176
3177TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3178 if (T.isNull())
3179 return TypeIdx();
3180 assert(!T.getLocalFastQualifiers());
3181
3182 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3183 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3184 // comparing keys of ASTDeclContextNameLookupTable.
3185 // If the type didn't come from the AST file use a specially marked index
3186 // so that any hash/key comparison fail since no such index is stored
3187 // in a AST file.
3188 if (I == TypeIdxs.end())
3189 return TypeIdx(-1);
3190 return I->second;
3191}
3192
John McCall0ad16662009-10-29 08:12:44 +00003193TemplateArgumentLocInfo
Sebastian Redl2c373b92010-10-05 15:59:54 +00003194ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3195 TemplateArgument::ArgKind Kind,
John McCall0ad16662009-10-29 08:12:44 +00003196 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003197 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00003198 switch (Kind) {
3199 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003200 return ReadExpr(F);
John McCall0ad16662009-10-29 08:12:44 +00003201 case TemplateArgument::Type:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003202 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003203 case TemplateArgument::Template: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003204 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3205 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003206 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003207 }
John McCall0ad16662009-10-29 08:12:44 +00003208 case TemplateArgument::Null:
3209 case TemplateArgument::Integral:
3210 case TemplateArgument::Declaration:
3211 case TemplateArgument::Pack:
3212 return TemplateArgumentLocInfo();
3213 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00003214 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00003215 return TemplateArgumentLocInfo();
3216}
3217
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003218TemplateArgumentLoc
Sebastian Redl2c373b92010-10-05 15:59:54 +00003219ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003220 const RecordData &Record, unsigned &Index) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003221 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003222
3223 if (Arg.getKind() == TemplateArgument::Expression) {
3224 if (Record[Index++]) // bool InfoHasSameExpr.
3225 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3226 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00003227 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003228 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003229}
3230
Sebastian Redl2c499f62010-08-18 23:56:43 +00003231Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00003232 return GetDecl(ID);
3233}
3234
Sebastian Redl2c499f62010-08-18 23:56:43 +00003235TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003236 if (!DeclsLoaded[0]) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00003237 ReadDeclRecord(0, 1);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003238 if (DeserializationListener)
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003239 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003240 }
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00003241
3242 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3243}
3244
Sebastian Redl539c5062010-08-18 23:57:32 +00003245Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003246 if (ID == 0)
3247 return 0;
3248
Douglas Gregor745ed142009-04-25 18:35:21 +00003249 if (ID > DeclsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003250 Error("declaration ID out-of-range for AST file");
Douglas Gregor745ed142009-04-25 18:35:21 +00003251 return 0;
3252 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003253
Douglas Gregor745ed142009-04-25 18:35:21 +00003254 unsigned Index = ID - 1;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003255 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003256 ReadDeclRecord(Index, ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003257 if (DeserializationListener)
3258 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3259 }
Douglas Gregor745ed142009-04-25 18:35:21 +00003260
3261 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003262}
3263
Chris Lattner9c28af02009-04-27 05:46:25 +00003264/// \brief Resolve the offset of a statement into a statement.
3265///
3266/// This operation will read a new statement from the external
3267/// source each time it is called, and is meant to be used via a
3268/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00003269Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Sebastian Redl5c415f32010-07-22 17:01:13 +00003270 // Offset here is a global offset across the entire chain.
3271 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3272 PerFileData &F = *Chain[N - I - 1];
3273 if (Offset < F.SizeInBits) {
3274 // Since we know that this statement is part of a decl, make sure to use
3275 // the decl cursor to read it.
3276 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003277 return ReadStmtFromStream(F);
Sebastian Redl5c415f32010-07-22 17:01:13 +00003278 }
3279 Offset -= F.SizeInBits;
3280 }
3281 llvm_unreachable("Broken chain");
Douglas Gregor3c3aa612009-04-18 00:07:54 +00003282}
3283
Sebastian Redl2c499f62010-08-18 23:56:43 +00003284bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003285 bool (*isKindWeWant)(Decl::Kind),
John McCall75b960e2010-06-01 09:23:16 +00003286 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump11289f42009-09-09 15:08:12 +00003287 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003288 "DeclContext has no lexical decls in storage");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003289
Sebastian Redl5c415f32010-07-22 17:01:13 +00003290 // There might be lexical decls in multiple parts of the chain, for the TU
3291 // at least.
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003292 // DeclContextOffsets might reallocate as we load additional decls below,
3293 // so make a copy of the vector.
3294 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl5c415f32010-07-22 17:01:13 +00003295 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3296 I != E; ++I) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003297 // IDs can be 0 if this context doesn't contain declarations.
3298 if (!I->LexicalDecls)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003299 continue;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003300
3301 // Load all of the declaration IDs
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003302 for (const KindDeclIDPair *ID = I->LexicalDecls,
3303 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3304 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3305 continue;
3306
3307 Decl *D = GetDecl(ID->second);
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003308 assert(D && "Null decl in lexical decls");
3309 Decls.push_back(D);
3310 }
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003311 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003312
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003313 ++NumLexicalDeclContextsRead;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003314 return false;
3315}
3316
John McCall75b960e2010-06-01 09:23:16 +00003317DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00003318ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00003319 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003320 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003321 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003322 if (!Name)
3323 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3324 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003325
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003326 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl471ac2f2010-08-24 00:49:55 +00003327 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003328 // and namespaces. For any given name, the last available results replace
3329 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl5c415f32010-07-22 17:01:13 +00003330 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003331 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl5c415f32010-07-22 17:01:13 +00003332 I != E; ++I) {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003333 if (!I->NameLookupTableData)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003334 continue;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003335
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003336 ASTDeclContextNameLookupTable *LookupTable =
3337 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3338 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3339 if (Pos == LookupTable->end())
Sebastian Redl5c415f32010-07-22 17:01:13 +00003340 continue;
3341
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003342 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3343 for (; Data.first != Data.second; ++Data.first)
3344 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003345 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003346 }
3347
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003348 ++NumVisibleDeclContextsRead;
John McCall75b960e2010-06-01 09:23:16 +00003349
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003350 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00003351 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003352}
3353
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00003354void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3355 assert(DC->hasExternalVisibleStorage() &&
3356 "DeclContext has no visible decls in storage");
3357
3358 llvm::SmallVector<NamedDecl *, 64> Decls;
3359 // There might be visible decls in multiple parts of the chain, for the TU
3360 // and namespaces.
3361 DeclContextInfos &Infos = DeclContextOffsets[DC];
3362 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3363 I != E; ++I) {
3364 if (!I->NameLookupTableData)
3365 continue;
3366
3367 ASTDeclContextNameLookupTable *LookupTable =
3368 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3369 for (ASTDeclContextNameLookupTable::item_iterator
3370 ItemI = LookupTable->item_begin(),
3371 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3372 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3373 = *ItemI;
3374 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3375 Decls.clear();
3376 for (; Data.first != Data.second; ++Data.first)
3377 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3378 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3379 }
3380 }
3381}
3382
Sebastian Redl2c499f62010-08-18 23:56:43 +00003383void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003384 assert(Consumer);
3385 while (!InterestingDecls.empty()) {
3386 DeclGroupRef DG(InterestingDecls.front());
3387 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00003388 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003389 }
3390}
3391
Sebastian Redl2c499f62010-08-18 23:56:43 +00003392void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00003393 this->Consumer = Consumer;
3394
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003395 if (!Consumer)
3396 return;
3397
3398 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003399 // Force deserialization of this decl, which will cause it to be queued for
3400 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00003401 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003402 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00003403
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003404 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003405}
3406
Sebastian Redl2c499f62010-08-18 23:56:43 +00003407void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003408 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003409
Mike Stump11289f42009-09-09 15:08:12 +00003410 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003411 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00003412 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00003413 unsigned NumDeclsLoaded
3414 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3415 (Decl *)0);
3416 unsigned NumIdentifiersLoaded
3417 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3418 IdentifiersLoaded.end(),
3419 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00003420 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003421 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3422 SelectorsLoaded.end(),
3423 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00003424
Douglas Gregorc5046832009-04-27 18:38:38 +00003425 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3426 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor258ae542009-04-27 06:38:32 +00003427 if (TotalNumSLocEntries)
3428 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3429 NumSLocEntriesRead, TotalNumSLocEntries,
3430 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00003431 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003432 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003433 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3434 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3435 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003436 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003437 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3438 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00003439 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003440 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00003441 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3442 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00003443 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003444 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00003445 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3446 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003447 if (TotalNumStatements)
3448 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3449 NumStatementsRead, TotalNumStatements,
3450 ((float)NumStatementsRead/TotalNumStatements * 100));
3451 if (TotalNumMacros)
3452 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3453 NumMacrosRead, TotalNumMacros,
3454 ((float)NumMacrosRead/TotalNumMacros * 100));
3455 if (TotalLexicalDeclContexts)
3456 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3457 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3458 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3459 * 100));
3460 if (TotalVisibleDeclContexts)
3461 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3462 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3463 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3464 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003465 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003466 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003467 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3468 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00003469 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003470 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003471 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003472 std::fprintf(stderr, "\n");
3473}
3474
Sebastian Redl2c499f62010-08-18 23:56:43 +00003475void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00003476 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003477 S.ExternalSource = this;
3478
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003479 // Makes sure any declarations that were deserialized "too early"
3480 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003481 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3482 if (SemaObj->TUScope)
John McCall48871652010-08-21 09:40:31 +00003483 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003484
3485 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003486 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003487 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00003488
3489 // If there were any tentative definitions, deserialize them and add
Sebastian Redl35351a92010-01-31 22:27:38 +00003490 // them to Sema's list of tentative definitions.
Douglas Gregord4df8652009-04-22 22:02:47 +00003491 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3492 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redl35351a92010-01-31 22:27:38 +00003493 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregord4df8652009-04-22 22:02:47 +00003494 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00003495
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003496 // If there were any unused file scoped decls, deserialize them and add to
3497 // Sema's list of unused file scoped decls.
3498 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3499 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3500 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattner90073802010-02-12 00:07:30 +00003501 }
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003502
3503 // If there were any locally-scoped external declarations,
3504 // deserialize them and add them to Sema's table of locally-scoped
3505 // external declarations.
3506 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3507 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3508 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3509 }
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003510
3511 // If there were any ext_vector type declarations, deserialize them
3512 // and add them to Sema's vector of such declarations.
3513 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3514 SemaObj->ExtVectorDecls.push_back(
3515 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003516
3517 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3518 // Can we cut them down before writing them ?
3519
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003520 // If there were any dynamic classes declarations, deserialize them
3521 // and add them to Sema's vector of such declarations.
3522 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3523 SemaObj->DynamicClasses.push_back(
3524 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003525
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003526 // Load the offsets of the declarations that Sema references.
3527 // They will be lazily deserialized when needed.
3528 if (!SemaDeclRefs.empty()) {
3529 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3530 SemaObj->StdNamespace = SemaDeclRefs[0];
3531 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3532 }
3533
Sebastian Redl2c373b92010-10-05 15:59:54 +00003534 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
3535
3536 // If there are @selector references added them to its pool. This is for
3537 // implementation of -Wselector.
3538 if (!F->ReferencedSelectorsData.empty()) {
3539 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
3540 unsigned I = 0;
3541 while (I < DataSize) {
3542 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
3543 SourceLocation SelLoc = ReadSourceLocation(
3544 *F, F->ReferencedSelectorsData, I);
3545 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3546 }
3547 }
3548
3549 // If there were any pending implicit instantiations, deserialize them
3550 // and add them to Sema's queue of such instantiations.
3551 assert(F->PendingInstantiations.size() % 2 == 0 &&
3552 "Expected pairs of entries");
3553 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
3554 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
3555 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
3556 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
3557 }
3558 }
3559
3560 // The two special data sets below always come from the most recent PCH,
3561 // which is at the front of the chain.
3562 PerFileData &F = *Chain.front();
3563
3564 // If there were any weak undeclared identifiers, deserialize them and add to
3565 // Sema's list of weak undeclared identifiers.
3566 if (!WeakUndeclaredIdentifiers.empty()) {
3567 unsigned Idx = 0;
3568 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3569 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3570 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3571 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
3572 bool Used = WeakUndeclaredIdentifiers[Idx++];
3573 Sema::WeakInfo WI(AliasId, Loc);
3574 WI.setUsed(Used);
3575 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3576 }
3577 }
3578
3579 // If there were any VTable uses, deserialize the information and add it
3580 // to Sema's vector and map of VTable uses.
3581 if (!VTableUses.empty()) {
3582 unsigned Idx = 0;
3583 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3584 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3585 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
3586 bool DefinitionRequired = VTableUses[Idx++];
3587 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3588 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003589 }
3590 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00003591}
3592
Sebastian Redl2c499f62010-08-18 23:56:43 +00003593IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redl78f51772010-08-02 18:30:12 +00003594 // Try to find this name within our on-disk hash tables. We start with the
3595 // most recent one, since that one contains the most up-to-date info.
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003596 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003597 ASTIdentifierLookupTable *IdTable
3598 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003599 if (!IdTable)
3600 continue;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003601 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003602 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003603 if (Pos == IdTable->end())
3604 continue;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003605
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003606 // Dereferencing the iterator has the effect of building the
3607 // IdentifierInfo node and populating it with the various
3608 // declarations it needs.
Sebastian Redl78f51772010-08-02 18:30:12 +00003609 return *Pos;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003610 }
Sebastian Redl78f51772010-08-02 18:30:12 +00003611 return 0;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003612}
3613
Douglas Gregor57756ea2010-10-14 22:11:03 +00003614namespace clang {
3615 /// \brief An identifier-lookup iterator that enumerates all of the
3616 /// identifiers stored within a set of AST files.
3617 class ASTIdentifierIterator : public IdentifierIterator {
3618 /// \brief The AST reader whose identifiers are being enumerated.
3619 const ASTReader &Reader;
3620
3621 /// \brief The current index into the chain of AST files stored in
3622 /// the AST reader.
3623 unsigned Index;
3624
3625 /// \brief The current position within the identifier lookup table
3626 /// of the current AST file.
3627 ASTIdentifierLookupTable::key_iterator Current;
3628
3629 /// \brief The end position within the identifier lookup table of
3630 /// the current AST file.
3631 ASTIdentifierLookupTable::key_iterator End;
3632
3633 public:
3634 explicit ASTIdentifierIterator(const ASTReader &Reader);
3635
3636 virtual llvm::StringRef Next();
3637 };
3638}
3639
3640ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
3641 : Reader(Reader), Index(Reader.Chain.size() - 1) {
3642 ASTIdentifierLookupTable *IdTable
3643 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3644 Current = IdTable->key_begin();
3645 End = IdTable->key_end();
3646}
3647
3648llvm::StringRef ASTIdentifierIterator::Next() {
3649 while (Current == End) {
3650 // If we have exhausted all of our AST files, we're done.
3651 if (Index == 0)
3652 return llvm::StringRef();
3653
3654 --Index;
3655 ASTIdentifierLookupTable *IdTable
3656 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3657 Current = IdTable->key_begin();
3658 End = IdTable->key_end();
3659 }
3660
3661 // We have any identifiers remaining in the current AST file; return
3662 // the next one.
3663 std::pair<const char*, unsigned> Key = *Current;
3664 ++Current;
3665 return llvm::StringRef(Key.first, Key.second);
3666}
3667
3668IdentifierIterator *ASTReader::getIdentifiers() const {
3669 return new ASTIdentifierIterator(*this);
3670}
3671
Mike Stump11289f42009-09-09 15:08:12 +00003672std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redl2c499f62010-08-18 23:56:43 +00003673ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redlada023c2010-08-04 20:40:17 +00003674 // Find this selector in a hash table. We want to find the most recent entry.
3675 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3676 PerFileData &F = *Chain[I];
3677 if (!F.SelectorLookupTable)
3678 continue;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003679
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003680 ASTSelectorLookupTable *PoolTable
3681 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3682 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redlada023c2010-08-04 20:40:17 +00003683 if (Pos != PoolTable->end()) {
3684 ++NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003685 // FIXME: Not quite happy with the statistics here. We probably should
3686 // disable this tracking when called via LoadSelector.
3687 // Also, should entries without methods count as misses?
3688 ++NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003689 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redlada023c2010-08-04 20:40:17 +00003690 if (DeserializationListener)
3691 DeserializationListener->SelectorRead(Data.ID, Sel);
3692 return std::make_pair(Data.Instance, Data.Factory);
3693 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003694 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003695
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003696 ++NumMethodPoolMisses;
Sebastian Redlada023c2010-08-04 20:40:17 +00003697 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorc78d3462009-04-24 21:10:55 +00003698}
3699
Sebastian Redl2c499f62010-08-18 23:56:43 +00003700void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003701 // It would be complicated to avoid reading the methods anyway. So don't.
3702 ReadMethodPool(Sel);
3703}
3704
Sebastian Redl2c499f62010-08-18 23:56:43 +00003705void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00003706 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00003707 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00003708 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00003709 if (DeserializationListener)
3710 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003711}
3712
Douglas Gregor1342e842009-07-06 18:54:52 +00003713/// \brief Set the globally-visible declarations associated with the given
3714/// identifier.
3715///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003716/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00003717/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00003718/// them.
3719///
3720/// \param II an IdentifierInfo that refers to one or more globally-visible
3721/// declarations.
3722///
3723/// \param DeclIDs the set of declaration IDs with the name @p II that are
3724/// visible at global scope.
3725///
3726/// \param Nonrecursive should be true to indicate that the caller knows that
3727/// this call is non-recursive, and therefore the globally-visible declarations
3728/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00003729void
Sebastian Redl2c499f62010-08-18 23:56:43 +00003730ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregor1342e842009-07-06 18:54:52 +00003731 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
3732 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003733 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00003734 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
3735 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
3736 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003737 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00003738 return;
3739 }
Mike Stump11289f42009-09-09 15:08:12 +00003740
Douglas Gregor1342e842009-07-06 18:54:52 +00003741 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
3742 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
3743 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00003744 if (SemaObj->TUScope) {
3745 // Introduce this declaration into the translation-unit scope
3746 // and add it to the declaration chain for this identifier, so
3747 // that (unqualified) name lookup will find it.
John McCall48871652010-08-21 09:40:31 +00003748 SemaObj->TUScope->AddDecl(D);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00003749 }
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003750 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregor1342e842009-07-06 18:54:52 +00003751 } else {
3752 // Queue this declaration so that it will be added to the
3753 // translation unit scope and identifier's declaration chain
3754 // once a Sema object is known.
3755 PreloadedDecls.push_back(D);
3756 }
3757 }
3758}
3759
Sebastian Redl2c499f62010-08-18 23:56:43 +00003760IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003761 if (ID == 0)
3762 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00003763
Sebastian Redlc713b962010-07-21 00:46:22 +00003764 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003765 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003766 return 0;
3767 }
Mike Stump11289f42009-09-09 15:08:12 +00003768
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003769 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc713b962010-07-21 00:46:22 +00003770 ID -= 1;
3771 if (!IdentifiersLoaded[ID]) {
3772 unsigned Index = ID;
3773 const char *Str = 0;
3774 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3775 PerFileData *F = Chain[N - I - 1];
3776 if (Index < F->LocalNumIdentifiers) {
3777 uint32_t Offset = F->IdentifierOffsets[Index];
3778 Str = F->IdentifierTableData + Offset;
3779 break;
3780 }
3781 Index -= F->LocalNumIdentifiers;
3782 }
3783 assert(Str && "Broken Chain");
Douglas Gregor5287b4e2009-04-25 21:04:17 +00003784
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003785 // All of the strings in the AST file are preceded by a 16-bit length.
3786 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00003787 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
3788 // unsigned integers. This is important to avoid integer overflow when
3789 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00003790 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00003791 unsigned StrLen = (((unsigned) StrLenPtr[0])
3792 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00003793 IdentifiersLoaded[ID]
Kovarththanan Rajaratnama3b09592010-03-12 10:32:27 +00003794 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003795 if (DeserializationListener)
3796 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003797 }
Mike Stump11289f42009-09-09 15:08:12 +00003798
Sebastian Redlc713b962010-07-21 00:46:22 +00003799 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003800}
3801
Sebastian Redl2c499f62010-08-18 23:56:43 +00003802void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00003803 ReadSLocEntryRecord(ID);
3804}
3805
Sebastian Redl2c499f62010-08-18 23:56:43 +00003806Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00003807 if (ID == 0)
3808 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00003809
Sebastian Redlada023c2010-08-04 20:40:17 +00003810 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003811 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00003812 return Selector();
3813 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003814
Sebastian Redlada023c2010-08-04 20:40:17 +00003815 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003816 // Load this selector from the selector table.
Sebastian Redlada023c2010-08-04 20:40:17 +00003817 unsigned Idx = ID - 1;
3818 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3819 PerFileData &F = *Chain[N - I - 1];
3820 if (Idx < F.LocalNumSelectors) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003821 ASTSelectorLookupTrait Trait(*this);
Sebastian Redlada023c2010-08-04 20:40:17 +00003822 SelectorsLoaded[ID - 1] =
3823 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
3824 if (DeserializationListener)
3825 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
3826 break;
3827 }
3828 Idx -= F.LocalNumSelectors;
3829 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003830 }
3831
Sebastian Redlada023c2010-08-04 20:40:17 +00003832 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00003833}
3834
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003835Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00003836 return DecodeSelector(ID);
3837}
3838
Sebastian Redl2c499f62010-08-18 23:56:43 +00003839uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00003840 // ID 0 (the null selector) is considered an external selector.
3841 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00003842}
3843
Mike Stump11289f42009-09-09 15:08:12 +00003844DeclarationName
Sebastian Redl2c499f62010-08-18 23:56:43 +00003845ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003846 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
3847 switch (Kind) {
3848 case DeclarationName::Identifier:
3849 return DeclarationName(GetIdentifierInfo(Record, Idx));
3850
3851 case DeclarationName::ObjCZeroArgSelector:
3852 case DeclarationName::ObjCOneArgSelector:
3853 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff3c301dc2009-04-23 15:15:40 +00003854 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003855
3856 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00003857 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00003858 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003859
3860 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00003861 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00003862 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003863
3864 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00003865 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor2211d342009-08-05 05:36:45 +00003866 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003867
3868 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00003869 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003870 (OverloadedOperatorKind)Record[Idx++]);
3871
Alexis Hunt3d221f22009-11-29 07:34:05 +00003872 case DeclarationName::CXXLiteralOperatorName:
3873 return Context->DeclarationNames.getCXXLiteralOperatorName(
3874 GetIdentifierInfo(Record, Idx));
3875
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003876 case DeclarationName::CXXUsingDirective:
3877 return DeclarationName::getUsingDirectiveName();
3878 }
3879
3880 // Required to silence GCC warning
3881 return DeclarationName();
3882}
Douglas Gregor55abb232009-04-10 20:39:37 +00003883
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003884void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
3885 DeclarationNameLoc &DNLoc,
3886 DeclarationName Name,
3887 const RecordData &Record, unsigned &Idx) {
3888 switch (Name.getNameKind()) {
3889 case DeclarationName::CXXConstructorName:
3890 case DeclarationName::CXXDestructorName:
3891 case DeclarationName::CXXConversionFunctionName:
3892 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
3893 break;
3894
3895 case DeclarationName::CXXOperatorName:
3896 DNLoc.CXXOperatorName.BeginOpNameLoc
3897 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
3898 DNLoc.CXXOperatorName.EndOpNameLoc
3899 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
3900 break;
3901
3902 case DeclarationName::CXXLiteralOperatorName:
3903 DNLoc.CXXLiteralOperatorName.OpNameLoc
3904 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
3905 break;
3906
3907 case DeclarationName::Identifier:
3908 case DeclarationName::ObjCZeroArgSelector:
3909 case DeclarationName::ObjCOneArgSelector:
3910 case DeclarationName::ObjCMultiArgSelector:
3911 case DeclarationName::CXXUsingDirective:
3912 break;
3913 }
3914}
3915
3916void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
3917 DeclarationNameInfo &NameInfo,
3918 const RecordData &Record, unsigned &Idx) {
3919 NameInfo.setName(ReadDeclarationName(Record, Idx));
3920 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
3921 DeclarationNameLoc DNLoc;
3922 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
3923 NameInfo.setInfo(DNLoc);
3924}
3925
3926void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
3927 const RecordData &Record, unsigned &Idx) {
3928 Info.NNS = ReadNestedNameSpecifier(Record, Idx);
3929 Info.NNSRange = ReadSourceRange(F, Record, Idx);
3930 unsigned NumTPLists = Record[Idx++];
3931 Info.NumTemplParamLists = NumTPLists;
3932 if (NumTPLists) {
3933 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
3934 for (unsigned i=0; i != NumTPLists; ++i)
3935 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
3936 }
3937}
3938
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003939TemplateName
Sebastian Redl2c499f62010-08-18 23:56:43 +00003940ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003941 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003942 switch (Kind) {
3943 case TemplateName::Template:
3944 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
3945
3946 case TemplateName::OverloadedTemplate: {
3947 unsigned size = Record[Idx++];
3948 UnresolvedSet<8> Decls;
3949 while (size--)
3950 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
3951
3952 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
3953 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003954
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003955 case TemplateName::QualifiedTemplate: {
3956 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3957 bool hasTemplKeyword = Record[Idx++];
3958 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
3959 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
3960 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003961
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003962 case TemplateName::DependentTemplate: {
3963 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3964 if (Record[Idx++]) // isIdentifier
3965 return Context->getDependentTemplateName(NNS,
3966 GetIdentifierInfo(Record, Idx));
3967 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003968 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003969 }
3970 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003971
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003972 assert(0 && "Unhandled template name kind!");
3973 return TemplateName();
3974}
3975
3976TemplateArgument
Sebastian Redl2c373b92010-10-05 15:59:54 +00003977ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003978 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003979 switch ((TemplateArgument::ArgKind)Record[Idx++]) {
3980 case TemplateArgument::Null:
3981 return TemplateArgument();
3982 case TemplateArgument::Type:
3983 return TemplateArgument(GetType(Record[Idx++]));
3984 case TemplateArgument::Declaration:
3985 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00003986 case TemplateArgument::Integral: {
3987 llvm::APSInt Value = ReadAPSInt(Record, Idx);
3988 QualType T = GetType(Record[Idx++]);
3989 return TemplateArgument(Value, T);
3990 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003991 case TemplateArgument::Template:
3992 return TemplateArgument(ReadTemplateName(Record, Idx));
3993 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003994 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003995 case TemplateArgument::Pack: {
3996 unsigned NumArgs = Record[Idx++];
3997 llvm::SmallVector<TemplateArgument, 8> Args;
3998 Args.reserve(NumArgs);
3999 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004000 Args.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004001 TemplateArgument TemplArg;
4002 TemplArg.setArgumentPack(Args.data(), Args.size(), /*CopyArgs=*/true);
4003 return TemplArg;
4004 }
4005 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004006
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004007 assert(0 && "Unhandled template argument kind!");
4008 return TemplateArgument();
4009}
4010
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004011TemplateParameterList *
Sebastian Redl2c373b92010-10-05 15:59:54 +00004012ASTReader::ReadTemplateParameterList(PerFileData &F,
4013 const RecordData &Record, unsigned &Idx) {
4014 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4015 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4016 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004017
4018 unsigned NumParams = Record[Idx++];
4019 llvm::SmallVector<NamedDecl *, 16> Params;
4020 Params.reserve(NumParams);
4021 while (NumParams--)
4022 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004023
4024 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004025 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4026 Params.data(), Params.size(), RAngleLoc);
4027 return TemplateParams;
4028}
4029
4030void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004031ASTReader::
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004032ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004033 PerFileData &F, const RecordData &Record,
4034 unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004035 unsigned NumTemplateArgs = Record[Idx++];
4036 TemplArgs.reserve(NumTemplateArgs);
4037 while (NumTemplateArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004038 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004039}
4040
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004041/// \brief Read a UnresolvedSet structure.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004042void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004043 const RecordData &Record, unsigned &Idx) {
4044 unsigned NumDecls = Record[Idx++];
4045 while (NumDecls--) {
4046 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4047 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4048 Set.addDecl(D, AS);
4049 }
4050}
4051
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004052CXXBaseSpecifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00004053ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky19b9f952010-07-26 16:56:01 +00004054 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004055 bool isVirtual = static_cast<bool>(Record[Idx++]);
4056 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4057 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004058 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4059 SourceRange Range = ReadSourceRange(F, Record, Idx);
Nick Lewycky19b9f952010-07-26 16:56:01 +00004060 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004061}
4062
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004063std::pair<CXXBaseOrMemberInitializer **, unsigned>
Sebastian Redl2c373b92010-10-05 15:59:54 +00004064ASTReader::ReadCXXBaseOrMemberInitializers(PerFileData &F,
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004065 const RecordData &Record,
4066 unsigned &Idx) {
4067 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0;
4068 unsigned NumInitializers = Record[Idx++];
4069 if (NumInitializers) {
4070 ASTContext &C = *getContext();
4071
4072 BaseOrMemberInitializers
4073 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
4074 for (unsigned i=0; i != NumInitializers; ++i) {
4075 TypeSourceInfo *BaseClassInfo = 0;
4076 bool IsBaseVirtual = false;
4077 FieldDecl *Member = 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004078
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004079 bool IsBaseInitializer = Record[Idx++];
4080 if (IsBaseInitializer) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004081 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004082 IsBaseVirtual = Record[Idx++];
4083 } else {
4084 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
4085 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00004086 SourceLocation MemberLoc = ReadSourceLocation(F, Record, Idx);
4087 Expr *Init = ReadExpr(F);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004088 FieldDecl *AnonUnionMember
4089 = cast_or_null<FieldDecl>(GetDecl(Record[Idx++]));
Sebastian Redl2c373b92010-10-05 15:59:54 +00004090 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4091 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004092 bool IsWritten = Record[Idx++];
4093 unsigned SourceOrderOrNumArrayIndices;
4094 llvm::SmallVector<VarDecl *, 8> Indices;
4095 if (IsWritten) {
4096 SourceOrderOrNumArrayIndices = Record[Idx++];
4097 } else {
4098 SourceOrderOrNumArrayIndices = Record[Idx++];
4099 Indices.reserve(SourceOrderOrNumArrayIndices);
4100 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4101 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4102 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004103
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004104 CXXBaseOrMemberInitializer *BOMInit;
4105 if (IsBaseInitializer) {
4106 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
4107 IsBaseVirtual, LParenLoc,
4108 Init, RParenLoc);
4109 } else if (IsWritten) {
4110 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc,
4111 LParenLoc, Init, RParenLoc);
4112 } else {
4113 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc,
4114 LParenLoc, Init, RParenLoc,
4115 Indices.data(),
4116 Indices.size());
4117 }
4118
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00004119 if (IsWritten)
4120 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004121 BOMInit->setAnonUnionMember(AnonUnionMember);
4122 BaseOrMemberInitializers[i] = BOMInit;
4123 }
4124 }
4125
4126 return std::make_pair(BaseOrMemberInitializers, NumInitializers);
4127}
4128
Chris Lattnerca025db2010-05-07 21:43:38 +00004129NestedNameSpecifier *
Sebastian Redl2c499f62010-08-18 23:56:43 +00004130ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004131 unsigned N = Record[Idx++];
4132 NestedNameSpecifier *NNS = 0, *Prev = 0;
4133 for (unsigned I = 0; I != N; ++I) {
4134 NestedNameSpecifier::SpecifierKind Kind
4135 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4136 switch (Kind) {
4137 case NestedNameSpecifier::Identifier: {
4138 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4139 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4140 break;
4141 }
4142
4143 case NestedNameSpecifier::Namespace: {
4144 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4145 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4146 break;
4147 }
4148
4149 case NestedNameSpecifier::TypeSpec:
4150 case NestedNameSpecifier::TypeSpecWithTemplate: {
4151 Type *T = GetType(Record[Idx++]).getTypePtr();
4152 bool Template = Record[Idx++];
4153 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4154 break;
4155 }
4156
4157 case NestedNameSpecifier::Global: {
4158 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4159 // No associated value, and there can't be a prefix.
4160 break;
4161 }
Chris Lattnerca025db2010-05-07 21:43:38 +00004162 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00004163 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00004164 }
4165 return NNS;
4166}
4167
4168SourceRange
Sebastian Redl2c373b92010-10-05 15:59:54 +00004169ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4170 unsigned &Idx) {
4171 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4172 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00004173 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00004174}
4175
Douglas Gregor1daeb692009-04-13 18:14:40 +00004176/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004177llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004178 unsigned BitWidth = Record[Idx++];
4179 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4180 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4181 Idx += NumWords;
4182 return Result;
4183}
4184
4185/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004186llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004187 bool isUnsigned = Record[Idx++];
4188 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4189}
4190
Douglas Gregore0a3a512009-04-14 21:55:33 +00004191/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004192llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004193 return llvm::APFloat(ReadAPInt(Record, Idx));
4194}
4195
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004196// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00004197std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004198 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00004199 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004200 Idx += Len;
4201 return Result;
4202}
4203
Sebastian Redl2c499f62010-08-18 23:56:43 +00004204CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00004205 unsigned &Idx) {
4206 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4207 return CXXTemporary::Create(*Context, Decl);
4208}
4209
Sebastian Redl2c499f62010-08-18 23:56:43 +00004210DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00004211 return Diag(SourceLocation(), DiagID);
4212}
4213
Sebastian Redl2c499f62010-08-18 23:56:43 +00004214DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004215 return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00004216}
Douglas Gregora9af1d12009-04-17 00:04:06 +00004217
Douglas Gregora868bbd2009-04-21 22:25:48 +00004218/// \brief Retrieve the identifier table associated with the
4219/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004220IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004221 assert(PP && "Forgot to set Preprocessor ?");
4222 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00004223}
4224
Douglas Gregora9af1d12009-04-17 00:04:06 +00004225/// \brief Record that the given ID maps to the given switch-case
4226/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004227void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004228 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4229 SwitchCaseStmts[ID] = SC;
4230}
4231
4232/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004233SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004234 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4235 return SwitchCaseStmts[ID];
4236}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004237
4238/// \brief Record that the given label statement has been
4239/// deserialized and has the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004240void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump11289f42009-09-09 15:08:12 +00004241 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004242 "Deserialized label twice");
4243 LabelStmts[ID] = S;
4244
4245 // If we've already seen any goto statements that point to this
4246 // label, resolve them now.
4247 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
4248 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
4249 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
4250 Goto->second->setLabel(S);
4251 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor779d8652009-04-17 18:58:21 +00004252
4253 // If we've already seen any address-label statements that point to
4254 // this label, resolve them now.
4255 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump11289f42009-09-09 15:08:12 +00004256 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor779d8652009-04-17 18:58:21 +00004257 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump11289f42009-09-09 15:08:12 +00004258 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor779d8652009-04-17 18:58:21 +00004259 AddrLabel != AddrLabels.second; ++AddrLabel)
4260 AddrLabel->second->setLabel(S);
4261 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004262}
4263
4264/// \brief Set the label of the given statement to the label
4265/// identified by ID.
4266///
4267/// Depending on the order in which the label and other statements
4268/// referencing that label occur, this operation may complete
4269/// immediately (updating the statement) or it may queue the
4270/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004271void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004272 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4273 if (Label != LabelStmts.end()) {
4274 // We've already seen this label, so set the label of the goto and
4275 // we're done.
4276 S->setLabel(Label->second);
4277 } else {
4278 // We haven't seen this label yet, so add this goto to the set of
4279 // unresolved goto statements.
4280 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
4281 }
4282}
Douglas Gregor779d8652009-04-17 18:58:21 +00004283
4284/// \brief Set the label of the given expression to the label
4285/// identified by ID.
4286///
4287/// Depending on the order in which the label and other statements
4288/// referencing that label occur, this operation may complete
4289/// immediately (updating the statement) or it may queue the
4290/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004291void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor779d8652009-04-17 18:58:21 +00004292 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4293 if (Label != LabelStmts.end()) {
4294 // We've already seen this label, so set the label of the
4295 // label-address expression and we're done.
4296 S->setLabel(Label->second);
4297 } else {
4298 // We haven't seen this label yet, so add this label-address
4299 // expression to the set of unresolved label-address expressions.
4300 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
4301 }
4302}
Douglas Gregor1342e842009-07-06 18:54:52 +00004303
Sebastian Redl2c499f62010-08-18 23:56:43 +00004304void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004305 assert(NumCurrentElementsDeserializing &&
4306 "FinishedDeserializing not paired with StartedDeserializing");
4307 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004308 // If any identifiers with corresponding top-level declarations have
4309 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004310 while (!PendingIdentifierInfos.empty()) {
4311 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4312 PendingIdentifierInfos.front().DeclIDs, true);
4313 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00004314 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004315
4316 // We are not in recursive loading, so it's safe to pass the "interesting"
4317 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004318 if (Consumer)
4319 PassInterestingDeclsToConsumer();
Douglas Gregor1342e842009-07-06 18:54:52 +00004320 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004321 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00004322}
Douglas Gregorb473b072010-08-19 00:28:17 +00004323
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004324ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
4325 const char *isysroot, bool DisableValidation)
4326 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4327 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4328 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4329 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
4330 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00004331 TotalNumSLocEntries(0), NextSLocOffset(0), NumStatementsRead(0),
4332 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
4333 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004334 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4335 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4336 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
4337 RelocatablePCH = false;
4338}
4339
4340ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4341 Diagnostic &Diags, const char *isysroot,
4342 bool DisableValidation)
4343 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4344 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
4345 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
4346 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00004347 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4348 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4349 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4350 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4351 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4352 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004353 RelocatablePCH = false;
4354}
4355
4356ASTReader::~ASTReader() {
4357 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4358 delete Chain[e - i - 1];
4359 // Delete all visible decl lookup tables
4360 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4361 E = DeclContextOffsets.end();
4362 I != E; ++I) {
4363 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4364 J != F; ++J) {
4365 if (J->NameLookupTableData)
4366 delete static_cast<ASTDeclContextNameLookupTable*>(
4367 J->NameLookupTableData);
4368 }
4369 }
4370 for (DeclContextVisibleUpdatesPending::iterator
4371 I = PendingVisibleUpdates.begin(),
4372 E = PendingVisibleUpdates.end();
4373 I != E; ++I) {
4374 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4375 F = I->second.end();
4376 J != F; ++J)
4377 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4378 }
4379}
4380
Sebastian Redl009e7f22010-10-05 16:15:19 +00004381ASTReader::PerFileData::PerFileData(ASTFileType Ty)
4382 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00004383 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
4384 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
4385 MacroDefinitionOffsets(0), LocalNumSelectors(0), SelectorOffsets(0),
4386 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
4387 DeclOffsets(0), LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redl3f6b7532010-10-01 19:59:12 +00004388 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregorb473b072010-08-19 00:28:17 +00004389{}
4390
4391ASTReader::PerFileData::~PerFileData() {
4392 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
4393 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4394}
4395