blob: 7e7bd253aff6e16940d39c0d88cfc00cf3024f76 [file] [log] [blame]
Sebastian Redl3b3c8742010-08-18 23:57:11 +00001//===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redl2c499f62010-08-18 23:56:43 +000010// This file defines the ASTReader class, which reads AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner92ba5ff2009-04-27 05:14:47 +000013
Sebastian Redlf5b13462010-08-18 23:57:17 +000014#include "clang/Serialization/ASTReader.h"
15#include "clang/Serialization/ASTDeserializationListener.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregor55abb232009-04-10 20:39:37 +000017#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbar732ef8a2009-11-11 23:58:53 +000018#include "clang/Frontend/Utils.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000019#include "clang/Sema/Sema.h"
John McCallcc14d1f2010-08-24 08:50:51 +000020#include "clang/Sema/Scope.h"
Douglas Gregor1a0d0b92009-04-14 00:24:19 +000021#include "clang/AST/ASTConsumer.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000022#include "clang/AST/ASTContext.h"
John McCall19c1bfd2010-08-25 05:32:35 +000023#include "clang/AST/DeclTemplate.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Chris Lattner34321bc2009-04-10 21:41:48 +000028#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000029#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000030#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000031#include "clang/Lex/HeaderSearch.h"
Douglas Gregora868bbd2009-04-21 22:25:48 +000032#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000033#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000034#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000035#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000036#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000037#include "clang/Basic/TargetInfo.h"
Douglas Gregord54f3a12009-10-05 21:07:28 +000038#include "clang/Basic/Version.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000039#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000040#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000041#include "llvm/Support/MemoryBuffer.h"
John McCall0ad16662009-10-29 08:12:44 +000042#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000043#include "llvm/Support/Path.h"
Michael J. Spencerf25faaa2010-12-09 17:36:38 +000044#include "llvm/Support/system_error.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000045#include <algorithm>
Douglas Gregorc379c072009-04-28 18:58:38 +000046#include <iterator>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000047#include <cstdio>
Douglas Gregorc5046832009-04-27 18:38:38 +000048#include <sys/stat.h>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000049using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000050using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000051
52//===----------------------------------------------------------------------===//
Sebastian Redld44cd6a2010-08-18 23:57:06 +000053// PCH validator implementation
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000054//===----------------------------------------------------------------------===//
55
Sebastian Redl3e31c722010-08-18 23:56:56 +000056ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000057
58bool
59PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
60 const LangOptions &PPLangOpts = PP.getLangOptions();
61#define PARSE_LANGOPT_BENIGN(Option)
62#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
63 if (PPLangOpts.Option != LangOpts.Option) { \
64 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
65 return true; \
66 }
67
68 PARSE_LANGOPT_BENIGN(Trigraphs);
69 PARSE_LANGOPT_BENIGN(BCPLComment);
70 PARSE_LANGOPT_BENIGN(DollarIdents);
71 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
72 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carruthe03aa552010-04-17 20:17:31 +000073 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000074 PARSE_LANGOPT_BENIGN(ImplicitInt);
75 PARSE_LANGOPT_BENIGN(Digraphs);
76 PARSE_LANGOPT_BENIGN(HexFloats);
77 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
78 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +000079 PARSE_LANGOPT_BENIGN(MSCVersion);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000080 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
81 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
82 PARSE_LANGOPT_BENIGN(CXXOperatorName);
83 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
84 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
85 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian45878032010-02-09 19:31:38 +000086 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +000087 PARSE_LANGOPT_IMPORTANT(AppleKext, diag::warn_pch_apple_kext);
Ted Kremenek1d56c9e2010-12-23 21:35:43 +000088 PARSE_LANGOPT_IMPORTANT(ObjCDefaultSynthProperties,
89 diag::warn_pch_objc_auto_properties);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +000090 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian62c56022010-04-22 21:01:59 +000091 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000092 PARSE_LANGOPT_BENIGN(PascalStrings);
93 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump11289f42009-09-09 15:08:12 +000094 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000095 diag::warn_pch_lax_vector_conversions);
Nate Begeman9d905792009-06-25 22:57:40 +000096 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000097 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +000098 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000099 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
100 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
101 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump11289f42009-09-09 15:08:12 +0000102 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000103 diag::warn_pch_thread_safe_statics);
Daniel Dunbara77eaeb2009-09-03 04:54:28 +0000104 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000105 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
106 PARSE_LANGOPT_BENIGN(EmitAllDecls);
107 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattner51924e512010-06-26 21:25:03 +0000108 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump11289f42009-09-09 15:08:12 +0000109 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000110 diag::warn_pch_heinous_extensions);
111 // FIXME: Most of the options below are benign if the macro wasn't
112 // used. Unfortunately, this means that a PCH compiled without
113 // optimization can't be used with optimization turned on, even
114 // though the only thing that changes is whether __OPTIMIZE__ was
115 // defined... but if __OPTIMIZE__ never showed up in the header, it
116 // doesn't matter. We could consider making this some special kind
117 // of check.
118 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
119 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
120 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
121 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
122 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
123 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
124 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
125 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsoned4e2952009-11-05 20:14:16 +0000126 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +0000127 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000128 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump11289f42009-09-09 15:08:12 +0000129 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000130 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
131 return true;
132 }
133 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000134 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
135 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000136 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman9d905792009-06-25 22:57:40 +0000137 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Peter Collingbourne546d0792010-12-01 19:14:57 +0000138 PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda);
Mike Stumpd9546382009-12-12 01:27:46 +0000139 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbar143021e2009-09-21 04:16:19 +0000140 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000141 PARSE_LANGOPT_BENIGN(SpellChecking);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +0000142#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000143#undef PARSE_LANGOPT_BENIGN
144
145 return false;
146}
147
Daniel Dunbar20a682d2009-11-11 00:52:11 +0000148bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
149 if (Triple == PP.getTargetInfo().getTriple().str())
150 return false;
151
152 Reader.Diag(diag::warn_pch_target_triple)
153 << Triple << PP.getTargetInfo().getTriple().str();
154 return true;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000155}
156
Benjamin Kramer90b5b682010-11-25 18:29:30 +0000157namespace {
158 struct EmptyStringRef {
159 bool operator ()(llvm::StringRef r) const { return r.empty(); }
160 };
161 struct EmptyBlock {
162 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
163 };
164}
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000165
166static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
167 PCHPredefinesBlocks R) {
168 // First, sum up the lengths.
169 unsigned LL = 0, RL = 0;
170 for (unsigned I = 0, N = L.size(); I != N; ++I) {
171 LL += L[I].size();
172 }
173 for (unsigned I = 0, N = R.size(); I != N; ++I) {
174 RL += R[I].Data.size();
175 }
176 if (LL != RL)
177 return false;
178 if (LL == 0 && RL == 0)
179 return true;
180
181 // Kick out empty parts, they confuse the algorithm below.
182 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
183 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
184
185 // Do it the hard way. At this point, both vectors must be non-empty.
186 llvm::StringRef LR = L[0], RR = R[0].Data;
187 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbar01ad0a72010-07-16 00:00:11 +0000188 (void) RN;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000189 for (;;) {
190 // Compare the current pieces.
191 if (LR.size() == RR.size()) {
192 // If they're the same length, it's pretty easy.
193 if (LR != RR)
194 return false;
195 // Both pieces are done, advance.
196 ++LI;
197 ++RI;
198 // If either string is done, they're both done, since they're the same
199 // length.
200 if (LI == LN) {
201 assert(RI == RN && "Strings not the same length after all?");
202 return true;
203 }
204 LR = L[LI];
205 RR = R[RI].Data;
206 } else if (LR.size() < RR.size()) {
207 // Right piece is longer.
208 if (!RR.startswith(LR))
209 return false;
210 ++LI;
211 assert(LI != LN && "Strings not the same length after all?");
212 RR = RR.substr(LR.size());
213 LR = L[LI];
214 } else {
215 // Left piece is longer.
216 if (!LR.startswith(RR))
217 return false;
218 ++RI;
219 assert(RI != RN && "Strings not the same length after all?");
220 LR = LR.substr(RR.size());
221 RR = R[RI].Data;
222 }
223 }
224}
225
226static std::pair<FileID, llvm::StringRef::size_type>
227FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
228 std::pair<FileID, llvm::StringRef::size_type> Res;
229 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
230 Res.second = Buffers[I].Data.find(MacroDef);
231 if (Res.second != llvm::StringRef::npos) {
232 Res.first = Buffers[I].BufferID;
233 break;
234 }
235 }
236 return Res;
237}
238
239bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000240 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000241 std::string &SuggestedPredefines) {
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000242 // We are in the context of an implicit include, so the predefines buffer will
243 // have a #include entry for the PCH file itself (as normalized by the
244 // preprocessor initialization). Find it and skip over it in the checking
245 // below.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000246 llvm::SmallString<256> PCHInclude;
247 PCHInclude += "#include \"";
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000248 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000249 PCHInclude += "\"\n";
250 std::pair<llvm::StringRef,llvm::StringRef> Split =
251 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
252 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000253 if (Left == PP.getPredefines()) {
254 Error("Missing PCH include entry!");
255 return true;
256 }
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000257
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000258 // If the concatenation of all the PCH buffers is equal to the adjusted
259 // command line, we're done.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000260 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
261 CommandLine.push_back(Left);
262 CommandLine.push_back(Right);
263 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000264 return false;
265
266 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000267
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000268 // The predefines buffers are different. Determine what the differences are,
269 // and whether they require us to reject the PCH file.
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000270 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000271 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
272 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000273
274 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
275 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000276
277 // Pick out implicit #includes after the PCH and don't consider them for
278 // validation; we will insert them into SuggestedPredefines so that the
279 // preprocessor includes them.
280 std::string IncludesAfterPCH;
281 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
282 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
283 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
284 if (AfterPCHLines[i].startswith("#include ")) {
285 IncludesAfterPCH += AfterPCHLines[i];
286 IncludesAfterPCH += '\n';
287 } else {
288 CmdLineLines.push_back(AfterPCHLines[i]);
289 }
290 }
291
292 // Make sure we add the includes last into SuggestedPredefines before we
293 // exit this function.
294 struct AddIncludesRAII {
295 std::string &SuggestedPredefines;
296 std::string &IncludesAfterPCH;
297
298 AddIncludesRAII(std::string &SuggestedPredefines,
299 std::string &IncludesAfterPCH)
300 : SuggestedPredefines(SuggestedPredefines),
301 IncludesAfterPCH(IncludesAfterPCH) { }
302 ~AddIncludesRAII() {
303 SuggestedPredefines += IncludesAfterPCH;
304 }
305 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000306
Daniel Dunbar499baed2009-11-11 05:26:28 +0000307 // Sort both sets of predefined buffer lines, since we allow some extra
308 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000309 std::sort(CmdLineLines.begin(), CmdLineLines.end());
310 std::sort(PCHLines.begin(), PCHLines.end());
311
Daniel Dunbar499baed2009-11-11 05:26:28 +0000312 // Determine which predefines that were used to build the PCH file are missing
313 // from the command line.
314 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000315 std::set_difference(PCHLines.begin(), PCHLines.end(),
316 CmdLineLines.begin(), CmdLineLines.end(),
317 std::back_inserter(MissingPredefines));
318
319 bool MissingDefines = false;
320 bool ConflictingDefines = false;
321 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000322 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000323 if (Missing.startswith("#include ")) {
324 // An -include was specified when generating the PCH; it is included in
325 // the PCH, just ignore it.
326 continue;
327 }
Daniel Dunbar499baed2009-11-11 05:26:28 +0000328 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000329 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
330 return true;
331 }
Mike Stump11289f42009-09-09 15:08:12 +0000332
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000333 // This is a macro definition. Determine the name of the macro we're
334 // defining.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000335 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000336 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000337 = Missing.find_first_of("( \n\r", StartOfMacroName);
338 assert(EndOfMacroName != std::string::npos &&
339 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000340 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000341
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000342 // Determine whether this macro was given a different definition on the
343 // command line.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000344 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000345 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000346 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000347 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
348 MacroDefStart);
349 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000350 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000351 // Different macro; we're done.
352 ConflictPos = CmdLineLines.end();
Mike Stump11289f42009-09-09 15:08:12 +0000353 break;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000354 }
Mike Stump11289f42009-09-09 15:08:12 +0000355
356 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000357 "Invalid #define in predefines buffer?");
Mike Stump11289f42009-09-09 15:08:12 +0000358 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000359 (*ConflictPos)[MacroDefLen] != '(')
360 continue; // Longer macro name; keep trying.
Mike Stump11289f42009-09-09 15:08:12 +0000361
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000362 // We found a conflicting macro definition.
363 break;
364 }
Mike Stump11289f42009-09-09 15:08:12 +0000365
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000366 if (ConflictPos != CmdLineLines.end()) {
367 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
368 << MacroName;
369
370 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000371 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
372 FindMacro(Buffers, Missing);
373 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
374 SourceLocation PCHMissingLoc =
375 SourceMgr.getLocForStartOfFile(MacroLoc.first)
376 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar499baed2009-11-11 05:26:28 +0000377 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000378
379 ConflictingDefines = true;
380 continue;
381 }
Mike Stump11289f42009-09-09 15:08:12 +0000382
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000383 // If the macro doesn't conflict, then we'll just pick up the macro
384 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000385 if (ConflictingDefines)
386 continue; // Don't complain if there are already conflicting defs
Mike Stump11289f42009-09-09 15:08:12 +0000387
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000388 if (!MissingDefines) {
389 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
390 MissingDefines = true;
391 }
392
393 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000394 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
395 FindMacro(Buffers, Missing);
396 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
397 SourceLocation PCHMissingLoc =
398 SourceMgr.getLocForStartOfFile(MacroLoc.first)
399 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000400 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
401 }
Mike Stump11289f42009-09-09 15:08:12 +0000402
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000403 if (ConflictingDefines)
404 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000405
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000406 // Determine what predefines were introduced based on command-line
407 // parameters that were not present when building the PCH
408 // file. Extra #defines are okay, so long as the identifiers being
409 // defined were not used within the precompiled header.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000410 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000411 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
412 PCHLines.begin(), PCHLines.end(),
Mike Stump11289f42009-09-09 15:08:12 +0000413 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000414 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000415 llvm::StringRef &Extra = ExtraPredefines[I];
416 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000417 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
418 return true;
419 }
420
421 // This is an extra macro definition. Determine the name of the
422 // macro we're defining.
423 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000424 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000425 = Extra.find_first_of("( \n\r", StartOfMacroName);
426 assert(EndOfMacroName != std::string::npos &&
427 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000428 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000429
430 // Check whether this name was used somewhere in the PCH file. If
431 // so, defining it as a macro could change behavior, so we reject
432 // the PCH file.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000433 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar045c92f2009-11-11 00:52:00 +0000434 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000435 return true;
436 }
437
438 // Add this definition to the suggested predefines buffer.
439 SuggestedPredefines += Extra;
440 SuggestedPredefines += '\n';
441 }
442
443 // If we get here, it's because the predefines buffer had compatible
444 // contents. Accept the PCH file.
445 return false;
446}
447
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000448void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
449 unsigned ID) {
450 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
451 ++NumHeaderInfos;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000452}
453
454void PCHValidator::ReadCounter(unsigned Value) {
455 PP.setCounterValue(Value);
456}
457
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000458//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +0000459// AST reader implementation
Douglas Gregora868bbd2009-04-21 22:25:48 +0000460//===----------------------------------------------------------------------===//
461
Sebastian Redl07a89a82010-07-30 00:29:29 +0000462void
Sebastian Redl3e31c722010-08-18 23:56:56 +0000463ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redl07a89a82010-07-30 00:29:29 +0000464 DeserializationListener = Listener;
Sebastian Redl07a89a82010-07-30 00:29:29 +0000465}
466
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000467
Douglas Gregora868bbd2009-04-21 22:25:48 +0000468namespace {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000469class ASTSelectorLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000470 ASTReader &Reader;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000471
472public:
Sebastian Redl834bb972010-08-04 17:20:04 +0000473 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +0000474 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +0000475 ObjCMethodList Instance, Factory;
476 };
Douglas Gregorc78d3462009-04-24 21:10:55 +0000477
478 typedef Selector external_key_type;
479 typedef external_key_type internal_key_type;
480
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000481 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump11289f42009-09-09 15:08:12 +0000482
Douglas Gregorc78d3462009-04-24 21:10:55 +0000483 static bool EqualKey(const internal_key_type& a,
484 const internal_key_type& b) {
485 return a == b;
486 }
Mike Stump11289f42009-09-09 15:08:12 +0000487
Douglas Gregorc78d3462009-04-24 21:10:55 +0000488 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +0000489 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000490 }
Mike Stump11289f42009-09-09 15:08:12 +0000491
Douglas Gregorc78d3462009-04-24 21:10:55 +0000492 // This hopefully will just get inlined and removed by the optimizer.
493 static const internal_key_type&
494 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000495
Douglas Gregorc78d3462009-04-24 21:10:55 +0000496 static std::pair<unsigned, unsigned>
497 ReadKeyDataLength(const unsigned char*& d) {
498 using namespace clang::io;
499 unsigned KeyLen = ReadUnalignedLE16(d);
500 unsigned DataLen = ReadUnalignedLE16(d);
501 return std::make_pair(KeyLen, DataLen);
502 }
Mike Stump11289f42009-09-09 15:08:12 +0000503
Douglas Gregor95c13f52009-04-25 17:48:32 +0000504 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000505 using namespace clang::io;
Chris Lattner8575daa2009-04-27 21:45:14 +0000506 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000507 unsigned N = ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000508 IdentifierInfo *FirstII
Douglas Gregorc78d3462009-04-24 21:10:55 +0000509 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
510 if (N == 0)
511 return SelTable.getNullarySelector(FirstII);
512 else if (N == 1)
513 return SelTable.getUnarySelector(FirstII);
514
515 llvm::SmallVector<IdentifierInfo *, 16> Args;
516 Args.push_back(FirstII);
517 for (unsigned I = 1; I != N; ++I)
518 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
519
Douglas Gregor038c3382009-05-22 22:45:36 +0000520 return SelTable.getSelector(N, Args.data());
Douglas Gregorc78d3462009-04-24 21:10:55 +0000521 }
Mike Stump11289f42009-09-09 15:08:12 +0000522
Douglas Gregorc78d3462009-04-24 21:10:55 +0000523 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
524 using namespace clang::io;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000525
526 data_type Result;
527
Sebastian Redl834bb972010-08-04 17:20:04 +0000528 Result.ID = ReadUnalignedLE32(d);
529 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
530 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
531
Douglas Gregorc78d3462009-04-24 21:10:55 +0000532 // Load instance methods
533 ObjCMethodList *Prev = 0;
534 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000535 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000536 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000537 if (!Result.Instance.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000538 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000539 Result.Instance.Method = Method;
540 Prev = &Result.Instance;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000541 continue;
542 }
543
Ted Kremenekda4abf12010-02-11 00:53:01 +0000544 ObjCMethodList *Mem =
545 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
546 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000547 Prev = Prev->Next;
548 }
549
550 // Load factory methods
551 Prev = 0;
552 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000553 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000554 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000555 if (!Result.Factory.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000556 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000557 Result.Factory.Method = Method;
558 Prev = &Result.Factory;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000559 continue;
560 }
561
Ted Kremenekda4abf12010-02-11 00:53:01 +0000562 ObjCMethodList *Mem =
563 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
564 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000565 Prev = Prev->Next;
566 }
567
568 return Result;
569 }
570};
Mike Stump11289f42009-09-09 15:08:12 +0000571
572} // end anonymous namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +0000573
574/// \brief The on-disk hash table used for the global method pool.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000575typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
576 ASTSelectorLookupTable;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000577
Sebastian Redl2c373b92010-10-05 15:59:54 +0000578namespace clang {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000579class ASTIdentifierLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000580 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +0000581 ASTReader::PerFileData &F;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000582
583 // If we know the IdentifierInfo in advance, it is here and we will
584 // not build a new one. Used when deserializing information about an
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000585 // identifier that was constructed before the AST file was read.
Douglas Gregora868bbd2009-04-21 22:25:48 +0000586 IdentifierInfo *KnownII;
587
588public:
589 typedef IdentifierInfo * data_type;
590
591 typedef const std::pair<const char*, unsigned> external_key_type;
592
593 typedef external_key_type internal_key_type;
594
Sebastian Redl2c373b92010-10-05 15:59:54 +0000595 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000596 IdentifierInfo *II = 0)
Sebastian Redl2c373b92010-10-05 15:59:54 +0000597 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump11289f42009-09-09 15:08:12 +0000598
Douglas Gregora868bbd2009-04-21 22:25:48 +0000599 static bool EqualKey(const internal_key_type& a,
600 const internal_key_type& b) {
601 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
602 : false;
603 }
Mike Stump11289f42009-09-09 15:08:12 +0000604
Douglas Gregora868bbd2009-04-21 22:25:48 +0000605 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000606 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregora868bbd2009-04-21 22:25:48 +0000607 }
Mike Stump11289f42009-09-09 15:08:12 +0000608
Douglas Gregora868bbd2009-04-21 22:25:48 +0000609 // This hopefully will just get inlined and removed by the optimizer.
610 static const internal_key_type&
611 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000612
Douglas Gregor57756ea2010-10-14 22:11:03 +0000613 // This hopefully will just get inlined and removed by the optimizer.
614 static const external_key_type&
615 GetExternalKey(const internal_key_type& x) { return x; }
616
Douglas Gregora868bbd2009-04-21 22:25:48 +0000617 static std::pair<unsigned, unsigned>
618 ReadKeyDataLength(const unsigned char*& d) {
619 using namespace clang::io;
Douglas Gregor6b7bf5a2009-04-25 20:26:24 +0000620 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor5287b4e2009-04-25 21:04:17 +0000621 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000622 return std::make_pair(KeyLen, DataLen);
623 }
Mike Stump11289f42009-09-09 15:08:12 +0000624
Douglas Gregora868bbd2009-04-21 22:25:48 +0000625 static std::pair<const char*, unsigned>
626 ReadKey(const unsigned char* d, unsigned n) {
627 assert(n >= 2 && d[n-1] == '\0');
628 return std::make_pair((const char*) d, n-1);
629 }
Mike Stump11289f42009-09-09 15:08:12 +0000630
631 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregora868bbd2009-04-21 22:25:48 +0000632 const unsigned char* d,
633 unsigned DataLen) {
634 using namespace clang::io;
Sebastian Redl539c5062010-08-18 23:57:32 +0000635 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000636 bool IsInteresting = ID & 0x01;
637
638 // Wipe out the "is interesting" bit.
639 ID = ID >> 1;
640
641 if (!IsInteresting) {
Sebastian Redl98912122010-07-27 23:01:28 +0000642 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregor1d583f22009-04-28 21:18:29 +0000643 // and associate it with the persistent ID.
644 IdentifierInfo *II = KnownII;
645 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000646 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000647 Reader.SetIdentifierInfo(ID, II);
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000648 II->setIsFromAST();
Douglas Gregor1d583f22009-04-28 21:18:29 +0000649 return II;
650 }
651
Douglas Gregorb9256522009-04-28 21:32:13 +0000652 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000653 bool CPlusPlusOperatorKeyword = Bits & 0x01;
654 Bits >>= 1;
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000655 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
656 Bits >>= 1;
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000657 bool Poisoned = Bits & 0x01;
658 Bits >>= 1;
659 bool ExtensionToken = Bits & 0x01;
660 Bits >>= 1;
661 bool hasMacroDefinition = Bits & 0x01;
662 Bits >>= 1;
663 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
664 Bits >>= 10;
Mike Stump11289f42009-09-09 15:08:12 +0000665
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000666 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorb9256522009-04-28 21:32:13 +0000667 DataLen -= 6;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000668
669 // Build the IdentifierInfo itself and link the identifier ID with
670 // the new IdentifierInfo.
671 IdentifierInfo *II = KnownII;
672 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000673 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000674 Reader.SetIdentifierInfo(ID, II);
675
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000676 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000677 // Token IDs are read-only.
678 if (HasRevertedTokenIDToIdentifier)
679 II->RevertTokenIDToIdentifier();
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000680 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump11289f42009-09-09 15:08:12 +0000681 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000682 "Incorrect extension token flag");
683 (void)ExtensionToken;
684 II->setIsPoisoned(Poisoned);
685 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
686 "Incorrect C++ operator keyword flag");
687 (void)CPlusPlusOperatorKeyword;
688
Douglas Gregorc3366a52009-04-21 23:56:24 +0000689 // If this identifier is a macro, deserialize the macro
690 // definition.
691 if (hasMacroDefinition) {
Douglas Gregorb9256522009-04-28 21:32:13 +0000692 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor5ef9e332010-10-30 00:23:06 +0000693 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregorb9256522009-04-28 21:32:13 +0000694 DataLen -= 4;
Douglas Gregorc3366a52009-04-21 23:56:24 +0000695 }
Douglas Gregora868bbd2009-04-21 22:25:48 +0000696
697 // Read all of the declarations visible at global scope with this
698 // name.
Chris Lattner1d728882009-04-27 22:17:41 +0000699 if (Reader.getContext() == 0) return II;
Douglas Gregor1342e842009-07-06 18:54:52 +0000700 if (DataLen > 0) {
701 llvm::SmallVector<uint32_t, 4> DeclIDs;
702 for (; DataLen > 0; DataLen -= 4)
703 DeclIDs.push_back(ReadUnalignedLE32(d));
704 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000705 }
Mike Stump11289f42009-09-09 15:08:12 +0000706
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000707 II->setIsFromAST();
Douglas Gregora868bbd2009-04-21 22:25:48 +0000708 return II;
709 }
710};
Mike Stump11289f42009-09-09 15:08:12 +0000711
712} // end anonymous namespace
Douglas Gregora868bbd2009-04-21 22:25:48 +0000713
714/// \brief The on-disk hash table used to contain information about
715/// all of the identifiers in the program.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000716typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
717 ASTIdentifierLookupTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000718
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000719namespace {
720class ASTDeclContextNameLookupTrait {
721 ASTReader &Reader;
722
723public:
724 /// \brief Pair of begin/end iterators for DeclIDs.
725 typedef std::pair<DeclID *, DeclID *> data_type;
726
727 /// \brief Special internal key for declaration names.
728 /// The hash table creates keys for comparison; we do not create
729 /// a DeclarationName for the internal key to avoid deserializing types.
730 struct DeclNameKey {
731 DeclarationName::NameKind Kind;
732 uint64_t Data;
733 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
734 };
735
736 typedef DeclarationName external_key_type;
737 typedef DeclNameKey internal_key_type;
738
739 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
740
741 static bool EqualKey(const internal_key_type& a,
742 const internal_key_type& b) {
743 return a.Kind == b.Kind && a.Data == b.Data;
744 }
745
746 unsigned ComputeHash(const DeclNameKey &Key) const {
747 llvm::FoldingSetNodeID ID;
748 ID.AddInteger(Key.Kind);
749
750 switch (Key.Kind) {
751 case DeclarationName::Identifier:
752 case DeclarationName::CXXLiteralOperatorName:
753 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
754 break;
755 case DeclarationName::ObjCZeroArgSelector:
756 case DeclarationName::ObjCOneArgSelector:
757 case DeclarationName::ObjCMultiArgSelector:
758 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
759 break;
760 case DeclarationName::CXXConstructorName:
761 case DeclarationName::CXXDestructorName:
762 case DeclarationName::CXXConversionFunctionName:
763 ID.AddInteger((TypeID)Key.Data);
764 break;
765 case DeclarationName::CXXOperatorName:
766 ID.AddInteger((OverloadedOperatorKind)Key.Data);
767 break;
768 case DeclarationName::CXXUsingDirective:
769 break;
770 }
771
772 return ID.ComputeHash();
773 }
774
775 internal_key_type GetInternalKey(const external_key_type& Name) const {
776 DeclNameKey Key;
777 Key.Kind = Name.getNameKind();
778 switch (Name.getNameKind()) {
779 case DeclarationName::Identifier:
780 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
781 break;
782 case DeclarationName::ObjCZeroArgSelector:
783 case DeclarationName::ObjCOneArgSelector:
784 case DeclarationName::ObjCMultiArgSelector:
785 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
786 break;
787 case DeclarationName::CXXConstructorName:
788 case DeclarationName::CXXDestructorName:
789 case DeclarationName::CXXConversionFunctionName:
790 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
791 break;
792 case DeclarationName::CXXOperatorName:
793 Key.Data = Name.getCXXOverloadedOperator();
794 break;
795 case DeclarationName::CXXLiteralOperatorName:
796 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
797 break;
798 case DeclarationName::CXXUsingDirective:
799 break;
800 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000801
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000802 return Key;
803 }
804
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000805 external_key_type GetExternalKey(const internal_key_type& Key) const {
806 ASTContext *Context = Reader.getContext();
807 switch (Key.Kind) {
808 case DeclarationName::Identifier:
809 return DeclarationName((IdentifierInfo*)Key.Data);
810
811 case DeclarationName::ObjCZeroArgSelector:
812 case DeclarationName::ObjCOneArgSelector:
813 case DeclarationName::ObjCMultiArgSelector:
814 return DeclarationName(Selector(Key.Data));
815
816 case DeclarationName::CXXConstructorName:
817 return Context->DeclarationNames.getCXXConstructorName(
818 Context->getCanonicalType(Reader.GetType(Key.Data)));
819
820 case DeclarationName::CXXDestructorName:
821 return Context->DeclarationNames.getCXXDestructorName(
822 Context->getCanonicalType(Reader.GetType(Key.Data)));
823
824 case DeclarationName::CXXConversionFunctionName:
825 return Context->DeclarationNames.getCXXConversionFunctionName(
826 Context->getCanonicalType(Reader.GetType(Key.Data)));
827
828 case DeclarationName::CXXOperatorName:
829 return Context->DeclarationNames.getCXXOperatorName(
830 (OverloadedOperatorKind)Key.Data);
831
832 case DeclarationName::CXXLiteralOperatorName:
833 return Context->DeclarationNames.getCXXLiteralOperatorName(
834 (IdentifierInfo*)Key.Data);
835
836 case DeclarationName::CXXUsingDirective:
837 return DeclarationName::getUsingDirectiveName();
838 }
839
840 llvm_unreachable("Invalid Name Kind ?");
841 }
842
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000843 static std::pair<unsigned, unsigned>
844 ReadKeyDataLength(const unsigned char*& d) {
845 using namespace clang::io;
846 unsigned KeyLen = ReadUnalignedLE16(d);
847 unsigned DataLen = ReadUnalignedLE16(d);
848 return std::make_pair(KeyLen, DataLen);
849 }
850
851 internal_key_type ReadKey(const unsigned char* d, unsigned) {
852 using namespace clang::io;
853
854 DeclNameKey Key;
855 Key.Kind = (DeclarationName::NameKind)*d++;
856 switch (Key.Kind) {
857 case DeclarationName::Identifier:
858 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
859 break;
860 case DeclarationName::ObjCZeroArgSelector:
861 case DeclarationName::ObjCOneArgSelector:
862 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000863 Key.Data =
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000864 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
865 break;
866 case DeclarationName::CXXConstructorName:
867 case DeclarationName::CXXDestructorName:
868 case DeclarationName::CXXConversionFunctionName:
869 Key.Data = ReadUnalignedLE32(d); // TypeID
870 break;
871 case DeclarationName::CXXOperatorName:
872 Key.Data = *d++; // OverloadedOperatorKind
873 break;
874 case DeclarationName::CXXLiteralOperatorName:
875 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
876 break;
877 case DeclarationName::CXXUsingDirective:
878 break;
879 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000880
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000881 return Key;
882 }
883
884 data_type ReadData(internal_key_type, const unsigned char* d,
885 unsigned DataLen) {
886 using namespace clang::io;
887 unsigned NumDecls = ReadUnalignedLE16(d);
888 DeclID *Start = (DeclID *)d;
889 return std::make_pair(Start, Start + NumDecls);
890 }
891};
892
893} // end anonymous namespace
894
895/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
896typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
897 ASTDeclContextNameLookupTable;
898
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000899bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
900 const std::pair<uint64_t, uint64_t> &Offsets,
901 DeclContextInfo &Info) {
902 SavedStreamPosition SavedPosition(Cursor);
903 // First the lexical decls.
904 if (Offsets.first != 0) {
905 Cursor.JumpToBit(Offsets.first);
906
907 RecordData Record;
908 const char *Blob;
909 unsigned BlobLen;
910 unsigned Code = Cursor.ReadCode();
911 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
912 if (RecCode != DECL_CONTEXT_LEXICAL) {
913 Error("Expected lexical block");
914 return true;
915 }
916
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000917 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
918 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000919 } else {
920 Info.LexicalDecls = 0;
921 Info.NumLexicalDecls = 0;
922 }
923
924 // Now the lookup table.
925 if (Offsets.second != 0) {
926 Cursor.JumpToBit(Offsets.second);
927
928 RecordData Record;
929 const char *Blob;
930 unsigned BlobLen;
931 unsigned Code = Cursor.ReadCode();
932 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
933 if (RecCode != DECL_CONTEXT_VISIBLE) {
934 Error("Expected visible lookup table block");
935 return true;
936 }
937 Info.NameLookupTableData
938 = ASTDeclContextNameLookupTable::Create(
939 (const unsigned char *)Blob + Record[0],
940 (const unsigned char *)Blob,
941 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl9d8f58b2010-08-24 00:50:00 +0000942 } else {
943 Info.NameLookupTableData = 0;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000944 }
945
946 return false;
947}
948
Sebastian Redl2c499f62010-08-18 23:56:43 +0000949void ASTReader::Error(const char *Msg) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000950 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000951}
952
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000953/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000954bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000955 if (Listener)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000956 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000957 ActualOriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000958 SuggestedPredefines);
Douglas Gregorc379c072009-04-28 18:58:38 +0000959 return false;
Douglas Gregor92863e42009-04-10 23:10:45 +0000960}
961
Douglas Gregorc5046832009-04-27 18:38:38 +0000962//===----------------------------------------------------------------------===//
963// Source Manager Deserialization
964//===----------------------------------------------------------------------===//
965
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000966/// \brief Read the line table in the source manager block.
Sebastian Redl2c373b92010-10-05 15:59:54 +0000967/// \returns true if there was an error.
968bool ASTReader::ParseLineTable(PerFileData &F,
969 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000970 unsigned Idx = 0;
971 LineTableInfo &LineTable = SourceMgr.getLineTable();
972
973 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +0000974 std::map<int, int> FileIDs;
975 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000976 // Extract the file name
977 unsigned FilenameLen = Record[Idx++];
978 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
979 Idx += FilenameLen;
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000980 MaybeAddSystemRootToFilename(Filename);
Mike Stump11289f42009-09-09 15:08:12 +0000981 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregora8854652009-04-13 17:12:42 +0000982 Filename.size());
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000983 }
984
985 // Parse the line entries
986 std::vector<LineEntry> Entries;
987 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000988 int FID = Record[Idx++];
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000989
990 // Extract the line entries
991 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000992 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000993 Entries.clear();
994 Entries.reserve(NumEntries);
995 for (unsigned I = 0; I != NumEntries; ++I) {
996 unsigned FileOffset = Record[Idx++];
997 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000998 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +0000999 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001000 = (SrcMgr::CharacteristicKind)Record[Idx++];
1001 unsigned IncludeOffset = Record[Idx++];
1002 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1003 FileKind, IncludeOffset));
1004 }
1005 LineTable.AddEntry(FID, Entries);
1006 }
1007
1008 return false;
1009}
1010
Douglas Gregorc5046832009-04-27 18:38:38 +00001011namespace {
1012
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001013class ASTStatData {
Douglas Gregorc5046832009-04-27 18:38:38 +00001014public:
Douglas Gregorc5046832009-04-27 18:38:38 +00001015 const ino_t ino;
1016 const dev_t dev;
1017 const mode_t mode;
1018 const time_t mtime;
1019 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +00001020
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001021 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner2a6fa472010-11-23 19:28:12 +00001022 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregorc5046832009-04-27 18:38:38 +00001023};
1024
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001025class ASTStatLookupTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001026 public:
1027 typedef const char *external_key_type;
1028 typedef const char *internal_key_type;
1029
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001030 typedef ASTStatData data_type;
Douglas Gregorc5046832009-04-27 18:38:38 +00001031
1032 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001033 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001034 }
1035
1036 static internal_key_type GetInternalKey(const char *path) { return path; }
1037
1038 static bool EqualKey(internal_key_type a, internal_key_type b) {
1039 return strcmp(a, b) == 0;
1040 }
1041
1042 static std::pair<unsigned, unsigned>
1043 ReadKeyDataLength(const unsigned char*& d) {
1044 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1045 unsigned DataLen = (unsigned) *d++;
1046 return std::make_pair(KeyLen + 1, DataLen);
1047 }
1048
1049 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1050 return (const char *)d;
1051 }
1052
1053 static data_type ReadData(const internal_key_type, const unsigned char *d,
1054 unsigned /*DataLen*/) {
1055 using namespace clang::io;
1056
Douglas Gregorc5046832009-04-27 18:38:38 +00001057 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1058 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1059 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +00001060 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +00001061 off_t size = (off_t) ReadUnalignedLE64(d);
1062 return data_type(ino, dev, mode, mtime, size);
1063 }
1064};
1065
1066/// \brief stat() cache for precompiled headers.
1067///
1068/// This cache is very similar to the stat cache used by pretokenized
1069/// headers.
Chris Lattner226efd32010-11-23 19:19:34 +00001070class ASTStatCache : public FileSystemStatCache {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001071 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregorc5046832009-04-27 18:38:38 +00001072 CacheTy *Cache;
1073
1074 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +00001075public:
Chris Lattner2a6fa472010-11-23 19:28:12 +00001076 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1077 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +00001078 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1079 Cache = CacheTy::Create(Buckets, Base);
1080 }
1081
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001082 ~ASTStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +00001083
Chris Lattnerdd278432010-11-23 21:17:56 +00001084 LookupResult getStat(const char *Path, struct stat &StatBuf,
1085 int *FileDescriptor) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001086 // Do the lookup for the file's data in the AST file.
Chris Lattner226efd32010-11-23 19:19:34 +00001087 CacheTy::iterator I = Cache->find(Path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001088
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001089 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregorc5046832009-04-27 18:38:38 +00001090 if (I == Cache->end()) {
1091 ++NumStatMisses;
Chris Lattnerdd278432010-11-23 21:17:56 +00001092 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregorc5046832009-04-27 18:38:38 +00001093 }
Mike Stump11289f42009-09-09 15:08:12 +00001094
Douglas Gregorc5046832009-04-27 18:38:38 +00001095 ++NumStatHits;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001096 ASTStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001097
Chris Lattner226efd32010-11-23 19:19:34 +00001098 StatBuf.st_ino = Data.ino;
1099 StatBuf.st_dev = Data.dev;
1100 StatBuf.st_mtime = Data.mtime;
1101 StatBuf.st_mode = Data.mode;
1102 StatBuf.st_size = Data.size;
Chris Lattner8f0583d2010-11-23 20:05:15 +00001103 return CacheExists;
Douglas Gregorc5046832009-04-27 18:38:38 +00001104 }
1105};
1106} // end anonymous namespace
1107
1108
Sebastian Redl393f8b72010-07-19 20:52:06 +00001109/// \brief Read a source manager block
Sebastian Redl2c499f62010-08-18 23:56:43 +00001110ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001111 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +00001112
Sebastian Redl393f8b72010-07-19 20:52:06 +00001113 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001114
Douglas Gregor258ae542009-04-27 06:38:32 +00001115 // Set the source-location entry cursor to the current position in
1116 // the stream. This cursor will be used to read the contents of the
1117 // source manager block initially, and then lazily read
1118 // source-location entries as needed.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001119 SLocEntryCursor = F.Stream;
Douglas Gregor258ae542009-04-27 06:38:32 +00001120
1121 // The stream itself is going to skip over the source manager block.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001122 if (F.Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001123 Error("malformed block record in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001124 return Failure;
1125 }
1126
1127 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001128 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001129 Error("malformed source manager block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001130 return Failure;
1131 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001132
Douglas Gregora7f71a92009-04-10 03:52:48 +00001133 RecordData Record;
1134 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001135 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001136 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001137 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001138 Error("error at end of Source Manager block in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001139 return Failure;
1140 }
Douglas Gregor92863e42009-04-10 23:10:45 +00001141 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001142 }
Mike Stump11289f42009-09-09 15:08:12 +00001143
Douglas Gregora7f71a92009-04-10 03:52:48 +00001144 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1145 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +00001146 SLocEntryCursor.ReadSubBlockID();
1147 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001148 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001149 return Failure;
1150 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001151 continue;
1152 }
Mike Stump11289f42009-09-09 15:08:12 +00001153
Douglas Gregora7f71a92009-04-10 03:52:48 +00001154 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001155 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001156 continue;
1157 }
Mike Stump11289f42009-09-09 15:08:12 +00001158
Douglas Gregora7f71a92009-04-10 03:52:48 +00001159 // Read a record.
1160 const char *BlobStart;
1161 unsigned BlobLen;
1162 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +00001163 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001164 default: // Default behavior: ignore.
1165 break;
1166
Sebastian Redl539c5062010-08-18 23:57:32 +00001167 case SM_LINE_TABLE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00001168 if (ParseLineTable(F, Record))
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001169 return Failure;
Chris Lattner184e65d2009-04-14 23:22:57 +00001170 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00001171
Sebastian Redl539c5062010-08-18 23:57:32 +00001172 case SM_SLOC_FILE_ENTRY:
1173 case SM_SLOC_BUFFER_ENTRY:
1174 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor258ae542009-04-27 06:38:32 +00001175 // Once we hit one of the source location entries, we're done.
1176 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001177 }
1178 }
1179}
1180
Sebastian Redl06750302010-07-20 21:50:20 +00001181/// \brief Get a cursor that's correctly positioned for reading the source
1182/// location entry with the given ID.
Sebastian Redl2c373b92010-10-05 15:59:54 +00001183ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl06750302010-07-20 21:50:20 +00001184 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1185 "SLocCursorForID should only be called for real IDs.");
1186
1187 ID -= 1;
1188 PerFileData *F = 0;
1189 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1190 F = Chain[N - I - 1];
1191 if (ID < F->LocalNumSLocEntries)
1192 break;
1193 ID -= F->LocalNumSLocEntries;
1194 }
1195 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1196
1197 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00001198 return F;
Sebastian Redl06750302010-07-20 21:50:20 +00001199}
1200
Douglas Gregor258ae542009-04-27 06:38:32 +00001201/// \brief Read in the source location entry with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001202ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001203 if (ID == 0)
1204 return Success;
1205
1206 if (ID > TotalNumSLocEntries) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001207 Error("source location entry ID out-of-range for AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001208 return Failure;
1209 }
1210
Sebastian Redl2c373b92010-10-05 15:59:54 +00001211 PerFileData *F = SLocCursorForID(ID);
1212 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001213
Douglas Gregor258ae542009-04-27 06:38:32 +00001214 ++NumSLocEntriesRead;
Douglas Gregor258ae542009-04-27 06:38:32 +00001215 unsigned Code = SLocEntryCursor.ReadCode();
1216 if (Code == llvm::bitc::END_BLOCK ||
1217 Code == llvm::bitc::ENTER_SUBBLOCK ||
1218 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001219 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001220 return Failure;
1221 }
1222
Douglas Gregor258ae542009-04-27 06:38:32 +00001223 RecordData Record;
1224 const char *BlobStart;
1225 unsigned BlobLen;
1226 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1227 default:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001228 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001229 return Failure;
1230
Sebastian Redl539c5062010-08-18 23:57:32 +00001231 case SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001232 std::string Filename(BlobStart, BlobStart + BlobLen);
1233 MaybeAddSystemRootToFilename(Filename);
Chris Lattner5159f612010-11-23 08:35:12 +00001234 const FileEntry *File = FileMgr.getFile(Filename);
Axel Naumann63fbaed2011-01-27 10:55:51 +00001235 if (File == 0)
1236 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1237 (time_t)Record[5]);
Chris Lattnerd20dc872009-06-15 04:35:16 +00001238 if (File == 0) {
1239 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001240 ErrorStr += Filename;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001241 ErrorStr += "' referenced by AST file";
Chris Lattnerd20dc872009-06-15 04:35:16 +00001242 Error(ErrorStr.c_str());
1243 return Failure;
1244 }
Mike Stump11289f42009-09-09 15:08:12 +00001245
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001246 if (Record.size() < 10) {
Ted Kremenekabb1ddd2010-03-18 21:23:05 +00001247 Error("source location entry is incorrect");
1248 return Failure;
1249 }
1250
Douglas Gregorce3a8292010-07-27 00:27:13 +00001251 if (!DisableValidation &&
1252 ((off_t)Record[4] != File->getSize()
Douglas Gregor08288f22010-04-09 15:54:22 +00001253#if !defined(LLVM_ON_WIN32)
1254 // In our regression testing, the Windows file system seems to
1255 // have inconsistent modification times that sometimes
1256 // erroneously trigger this error-handling path.
Douglas Gregorce3a8292010-07-27 00:27:13 +00001257 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor08288f22010-04-09 15:54:22 +00001258#endif
Douglas Gregorce3a8292010-07-27 00:27:13 +00001259 )) {
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001260 Diag(diag::err_fe_pch_file_modified)
1261 << Filename;
1262 return Failure;
1263 }
1264
Chris Lattner26b5c192010-11-23 09:19:42 +00001265 FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]),
1266 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor258ae542009-04-27 06:38:32 +00001267 ID, Record[0]);
1268 if (Record[3])
1269 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1270 .setHasLineDirectives();
1271
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001272 // Reconstruct header-search information for this file.
1273 HeaderFileInfo HFI;
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001274 HFI.isImport = Record[6];
1275 HFI.DirInfo = Record[7];
1276 HFI.NumIncludes = Record[8];
1277 HFI.ControllingMacroID = Record[9];
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001278 if (Listener)
1279 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor258ae542009-04-27 06:38:32 +00001280 break;
1281 }
1282
Sebastian Redl539c5062010-08-18 23:57:32 +00001283 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor258ae542009-04-27 06:38:32 +00001284 const char *Name = BlobStart;
1285 unsigned Offset = Record[0];
1286 unsigned Code = SLocEntryCursor.ReadCode();
1287 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001288 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +00001289 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001290
Sebastian Redl539c5062010-08-18 23:57:32 +00001291 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001292 Error("AST record has invalid code");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001293 return Failure;
1294 }
1295
Douglas Gregor258ae542009-04-27 06:38:32 +00001296 llvm::MemoryBuffer *Buffer
Chris Lattner58c79342010-04-05 22:42:27 +00001297 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1298 Name);
Douglas Gregor258ae542009-04-27 06:38:32 +00001299 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump11289f42009-09-09 15:08:12 +00001300
Douglas Gregore6648fb2009-04-28 20:33:11 +00001301 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001302 PCHPredefinesBlock Block = {
1303 BufferID,
1304 llvm::StringRef(BlobStart, BlobLen - 1)
1305 };
1306 PCHPredefinesBuffers.push_back(Block);
Douglas Gregore6648fb2009-04-28 20:33:11 +00001307 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001308
1309 break;
1310 }
1311
Sebastian Redl539c5062010-08-18 23:57:32 +00001312 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001313 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001314 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001315 ReadSourceLocation(*F, Record[2]),
1316 ReadSourceLocation(*F, Record[3]),
Douglas Gregor258ae542009-04-27 06:38:32 +00001317 Record[4],
1318 ID,
1319 Record[0]);
1320 break;
Mike Stump11289f42009-09-09 15:08:12 +00001321 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001322 }
1323
1324 return Success;
1325}
1326
Chris Lattnere78a6be2009-04-27 01:05:14 +00001327/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1328/// specified cursor. Read the abbreviations that are at the top of the block
1329/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001330bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001331 unsigned BlockID) {
1332 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001333 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001334 return Failure;
1335 }
Mike Stump11289f42009-09-09 15:08:12 +00001336
Chris Lattnere78a6be2009-04-27 01:05:14 +00001337 while (true) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001338 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattnere78a6be2009-04-27 01:05:14 +00001339 unsigned Code = Cursor.ReadCode();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001340
Chris Lattnere78a6be2009-04-27 01:05:14 +00001341 // We expect all abbrevs to be at the start of the block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001342 if (Code != llvm::bitc::DEFINE_ABBREV) {
1343 Cursor.JumpToBit(Offset);
Chris Lattnere78a6be2009-04-27 01:05:14 +00001344 return false;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001345 }
Chris Lattnere78a6be2009-04-27 01:05:14 +00001346 Cursor.ReadAbbrevRecord();
1347 }
1348}
1349
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001350PreprocessedEntity *ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001351 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregor796d76a2010-10-20 22:00:55 +00001352 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump11289f42009-09-09 15:08:12 +00001353
Douglas Gregorc3366a52009-04-21 23:56:24 +00001354 // Keep track of where we are in the stream, then jump back there
1355 // after reading this macro.
1356 SavedStreamPosition SavedPosition(Stream);
1357
1358 Stream.JumpToBit(Offset);
1359 RecordData Record;
1360 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1361 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001362
Douglas Gregorc3366a52009-04-21 23:56:24 +00001363 while (true) {
1364 unsigned Code = Stream.ReadCode();
1365 switch (Code) {
1366 case llvm::bitc::END_BLOCK:
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001367 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001368
1369 case llvm::bitc::ENTER_SUBBLOCK:
1370 // No known subblocks, always skip them.
1371 Stream.ReadSubBlockID();
1372 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001373 Error("malformed block record in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001374 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001375 }
1376 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001377
Douglas Gregorc3366a52009-04-21 23:56:24 +00001378 case llvm::bitc::DEFINE_ABBREV:
1379 Stream.ReadAbbrevRecord();
1380 continue;
1381 default: break;
1382 }
1383
1384 // Read a record.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001385 const char *BlobStart = 0;
1386 unsigned BlobLen = 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001387 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001388 PreprocessorRecordTypes RecType =
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001389 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001390 BlobLen);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001391 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001392 case PP_MACRO_OBJECT_LIKE:
1393 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001394 // If we already have a macro, that means that we've hit the end
1395 // of the definition of the macro we were looking for. We're
1396 // done.
1397 if (Macro)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001398 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001399
1400 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1401 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001402 Error("macro must have a name in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001403 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001404 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00001405 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001406 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001407
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001408 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001409 MI->setIsUsed(isUsed);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001410 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001411
Douglas Gregoraae92242010-03-19 21:51:54 +00001412 unsigned NextIndex = 3;
Sebastian Redl539c5062010-08-18 23:57:32 +00001413 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001414 // Decode function-like macro info.
1415 bool isC99VarArgs = Record[3];
1416 bool isGNUVarArgs = Record[4];
1417 MacroArgs.clear();
1418 unsigned NumArgs = Record[5];
Douglas Gregoraae92242010-03-19 21:51:54 +00001419 NextIndex = 6 + NumArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001420 for (unsigned i = 0; i != NumArgs; ++i)
1421 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1422
1423 // Install function-like macro info.
1424 MI->setIsFunctionLike();
1425 if (isC99VarArgs) MI->setIsC99Varargs();
1426 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001427 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001428 PP->getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001429 }
1430
1431 // Finally, install the macro.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001432 PP->setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001433
1434 // Remember that we saw this macro last so that we add the tokens that
1435 // form its body to it.
1436 Macro = MI;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001437
Douglas Gregoraae92242010-03-19 21:51:54 +00001438 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1439 // We have a macro definition. Load it now.
1440 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1441 getMacroDefinition(Record[NextIndex]));
1442 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001443
Douglas Gregorc3366a52009-04-21 23:56:24 +00001444 ++NumMacrosRead;
1445 break;
1446 }
Mike Stump11289f42009-09-09 15:08:12 +00001447
Sebastian Redl539c5062010-08-18 23:57:32 +00001448 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001449 // If we see a TOKEN before a PP_MACRO_*, then the file is
1450 // erroneous, just pretend we didn't see this.
1451 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001452
Douglas Gregorc3366a52009-04-21 23:56:24 +00001453 Token Tok;
1454 Tok.startToken();
Sebastian Redl2c373b92010-10-05 15:59:54 +00001455 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001456 Tok.setLength(Record[1]);
1457 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1458 Tok.setIdentifierInfo(II);
1459 Tok.setKind((tok::TokenKind)Record[3]);
1460 Tok.setFlag((Token::TokenFlags)Record[4]);
1461 Macro->AddTokenToBody(Tok);
1462 break;
1463 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001464
Sebastian Redl539c5062010-08-18 23:57:32 +00001465 case PP_MACRO_INSTANTIATION: {
Douglas Gregoraae92242010-03-19 21:51:54 +00001466 // If we already have a macro, that means that we've hit the end
1467 // of the definition of the macro we were looking for. We're
1468 // done.
1469 if (Macro)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001470 return 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001471
Douglas Gregoraae92242010-03-19 21:51:54 +00001472 if (!PP->getPreprocessingRecord()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001473 Error("missing preprocessing record in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001474 return 0;
Douglas Gregoraae92242010-03-19 21:51:54 +00001475 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001476
Douglas Gregoraae92242010-03-19 21:51:54 +00001477 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001478 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1479 return PE;
Douglas Gregoraae92242010-03-19 21:51:54 +00001480
1481 MacroInstantiation *MI
1482 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
Sebastian Redl2c373b92010-10-05 15:59:54 +00001483 SourceRange(ReadSourceLocation(F, Record[1]),
1484 ReadSourceLocation(F, Record[2])),
Douglas Gregoraae92242010-03-19 21:51:54 +00001485 getMacroDefinition(Record[4]));
1486 PPRec.SetPreallocatedEntity(Record[0], MI);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001487 return MI;
Douglas Gregoraae92242010-03-19 21:51:54 +00001488 }
1489
Sebastian Redl539c5062010-08-18 23:57:32 +00001490 case PP_MACRO_DEFINITION: {
Douglas Gregoraae92242010-03-19 21:51:54 +00001491 // If we already have a macro, that means that we've hit the end
1492 // of the definition of the macro we were looking for. We're
1493 // done.
1494 if (Macro)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001495 return 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001496
Douglas Gregoraae92242010-03-19 21:51:54 +00001497 if (!PP->getPreprocessingRecord()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001498 Error("missing preprocessing record in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001499 return 0;
Douglas Gregoraae92242010-03-19 21:51:54 +00001500 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001501
Douglas Gregoraae92242010-03-19 21:51:54 +00001502 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001503 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1504 return PE;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001505
Douglas Gregor91096292010-10-02 19:29:26 +00001506 if (Record[1] > MacroDefinitionsLoaded.size()) {
Douglas Gregoraae92242010-03-19 21:51:54 +00001507 Error("out-of-bounds macro definition record");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001508 return 0;
Douglas Gregoraae92242010-03-19 21:51:54 +00001509 }
1510
Douglas Gregor91096292010-10-02 19:29:26 +00001511 // Decode the identifier info and then check again; if the macro is
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001512 // still defined and associated with the identifier,
Douglas Gregor91096292010-10-02 19:29:26 +00001513 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1514 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1515 MacroDefinition *MD
1516 = new (PPRec) MacroDefinition(II,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001517 ReadSourceLocation(F, Record[5]),
Douglas Gregor36ea4d42010-10-01 20:33:34 +00001518 SourceRange(
Sebastian Redl2c373b92010-10-05 15:59:54 +00001519 ReadSourceLocation(F, Record[2]),
1520 ReadSourceLocation(F, Record[3])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001521
Douglas Gregor91096292010-10-02 19:29:26 +00001522 PPRec.SetPreallocatedEntity(Record[0], MD);
1523 MacroDefinitionsLoaded[Record[1] - 1] = MD;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001524
Douglas Gregor91096292010-10-02 19:29:26 +00001525 if (DeserializationListener)
1526 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1527 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001528
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001529 return MacroDefinitionsLoaded[Record[1] - 1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001530 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001531
Douglas Gregor796d76a2010-10-20 22:00:55 +00001532 case PP_INCLUSION_DIRECTIVE: {
1533 // If we already have a macro, that means that we've hit the end
1534 // of the definition of the macro we were looking for. We're
1535 // done.
1536 if (Macro)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001537 return 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001538
Douglas Gregor796d76a2010-10-20 22:00:55 +00001539 if (!PP->getPreprocessingRecord()) {
1540 Error("missing preprocessing record in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001541 return 0;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001542 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001543
Douglas Gregor796d76a2010-10-20 22:00:55 +00001544 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001545 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1546 return PE;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001547
1548 const char *FullFileNameStart = BlobStart + Record[3];
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001549 const FileEntry *File
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001550 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
1551 BlobLen - Record[3]));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001552
Douglas Gregor796d76a2010-10-20 22:00:55 +00001553 // FIXME: Stable encoding
1554 InclusionDirective::InclusionKind Kind
1555 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1556 InclusionDirective *ID
Douglas Gregorf09b6c92010-11-01 15:03:47 +00001557 = new (PPRec) InclusionDirective(PPRec, Kind,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001558 llvm::StringRef(BlobStart, Record[3]),
1559 Record[4],
1560 File,
1561 SourceRange(ReadSourceLocation(F, Record[1]),
1562 ReadSourceLocation(F, Record[2])));
1563 PPRec.SetPreallocatedEntity(Record[0], ID);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001564 return ID;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001565 }
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001566 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001567 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001568
1569 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001570}
1571
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001572void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1573 uint64_t Offset) {
1574 // Note that this identifier has a macro definition.
1575 II->setHasMacroDefinition(true);
1576
1577 // Adjust the offset based on our position in the chain.
1578 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1579 if (Chain[I] == &F)
1580 break;
1581
1582 Offset += Chain[I]->SizeInBits;
1583 }
1584
1585 UnreadMacroRecordOffsets[II] = Offset;
1586}
1587
Sebastian Redl2c499f62010-08-18 23:56:43 +00001588void ASTReader::ReadDefinedMacros() {
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001589 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001590 PerFileData &F = *Chain[N - I - 1];
1591 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001592
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001593 // If there was no preprocessor block, skip this file.
1594 if (!MacroCursor.getBitStreamReader())
1595 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001596
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001597 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001598 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001599
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001600 RecordData Record;
1601 while (true) {
Sebastian Redl4102dd52010-09-28 02:55:49 +00001602 uint64_t Offset = Cursor.GetCurrentBitNo();
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001603 unsigned Code = Cursor.ReadCode();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001604 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001605 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001606
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001607 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1608 // No known subblocks, always skip them.
1609 Cursor.ReadSubBlockID();
1610 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001611 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001612 return;
1613 }
1614 continue;
1615 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001616
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001617 if (Code == llvm::bitc::DEFINE_ABBREV) {
1618 Cursor.ReadAbbrevRecord();
1619 continue;
1620 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001621
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001622 // Read a record.
1623 const char *BlobStart;
1624 unsigned BlobLen;
1625 Record.clear();
1626 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1627 default: // Default behavior: ignore.
1628 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001629
Sebastian Redl539c5062010-08-18 23:57:32 +00001630 case PP_MACRO_OBJECT_LIKE:
1631 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001632 DecodeIdentifierInfo(Record[0]);
1633 break;
1634
Sebastian Redl539c5062010-08-18 23:57:32 +00001635 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001636 // Ignore tokens.
1637 break;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001638
Sebastian Redl539c5062010-08-18 23:57:32 +00001639 case PP_MACRO_INSTANTIATION:
1640 case PP_MACRO_DEFINITION:
Douglas Gregor796d76a2010-10-20 22:00:55 +00001641 case PP_INCLUSION_DIRECTIVE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001642 // Read the macro record.
Sebastian Redl4102dd52010-09-28 02:55:49 +00001643 // FIXME: That's a stupid way to do this. We should reuse this cursor.
Sebastian Redl2c373b92010-10-05 15:59:54 +00001644 ReadMacroRecord(F, Offset);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001645 break;
1646 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001647 }
1648 }
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001649
1650 // Drain the unread macro-record offsets map.
1651 while (!UnreadMacroRecordOffsets.empty())
1652 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1653}
1654
1655void ASTReader::LoadMacroDefinition(
1656 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1657 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1658 PerFileData *F = 0;
1659 uint64_t Offset = Pos->second;
1660 UnreadMacroRecordOffsets.erase(Pos);
1661
1662 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1663 if (Offset < Chain[I]->SizeInBits) {
1664 F = Chain[I];
1665 break;
1666 }
1667
1668 Offset -= Chain[I]->SizeInBits;
1669 }
1670 if (!F) {
1671 Error("Malformed macro record offset");
1672 return;
1673 }
1674
1675 ReadMacroRecord(*F, Offset);
1676}
1677
1678void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1679 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1680 = UnreadMacroRecordOffsets.find(II);
1681 LoadMacroDefinition(Pos);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001682}
1683
Sebastian Redl50e26582010-09-15 19:54:06 +00001684MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor91096292010-10-02 19:29:26 +00001685 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregoraae92242010-03-19 21:51:54 +00001686 return 0;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001687
Douglas Gregor91096292010-10-02 19:29:26 +00001688 if (!MacroDefinitionsLoaded[ID - 1]) {
1689 unsigned Index = ID - 1;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001690 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1691 PerFileData &F = *Chain[N - I - 1];
1692 if (Index < F.LocalNumMacroDefinitions) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001693 ReadMacroRecord(F, F.MacroDefinitionOffsets[Index]);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001694 break;
1695 }
1696 Index -= F.LocalNumMacroDefinitions;
1697 }
Douglas Gregor91096292010-10-02 19:29:26 +00001698 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001699 }
1700
Douglas Gregor91096292010-10-02 19:29:26 +00001701 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001702}
1703
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001704/// \brief If we are loading a relocatable PCH file, and the filename is
1705/// not an absolute path, add the system root to the beginning of the file
1706/// name.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001707void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001708 // If this is not a relocatable PCH file, there's nothing to do.
1709 if (!RelocatablePCH)
1710 return;
Mike Stump11289f42009-09-09 15:08:12 +00001711
Michael J. Spencerf28df4c2010-12-17 21:22:22 +00001712 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001713 return;
1714
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001715 if (isysroot == 0) {
1716 // If no system root was given, default to '/'
1717 Filename.insert(Filename.begin(), '/');
1718 return;
1719 }
Mike Stump11289f42009-09-09 15:08:12 +00001720
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001721 unsigned Length = strlen(isysroot);
1722 if (isysroot[Length - 1] != '/')
1723 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001724
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001725 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1726}
1727
Sebastian Redl2c499f62010-08-18 23:56:43 +00001728ASTReader::ASTReadResult
Sebastian Redl3e31c722010-08-18 23:56:56 +00001729ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001730 llvm::BitstreamCursor &Stream = F.Stream;
1731
Sebastian Redl539c5062010-08-18 23:57:32 +00001732 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001733 Error("malformed block record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001734 return Failure;
1735 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001736
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001737 // Read all of the records and blocks for the ASt file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001738 RecordData Record;
Sebastian Redl393f8b72010-07-19 20:52:06 +00001739 bool First = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001740 while (!Stream.AtEndOfStream()) {
1741 unsigned Code = Stream.ReadCode();
1742 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001743 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001744 Error("error at end of module block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001745 return Failure;
1746 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001747
Douglas Gregor55abb232009-04-10 20:39:37 +00001748 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001749 }
1750
1751 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1752 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001753 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001754 // We lazily load the decls block, but we want to set up the
1755 // DeclsCursor cursor to point into it. Clone our current bitcode
1756 // cursor to it, enter the block and read the abbrevs in that block.
1757 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001758 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001759 if (Stream.SkipBlock() || // Skip with the main cursor.
1760 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001761 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001762 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001763 return Failure;
1764 }
1765 break;
Mike Stump11289f42009-09-09 15:08:12 +00001766
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00001767 case DECL_UPDATES_BLOCK_ID:
1768 if (Stream.SkipBlock()) {
1769 Error("malformed block record in AST file");
1770 return Failure;
1771 }
1772 break;
1773
Sebastian Redl539c5062010-08-18 23:57:32 +00001774 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001775 F.MacroCursor = Stream;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001776 if (PP)
1777 PP->setExternalSource(this);
1778
Douglas Gregor796d76a2010-10-20 22:00:55 +00001779 if (Stream.SkipBlock() ||
1780 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001781 Error("malformed block record in AST file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001782 return Failure;
1783 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001784 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001785 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001786
Sebastian Redl539c5062010-08-18 23:57:32 +00001787 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001788 switch (ReadSourceManagerBlock(F)) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001789 case Success:
1790 break;
1791
1792 case Failure:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001793 Error("malformed source manager block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001794 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001795
1796 case IgnorePCH:
1797 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001798 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001799 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001800 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00001801 First = false;
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001802 continue;
1803 }
1804
1805 if (Code == llvm::bitc::DEFINE_ABBREV) {
1806 Stream.ReadAbbrevRecord();
1807 continue;
1808 }
1809
1810 // Read and process a record.
1811 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001812 const char *BlobStart = 0;
1813 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001814 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001815 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001816 default: // Default behavior: ignore.
1817 break;
1818
Sebastian Redl539c5062010-08-18 23:57:32 +00001819 case METADATA: {
1820 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1821 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001822 : diag::warn_pch_version_too_new);
1823 return IgnorePCH;
1824 }
1825
1826 RelocatablePCH = Record[4];
1827 if (Listener) {
1828 std::string TargetTriple(BlobStart, BlobLen);
1829 if (Listener->ReadTargetTriple(TargetTriple))
1830 return IgnorePCH;
1831 }
1832 break;
1833 }
1834
Sebastian Redl539c5062010-08-18 23:57:32 +00001835 case CHAINED_METADATA: {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001836 if (!First) {
1837 Error("CHAINED_METADATA is not first record in block");
1838 return Failure;
1839 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001840 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1841 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001842 : diag::warn_pch_version_too_new);
1843 return IgnorePCH;
1844 }
1845
Sebastian Redl009e7f22010-10-05 16:15:19 +00001846 // Load the chained file, which is always a PCH file.
1847 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001848 case Failure: return Failure;
1849 // If we have to ignore the dependency, we'll have to ignore this too.
1850 case IgnorePCH: return IgnorePCH;
1851 case Success: break;
1852 }
1853 break;
1854 }
1855
Sebastian Redl539c5062010-08-18 23:57:32 +00001856 case TYPE_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001857 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001858 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001859 return Failure;
1860 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001861 F.TypeOffsets = (const uint32_t *)BlobStart;
1862 F.LocalNumTypes = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001863 break;
1864
Sebastian Redl539c5062010-08-18 23:57:32 +00001865 case DECL_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001866 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001867 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001868 return Failure;
1869 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001870 F.DeclOffsets = (const uint32_t *)BlobStart;
1871 F.LocalNumDecls = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001872 break;
Douglas Gregor55abb232009-04-10 20:39:37 +00001873
Sebastian Redl539c5062010-08-18 23:57:32 +00001874 case TU_UPDATE_LEXICAL: {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001875 DeclContextInfo Info = {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001876 /* No visible information */ 0,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001877 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
1878 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001879 };
Douglas Gregoraa433012010-10-01 01:18:02 +00001880 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
1881 .push_back(Info);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001882 break;
1883 }
1884
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001885 case UPDATE_VISIBLE: {
1886 serialization::DeclID ID = Record[0];
1887 void *Table = ASTDeclContextNameLookupTable::Create(
1888 (const unsigned char *)BlobStart + Record[1],
1889 (const unsigned char *)BlobStart,
1890 ASTDeclContextNameLookupTrait(*this));
Douglas Gregoraa433012010-10-01 01:18:02 +00001891 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001892 DeclContextInfo Info = {
1893 Table, /* No lexical inforamtion */ 0, 0
1894 };
1895 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1896 } else
1897 PendingVisibleUpdates[ID].push_back(Table);
1898 break;
1899 }
1900
Sebastian Redl539c5062010-08-18 23:57:32 +00001901 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001902 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1903 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001904 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001905 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1906 Latest > FirstLatestDeclIDs[First]) &&
1907 "The new latest is supposed to come after the previous latest");
1908 FirstLatestDeclIDs[First] = Latest;
1909 }
1910 break;
1911 }
1912
Sebastian Redl539c5062010-08-18 23:57:32 +00001913 case LANGUAGE_OPTIONS:
Douglas Gregorce3a8292010-07-27 00:27:13 +00001914 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor55abb232009-04-10 20:39:37 +00001915 return IgnorePCH;
1916 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00001917
Sebastian Redl539c5062010-08-18 23:57:32 +00001918 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001919 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00001920 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001921 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001922 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00001923 (const unsigned char *)F.IdentifierTableData + Record[0],
1924 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001925 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001926 if (PP)
1927 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00001928 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001929 break;
1930
Sebastian Redl539c5062010-08-18 23:57:32 +00001931 case IDENTIFIER_OFFSET:
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001932 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001933 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001934 return Failure;
1935 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001936 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1937 F.LocalNumIdentifiers = Record[0];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001938 break;
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001939
Sebastian Redl539c5062010-08-18 23:57:32 +00001940 case EXTERNAL_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001941 // Optimization for the first block.
1942 if (ExternalDefinitions.empty())
1943 ExternalDefinitions.swap(Record);
1944 else
1945 ExternalDefinitions.insert(ExternalDefinitions.end(),
1946 Record.begin(), Record.end());
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001947 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00001948
Sebastian Redl539c5062010-08-18 23:57:32 +00001949 case SPECIAL_TYPES:
Sebastian Redlb293a452010-07-20 21:20:32 +00001950 // Optimization for the first block
1951 if (SpecialTypes.empty())
1952 SpecialTypes.swap(Record);
1953 else
1954 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregor652d82a2009-04-18 05:55:16 +00001955 break;
1956
Sebastian Redl539c5062010-08-18 23:57:32 +00001957 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001958 TotalNumStatements += Record[0];
1959 TotalNumMacros += Record[1];
1960 TotalLexicalDeclContexts += Record[2];
1961 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00001962 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00001963
Sebastian Redl539c5062010-08-18 23:57:32 +00001964 case TENTATIVE_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001965 // Optimization for the first block.
1966 if (TentativeDefinitions.empty())
1967 TentativeDefinitions.swap(Record);
1968 else
1969 TentativeDefinitions.insert(TentativeDefinitions.end(),
1970 Record.begin(), Record.end());
Douglas Gregord4df8652009-04-22 22:02:47 +00001971 break;
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001972
Sebastian Redl539c5062010-08-18 23:57:32 +00001973 case UNUSED_FILESCOPED_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001974 // Optimization for the first block.
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00001975 if (UnusedFileScopedDecls.empty())
1976 UnusedFileScopedDecls.swap(Record);
Sebastian Redlb293a452010-07-20 21:20:32 +00001977 else
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00001978 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1979 Record.begin(), Record.end());
Tanya Lattner90073802010-02-12 00:07:30 +00001980 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001981
Sebastian Redl539c5062010-08-18 23:57:32 +00001982 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001983 // Later blocks overwrite earlier ones.
1984 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00001985 break;
1986
Sebastian Redl539c5062010-08-18 23:57:32 +00001987 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001988 // Optimization for the first block.
1989 if (LocallyScopedExternalDecls.empty())
1990 LocallyScopedExternalDecls.swap(Record);
1991 else
1992 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1993 Record.begin(), Record.end());
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001994 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001995
Sebastian Redl539c5062010-08-18 23:57:32 +00001996 case SELECTOR_OFFSETS:
Sebastian Redla19a67f2010-08-03 21:58:15 +00001997 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00001998 F.LocalNumSelectors = Record[0];
Douglas Gregor95c13f52009-04-25 17:48:32 +00001999 break;
2000
Sebastian Redl539c5062010-08-18 23:57:32 +00002001 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00002002 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002003 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00002004 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002005 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00002006 F.SelectorLookupTableData + Record[0],
2007 F.SelectorLookupTableData,
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002008 ASTSelectorLookupTrait(*this));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002009 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002010 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00002011
Sebastian Redl96371b42010-09-22 00:42:30 +00002012 case REFERENCED_SELECTOR_POOL:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002013 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002014 break;
2015
Sebastian Redl539c5062010-08-18 23:57:32 +00002016 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002017 if (!Record.empty() && Listener)
2018 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002019 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002020
Sebastian Redl539c5062010-08-18 23:57:32 +00002021 case SOURCE_LOCATION_OFFSETS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002022 F.SLocOffsets = (const uint32_t *)BlobStart;
2023 F.LocalNumSLocEntries = Record[0];
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002024 F.LocalSLocSize = Record[1];
Douglas Gregor258ae542009-04-27 06:38:32 +00002025 break;
2026
Sebastian Redl539c5062010-08-18 23:57:32 +00002027 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl96371b42010-09-22 00:42:30 +00002028 if (PreloadSLocEntries.empty())
2029 PreloadSLocEntries.swap(Record);
2030 else
2031 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2032 Record.begin(), Record.end());
Douglas Gregor258ae542009-04-27 06:38:32 +00002033 break;
Douglas Gregorc5046832009-04-27 18:38:38 +00002034
Sebastian Redl539c5062010-08-18 23:57:32 +00002035 case STAT_CACHE: {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002036 ASTStatCache *MyStatCache =
2037 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
Douglas Gregord2eb58a2009-10-16 18:18:30 +00002038 (const unsigned char *)BlobStart,
2039 NumStatHits, NumStatMisses);
2040 FileMgr.addStatCache(MyStatCache);
Sebastian Redl34522812010-07-16 17:50:48 +00002041 F.StatCache = MyStatCache;
Douglas Gregorc5046832009-04-27 18:38:38 +00002042 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00002043 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002044
Sebastian Redl539c5062010-08-18 23:57:32 +00002045 case EXT_VECTOR_DECLS:
Sebastian Redl04f5c312010-07-28 21:38:49 +00002046 // Optimization for the first block.
2047 if (ExtVectorDecls.empty())
2048 ExtVectorDecls.swap(Record);
2049 else
2050 ExtVectorDecls.insert(ExtVectorDecls.end(),
2051 Record.begin(), Record.end());
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002052 break;
2053
Sebastian Redl539c5062010-08-18 23:57:32 +00002054 case VTABLE_USES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002055 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002056 VTableUses.swap(Record);
2057 break;
2058
Sebastian Redl539c5062010-08-18 23:57:32 +00002059 case DYNAMIC_CLASSES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002060 // Optimization for the first block.
2061 if (DynamicClasses.empty())
2062 DynamicClasses.swap(Record);
2063 else
2064 DynamicClasses.insert(DynamicClasses.end(),
2065 Record.begin(), Record.end());
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002066 break;
2067
Sebastian Redl539c5062010-08-18 23:57:32 +00002068 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002069 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002070 break;
2071
Sebastian Redl539c5062010-08-18 23:57:32 +00002072 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002073 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002074 SemaDeclRefs.swap(Record);
2075 break;
2076
Sebastian Redl539c5062010-08-18 23:57:32 +00002077 case ORIGINAL_FILE_NAME:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002078 // The primary AST will be the last to get here, so it will be the one
Sebastian Redlb293a452010-07-20 21:20:32 +00002079 // that's used.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00002080 ActualOriginalFileName.assign(BlobStart, BlobLen);
2081 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002082 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00002083 break;
Mike Stump11289f42009-09-09 15:08:12 +00002084
Sebastian Redl539c5062010-08-18 23:57:32 +00002085 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00002086 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002087 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2088 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2089 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregord54f3a12009-10-05 21:07:28 +00002090 return IgnorePCH;
2091 }
2092 break;
2093 }
Sebastian Redlfa061442010-07-21 20:07:32 +00002094
Sebastian Redl539c5062010-08-18 23:57:32 +00002095 case MACRO_DEFINITION_OFFSETS:
Sebastian Redlfa061442010-07-21 20:07:32 +00002096 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2097 F.NumPreallocatedPreprocessingEntities = Record[0];
2098 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregoraae92242010-03-19 21:51:54 +00002099 break;
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002100
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002101 case DECL_UPDATE_OFFSETS: {
2102 if (Record.size() % 2 != 0) {
2103 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2104 return Failure;
2105 }
2106 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2107 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2108 .push_back(std::make_pair(&F, Record[I+1]));
2109 break;
2110 }
2111
Sebastian Redl539c5062010-08-18 23:57:32 +00002112 case DECL_REPLACEMENTS: {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002113 if (Record.size() % 2 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002114 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002115 return Failure;
2116 }
2117 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl539c5062010-08-18 23:57:32 +00002118 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002119 std::make_pair(&F, Record[I+1]);
2120 break;
2121 }
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002122
2123 case CXX_BASE_SPECIFIER_OFFSETS: {
2124 if (F.LocalNumCXXBaseSpecifiers != 0) {
2125 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2126 return Failure;
2127 }
2128
2129 F.LocalNumCXXBaseSpecifiers = Record[0];
2130 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2131 break;
2132 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002133
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002134 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002135 if (Record.size() % 2 != 0) {
2136 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2137 return Failure;
2138 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002139 if (PragmaDiagMappings.empty())
2140 PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002141 else
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002142 PragmaDiagMappings.insert(PragmaDiagMappings.end(),
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002143 Record.begin(), Record.end());
2144 break;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002145 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00002146 First = false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002147 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002148 Error("premature end of bitstream in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002149 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002150}
2151
Sebastian Redl009e7f22010-10-05 16:15:19 +00002152ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2153 ASTFileType Type) {
2154 switch(ReadASTCore(FileName, Type)) {
Sebastian Redl2abc0382010-07-16 20:41:52 +00002155 case Failure: return Failure;
2156 case IgnorePCH: return IgnorePCH;
2157 case Success: break;
2158 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002159
2160 // Here comes stuff that we only do once the entire chain is loaded.
2161
Sebastian Redl96371b42010-09-22 00:42:30 +00002162 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redlfa061442010-07-21 20:07:32 +00002163 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redlada023c2010-08-04 20:40:17 +00002164 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2165 TotalNumSelectors = 0;
Sebastian Redl9e687992010-07-19 22:06:55 +00002166 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl96371b42010-09-22 00:42:30 +00002167 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002168 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002169 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl9e687992010-07-19 22:06:55 +00002170 TotalNumTypes += Chain[I]->LocalNumTypes;
2171 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redlfa061442010-07-21 20:07:32 +00002172 TotalNumPreallocatedPreprocessingEntities +=
2173 Chain[I]->NumPreallocatedPreprocessingEntities;
2174 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redlada023c2010-08-04 20:40:17 +00002175 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl9e687992010-07-19 22:06:55 +00002176 }
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002177 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002178 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl9e687992010-07-19 22:06:55 +00002179 TypesLoaded.resize(TotalNumTypes);
2180 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redlfa061442010-07-21 20:07:32 +00002181 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2182 if (PP) {
2183 if (TotalNumIdentifiers > 0)
2184 PP->getHeaderSearchInfo().SetExternalLookup(this);
2185 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2186 if (!PP->getPreprocessingRecord())
2187 PP->createPreprocessingRecord();
2188 PP->getPreprocessingRecord()->SetExternalSource(*this,
2189 TotalNumPreallocatedPreprocessingEntities);
2190 }
2191 }
Sebastian Redlada023c2010-08-04 20:40:17 +00002192 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl96371b42010-09-22 00:42:30 +00002193 // Preload SLocEntries.
2194 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2195 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2196 if (Result != Success)
2197 return Result;
2198 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002199
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002200 // Check the predefines buffers.
Douglas Gregorce3a8292010-07-27 00:27:13 +00002201 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002202 return IgnorePCH;
2203
2204 if (PP) {
2205 // Initialization of keywords and pragmas occurs before the
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002206 // AST file is read, so there may be some identifiers that were
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002207 // loaded into the IdentifierTable before we intercepted the
2208 // creation of identifiers. Iterate through the list of known
2209 // identifiers and determine whether we have to establish
2210 // preprocessor definitions or top-level identifier declaration
2211 // chains for those identifiers.
2212 //
2213 // We copy the IdentifierInfo pointers to a small vector first,
2214 // since de-serializing declarations or macro definitions can add
2215 // new entries into the identifier table, invalidating the
2216 // iterators.
2217 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2218 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2219 IdEnd = PP->getIdentifierTable().end();
2220 Id != IdEnd; ++Id)
2221 Identifiers.push_back(Id->second);
Sebastian Redlfa061442010-07-21 20:07:32 +00002222 // We need to search the tables in all files.
Sebastian Redlfa061442010-07-21 20:07:32 +00002223 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002224 ASTIdentifierLookupTable *IdTable
2225 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2226 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl5c415f32010-07-22 17:01:13 +00002227 // ones.
2228 if (!IdTable)
2229 continue;
Sebastian Redlfa061442010-07-21 20:07:32 +00002230 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2231 IdentifierInfo *II = Identifiers[I];
2232 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00002233 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redlfa061442010-07-21 20:07:32 +00002234 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002235 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redlb293a452010-07-20 21:20:32 +00002236 if (Pos == IdTable->end())
2237 continue;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002238
Sebastian Redlb293a452010-07-20 21:20:32 +00002239 // Dereferencing the iterator has the effect of populating the
2240 // IdentifierInfo node with the various declarations it needs.
2241 (void)*Pos;
2242 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002243 }
2244 }
2245
2246 if (Context)
2247 InitializeContext(*Context);
2248
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002249 if (DeserializationListener)
2250 DeserializationListener->ReaderInitialized(this);
2251
Douglas Gregor936a5b42010-11-30 05:23:00 +00002252 // If this AST file is a precompiled preamble, then set the main file ID of
2253 // the source manager to the file source file from which the preamble was
2254 // built. This is the only valid way to use a precompiled preamble.
2255 if (Type == Preamble) {
2256 SourceLocation Loc
2257 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2258 if (Loc.isValid()) {
2259 std::pair<FileID, unsigned> Decomposed = SourceMgr.getDecomposedLoc(Loc);
2260 SourceMgr.SetPreambleFileID(Decomposed.first);
2261 }
2262 }
2263
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002264 return Success;
2265}
2266
Sebastian Redl009e7f22010-10-05 16:15:19 +00002267ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2268 ASTFileType Type) {
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002269 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl009e7f22010-10-05 16:15:19 +00002270 Chain.push_back(new PerFileData(Type));
Sebastian Redl34522812010-07-16 17:50:48 +00002271 PerFileData &F = *Chain.back();
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002272 if (Prev)
2273 Prev->NextInSource = &F;
2274 else
2275 FirstInSource = &F;
2276 F.Loaders.push_back(Prev);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002277
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002278 // Set the AST file name.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002279 F.FileName = FileName;
2280
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002281 // Open the AST file.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002282 //
2283 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2284 std::string ErrStr;
Michael J. Spencerf25faaa2010-12-09 17:36:38 +00002285 llvm::error_code ec;
2286 if (FileName == "-") {
Michael J. Spencerd9da7a12010-12-16 03:28:14 +00002287 ec = llvm::MemoryBuffer::getSTDIN(F.Buffer);
Michael J. Spencerf25faaa2010-12-09 17:36:38 +00002288 if (ec)
2289 ErrStr = ec.message();
2290 } else
Chris Lattner5159f612010-11-23 08:35:12 +00002291 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002292 if (!F.Buffer) {
2293 Error(ErrStr.c_str());
2294 return IgnorePCH;
2295 }
2296
2297 // Initialize the stream
2298 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2299 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl34522812010-07-16 17:50:48 +00002300 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002301 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002302 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002303
2304 // Sniff for the signature.
2305 if (Stream.Read(8) != 'C' ||
2306 Stream.Read(8) != 'P' ||
2307 Stream.Read(8) != 'C' ||
2308 Stream.Read(8) != 'H') {
2309 Diag(diag::err_not_a_pch_file) << FileName;
2310 return Failure;
2311 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002312
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002313 while (!Stream.AtEndOfStream()) {
2314 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002315
Douglas Gregor92863e42009-04-10 23:10:45 +00002316 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002317 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002318 return Failure;
2319 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002320
2321 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002322
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002323 // We only know the AST subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002324 switch (BlockID) {
2325 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002326 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002327 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002328 return Failure;
2329 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002330 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00002331 case AST_BLOCK_ID:
Sebastian Redl3e31c722010-08-18 23:56:56 +00002332 switch (ReadASTBlock(F)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002333 case Success:
2334 break;
2335
2336 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00002337 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00002338
2339 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00002340 // FIXME: We could consider reading through to the end of this
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002341 // AST block, skipping subblocks, to see if there are other
2342 // AST blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00002343
2344 // Clear out any preallocated source location entries, so that
2345 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002346 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00002347
2348 // Remove the stat cache.
Sebastian Redl34522812010-07-16 17:50:48 +00002349 if (F.StatCache)
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002350 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00002351
Douglas Gregor92863e42009-04-10 23:10:45 +00002352 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002353 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002354 break;
2355 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002356 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002357 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002358 return Failure;
2359 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002360 break;
2361 }
Mike Stump11289f42009-09-09 15:08:12 +00002362 }
2363
Sebastian Redl2abc0382010-07-16 20:41:52 +00002364 return Success;
2365}
2366
Sebastian Redl2c499f62010-08-18 23:56:43 +00002367void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002368 PP = &pp;
Sebastian Redlfa061442010-07-21 20:07:32 +00002369
2370 unsigned TotalNum = 0;
2371 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2372 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2373 if (TotalNum) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002374 if (!PP->getPreprocessingRecord())
2375 PP->createPreprocessingRecord();
Sebastian Redlfa061442010-07-21 20:07:32 +00002376 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregoraae92242010-03-19 21:51:54 +00002377 }
2378}
2379
Sebastian Redl2c499f62010-08-18 23:56:43 +00002380void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002381 Context = &Ctx;
2382 assert(Context && "Passed null context!");
2383
2384 assert(PP && "Forgot to set Preprocessor ?");
2385 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2386 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00002387 PP->setExternalSource(this);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002388
Douglas Gregoraa433012010-10-01 01:18:02 +00002389 // If we have an update block for the TU waiting, we have to add it before
2390 // deserializing the decl.
2391 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2392 if (DCU != DeclContextOffsets.end()) {
2393 // Insertion could invalidate map, so grab vector.
2394 DeclContextInfos T;
2395 T.swap(DCU->second);
2396 DeclContextOffsets.erase(DCU);
2397 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2398 }
2399
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002400 // Load the translation unit declaration
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00002401 GetTranslationUnitDecl();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002402
2403 // Load the special types.
2404 Context->setBuiltinVaListType(
Sebastian Redl539c5062010-08-18 23:57:32 +00002405 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2406 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002407 Context->setObjCIdType(GetType(Id));
Sebastian Redl539c5062010-08-18 23:57:32 +00002408 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002409 Context->setObjCSelType(GetType(Sel));
Sebastian Redl539c5062010-08-18 23:57:32 +00002410 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002411 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl539c5062010-08-18 23:57:32 +00002412 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002413 Context->setObjCClassType(GetType(Class));
Steve Naroff7cae42b2009-07-10 23:34:53 +00002414
Sebastian Redl539c5062010-08-18 23:57:32 +00002415 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002416 Context->setCFConstantStringType(GetType(String));
Mike Stump11289f42009-09-09 15:08:12 +00002417 if (unsigned FastEnum
Sebastian Redl539c5062010-08-18 23:57:32 +00002418 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002419 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl539c5062010-08-18 23:57:32 +00002420 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregor27821ce2009-07-07 16:35:42 +00002421 QualType FileType = GetType(File);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002422 if (FileType.isNull()) {
2423 Error("FILE type is NULL");
2424 return;
2425 }
John McCall9dd450b2009-09-21 23:43:11 +00002426 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregor27821ce2009-07-07 16:35:42 +00002427 Context->setFILEDecl(Typedef->getDecl());
2428 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002429 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002430 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002431 Error("Invalid FILE type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002432 return;
2433 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002434 Context->setFILEDecl(Tag->getDecl());
2435 }
2436 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002437 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002438 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002439 if (Jmp_bufType.isNull()) {
2440 Error("jmp_bug type is NULL");
2441 return;
2442 }
John McCall9dd450b2009-09-21 23:43:11 +00002443 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002444 Context->setjmp_bufDecl(Typedef->getDecl());
2445 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002446 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002447 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002448 Error("Invalid jmp_buf type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002449 return;
2450 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002451 Context->setjmp_bufDecl(Tag->getDecl());
2452 }
2453 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002454 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002455 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002456 if (Sigjmp_bufType.isNull()) {
2457 Error("sigjmp_buf type is NULL");
2458 return;
2459 }
John McCall9dd450b2009-09-21 23:43:11 +00002460 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002461 Context->setsigjmp_bufDecl(Typedef->getDecl());
2462 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002463 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002464 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stumpa4de80b2009-07-28 02:25:19 +00002465 Context->setsigjmp_bufDecl(Tag->getDecl());
2466 }
2467 }
Mike Stump11289f42009-09-09 15:08:12 +00002468 if (unsigned ObjCIdRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002469 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002470 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump11289f42009-09-09 15:08:12 +00002471 if (unsigned ObjCClassRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002472 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002473 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002474 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpd0153282009-10-20 02:12:22 +00002475 Context->setBlockDescriptorType(GetType(String));
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002476 if (unsigned String
Sebastian Redl539c5062010-08-18 23:57:32 +00002477 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002478 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002479 if (unsigned ObjCSelRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002480 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002481 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002482 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002483 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002484
Sebastian Redl539c5062010-08-18 23:57:32 +00002485 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002486 Context->setInt128Installed();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002487
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002488 ReadPragmaDiagnosticMappings(Context->getDiagnostics());
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002489}
2490
Douglas Gregor45fe0362009-05-12 01:31:05 +00002491/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002492/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00002493/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002494std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00002495 FileManager &FileMgr,
Daniel Dunbar3b951482009-12-03 09:13:06 +00002496 Diagnostic &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002497 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002498 std::string ErrStr;
2499 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner5159f612010-11-23 08:35:12 +00002500 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00002501 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00002502 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002503 return std::string();
2504 }
2505
2506 // Initialize the stream
2507 llvm::BitstreamReader StreamFile;
2508 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00002509 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00002510 (const unsigned char *)Buffer->getBufferEnd());
2511 Stream.init(StreamFile);
2512
2513 // Sniff for the signature.
2514 if (Stream.Read(8) != 'C' ||
2515 Stream.Read(8) != 'P' ||
2516 Stream.Read(8) != 'C' ||
2517 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002518 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002519 return std::string();
2520 }
2521
2522 RecordData Record;
2523 while (!Stream.AtEndOfStream()) {
2524 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002525
Douglas Gregor45fe0362009-05-12 01:31:05 +00002526 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2527 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00002528
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002529 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002530 switch (BlockID) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002531 case AST_BLOCK_ID:
2532 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002533 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002534 return std::string();
2535 }
2536 break;
Mike Stump11289f42009-09-09 15:08:12 +00002537
Douglas Gregor45fe0362009-05-12 01:31:05 +00002538 default:
2539 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002540 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002541 return std::string();
2542 }
2543 break;
2544 }
2545 continue;
2546 }
2547
2548 if (Code == llvm::bitc::END_BLOCK) {
2549 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002550 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002551 return std::string();
2552 }
2553 continue;
2554 }
2555
2556 if (Code == llvm::bitc::DEFINE_ABBREV) {
2557 Stream.ReadAbbrevRecord();
2558 continue;
2559 }
2560
2561 Record.clear();
2562 const char *BlobStart = 0;
2563 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002564 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl539c5062010-08-18 23:57:32 +00002565 == ORIGINAL_FILE_NAME)
Douglas Gregor45fe0362009-05-12 01:31:05 +00002566 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00002567 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00002568
2569 return std::string();
2570}
2571
Douglas Gregor55abb232009-04-10 20:39:37 +00002572/// \brief Parse the record that corresponds to a LangOptions data
2573/// structure.
2574///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002575/// This routine parses the language options from the AST file and then gives
2576/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00002577///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002578/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002579bool ASTReader::ParseLanguageOptions(
Douglas Gregor55abb232009-04-10 20:39:37 +00002580 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002581 if (Listener) {
2582 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00002583
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002584 #define PARSE_LANGOPT(Option) \
2585 LangOpts.Option = Record[Idx]; \
2586 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00002587
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002588 unsigned Idx = 0;
2589 PARSE_LANGOPT(Trigraphs);
2590 PARSE_LANGOPT(BCPLComment);
2591 PARSE_LANGOPT(DollarIdents);
2592 PARSE_LANGOPT(AsmPreprocessor);
2593 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00002594 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002595 PARSE_LANGOPT(ImplicitInt);
2596 PARSE_LANGOPT(Digraphs);
2597 PARSE_LANGOPT(HexFloats);
2598 PARSE_LANGOPT(C99);
2599 PARSE_LANGOPT(Microsoft);
2600 PARSE_LANGOPT(CPlusPlus);
2601 PARSE_LANGOPT(CPlusPlus0x);
2602 PARSE_LANGOPT(CXXOperatorNames);
2603 PARSE_LANGOPT(ObjC1);
2604 PARSE_LANGOPT(ObjC2);
2605 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00002606 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00002607 PARSE_LANGOPT(AppleKext);
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00002608 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00002609 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002610 PARSE_LANGOPT(PascalStrings);
2611 PARSE_LANGOPT(WritableStrings);
2612 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00002613 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002614 PARSE_LANGOPT(Exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +00002615 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002616 PARSE_LANGOPT(NeXTRuntime);
2617 PARSE_LANGOPT(Freestanding);
2618 PARSE_LANGOPT(NoBuiltin);
2619 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00002620 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002621 PARSE_LANGOPT(Blocks);
2622 PARSE_LANGOPT(EmitAllDecls);
2623 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00002624 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2625 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002626 PARSE_LANGOPT(HeinousExtensions);
2627 PARSE_LANGOPT(Optimize);
2628 PARSE_LANGOPT(OptimizeSize);
2629 PARSE_LANGOPT(Static);
2630 PARSE_LANGOPT(PICLevel);
2631 PARSE_LANGOPT(GNUInline);
2632 PARSE_LANGOPT(NoInline);
2633 PARSE_LANGOPT(AccessControl);
2634 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00002635 PARSE_LANGOPT(ShortWChar);
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00002636 PARSE_LANGOPT(ShortEnums);
Chris Lattner51924e512010-06-26 21:25:03 +00002637 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall457a04e2010-10-22 21:05:15 +00002638 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00002639 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00002640 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002641 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00002642 PARSE_LANGOPT(OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00002643 PARSE_LANGOPT(CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00002644 PARSE_LANGOPT(CatchUndefined);
2645 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002646 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00002647
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002648 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00002649 }
Douglas Gregor55abb232009-04-10 20:39:37 +00002650
2651 return false;
2652}
2653
Sebastian Redl2c499f62010-08-18 23:56:43 +00002654void ASTReader::ReadPreprocessedEntities() {
Douglas Gregoraae92242010-03-19 21:51:54 +00002655 ReadDefinedMacros();
2656}
2657
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002658PreprocessedEntity *ASTReader::ReadPreprocessedEntity(uint64_t Offset) {
2659 PerFileData *F = 0;
2660 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2661 if (Offset < Chain[I]->SizeInBits) {
2662 F = Chain[I];
2663 break;
2664 }
2665
2666 Offset -= Chain[I]->SizeInBits;
2667 }
2668
2669 if (!F) {
2670 Error("Malformed preprocessed entity offset");
2671 return 0;
2672 }
2673
2674 return ReadMacroRecord(*F, Offset);
2675}
2676
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002677void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002678 unsigned Idx = 0;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002679 while (Idx < PragmaDiagMappings.size()) {
2680 SourceLocation
2681 Loc = SourceLocation::getFromRawEncoding(PragmaDiagMappings[Idx++]);
2682 while (1) {
2683 assert(Idx < PragmaDiagMappings.size() &&
2684 "Invalid data, didn't find '-1' marking end of diag/map pairs");
2685 if (Idx >= PragmaDiagMappings.size())
2686 break; // Something is messed up but at least avoid infinite loop in
2687 // release build.
2688 unsigned DiagID = PragmaDiagMappings[Idx++];
2689 if (DiagID == (unsigned)-1)
2690 break; // no more diag/map pairs for this location.
2691 diag::Mapping Map = (diag::Mapping)PragmaDiagMappings[Idx++];
2692 Diag.setDiagnosticMapping(DiagID, Map, Loc);
2693 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002694 }
2695}
2696
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002697/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002698ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002699 PerFileData *F = 0;
2700 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2701 F = Chain[N - I - 1];
2702 if (Index < F->LocalNumTypes)
2703 break;
2704 Index -= F->LocalNumTypes;
2705 }
2706 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redl2c373b92010-10-05 15:59:54 +00002707 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002708}
2709
2710/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002711///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002712/// The index is the type ID, shifted and minus the number of predefs. This
2713/// routine actually reads the record corresponding to the type at the given
2714/// location. It is a helper routine for GetType, which deals with reading type
2715/// IDs.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002716QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002717 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00002718 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00002719
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002720 // Keep track of where we are in the stream, then jump back there
2721 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002722 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002723
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002724 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00002725
Douglas Gregor1342e842009-07-06 18:54:52 +00002726 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00002727 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00002728
Sebastian Redl2c373b92010-10-05 15:59:54 +00002729 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002730 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002731 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00002732 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2733 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002734 if (Record.size() != 2) {
2735 Error("Incorrect encoding of extended qualifier type");
2736 return QualType();
2737 }
Douglas Gregor455b8f42009-04-15 22:00:08 +00002738 QualType Base = GetType(Record[0]);
John McCall8ccfcb52009-09-24 19:53:00 +00002739 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2740 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00002741 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002742
Sebastian Redl539c5062010-08-18 23:57:32 +00002743 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002744 if (Record.size() != 1) {
2745 Error("Incorrect encoding of complex type");
2746 return QualType();
2747 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002748 QualType ElemType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002749 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002750 }
2751
Sebastian Redl539c5062010-08-18 23:57:32 +00002752 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002753 if (Record.size() != 1) {
2754 Error("Incorrect encoding of pointer type");
2755 return QualType();
2756 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002757 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002758 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002759 }
2760
Sebastian Redl539c5062010-08-18 23:57:32 +00002761 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002762 if (Record.size() != 1) {
2763 Error("Incorrect encoding of block pointer type");
2764 return QualType();
2765 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002766 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002767 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002768 }
2769
Sebastian Redl539c5062010-08-18 23:57:32 +00002770 case TYPE_LVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002771 if (Record.size() != 1) {
2772 Error("Incorrect encoding of lvalue reference type");
2773 return QualType();
2774 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002775 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002776 return Context->getLValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002777 }
2778
Sebastian Redl539c5062010-08-18 23:57:32 +00002779 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002780 if (Record.size() != 1) {
2781 Error("Incorrect encoding of rvalue reference type");
2782 return QualType();
2783 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002784 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002785 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002786 }
2787
Sebastian Redl539c5062010-08-18 23:57:32 +00002788 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00002789 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002790 Error("Incorrect encoding of member pointer type");
2791 return QualType();
2792 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002793 QualType PointeeType = GetType(Record[0]);
2794 QualType ClassType = GetType(Record[1]);
Douglas Gregor0cdc8322010-12-10 17:03:06 +00002795 if (PointeeType.isNull() || ClassType.isNull())
2796 return QualType();
2797
Chris Lattner8575daa2009-04-27 21:45:14 +00002798 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002799 }
2800
Sebastian Redl539c5062010-08-18 23:57:32 +00002801 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002802 QualType ElementType = GetType(Record[0]);
2803 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2804 unsigned IndexTypeQuals = Record[2];
2805 unsigned Idx = 3;
2806 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00002807 return Context->getConstantArrayType(ElementType, Size,
2808 ASM, IndexTypeQuals);
2809 }
2810
Sebastian Redl539c5062010-08-18 23:57:32 +00002811 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002812 QualType ElementType = GetType(Record[0]);
2813 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2814 unsigned IndexTypeQuals = Record[2];
Chris Lattner8575daa2009-04-27 21:45:14 +00002815 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002816 }
2817
Sebastian Redl539c5062010-08-18 23:57:32 +00002818 case TYPE_VARIABLE_ARRAY: {
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002819 QualType ElementType = GetType(Record[0]);
2820 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2821 unsigned IndexTypeQuals = Record[2];
Sebastian Redl2c373b92010-10-05 15:59:54 +00002822 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
2823 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
2824 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor04318252009-07-06 15:59:29 +00002825 ASM, IndexTypeQuals,
2826 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002827 }
2828
Sebastian Redl539c5062010-08-18 23:57:32 +00002829 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002830 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002831 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002832 return QualType();
2833 }
2834
2835 QualType ElementType = GetType(Record[0]);
2836 unsigned NumElements = Record[1];
Bob Wilsonaeb56442010-11-10 21:56:12 +00002837 unsigned VecKind = Record[2];
Chris Lattner37141f42010-06-23 06:00:24 +00002838 return Context->getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00002839 (VectorType::VectorKind)VecKind);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002840 }
2841
Sebastian Redl539c5062010-08-18 23:57:32 +00002842 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002843 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002844 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002845 return QualType();
2846 }
2847
2848 QualType ElementType = GetType(Record[0]);
2849 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00002850 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002851 }
2852
Sebastian Redl539c5062010-08-18 23:57:32 +00002853 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002854 if (Record.size() != 4) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002855 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002856 return QualType();
2857 }
2858 QualType ResultType = GetType(Record[0]);
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002859 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00002860 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002861 }
2862
Sebastian Redl539c5062010-08-18 23:57:32 +00002863 case TYPE_FUNCTION_PROTO: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002864 QualType ResultType = GetType(Record[0]);
John McCalldb40c7f2010-12-14 08:05:40 +00002865
2866 FunctionProtoType::ExtProtoInfo EPI;
2867 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
2868 /*regparm*/ Record[2],
2869 static_cast<CallingConv>(Record[3]));
2870
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002871 unsigned Idx = 4;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002872 unsigned NumParams = Record[Idx++];
2873 llvm::SmallVector<QualType, 16> ParamTypes;
2874 for (unsigned I = 0; I != NumParams; ++I)
2875 ParamTypes.push_back(GetType(Record[Idx++]));
John McCalldb40c7f2010-12-14 08:05:40 +00002876
2877 EPI.Variadic = Record[Idx++];
2878 EPI.TypeQuals = Record[Idx++];
Douglas Gregordb9d6642011-01-26 05:01:58 +00002879 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
John McCalldb40c7f2010-12-14 08:05:40 +00002880 EPI.HasExceptionSpec = Record[Idx++];
2881 EPI.HasAnyExceptionSpec = Record[Idx++];
2882 EPI.NumExceptions = Record[Idx++];
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002883 llvm::SmallVector<QualType, 2> Exceptions;
John McCalldb40c7f2010-12-14 08:05:40 +00002884 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002885 Exceptions.push_back(GetType(Record[Idx++]));
John McCalldb40c7f2010-12-14 08:05:40 +00002886 EPI.Exceptions = Exceptions.data();
Jay Foad7d0479f2009-05-21 09:52:38 +00002887 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalldb40c7f2010-12-14 08:05:40 +00002888 EPI);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002889 }
2890
Sebastian Redl539c5062010-08-18 23:57:32 +00002891 case TYPE_UNRESOLVED_USING:
John McCallb96ec562009-12-04 22:46:56 +00002892 return Context->getTypeDeclType(
2893 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2894
Sebastian Redl539c5062010-08-18 23:57:32 +00002895 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002896 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002897 Error("incorrect encoding of typedef type");
2898 return QualType();
2899 }
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002900 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2901 QualType Canonical = GetType(Record[1]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00002902 if (!Canonical.isNull())
2903 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002904 return Context->getTypedefType(Decl, Canonical);
2905 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002906
Sebastian Redl539c5062010-08-18 23:57:32 +00002907 case TYPE_TYPEOF_EXPR:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002908 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002909
Sebastian Redl539c5062010-08-18 23:57:32 +00002910 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002911 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002912 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002913 return QualType();
2914 }
2915 QualType UnderlyingType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002916 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002917 }
Mike Stump11289f42009-09-09 15:08:12 +00002918
Sebastian Redl539c5062010-08-18 23:57:32 +00002919 case TYPE_DECLTYPE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002920 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson81df7b82009-06-24 19:06:50 +00002921
Sebastian Redl539c5062010-08-18 23:57:32 +00002922 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002923 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002924 Error("incorrect encoding of record type");
2925 return QualType();
2926 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002927 bool IsDependent = Record[0];
2928 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCall424cec92011-01-19 06:33:43 +00002929 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002930 return T;
2931 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002932
Sebastian Redl539c5062010-08-18 23:57:32 +00002933 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002934 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002935 Error("incorrect encoding of enum type");
2936 return QualType();
2937 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002938 bool IsDependent = Record[0];
2939 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCall424cec92011-01-19 06:33:43 +00002940 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002941 return T;
2942 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00002943
John McCall81904512011-01-06 01:58:22 +00002944 case TYPE_ATTRIBUTED: {
2945 if (Record.size() != 3) {
2946 Error("incorrect encoding of attributed type");
2947 return QualType();
2948 }
2949 QualType modifiedType = GetType(Record[0]);
2950 QualType equivalentType = GetType(Record[1]);
2951 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
2952 return Context->getAttributedType(kind, modifiedType, equivalentType);
2953 }
2954
Abramo Bagnara924a8f32010-12-10 16:29:40 +00002955 case TYPE_PAREN: {
2956 if (Record.size() != 1) {
2957 Error("incorrect encoding of paren type");
2958 return QualType();
2959 }
2960 QualType InnerType = GetType(Record[0]);
2961 return Context->getParenType(InnerType);
2962 }
2963
Douglas Gregord2fa7662010-12-20 02:24:11 +00002964 case TYPE_PACK_EXPANSION: {
2965 if (Record.size() != 1) {
2966 Error("incorrect encoding of pack expansion type");
2967 return QualType();
2968 }
2969 QualType Pattern = GetType(Record[0]);
2970 if (Pattern.isNull())
2971 return QualType();
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00002972 llvm::Optional<unsigned> NumExpansions;
2973 if (Record[1])
2974 NumExpansions = Record[1] - 1;
2975 return Context->getPackExpansionType(Pattern, NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00002976 }
2977
Sebastian Redl539c5062010-08-18 23:57:32 +00002978 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002979 unsigned Idx = 0;
2980 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2981 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2982 QualType NamedType = GetType(Record[Idx++]);
2983 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00002984 }
2985
Sebastian Redl539c5062010-08-18 23:57:32 +00002986 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00002987 unsigned Idx = 0;
2988 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00002989 return Context->getObjCInterfaceType(ItfD);
2990 }
2991
Sebastian Redl539c5062010-08-18 23:57:32 +00002992 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00002993 unsigned Idx = 0;
2994 QualType Base = GetType(Record[Idx++]);
Chris Lattner587cbe12009-04-22 06:45:28 +00002995 unsigned NumProtos = Record[Idx++];
2996 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2997 for (unsigned I = 0; I != NumProtos; ++I)
2998 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002999 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00003000 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003001
Sebastian Redl539c5062010-08-18 23:57:32 +00003002 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00003003 unsigned Idx = 0;
John McCall8b07ec22010-05-15 11:32:37 +00003004 QualType Pointee = GetType(Record[Idx++]);
3005 return Context->getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00003006 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00003007
Sebastian Redl539c5062010-08-18 23:57:32 +00003008 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00003009 unsigned Idx = 0;
3010 QualType Parm = GetType(Record[Idx++]);
3011 QualType Replacement = GetType(Record[Idx++]);
3012 return
3013 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3014 Replacement);
3015 }
John McCalle78aac42010-03-10 03:28:59 +00003016
Douglas Gregorada4b792011-01-14 02:55:32 +00003017 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3018 unsigned Idx = 0;
3019 QualType Parm = GetType(Record[Idx++]);
3020 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3021 return Context->getSubstTemplateTypeParmPackType(
3022 cast<TemplateTypeParmType>(Parm),
3023 ArgPack);
3024 }
3025
Sebastian Redl539c5062010-08-18 23:57:32 +00003026 case TYPE_INJECTED_CLASS_NAME: {
John McCalle78aac42010-03-10 03:28:59 +00003027 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
3028 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003029 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003030 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003031 return
3032 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00003033 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003034
Sebastian Redl539c5062010-08-18 23:57:32 +00003035 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003036 unsigned Idx = 0;
3037 unsigned Depth = Record[Idx++];
3038 unsigned Index = Record[Idx++];
3039 bool Pack = Record[Idx++];
3040 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
3041 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
3042 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003043
Sebastian Redl539c5062010-08-18 23:57:32 +00003044 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003045 unsigned Idx = 0;
3046 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3047 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3048 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00003049 QualType Canon = GetType(Record[Idx++]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003050 if (!Canon.isNull())
3051 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00003052 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003053 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003054
Sebastian Redl539c5062010-08-18 23:57:32 +00003055 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003056 unsigned Idx = 0;
3057 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3058 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3059 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3060 unsigned NumArgs = Record[Idx++];
3061 llvm::SmallVector<TemplateArgument, 8> Args;
3062 Args.reserve(NumArgs);
3063 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003064 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003065 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3066 Args.size(), Args.data());
3067 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003068
Sebastian Redl539c5062010-08-18 23:57:32 +00003069 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003070 unsigned Idx = 0;
3071
3072 // ArrayType
3073 QualType ElementType = GetType(Record[Idx++]);
3074 ArrayType::ArraySizeModifier ASM
3075 = (ArrayType::ArraySizeModifier)Record[Idx++];
3076 unsigned IndexTypeQuals = Record[Idx++];
3077
3078 // DependentSizedArrayType
Sebastian Redl2c373b92010-10-05 15:59:54 +00003079 Expr *NumElts = ReadExpr(*Loc.F);
3080 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003081
3082 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3083 IndexTypeQuals, Brackets);
3084 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003085
Sebastian Redl539c5062010-08-18 23:57:32 +00003086 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003087 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003088 bool IsDependent = Record[Idx++];
Douglas Gregor5590be02011-01-15 06:45:20 +00003089 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003090 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003091 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003092 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003093 QualType T;
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003094 if (Canon.isNull())
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003095 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3096 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003097 else
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003098 T = Context->getTemplateSpecializationType(Name, Args.data(),
3099 Args.size(), Canon);
John McCall424cec92011-01-19 06:33:43 +00003100 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003101 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003102 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003103 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003104 // Suppress a GCC warning
3105 return QualType();
3106}
3107
Sebastian Redl2c373b92010-10-05 15:59:54 +00003108class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00003109 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003110 ASTReader::PerFileData &F;
Sebastian Redlc67764e2010-07-22 22:43:28 +00003111 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redl2c499f62010-08-18 23:56:43 +00003112 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00003113 unsigned &Idx;
3114
Sebastian Redl2c373b92010-10-05 15:59:54 +00003115 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3116 unsigned &I) {
3117 return Reader.ReadSourceLocation(F, R, I);
3118 }
3119
John McCall8f115c62009-10-16 21:56:05 +00003120public:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003121 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl2c499f62010-08-18 23:56:43 +00003122 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003123 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3124 { }
John McCall8f115c62009-10-16 21:56:05 +00003125
John McCall17001972009-10-18 01:05:36 +00003126 // We want compile-time assurance that we've enumerated all of
3127 // these, so unfortunately we have to declare them first, then
3128 // define them out-of-line.
3129#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00003130#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00003131 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00003132#include "clang/AST/TypeLocNodes.def"
3133
John McCall17001972009-10-18 01:05:36 +00003134 void VisitFunctionTypeLoc(FunctionTypeLoc);
3135 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00003136};
3137
John McCall17001972009-10-18 01:05:36 +00003138void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00003139 // nothing to do
3140}
John McCall17001972009-10-18 01:05:36 +00003141void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003142 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorc9b7a592010-01-18 18:04:31 +00003143 if (TL.needsExtraLocalData()) {
3144 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3145 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3146 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3147 TL.setModeAttr(Record[Idx++]);
3148 }
John McCall8f115c62009-10-16 21:56:05 +00003149}
John McCall17001972009-10-18 01:05:36 +00003150void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003151 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003152}
John McCall17001972009-10-18 01:05:36 +00003153void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003154 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003155}
John McCall17001972009-10-18 01:05:36 +00003156void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003157 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003158}
John McCall17001972009-10-18 01:05:36 +00003159void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003160 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003161}
John McCall17001972009-10-18 01:05:36 +00003162void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003163 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003164}
John McCall17001972009-10-18 01:05:36 +00003165void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003166 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003167}
John McCall17001972009-10-18 01:05:36 +00003168void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003169 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3170 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003171 if (Record[Idx++])
Sebastian Redl2c373b92010-10-05 15:59:54 +00003172 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003173 else
John McCall17001972009-10-18 01:05:36 +00003174 TL.setSizeExpr(0);
3175}
3176void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3177 VisitArrayTypeLoc(TL);
3178}
3179void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3180 VisitArrayTypeLoc(TL);
3181}
3182void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3183 VisitArrayTypeLoc(TL);
3184}
3185void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3186 DependentSizedArrayTypeLoc TL) {
3187 VisitArrayTypeLoc(TL);
3188}
3189void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3190 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003191 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003192}
3193void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003194 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003195}
3196void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003197 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003198}
3199void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003200 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3201 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor7fb25412010-10-01 18:44:50 +00003202 TL.setTrailingReturn(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00003203 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCalle6347002009-10-23 01:28:53 +00003204 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall17001972009-10-18 01:05:36 +00003205 }
3206}
3207void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3208 VisitFunctionTypeLoc(TL);
3209}
3210void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3211 VisitFunctionTypeLoc(TL);
3212}
John McCallb96ec562009-12-04 22:46:56 +00003213void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003214 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallb96ec562009-12-04 22:46:56 +00003215}
John McCall17001972009-10-18 01:05:36 +00003216void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003217 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003218}
3219void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003220 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3221 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3222 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003223}
3224void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003225 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3226 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3227 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3228 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003229}
3230void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003231 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003232}
3233void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003234 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003235}
3236void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003237 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003238}
John McCall81904512011-01-06 01:58:22 +00003239void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3240 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3241 if (TL.hasAttrOperand()) {
3242 SourceRange range;
3243 range.setBegin(ReadSourceLocation(Record, Idx));
3244 range.setEnd(ReadSourceLocation(Record, Idx));
3245 TL.setAttrOperandParensRange(range);
3246 }
3247 if (TL.hasAttrExprOperand()) {
3248 if (Record[Idx++])
3249 TL.setAttrExprOperand(Reader.ReadExpr(F));
3250 else
3251 TL.setAttrExprOperand(0);
3252 } else if (TL.hasAttrEnumOperand())
3253 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3254}
John McCall17001972009-10-18 01:05:36 +00003255void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003256 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003257}
John McCallcebee162009-10-18 09:09:24 +00003258void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3259 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003260 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallcebee162009-10-18 09:09:24 +00003261}
Douglas Gregorada4b792011-01-14 02:55:32 +00003262void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3263 SubstTemplateTypeParmPackTypeLoc TL) {
3264 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3265}
John McCall17001972009-10-18 01:05:36 +00003266void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3267 TemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003268 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3269 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3270 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall0ad16662009-10-29 08:12:44 +00003271 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3272 TL.setArgLocInfo(i,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003273 Reader.GetTemplateArgumentLocInfo(F,
3274 TL.getTypePtr()->getArg(i).getKind(),
3275 Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003276}
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003277void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3278 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3279 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3280}
Abramo Bagnara6150c882010-05-11 21:36:43 +00003281void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003282 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3283 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003284}
John McCalle78aac42010-03-10 03:28:59 +00003285void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003286 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalle78aac42010-03-10 03:28:59 +00003287}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00003288void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003289 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3290 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3291 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003292}
John McCallc392f372010-06-11 00:33:02 +00003293void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3294 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003295 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3296 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3297 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3298 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3299 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003300 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3301 TL.setArgLocInfo(I,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003302 Reader.GetTemplateArgumentLocInfo(F,
3303 TL.getTypePtr()->getArg(I).getKind(),
3304 Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003305}
Douglas Gregord2fa7662010-12-20 02:24:11 +00003306void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3307 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3308}
John McCall17001972009-10-18 01:05:36 +00003309void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003310 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8b07ec22010-05-15 11:32:37 +00003311}
3312void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3313 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003314 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3315 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003316 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003317 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003318}
John McCallfc93cf92009-10-22 22:37:11 +00003319void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003320 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCallfc93cf92009-10-22 22:37:11 +00003321}
John McCall8f115c62009-10-16 21:56:05 +00003322
Sebastian Redl2c373b92010-10-05 15:59:54 +00003323TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003324 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00003325 unsigned &Idx) {
3326 QualType InfoTy = GetType(Record[Idx++]);
3327 if (InfoTy.isNull())
3328 return 0;
3329
John McCallbcd03502009-12-07 02:54:59 +00003330 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003331 TypeLocReader TLR(*this, F, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00003332 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00003333 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00003334 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00003335}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003336
Sebastian Redl539c5062010-08-18 23:57:32 +00003337QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00003338 unsigned FastQuals = ID & Qualifiers::FastMask;
3339 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003340
Sebastian Redl539c5062010-08-18 23:57:32 +00003341 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003342 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00003343 switch ((PredefinedTypeIDs)Index) {
3344 case PREDEF_TYPE_NULL_ID: return QualType();
3345 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3346 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003347
Sebastian Redl539c5062010-08-18 23:57:32 +00003348 case PREDEF_TYPE_CHAR_U_ID:
3349 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003350 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00003351 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003352 break;
3353
Sebastian Redl539c5062010-08-18 23:57:32 +00003354 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3355 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3356 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3357 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3358 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3359 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3360 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3361 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3362 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3363 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3364 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3365 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3366 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3367 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3368 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3369 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3370 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3371 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3372 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3373 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3374 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3375 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3376 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3377 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003378 }
3379
3380 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00003381 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003382 }
3383
Sebastian Redl539c5062010-08-18 23:57:32 +00003384 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003385 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00003386 if (TypesLoaded[Index].isNull()) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003387 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003388 if (TypesLoaded[Index].isNull())
3389 return QualType();
3390
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003391 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003392 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003393 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003394 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003395 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00003396 }
Mike Stump11289f42009-09-09 15:08:12 +00003397
John McCall8ccfcb52009-09-24 19:53:00 +00003398 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003399}
3400
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003401TypeID ASTReader::GetTypeID(QualType T) const {
3402 return MakeTypeID(T,
3403 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3404}
3405
3406TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3407 if (T.isNull())
3408 return TypeIdx();
3409 assert(!T.getLocalFastQualifiers());
3410
3411 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3412 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3413 // comparing keys of ASTDeclContextNameLookupTable.
3414 // If the type didn't come from the AST file use a specially marked index
3415 // so that any hash/key comparison fail since no such index is stored
3416 // in a AST file.
3417 if (I == TypeIdxs.end())
3418 return TypeIdx(-1);
3419 return I->second;
3420}
3421
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003422unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3423 unsigned Result = 0;
3424 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3425 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3426
3427 return Result;
3428}
3429
John McCall0ad16662009-10-29 08:12:44 +00003430TemplateArgumentLocInfo
Sebastian Redl2c373b92010-10-05 15:59:54 +00003431ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3432 TemplateArgument::ArgKind Kind,
John McCall0ad16662009-10-29 08:12:44 +00003433 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003434 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00003435 switch (Kind) {
3436 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003437 return ReadExpr(F);
John McCall0ad16662009-10-29 08:12:44 +00003438 case TemplateArgument::Type:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003439 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003440 case TemplateArgument::Template: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003441 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3442 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003443 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc,
3444 SourceLocation());
3445 }
3446 case TemplateArgument::TemplateExpansion: {
3447 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3448 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003449 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
3450 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc,
3451 EllipsisLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003452 }
John McCall0ad16662009-10-29 08:12:44 +00003453 case TemplateArgument::Null:
3454 case TemplateArgument::Integral:
3455 case TemplateArgument::Declaration:
3456 case TemplateArgument::Pack:
3457 return TemplateArgumentLocInfo();
3458 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00003459 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00003460 return TemplateArgumentLocInfo();
3461}
3462
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003463TemplateArgumentLoc
Sebastian Redl2c373b92010-10-05 15:59:54 +00003464ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003465 const RecordData &Record, unsigned &Index) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003466 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003467
3468 if (Arg.getKind() == TemplateArgument::Expression) {
3469 if (Record[Index++]) // bool InfoHasSameExpr.
3470 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3471 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00003472 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003473 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003474}
3475
Sebastian Redl2c499f62010-08-18 23:56:43 +00003476Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00003477 return GetDecl(ID);
3478}
3479
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003480uint64_t
3481ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3482 if (ID == 0)
3483 return 0;
3484
3485 --ID;
3486 uint64_t Offset = 0;
3487 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3488 if (ID < Chain[I]->LocalNumCXXBaseSpecifiers)
3489 return Offset + Chain[I]->CXXBaseSpecifiersOffsets[ID];
3490
3491 ID -= Chain[I]->LocalNumCXXBaseSpecifiers;
3492 Offset += Chain[I]->SizeInBits;
3493 }
3494
3495 assert(false && "CXXBaseSpecifiers not found");
3496 return 0;
3497}
3498
3499CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3500 // Figure out which AST file contains this offset.
3501 PerFileData *F = 0;
3502 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3503 if (Offset < Chain[I]->SizeInBits) {
3504 F = Chain[I];
3505 break;
3506 }
3507
3508 Offset -= Chain[I]->SizeInBits;
3509 }
3510
3511 if (!F) {
3512 Error("Malformed AST file: C++ base specifiers at impossible offset");
3513 return 0;
3514 }
3515
3516 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3517 SavedStreamPosition SavedPosition(Cursor);
3518 Cursor.JumpToBit(Offset);
3519 ReadingKindTracker ReadingKind(Read_Decl, *this);
3520 RecordData Record;
3521 unsigned Code = Cursor.ReadCode();
3522 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3523 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3524 Error("Malformed AST file: missing C++ base specifiers");
3525 return 0;
3526 }
3527
3528 unsigned Idx = 0;
3529 unsigned NumBases = Record[Idx++];
3530 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3531 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3532 for (unsigned I = 0; I != NumBases; ++I)
3533 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3534 return Bases;
3535}
3536
Sebastian Redl2c499f62010-08-18 23:56:43 +00003537TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003538 if (!DeclsLoaded[0]) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00003539 ReadDeclRecord(0, 1);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003540 if (DeserializationListener)
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003541 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003542 }
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00003543
3544 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3545}
3546
Sebastian Redl539c5062010-08-18 23:57:32 +00003547Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003548 if (ID == 0)
3549 return 0;
3550
Douglas Gregor745ed142009-04-25 18:35:21 +00003551 if (ID > DeclsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003552 Error("declaration ID out-of-range for AST file");
Douglas Gregor745ed142009-04-25 18:35:21 +00003553 return 0;
3554 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003555
Douglas Gregor745ed142009-04-25 18:35:21 +00003556 unsigned Index = ID - 1;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003557 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003558 ReadDeclRecord(Index, ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003559 if (DeserializationListener)
3560 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3561 }
Douglas Gregor745ed142009-04-25 18:35:21 +00003562
3563 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003564}
3565
Chris Lattner9c28af02009-04-27 05:46:25 +00003566/// \brief Resolve the offset of a statement into a statement.
3567///
3568/// This operation will read a new statement from the external
3569/// source each time it is called, and is meant to be used via a
3570/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00003571Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00003572 // Switch case IDs are per Decl.
3573 ClearSwitchCaseIDs();
3574
Sebastian Redl5c415f32010-07-22 17:01:13 +00003575 // Offset here is a global offset across the entire chain.
3576 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3577 PerFileData &F = *Chain[N - I - 1];
3578 if (Offset < F.SizeInBits) {
3579 // Since we know that this statement is part of a decl, make sure to use
3580 // the decl cursor to read it.
3581 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003582 return ReadStmtFromStream(F);
Sebastian Redl5c415f32010-07-22 17:01:13 +00003583 }
3584 Offset -= F.SizeInBits;
3585 }
3586 llvm_unreachable("Broken chain");
Douglas Gregor3c3aa612009-04-18 00:07:54 +00003587}
3588
Sebastian Redl2c499f62010-08-18 23:56:43 +00003589bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003590 bool (*isKindWeWant)(Decl::Kind),
John McCall75b960e2010-06-01 09:23:16 +00003591 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump11289f42009-09-09 15:08:12 +00003592 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003593 "DeclContext has no lexical decls in storage");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003594
Sebastian Redl5c415f32010-07-22 17:01:13 +00003595 // There might be lexical decls in multiple parts of the chain, for the TU
3596 // at least.
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003597 // DeclContextOffsets might reallocate as we load additional decls below,
3598 // so make a copy of the vector.
3599 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl5c415f32010-07-22 17:01:13 +00003600 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3601 I != E; ++I) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003602 // IDs can be 0 if this context doesn't contain declarations.
3603 if (!I->LexicalDecls)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003604 continue;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003605
3606 // Load all of the declaration IDs
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003607 for (const KindDeclIDPair *ID = I->LexicalDecls,
3608 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3609 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3610 continue;
3611
3612 Decl *D = GetDecl(ID->second);
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003613 assert(D && "Null decl in lexical decls");
3614 Decls.push_back(D);
3615 }
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003616 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003617
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003618 ++NumLexicalDeclContextsRead;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003619 return false;
3620}
3621
John McCall75b960e2010-06-01 09:23:16 +00003622DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00003623ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00003624 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003625 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003626 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003627 if (!Name)
3628 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3629 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003630
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003631 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl471ac2f2010-08-24 00:49:55 +00003632 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003633 // and namespaces. For any given name, the last available results replace
3634 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl5c415f32010-07-22 17:01:13 +00003635 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003636 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl5c415f32010-07-22 17:01:13 +00003637 I != E; ++I) {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003638 if (!I->NameLookupTableData)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003639 continue;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003640
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003641 ASTDeclContextNameLookupTable *LookupTable =
3642 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3643 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3644 if (Pos == LookupTable->end())
Sebastian Redl5c415f32010-07-22 17:01:13 +00003645 continue;
3646
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003647 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3648 for (; Data.first != Data.second; ++Data.first)
3649 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003650 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003651 }
3652
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003653 ++NumVisibleDeclContextsRead;
John McCall75b960e2010-06-01 09:23:16 +00003654
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003655 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00003656 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003657}
3658
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00003659void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3660 assert(DC->hasExternalVisibleStorage() &&
3661 "DeclContext has no visible decls in storage");
3662
3663 llvm::SmallVector<NamedDecl *, 64> Decls;
3664 // There might be visible decls in multiple parts of the chain, for the TU
3665 // and namespaces.
3666 DeclContextInfos &Infos = DeclContextOffsets[DC];
3667 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3668 I != E; ++I) {
3669 if (!I->NameLookupTableData)
3670 continue;
3671
3672 ASTDeclContextNameLookupTable *LookupTable =
3673 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3674 for (ASTDeclContextNameLookupTable::item_iterator
3675 ItemI = LookupTable->item_begin(),
3676 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3677 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3678 = *ItemI;
3679 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3680 Decls.clear();
3681 for (; Data.first != Data.second; ++Data.first)
3682 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3683 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3684 }
3685 }
3686}
3687
Sebastian Redl2c499f62010-08-18 23:56:43 +00003688void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003689 assert(Consumer);
3690 while (!InterestingDecls.empty()) {
3691 DeclGroupRef DG(InterestingDecls.front());
3692 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00003693 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003694 }
3695}
3696
Sebastian Redl2c499f62010-08-18 23:56:43 +00003697void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00003698 this->Consumer = Consumer;
3699
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003700 if (!Consumer)
3701 return;
3702
3703 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003704 // Force deserialization of this decl, which will cause it to be queued for
3705 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00003706 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003707 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00003708
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003709 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003710}
3711
Sebastian Redl2c499f62010-08-18 23:56:43 +00003712void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003713 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003714
Mike Stump11289f42009-09-09 15:08:12 +00003715 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003716 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00003717 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00003718 unsigned NumDeclsLoaded
3719 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3720 (Decl *)0);
3721 unsigned NumIdentifiersLoaded
3722 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3723 IdentifiersLoaded.end(),
3724 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00003725 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003726 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3727 SelectorsLoaded.end(),
3728 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00003729
Douglas Gregorc5046832009-04-27 18:38:38 +00003730 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3731 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor258ae542009-04-27 06:38:32 +00003732 if (TotalNumSLocEntries)
3733 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3734 NumSLocEntriesRead, TotalNumSLocEntries,
3735 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00003736 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003737 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003738 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3739 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3740 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003741 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003742 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3743 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00003744 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003745 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00003746 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3747 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00003748 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003749 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00003750 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3751 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003752 if (TotalNumStatements)
3753 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3754 NumStatementsRead, TotalNumStatements,
3755 ((float)NumStatementsRead/TotalNumStatements * 100));
3756 if (TotalNumMacros)
3757 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3758 NumMacrosRead, TotalNumMacros,
3759 ((float)NumMacrosRead/TotalNumMacros * 100));
3760 if (TotalLexicalDeclContexts)
3761 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3762 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3763 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3764 * 100));
3765 if (TotalVisibleDeclContexts)
3766 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3767 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3768 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3769 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003770 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003771 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003772 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3773 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00003774 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003775 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003776 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003777 std::fprintf(stderr, "\n");
3778}
3779
Sebastian Redl2c499f62010-08-18 23:56:43 +00003780void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00003781 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003782 S.ExternalSource = this;
3783
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003784 // Makes sure any declarations that were deserialized "too early"
3785 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003786 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3787 if (SemaObj->TUScope)
John McCall48871652010-08-21 09:40:31 +00003788 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003789
3790 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003791 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003792 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00003793
3794 // If there were any tentative definitions, deserialize them and add
Sebastian Redl35351a92010-01-31 22:27:38 +00003795 // them to Sema's list of tentative definitions.
Douglas Gregord4df8652009-04-22 22:02:47 +00003796 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3797 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redl35351a92010-01-31 22:27:38 +00003798 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregord4df8652009-04-22 22:02:47 +00003799 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00003800
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003801 // If there were any unused file scoped decls, deserialize them and add to
3802 // Sema's list of unused file scoped decls.
3803 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3804 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3805 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattner90073802010-02-12 00:07:30 +00003806 }
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003807
3808 // If there were any locally-scoped external declarations,
3809 // deserialize them and add them to Sema's table of locally-scoped
3810 // external declarations.
3811 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3812 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3813 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3814 }
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003815
3816 // If there were any ext_vector type declarations, deserialize them
3817 // and add them to Sema's vector of such declarations.
3818 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3819 SemaObj->ExtVectorDecls.push_back(
3820 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003821
3822 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3823 // Can we cut them down before writing them ?
3824
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003825 // If there were any dynamic classes declarations, deserialize them
3826 // and add them to Sema's vector of such declarations.
3827 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3828 SemaObj->DynamicClasses.push_back(
3829 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003830
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003831 // Load the offsets of the declarations that Sema references.
3832 // They will be lazily deserialized when needed.
3833 if (!SemaDeclRefs.empty()) {
3834 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3835 SemaObj->StdNamespace = SemaDeclRefs[0];
3836 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3837 }
3838
Sebastian Redl2c373b92010-10-05 15:59:54 +00003839 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
3840
3841 // If there are @selector references added them to its pool. This is for
3842 // implementation of -Wselector.
3843 if (!F->ReferencedSelectorsData.empty()) {
3844 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
3845 unsigned I = 0;
3846 while (I < DataSize) {
3847 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
3848 SourceLocation SelLoc = ReadSourceLocation(
3849 *F, F->ReferencedSelectorsData, I);
3850 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3851 }
3852 }
3853
3854 // If there were any pending implicit instantiations, deserialize them
3855 // and add them to Sema's queue of such instantiations.
3856 assert(F->PendingInstantiations.size() % 2 == 0 &&
3857 "Expected pairs of entries");
3858 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
3859 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
3860 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
3861 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
3862 }
3863 }
3864
3865 // The two special data sets below always come from the most recent PCH,
3866 // which is at the front of the chain.
3867 PerFileData &F = *Chain.front();
3868
3869 // If there were any weak undeclared identifiers, deserialize them and add to
3870 // Sema's list of weak undeclared identifiers.
3871 if (!WeakUndeclaredIdentifiers.empty()) {
3872 unsigned Idx = 0;
3873 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3874 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3875 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3876 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
3877 bool Used = WeakUndeclaredIdentifiers[Idx++];
3878 Sema::WeakInfo WI(AliasId, Loc);
3879 WI.setUsed(Used);
3880 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3881 }
3882 }
3883
3884 // If there were any VTable uses, deserialize the information and add it
3885 // to Sema's vector and map of VTable uses.
3886 if (!VTableUses.empty()) {
3887 unsigned Idx = 0;
3888 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3889 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3890 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
3891 bool DefinitionRequired = VTableUses[Idx++];
3892 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3893 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003894 }
3895 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00003896}
3897
Sebastian Redl2c499f62010-08-18 23:56:43 +00003898IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redl78f51772010-08-02 18:30:12 +00003899 // Try to find this name within our on-disk hash tables. We start with the
3900 // most recent one, since that one contains the most up-to-date info.
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003901 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003902 ASTIdentifierLookupTable *IdTable
3903 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003904 if (!IdTable)
3905 continue;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003906 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003907 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003908 if (Pos == IdTable->end())
3909 continue;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003910
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003911 // Dereferencing the iterator has the effect of building the
3912 // IdentifierInfo node and populating it with the various
3913 // declarations it needs.
Sebastian Redl78f51772010-08-02 18:30:12 +00003914 return *Pos;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003915 }
Sebastian Redl78f51772010-08-02 18:30:12 +00003916 return 0;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003917}
3918
Douglas Gregor57756ea2010-10-14 22:11:03 +00003919namespace clang {
3920 /// \brief An identifier-lookup iterator that enumerates all of the
3921 /// identifiers stored within a set of AST files.
3922 class ASTIdentifierIterator : public IdentifierIterator {
3923 /// \brief The AST reader whose identifiers are being enumerated.
3924 const ASTReader &Reader;
3925
3926 /// \brief The current index into the chain of AST files stored in
3927 /// the AST reader.
3928 unsigned Index;
3929
3930 /// \brief The current position within the identifier lookup table
3931 /// of the current AST file.
3932 ASTIdentifierLookupTable::key_iterator Current;
3933
3934 /// \brief The end position within the identifier lookup table of
3935 /// the current AST file.
3936 ASTIdentifierLookupTable::key_iterator End;
3937
3938 public:
3939 explicit ASTIdentifierIterator(const ASTReader &Reader);
3940
3941 virtual llvm::StringRef Next();
3942 };
3943}
3944
3945ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
3946 : Reader(Reader), Index(Reader.Chain.size() - 1) {
3947 ASTIdentifierLookupTable *IdTable
3948 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3949 Current = IdTable->key_begin();
3950 End = IdTable->key_end();
3951}
3952
3953llvm::StringRef ASTIdentifierIterator::Next() {
3954 while (Current == End) {
3955 // If we have exhausted all of our AST files, we're done.
3956 if (Index == 0)
3957 return llvm::StringRef();
3958
3959 --Index;
3960 ASTIdentifierLookupTable *IdTable
3961 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3962 Current = IdTable->key_begin();
3963 End = IdTable->key_end();
3964 }
3965
3966 // We have any identifiers remaining in the current AST file; return
3967 // the next one.
3968 std::pair<const char*, unsigned> Key = *Current;
3969 ++Current;
3970 return llvm::StringRef(Key.first, Key.second);
3971}
3972
3973IdentifierIterator *ASTReader::getIdentifiers() const {
3974 return new ASTIdentifierIterator(*this);
3975}
3976
Mike Stump11289f42009-09-09 15:08:12 +00003977std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redl2c499f62010-08-18 23:56:43 +00003978ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redlada023c2010-08-04 20:40:17 +00003979 // Find this selector in a hash table. We want to find the most recent entry.
3980 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3981 PerFileData &F = *Chain[I];
3982 if (!F.SelectorLookupTable)
3983 continue;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003984
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003985 ASTSelectorLookupTable *PoolTable
3986 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3987 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redlada023c2010-08-04 20:40:17 +00003988 if (Pos != PoolTable->end()) {
3989 ++NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003990 // FIXME: Not quite happy with the statistics here. We probably should
3991 // disable this tracking when called via LoadSelector.
3992 // Also, should entries without methods count as misses?
3993 ++NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003994 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redlada023c2010-08-04 20:40:17 +00003995 if (DeserializationListener)
3996 DeserializationListener->SelectorRead(Data.ID, Sel);
3997 return std::make_pair(Data.Instance, Data.Factory);
3998 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003999 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00004000
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004001 ++NumMethodPoolMisses;
Sebastian Redlada023c2010-08-04 20:40:17 +00004002 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorc78d3462009-04-24 21:10:55 +00004003}
4004
Sebastian Redl2c499f62010-08-18 23:56:43 +00004005void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004006 // It would be complicated to avoid reading the methods anyway. So don't.
4007 ReadMethodPool(Sel);
4008}
4009
Sebastian Redl2c499f62010-08-18 23:56:43 +00004010void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004011 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00004012 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00004013 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00004014 if (DeserializationListener)
4015 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004016}
4017
Douglas Gregor1342e842009-07-06 18:54:52 +00004018/// \brief Set the globally-visible declarations associated with the given
4019/// identifier.
4020///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004021/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00004022/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00004023/// them.
4024///
4025/// \param II an IdentifierInfo that refers to one or more globally-visible
4026/// declarations.
4027///
4028/// \param DeclIDs the set of declaration IDs with the name @p II that are
4029/// visible at global scope.
4030///
4031/// \param Nonrecursive should be true to indicate that the caller knows that
4032/// this call is non-recursive, and therefore the globally-visible declarations
4033/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00004034void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004035ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregor1342e842009-07-06 18:54:52 +00004036 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
4037 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004038 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004039 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4040 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4041 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004042 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00004043 return;
4044 }
Mike Stump11289f42009-09-09 15:08:12 +00004045
Douglas Gregor1342e842009-07-06 18:54:52 +00004046 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4047 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4048 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004049 if (SemaObj->TUScope) {
4050 // Introduce this declaration into the translation-unit scope
4051 // and add it to the declaration chain for this identifier, so
4052 // that (unqualified) name lookup will find it.
John McCall48871652010-08-21 09:40:31 +00004053 SemaObj->TUScope->AddDecl(D);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004054 }
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004055 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregor1342e842009-07-06 18:54:52 +00004056 } else {
4057 // Queue this declaration so that it will be added to the
4058 // translation unit scope and identifier's declaration chain
4059 // once a Sema object is known.
4060 PreloadedDecls.push_back(D);
4061 }
4062 }
4063}
4064
Sebastian Redl2c499f62010-08-18 23:56:43 +00004065IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004066 if (ID == 0)
4067 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00004068
Sebastian Redlc713b962010-07-21 00:46:22 +00004069 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004070 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004071 return 0;
4072 }
Mike Stump11289f42009-09-09 15:08:12 +00004073
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004074 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc713b962010-07-21 00:46:22 +00004075 ID -= 1;
4076 if (!IdentifiersLoaded[ID]) {
4077 unsigned Index = ID;
4078 const char *Str = 0;
4079 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4080 PerFileData *F = Chain[N - I - 1];
4081 if (Index < F->LocalNumIdentifiers) {
4082 uint32_t Offset = F->IdentifierOffsets[Index];
4083 Str = F->IdentifierTableData + Offset;
4084 break;
4085 }
4086 Index -= F->LocalNumIdentifiers;
4087 }
4088 assert(Str && "Broken Chain");
Douglas Gregor5287b4e2009-04-25 21:04:17 +00004089
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004090 // All of the strings in the AST file are preceded by a 16-bit length.
4091 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00004092 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4093 // unsigned integers. This is important to avoid integer overflow when
4094 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00004095 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00004096 unsigned StrLen = (((unsigned) StrLenPtr[0])
4097 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00004098 IdentifiersLoaded[ID]
Kovarththanan Rajaratnama3b09592010-03-12 10:32:27 +00004099 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlff4a2952010-07-23 23:49:55 +00004100 if (DeserializationListener)
4101 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004102 }
Mike Stump11289f42009-09-09 15:08:12 +00004103
Sebastian Redlc713b962010-07-21 00:46:22 +00004104 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004105}
4106
Sebastian Redl2c499f62010-08-18 23:56:43 +00004107void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00004108 ReadSLocEntryRecord(ID);
4109}
4110
Sebastian Redl2c499f62010-08-18 23:56:43 +00004111Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00004112 if (ID == 0)
4113 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00004114
Sebastian Redlada023c2010-08-04 20:40:17 +00004115 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004116 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00004117 return Selector();
4118 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004119
Sebastian Redlada023c2010-08-04 20:40:17 +00004120 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004121 // Load this selector from the selector table.
Sebastian Redlada023c2010-08-04 20:40:17 +00004122 unsigned Idx = ID - 1;
4123 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4124 PerFileData &F = *Chain[N - I - 1];
4125 if (Idx < F.LocalNumSelectors) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004126 ASTSelectorLookupTrait Trait(*this);
Sebastian Redlada023c2010-08-04 20:40:17 +00004127 SelectorsLoaded[ID - 1] =
4128 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4129 if (DeserializationListener)
4130 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4131 break;
4132 }
4133 Idx -= F.LocalNumSelectors;
4134 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004135 }
4136
Sebastian Redlada023c2010-08-04 20:40:17 +00004137 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00004138}
4139
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004140Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00004141 return DecodeSelector(ID);
4142}
4143
Sebastian Redl2c499f62010-08-18 23:56:43 +00004144uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00004145 // ID 0 (the null selector) is considered an external selector.
4146 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00004147}
4148
Mike Stump11289f42009-09-09 15:08:12 +00004149DeclarationName
Sebastian Redl2c499f62010-08-18 23:56:43 +00004150ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004151 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4152 switch (Kind) {
4153 case DeclarationName::Identifier:
4154 return DeclarationName(GetIdentifierInfo(Record, Idx));
4155
4156 case DeclarationName::ObjCZeroArgSelector:
4157 case DeclarationName::ObjCOneArgSelector:
4158 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff3c301dc2009-04-23 15:15:40 +00004159 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004160
4161 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004162 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004163 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004164
4165 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004166 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004167 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004168
4169 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004170 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004171 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004172
4173 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004174 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004175 (OverloadedOperatorKind)Record[Idx++]);
4176
Alexis Hunt3d221f22009-11-29 07:34:05 +00004177 case DeclarationName::CXXLiteralOperatorName:
4178 return Context->DeclarationNames.getCXXLiteralOperatorName(
4179 GetIdentifierInfo(Record, Idx));
4180
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004181 case DeclarationName::CXXUsingDirective:
4182 return DeclarationName::getUsingDirectiveName();
4183 }
4184
4185 // Required to silence GCC warning
4186 return DeclarationName();
4187}
Douglas Gregor55abb232009-04-10 20:39:37 +00004188
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004189void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4190 DeclarationNameLoc &DNLoc,
4191 DeclarationName Name,
4192 const RecordData &Record, unsigned &Idx) {
4193 switch (Name.getNameKind()) {
4194 case DeclarationName::CXXConstructorName:
4195 case DeclarationName::CXXDestructorName:
4196 case DeclarationName::CXXConversionFunctionName:
4197 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4198 break;
4199
4200 case DeclarationName::CXXOperatorName:
4201 DNLoc.CXXOperatorName.BeginOpNameLoc
4202 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4203 DNLoc.CXXOperatorName.EndOpNameLoc
4204 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4205 break;
4206
4207 case DeclarationName::CXXLiteralOperatorName:
4208 DNLoc.CXXLiteralOperatorName.OpNameLoc
4209 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4210 break;
4211
4212 case DeclarationName::Identifier:
4213 case DeclarationName::ObjCZeroArgSelector:
4214 case DeclarationName::ObjCOneArgSelector:
4215 case DeclarationName::ObjCMultiArgSelector:
4216 case DeclarationName::CXXUsingDirective:
4217 break;
4218 }
4219}
4220
4221void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4222 DeclarationNameInfo &NameInfo,
4223 const RecordData &Record, unsigned &Idx) {
4224 NameInfo.setName(ReadDeclarationName(Record, Idx));
4225 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4226 DeclarationNameLoc DNLoc;
4227 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4228 NameInfo.setInfo(DNLoc);
4229}
4230
4231void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4232 const RecordData &Record, unsigned &Idx) {
4233 Info.NNS = ReadNestedNameSpecifier(Record, Idx);
4234 Info.NNSRange = ReadSourceRange(F, Record, Idx);
4235 unsigned NumTPLists = Record[Idx++];
4236 Info.NumTemplParamLists = NumTPLists;
4237 if (NumTPLists) {
4238 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4239 for (unsigned i=0; i != NumTPLists; ++i)
4240 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4241 }
4242}
4243
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004244TemplateName
Douglas Gregor5590be02011-01-15 06:45:20 +00004245ASTReader::ReadTemplateName(PerFileData &F, const RecordData &Record,
4246 unsigned &Idx) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004247 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004248 switch (Kind) {
4249 case TemplateName::Template:
4250 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4251
4252 case TemplateName::OverloadedTemplate: {
4253 unsigned size = Record[Idx++];
4254 UnresolvedSet<8> Decls;
4255 while (size--)
4256 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4257
4258 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4259 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004260
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004261 case TemplateName::QualifiedTemplate: {
4262 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4263 bool hasTemplKeyword = Record[Idx++];
4264 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4265 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4266 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004267
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004268 case TemplateName::DependentTemplate: {
4269 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4270 if (Record[Idx++]) // isIdentifier
4271 return Context->getDependentTemplateName(NNS,
4272 GetIdentifierInfo(Record, Idx));
4273 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004274 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004275 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004276
4277 case TemplateName::SubstTemplateTemplateParmPack: {
4278 TemplateTemplateParmDecl *Param
4279 = cast_or_null<TemplateTemplateParmDecl>(GetDecl(Record[Idx++]));
4280 if (!Param)
4281 return TemplateName();
4282
4283 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
4284 if (ArgPack.getKind() != TemplateArgument::Pack)
4285 return TemplateName();
4286
4287 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
4288 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004289 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004290
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004291 assert(0 && "Unhandled template name kind!");
4292 return TemplateName();
4293}
4294
4295TemplateArgument
Sebastian Redl2c373b92010-10-05 15:59:54 +00004296ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00004297 const RecordData &Record, unsigned &Idx) {
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004298 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
4299 switch (Kind) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004300 case TemplateArgument::Null:
4301 return TemplateArgument();
4302 case TemplateArgument::Type:
4303 return TemplateArgument(GetType(Record[Idx++]));
4304 case TemplateArgument::Declaration:
4305 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00004306 case TemplateArgument::Integral: {
4307 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4308 QualType T = GetType(Record[Idx++]);
4309 return TemplateArgument(Value, T);
4310 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004311 case TemplateArgument::Template:
Douglas Gregor5590be02011-01-15 06:45:20 +00004312 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004313 case TemplateArgument::TemplateExpansion: {
Douglas Gregor5590be02011-01-15 06:45:20 +00004314 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregore1d60df2011-01-14 23:41:42 +00004315 llvm::Optional<unsigned> NumTemplateExpansions;
4316 if (unsigned NumExpansions = Record[Idx++])
4317 NumTemplateExpansions = NumExpansions - 1;
4318 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004319 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004320 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004321 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004322 case TemplateArgument::Pack: {
4323 unsigned NumArgs = Record[Idx++];
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004324 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4325 for (unsigned I = 0; I != NumArgs; ++I)
4326 Args[I] = ReadTemplateArgument(F, Record, Idx);
4327 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004328 }
4329 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004330
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004331 assert(0 && "Unhandled template argument kind!");
4332 return TemplateArgument();
4333}
4334
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004335TemplateParameterList *
Sebastian Redl2c373b92010-10-05 15:59:54 +00004336ASTReader::ReadTemplateParameterList(PerFileData &F,
4337 const RecordData &Record, unsigned &Idx) {
4338 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4339 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4340 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004341
4342 unsigned NumParams = Record[Idx++];
4343 llvm::SmallVector<NamedDecl *, 16> Params;
4344 Params.reserve(NumParams);
4345 while (NumParams--)
4346 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004347
4348 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004349 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4350 Params.data(), Params.size(), RAngleLoc);
4351 return TemplateParams;
4352}
4353
4354void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004355ASTReader::
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004356ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004357 PerFileData &F, const RecordData &Record,
4358 unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004359 unsigned NumTemplateArgs = Record[Idx++];
4360 TemplArgs.reserve(NumTemplateArgs);
4361 while (NumTemplateArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004362 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004363}
4364
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004365/// \brief Read a UnresolvedSet structure.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004366void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004367 const RecordData &Record, unsigned &Idx) {
4368 unsigned NumDecls = Record[Idx++];
4369 while (NumDecls--) {
4370 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4371 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4372 Set.addDecl(D, AS);
4373 }
4374}
4375
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004376CXXBaseSpecifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00004377ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky19b9f952010-07-26 16:56:01 +00004378 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004379 bool isVirtual = static_cast<bool>(Record[Idx++]);
4380 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4381 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004382 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4383 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor752a5952011-01-03 22:36:02 +00004384 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
4385 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo,
4386 EllipsisLoc);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004387}
4388
Alexis Hunt1d792652011-01-08 20:30:50 +00004389std::pair<CXXCtorInitializer **, unsigned>
4390ASTReader::ReadCXXCtorInitializers(PerFileData &F, const RecordData &Record,
4391 unsigned &Idx) {
4392 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004393 unsigned NumInitializers = Record[Idx++];
4394 if (NumInitializers) {
4395 ASTContext &C = *getContext();
4396
Alexis Hunt1d792652011-01-08 20:30:50 +00004397 CtorInitializers
4398 = new (C) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004399 for (unsigned i=0; i != NumInitializers; ++i) {
4400 TypeSourceInfo *BaseClassInfo = 0;
4401 bool IsBaseVirtual = false;
4402 FieldDecl *Member = 0;
Francois Pichetd583da02010-12-04 09:14:42 +00004403 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004404
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004405 bool IsBaseInitializer = Record[Idx++];
4406 if (IsBaseInitializer) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004407 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004408 IsBaseVirtual = Record[Idx++];
4409 } else {
Francois Pichetd583da02010-12-04 09:14:42 +00004410 bool IsIndirectMemberInitializer = Record[Idx++];
4411 if (IsIndirectMemberInitializer)
4412 IndirectMember = cast<IndirectFieldDecl>(GetDecl(Record[Idx++]));
4413 else
4414 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004415 }
Douglas Gregor44e7df62011-01-04 00:32:56 +00004416 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004417 Expr *Init = ReadExpr(F);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004418 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4419 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004420 bool IsWritten = Record[Idx++];
4421 unsigned SourceOrderOrNumArrayIndices;
4422 llvm::SmallVector<VarDecl *, 8> Indices;
4423 if (IsWritten) {
4424 SourceOrderOrNumArrayIndices = Record[Idx++];
4425 } else {
4426 SourceOrderOrNumArrayIndices = Record[Idx++];
4427 Indices.reserve(SourceOrderOrNumArrayIndices);
4428 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4429 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4430 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004431
Alexis Hunt1d792652011-01-08 20:30:50 +00004432 CXXCtorInitializer *BOMInit;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004433 if (IsBaseInitializer) {
Alexis Hunt1d792652011-01-08 20:30:50 +00004434 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
4435 LParenLoc, Init, RParenLoc,
4436 MemberOrEllipsisLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004437 } else if (IsWritten) {
Francois Pichetd583da02010-12-04 09:14:42 +00004438 if (Member)
Alexis Hunt1d792652011-01-08 20:30:50 +00004439 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
4440 LParenLoc, Init, RParenLoc);
Francois Pichetd583da02010-12-04 09:14:42 +00004441 else
Alexis Hunt1d792652011-01-08 20:30:50 +00004442 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
4443 MemberOrEllipsisLoc, LParenLoc,
4444 Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004445 } else {
Alexis Hunt1d792652011-01-08 20:30:50 +00004446 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
4447 LParenLoc, Init, RParenLoc,
4448 Indices.data(), Indices.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004449 }
4450
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00004451 if (IsWritten)
4452 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Alexis Hunt1d792652011-01-08 20:30:50 +00004453 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004454 }
4455 }
4456
Alexis Hunt1d792652011-01-08 20:30:50 +00004457 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004458}
4459
Chris Lattnerca025db2010-05-07 21:43:38 +00004460NestedNameSpecifier *
Sebastian Redl2c499f62010-08-18 23:56:43 +00004461ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004462 unsigned N = Record[Idx++];
4463 NestedNameSpecifier *NNS = 0, *Prev = 0;
4464 for (unsigned I = 0; I != N; ++I) {
4465 NestedNameSpecifier::SpecifierKind Kind
4466 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4467 switch (Kind) {
4468 case NestedNameSpecifier::Identifier: {
4469 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4470 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4471 break;
4472 }
4473
4474 case NestedNameSpecifier::Namespace: {
4475 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4476 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4477 break;
4478 }
4479
4480 case NestedNameSpecifier::TypeSpec:
4481 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall424cec92011-01-19 06:33:43 +00004482 const Type *T = GetType(Record[Idx++]).getTypePtrOrNull();
Douglas Gregor0cdc8322010-12-10 17:03:06 +00004483 if (!T)
4484 return 0;
4485
Chris Lattnerca025db2010-05-07 21:43:38 +00004486 bool Template = Record[Idx++];
4487 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4488 break;
4489 }
4490
4491 case NestedNameSpecifier::Global: {
4492 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4493 // No associated value, and there can't be a prefix.
4494 break;
4495 }
Chris Lattnerca025db2010-05-07 21:43:38 +00004496 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00004497 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00004498 }
4499 return NNS;
4500}
4501
4502SourceRange
Sebastian Redl2c373b92010-10-05 15:59:54 +00004503ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4504 unsigned &Idx) {
4505 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4506 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00004507 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00004508}
4509
Douglas Gregor1daeb692009-04-13 18:14:40 +00004510/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004511llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004512 unsigned BitWidth = Record[Idx++];
4513 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4514 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4515 Idx += NumWords;
4516 return Result;
4517}
4518
4519/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004520llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004521 bool isUnsigned = Record[Idx++];
4522 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4523}
4524
Douglas Gregore0a3a512009-04-14 21:55:33 +00004525/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004526llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004527 return llvm::APFloat(ReadAPInt(Record, Idx));
4528}
4529
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004530// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00004531std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004532 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00004533 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004534 Idx += Len;
4535 return Result;
4536}
4537
Sebastian Redl2c499f62010-08-18 23:56:43 +00004538CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00004539 unsigned &Idx) {
4540 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4541 return CXXTemporary::Create(*Context, Decl);
4542}
4543
Sebastian Redl2c499f62010-08-18 23:56:43 +00004544DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00004545 return Diag(SourceLocation(), DiagID);
4546}
4547
Sebastian Redl2c499f62010-08-18 23:56:43 +00004548DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004549 return Diags.Report(Loc, DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00004550}
Douglas Gregora9af1d12009-04-17 00:04:06 +00004551
Douglas Gregora868bbd2009-04-21 22:25:48 +00004552/// \brief Retrieve the identifier table associated with the
4553/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004554IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004555 assert(PP && "Forgot to set Preprocessor ?");
4556 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00004557}
4558
Douglas Gregora9af1d12009-04-17 00:04:06 +00004559/// \brief Record that the given ID maps to the given switch-case
4560/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004561void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004562 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4563 SwitchCaseStmts[ID] = SC;
4564}
4565
4566/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004567SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004568 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4569 return SwitchCaseStmts[ID];
4570}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004571
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00004572void ASTReader::ClearSwitchCaseIDs() {
4573 SwitchCaseStmts.clear();
4574}
4575
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004576/// \brief Record that the given label statement has been
4577/// deserialized and has the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004578void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump11289f42009-09-09 15:08:12 +00004579 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004580 "Deserialized label twice");
4581 LabelStmts[ID] = S;
4582
4583 // If we've already seen any goto statements that point to this
4584 // label, resolve them now.
4585 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
4586 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
4587 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
4588 Goto->second->setLabel(S);
4589 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor779d8652009-04-17 18:58:21 +00004590
4591 // If we've already seen any address-label statements that point to
4592 // this label, resolve them now.
4593 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump11289f42009-09-09 15:08:12 +00004594 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor779d8652009-04-17 18:58:21 +00004595 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump11289f42009-09-09 15:08:12 +00004596 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor779d8652009-04-17 18:58:21 +00004597 AddrLabel != AddrLabels.second; ++AddrLabel)
4598 AddrLabel->second->setLabel(S);
4599 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004600}
4601
4602/// \brief Set the label of the given statement to the label
4603/// identified by ID.
4604///
4605/// Depending on the order in which the label and other statements
4606/// referencing that label occur, this operation may complete
4607/// immediately (updating the statement) or it may queue the
4608/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004609void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004610 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4611 if (Label != LabelStmts.end()) {
4612 // We've already seen this label, so set the label of the goto and
4613 // we're done.
4614 S->setLabel(Label->second);
4615 } else {
4616 // We haven't seen this label yet, so add this goto to the set of
4617 // unresolved goto statements.
4618 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
4619 }
4620}
Douglas Gregor779d8652009-04-17 18:58:21 +00004621
4622/// \brief Set the label of the given expression to the label
4623/// identified by ID.
4624///
4625/// Depending on the order in which the label and other statements
4626/// referencing that label occur, this operation may complete
4627/// immediately (updating the statement) or it may queue the
4628/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004629void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor779d8652009-04-17 18:58:21 +00004630 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4631 if (Label != LabelStmts.end()) {
4632 // We've already seen this label, so set the label of the
4633 // label-address expression and we're done.
4634 S->setLabel(Label->second);
4635 } else {
4636 // We haven't seen this label yet, so add this label-address
4637 // expression to the set of unresolved label-address expressions.
4638 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
4639 }
4640}
Douglas Gregor1342e842009-07-06 18:54:52 +00004641
Sebastian Redl2c499f62010-08-18 23:56:43 +00004642void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004643 assert(NumCurrentElementsDeserializing &&
4644 "FinishedDeserializing not paired with StartedDeserializing");
4645 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004646 // If any identifiers with corresponding top-level declarations have
4647 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004648 while (!PendingIdentifierInfos.empty()) {
4649 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4650 PendingIdentifierInfos.front().DeclIDs, true);
4651 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00004652 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004653
4654 // We are not in recursive loading, so it's safe to pass the "interesting"
4655 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004656 if (Consumer)
4657 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidisad5f95c2010-10-24 17:26:31 +00004658
4659 assert(PendingForwardRefs.size() == 0 &&
4660 "Some forward refs did not get linked to the definition!");
Douglas Gregor1342e842009-07-06 18:54:52 +00004661 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004662 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00004663}
Douglas Gregorb473b072010-08-19 00:28:17 +00004664
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004665ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
4666 const char *isysroot, bool DisableValidation)
4667 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4668 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4669 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4670 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
4671 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00004672 TotalNumSLocEntries(0), NextSLocOffset(0), NumStatementsRead(0),
4673 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
4674 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004675 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4676 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4677 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
4678 RelocatablePCH = false;
4679}
4680
4681ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4682 Diagnostic &Diags, const char *isysroot,
4683 bool DisableValidation)
4684 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4685 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
4686 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
4687 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00004688 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4689 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4690 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4691 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4692 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4693 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004694 RelocatablePCH = false;
4695}
4696
4697ASTReader::~ASTReader() {
4698 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4699 delete Chain[e - i - 1];
4700 // Delete all visible decl lookup tables
4701 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4702 E = DeclContextOffsets.end();
4703 I != E; ++I) {
4704 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4705 J != F; ++J) {
4706 if (J->NameLookupTableData)
4707 delete static_cast<ASTDeclContextNameLookupTable*>(
4708 J->NameLookupTableData);
4709 }
4710 }
4711 for (DeclContextVisibleUpdatesPending::iterator
4712 I = PendingVisibleUpdates.begin(),
4713 E = PendingVisibleUpdates.end();
4714 I != E; ++I) {
4715 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4716 F = I->second.end();
4717 J != F; ++J)
4718 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4719 }
4720}
4721
Sebastian Redl009e7f22010-10-05 16:15:19 +00004722ASTReader::PerFileData::PerFileData(ASTFileType Ty)
4723 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00004724 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
4725 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
4726 MacroDefinitionOffsets(0), LocalNumSelectors(0), SelectorOffsets(0),
4727 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004728 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
4729 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redl3f6b7532010-10-01 19:59:12 +00004730 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregorb473b072010-08-19 00:28:17 +00004731{}
4732
4733ASTReader::PerFileData::~PerFileData() {
4734 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
4735 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4736}
4737